Export HTML Table to Open Formats (XML, CSV)
About
Export HTML Table to Open Formats is a collection of classes written in PHP (5.x) and JavaScript which allows to export of HTML tables into open formats such as XML or CSV files without any database. The data are taken directly from the HTML table cell and then converted to these formats. Key features are as follows:
  • direct export HTML tables to XML and CSV formats,
  • no need for a database connection,
  • supports large tables (multiple requests - AJAX),
  • supports rowspan and colspan tags,
  • easy to configure and using.
Note that this solution does not support nested tables.
Simple example
<html>

	<head>
		<-- [1] Add javascript library -->
		<script type="text/javascript" src="../script/jquery.js"></script>
		<script type="text/javascript" src="../script/jquery.callback.js"></script>
		<script type="text/javascript" src="../script/ExportHTMLTable.js"></script>		
	</head>
	
	<body>
		
		<-- [2] set the table identifier to be converted -->
		<table cellspacing="0" cellpadding="0" id="table_1">
		
			<tr>
				<td>11</td>
				<td>22</td>
				<td>33</td>
			</tr>
			
			<tr>
				<td>44</td>
				<td>55</td>
				<td>66</td>
			</tr>			

		</table>
	
		<-- [3] create object -->
		<script type="text/javascript">
			var exportTable1=new ExportHTMLTable('table_1');
		</script>
		
		<-- [4] execute functions -->
		<div>
			<input type="button" onclick="exportTable1.exportToCSV()" value="Export to CSV"/>
			<input type="button" onclick="exportTable1.exportToXML()" value="Export to XML"/>
		</div>
		
	</body>
	
</html>						
						
Result:

11 22 33
44 55 66
Folders structure
Folders and files structure:
  • /class
    • ExportHTMLTable.php - main PHP class,
    • ExportHTMLTableToCSV.php - class which allows export table do CSV,
    • ExportHTMLTableToXML.php - class which allows export table do XML.
  • /script
    • jquery.js - JQuery library,
    • jquery.callback.js - JQuery callback plugin,
    • ExportHTMLTable.js - main JS class.
  • export.php - AJAX drivers (PHP script processes requests).
Requirements
Export HTML Table to Open Formats requires enabled PHP modules as follow:
JS classes and functions
The most important class which gets the data and generates requests to the server is ExportHTMLTable. This class takes the ID table (via constructor), which must be exported to the selected format.
Key class fields are shown in the table below:
Field Default value Description
formId 'exportForm' name of auto-generated form which contains the exported data
tableId - identifier table which should be exported
blockSize 1024 size of sent data (fields) during the single request
configuration {url:'../export.php',dataType:'json'} an array contains the URL address of the script that processes the request and the type of returned data
The most important function in this class are shown in the table below:
Function name Description
exportToCSV() function exports data into a CSV format
exportToXML() function exports data into a XML format
PHP classes and functions
ExportHTMLTable
The main class that handles incoming requests, creates the session variables and collects data.
The main functions of class:
Function name Description
setData() function set variables from the request
getData() function return an array contains data from HTML table
isCSV() function is returned true if the selected format is CSV
isXML() function is returned true if the selected format is XML
isLastRequest() function is returned true if this is last request
isFirstRequest() function is returned true if this is first request
ExportHTMLTableToCSV
Class exports data to CSV format.
function __construct
($data, $headers=null, $separator=null, $inputEncoding='UTF-8', $outputEncoding='UTF-16')
Parameter Default value Description
data - array contains data from HTML table
headres null array conatains headers
separator chr(9) separator for CSV file
inputEncoding UTF-8 input encoding
outputEncoding UTF-16 encoding CSV document
The most important function in this class are shown in the table below:
Function name Description
download($filename=null) function creates a file named $filename
ExportHTMLTableToXML
Class exports data to XML format.
function __construct
($data, $rootName='table',$rowName='row',$columnName=null,$inputEncoding='UTF-8',$outputEncoding='UTF-8')
Parameter Default value Description
data - array contains data from HTML table
rootName table root name of the XML document
columnName null array conatains headers (name of cells)
inputEncoding UTF-8 input encoding
outputEncoding UTF-8 encoding XML document
The most important function in this class are shown in the table below:
Function name Description
download($filename=null) function creates a file named $filename
Examples
1. Export table with multiple request (10 column x 500 rows):

Column 1 Column 2 Column 3 Column 4 Column 5 Column 6 Column 7 Column 8 Column 9 Column 10
3843851914867653816729325861429996049
936996776933652714928877202336555677701
127194322913287229254690123392496445195
602785249445431023759891423235326616354
30121225512146828551139415172027675574
6919861922341919487996316265488035761500
9657654442156877343657482777880118391501
438869338688525732164619205239082036494
81674773203665725969925595320773676778
34451314928867511885431033421067341430
1698931656426295762947919161588472981566
9090410706273118931736714488271250624
266166994193564452114021914551768766053
567638256612492972747335610352595645
1872980750137776400422955638441473696776
3212467317248326433573745107905980064089
188073604428776225932462821658684192063
980433922691995131967644434119329415440
3095833422983440427757869368314660421645
17946188373295395525389195553459495237
65444123448755455411467695107968162499
499174132317835704677224981805411104978
685714538265366698323372995194250929917
325840592561334220538758864420917621076
73345614242900600189675926383091352521
86855240108032705470307393328176399108
917219127351680566788625236286880464346
412927735958231448379940837990798312634
987471721393197184391463938290405063223
565441712101989522394112987371535991744
33229766748111595382626596556024828377
63539774197630438078756437201662324722
7497726822271911298940780564926347685
429389310038781132397863509470441782097
1287332565586745215531886425488498377532
307841638351282424947400254310621141657
449193915051245611863536252714325685807
7774540873287729908926982896973762591168
63721525225411304842688922022323280344
8935559226327227458831566814052227463
11136270334288888129545850289588513530
328411918443299530929154845515481332930
28372149570745557348531382665552036461
225483332841297279527759504270777073361
3895512355236222929122054556513820155960
3645184287582335871669549387474579963608
935638521483791565392859697438842959135
585659038684948296518456953916834768556
148781193720965558962547582606593929813
359684865095529933677224405140610008026
4173368391922095870863042274150772271135
6485588037597287733963848366671848824
6546758541692795824660466304122633996
74031242616772099153849490387501369952
421339079405588933896263619910496109363
52738658794405437156788040532737444356
168423122136344463213768431136955599497
23911325811099851065382257105173571698
252156322359573592154358753907426248725
663824142691712623816155437696665063566
3879234433855975886824224913333098146011
21917567789920787925953558416054201530
6003676433694139179896001282773262345229
74166651804715986916523343372393282061
2594164580864717659843268477764955359667
7581574928001133303037636918900055129949
672988051032264340596984722188157281022
5856810121487262173730388136735790272265
942449665633893588622951645615243437576
302533143533223909712783539296706127
91758884364224181458849832595012448234
766642489018745170262366494718326222
5672218549953362783140412842895510166518
9018924117676361133331869296623219359311
8502225733429781159754831413967941062682
1466966677124500536637731179162315043987
7791490520768948875291628764598491886
548735256804938814821111652872627157398
482882116982104413545384863727616880363
29263704151677170198659878494846115662
802647404856845635423201963598497837177
2178540513654103198399934186715124506787
524011210325506375534231771141891934204
32202437114177992045341921687903393901
2417139572032699197348892726685546884107
4101884999966347964936626415925803511
4768770384148551263064276776277399123813
41233724356956397571961391177733311926
3276956557513281542927979112283409269
6968639835753435636956728557258559485567
910130123700953436353082151478444812267
173454856001375390182876679131181765145
2458804680672441108691815118179565326
88531644644912653922597219034158775229
90683512050561378699161892740282999136
29636720454817854763429193387052937273
8736363341204611579862034183781432248108
134229509290628238119515573483922695993
95667032265305759886333633969335646
793815159422918641294925390432516822877
5287556532505135454817196039702925973442
3062665855465031701764744593769416695476
55485588503671036490738174366403400575
43567823290833523840917485854249975671
688277430214974905889058255411654263837
9679120943279735922196223474965712819784
4809484463135447414475856570022621819
1735909561639598735935153618674263123127
14571166318909242644068377377869482284
3161173423783685893771592220999957746647
5794496091434500529926413062643643844055
717940429996008951698386857733014707604
8383602649984779348455208916197665004143
3012833174189379423435412436696459948177
3924808243585218939239661093522023075390
9682494286021615863379404272549148692682
5004475577949896438362107073228967644987
69646738383038818918608318917964926537
522466569689056356548787496743442223175
3767959766298146128936335866430006612
9390579888711338636827795322651172152
4976795780856604178267997879378054840
5651514557174507814533882299297495052991
815010159827722962899977315725206817
1525992967496240433710259301418186674411
452417374625447461595957678852051883207
77177818586346814355017272291372781065
9476640471444249802359132511855988389771
696151544029587568824317461614165547930
1957369650826990489441212339485432885022
2691465203157639239245267920616918608
230328559586872976658126362846651649058
7800757442582470793072496371802044531511
9297275819471423445957002298209622056574
818691419239591124838804848423840492417
75326176589827772041321890665777304879
547310477626819612045768664292462458866
26439349734230755488588689074288332460
907337378506642365830459859626914357633
4743571994502881140922564786717719113126
430973063551475724349651725544386601926
74412609136771015359366275901679783092
8338926755995956651062886540168686645425
77421556584178991795828923621171859990
2269851895255035818352192282966947616846
119177023264191904953403651529122244
1446754073119914441711998406420214257507
607849792820263482696585735683463207509
567686648715359118876375207879371153626
848487636014293491128887524976078598546
268998076079404178498398849861730554522
510634468613474194356014971986183238633
344483675811565619955012438439125453821
242234955975375616538147726877564953660
749419639577642181461545976362881713515
738969448748334091031449127691593057027
342185554797040641824331284759369399866
878182494472432313144550958792137953676
807977682984882044868349872645118891792
8204220994584700640087559108704267473355
7615571727742890949944405696792712051441
29293963985099445410683695321980355408
839214914671487717552603379983269066248
3916732314211265304298218595609481628564
435210134117586921761553253929570664963
2478838884297682709935611798979968869165
6746556434032897857139322206310098081137
705795527965534227234672709678218449219
288413968053996873966868362295771677903
3089278390088213329462697862165210649883
708582116711247011485401513573819461356
2433270934169951724184288887914554927924
321825572388952576221851484298118998
563487918949737724767120192462721349
41664355557334768732226253114365405688
544420888658983468389692829438216005541
66449532757922646387263192143330473678
76627803272596619990375323377098665721
497317367728246184892844556316511853223
1631657262058129295155941091929755270
600923342111550082153366543816767854338
8006246014476291590189626160244397598707
191450233141615546754882338500474099976
88457923968961653977178337981843502984
852743192921027507915852752210253322871
84685798933352508618678352045328928639
5685194800739189052482874117091314265
7677191832995913670542247561818862222
766550438690464620481921243180566911784
877617413621355504114166728541355124282
15111989125672770797456297470957671021
8154221425672966374840874967303494624078
6093921276397400100872886601637761796994
190666906773904273408027102575905839785
82801014041847320578651768241740498758
23412555042356824776011231504828215228
9114978982092565773849379297150152623892
6971766337201718546931334416683313283399
408378325936696446689549596614377241875
961735964184584812332227680546531464620
9214825671194905284154883095399366051615
8112393918529052121033927046476043747066
322696019328475797893388125586788124951
625334098867833359926957730693619271366
45318277568887744779692849945101116824
2734319458778144477422228275116594792
7824476796664804243734003538710538894241
5976804875222112613766166638191162462026
79652024549643283196538556961154270449
604154788001688069078566834156896938755
39479402421601782058700988799946291931
495013994013371264296147097103166917990
1903955590009360169761398944709268711130
5663277780183035578724658010903802986
655949571665773465384697592350895128376
188157442164616793374929812548315794630
27702797864475493252832798490665636957
2446991655477823672347894513838109317
3834146980888597251612849941548802161
448912156160597520708741907599271131460
419545490793725293499738364485442632087
358798985094535718537698178975556113335
479659674418657977955178136771338037335
66346137463924337011679256573047564820
824772826368306238811502070582539729609
268385257464631340156512926444978335125
921784536377435387352826899272950694825
7839159671297523353696651143706951831383
2865750369658982832018513449694979061429
422143953277836856394853299965669378099
5745423615791403311746827184695232906547
9455944335467479123111016010477418977105
975093862441892385080562444790692928813
4687949957012505756310956149621948663200
779877337583350951280965471978783765692
8064514158976761409420143776437331334345
338543408462511660397993534536327458508
908441584996821242204364360436598993375
5537296465504324280928987451129680848157
935823678080727619751896325354125669167
33497927773470975258989244682766701813
4145457263851378208750718613438867288231
469348706363694826882329582509980526381
4078659849189703829294233434715160499724
6453498644199493462854489974185565549364
1638135326315818135727828846822686561
41439684525765389823708422452617318762
859861553120797014894647800827242399050
131710818250656455194425408281995281
6720556531236377622513351296772919500
8377411229542413840544435524604219425093
65573547902515169914992904118625477755
7346377242808089496758506810893932276647
150655215135650214122319989443883484238
20456227992981920898416786278254533107
37391639413628831724030132424828777144
374030042589924636163229722805127226918
754283061317294330496161281427664098137
943529810855385150115931346483516988
8868801917058893553872172423473939791400
725824179361036374765453487612980355052
6937152198245638638338497013582195069687
358511124248241655851829349774790265973
995897269710404052112666894960515906736
973375926707955083427745773631593063758
8790185945196964742014112156673031553898
26187202744845094538278245844673113071
3212937732052280664614564236836257208890
103061377766101796151879740453126837637
6936567177917937554277784420924257080
2922567244482961529806849155974622268
9005796117644640573866901735164793047775
16506276437626234104209819510718915296
91832734559280549768771288362676248132
462035133379437872468502173174375289814
798180886506906390293046282051854761293
4620732413247244312319235673430511859109
314714651916979361579821000526564318055
5487479117724524312182899871773017583652
952534376266118611880502122646632048839
80436551497637385447658728585694625314
7930883921503654461077734468959165217736
923611236544561358597009286669775724345
51533229263624129452895379611526498656
40529252415599078610222530442801027965
53040494684252745764554948198489066865
110485232212960217164314489483949087563
972293146770179766488364846932912331501
4039990133728993232819979020164592801846
229945945584921574878214890517782822476
79325585108215882647210771724928395115
314432991012316647312620892976673608550
4560433869435630597232679715652740237881
4415198754556531905734745263359263876129
6777591254263486625486815290875790069163
415061340539164687414033576819124025840
8290870519656885826503527603265969767
4343146578891174437482612930943231649785
338212409220355246714564827114893869245
592336329926429159985692476357761034072
93069741689090368825462385288311745884
3036335871042156482755172132384071523923
271442391883236644740658030440799692801
6234978932803804137489903820969832935889
2694457535626226657681461591394834178814
755716879992997110486275747643579294478
185940781258859761296743375365534488
1618961862748610936813014399827343786
1065900484309686810441316022407537025532
81865888186257960577335569645651899432
3701687227894478710998209836401468366423
82387257289996322705466520282189211215
582342858419703275775644390328812315693
781668079460935339978729969403667833990
616356236385171549193234160909275733948
3475489069057633782363771022520815225103
6533354158591858164845319132364256712163
348574721137109641096384186672983458855
59144795343694180753093787043363465798
664629559638457523752366209670743026141
40307733932159811369579453234423584798
15246806331881410653976972826410608443
607156966988011497342307543960940591264
3221530911818755943354101558806666728359
332032268690114389884906368119253402350
227946894229574376085896445770696642860
7184801242157503210038323986121032216549
470949769306746892602749158037744056202
6241712061735431553663342198846531236975
653846073777533786892770804235351903602
629459622366904041348947943803088167072
9530988457309566699320497133318467483859
294673361464726324413718360480134772990
8792887745969519787563912449820788262863
9085728684109741289045824907925582091471
95153025544033015548260618152172361099
642303760598816404288456477950643022478
8488869164704524934563682665561725663007
78848053798988892447782095215831623488
9220880328836398381961967207471166999735
8195631994603700375767203370919151356011
5062625615919850348048609645938612158290
661518415369213535615152558281165262832
649112853560297241514379678188285149938
21125738603591065537788165169027405884
60657418202114174346146056320865305690
277675193167102470473704913644540205205
220517152980247874409695585486196551
23325322271762710590147644123876236114
941784634570654401459564377505255497655
4858511421985254824238333235643562967043
7233707184655516123525495368462164013406
5049675137601345708943335859400014513548
193945631954212326242322278618173273273
782914762487388285612111756790032004477
776549772338526960414326962162697364189
527895244269163863004335145227860821356
407463175761469271834214266931264901780
963418281556415932233168105624870218871
1984826577231251776585433212696424404724
855966198916280343021249631307346559453
875738212402982357593745322262411898095
644830068701021925737335775488270767888
3871510573519231924145351402228149665265
431171855787573935324064871856742051109
453034658105421688187834117716067194115
1151664859688323335897941150877135591949
2242551662899365179522534906817695009636
2938115723737787814855598737730684379
985049037939527849051405723572137116640
8566872539817902464838411957711234242060
274773444721264342056361662608521421905
33547425043971967765007717783354114
7900439054043840830290311907865848164864
195140621284151782335274186871463234112
641844146654355285065168126410995841073
43583533545478113427839171858325131355
54058703765030147920820283909458055249
6812145889312444402178327959274465297186
552222082526782944352094405348769377752
653583356300692811851611642900156435051
6896381582944803373337811953198928497865
8191130973395421314637677202307436542373
85031218833923916078073896459549102524
23445260926630163225919949637548478242
9033396961258570407194737744641244006322
3804961920087447379134376174624329707
39701100891590546328421845528889711792
7450613173537094117973722493356533981878
471164708211162589468089472846666738662
6643302029745110661631603235266210365051
5556150575328935969957094826150149918086
979229249083101650332019881712875871937
5073384168958298885596712385633756417604
213352919728693328649338298795685199880
2446757532852595610791179398247079936656
8175792614536294726340826713854988375962
77986621479646929813979611444262189269
6698608334105462581444799838121263796479
9971367261151612282748175997554485667
618157994278194380851150277879678226828
792012098522844145986567296364060157304
71072222519489949031686380508490561974
952021142534721494348754664128433473107
925286531190632153986320303419604430431
12492909917178896356542221404917609869
6995104066729054481666076650129725984963
1265914438371015961784494154364574311646
587646041552582577559063181514734454249
96299354928593944579821344570137576595
537025676616850667629720631773035352890
6458247737122803544152804462839894297284
5992109292171914255630563078966266715930
55395102364818315838282560364024955376
885641028022688389724106226543548159185
8367341475347075337572985434633599388318
722398963518919955863727757229242269037
3186934736062981433777812214484422943913
82826019800506041186351780143584077184
7269443557148966512973442137799743481
1799445113189007829011614265352048514319
8741562019577112970280308045921223414435
8414943827299120757371962674938786084354
6975791732033083336519466193287545411654
5709286270821381556496656081930683401110
16296436763336830233930973048703047796
2163637264871718923049674045392983436609
603547216280799784501311180862357255326
107963916425528479887008566518830697370
1607885671554488900315647645120958094313
512427666681114115209291462687485045886
264788697591468059419246423248166912877
6344354490333700170131324589593038284068
70553045434787610737831036856212756297
790666723288557569548324188919172207363
2805623353771961814723214200745990117406
3483224730898537148257249247668571788544
8882489713604080284178703162332836511148
2322946178433748456296958615298505487
546220148078564445039153183829963622611
442229340625537806475446781572585852116
8816369335566720103316793197565231563411
141741857015834592446561845679562991028
6701111894764390415360194586435269205267
6736456253801180413763695734728498171939
3431614075053573655917996362986287687409
9798684324662639625955328024350463863416
9895653399887351660848042543866831973703
80731811987238722523647781317731751966
214657169369252634174067139613326033800
393292072636589237540407844468079853868
396874816855879346615666524635744243335
9100803381057515953861065355311573916492
7619789979837039138137085826739691783652
2946533063042777368621358163559830672896
1205456486733211571535791405303524803057
15561001704706639815186206046056439233
720726282365951176171219322243293251254
52518621391882072407455244345442486958
93106592988142196082628605688144467
291620244019584139925921551753961574842
1394219621719730949625475035339560257806
673578976987278397928701489405156921021
4631170464576162920129504863130160995591
435425631036216683002806517366455172628
132220427727440473373734437174423246674
11028545218647512867548444565059447765
34283787234777417761589552837503506944
577224318135497885816038639979637628993
603572418863372550026477378473362618105
2370726077164156622485105359871596145190
649477327609993972787605717347151982907
7491994018532911392370423616676198234778
11101082587875983441665101961020521368
5439823573002204462866986575119261484052
2259622327466790172846047101651464023031
540071248211951864527967708815748952718
60679569774479604841867360829517154102
4086193848684345778133207495792246161880
673478292331166598049131929840560243358
361980037074406213563528816487297163053
32877472286790210711342612890221127330
1852921720129767821356357317640878515459
6186284026222241801918873939334139305988
91589987167330406996196750773267357189
7305973833157432158856208952219798306604
2197848598869234997864226191587169062792
8836864520918292983736183828795935335968
488167014771809867989647684221618165092
214032545898832134056336405906658583389
287059957063354138345050745718433938684
41479775666480993230834986239887652167
5741724775429848746095352595828560361590
44102386166981548518812449426467405273
82768969844687359430477257489865552992
110729872530933382282507467839202951328
545776221916249889872119024838318249
7156451796862561906924731086501358055867
790131861205316196703007318023541091912
380567221538293950015057277335954984564
2. Export table with rowspan and colspan attributes:

Column 1 Column 2 Column 3
a c
d e f
g
Support
If you have any questions, please contact me: [email protected]