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
7924431940999165637748745404959095981072
8550593512873289863377334062628952928361
554895201089359814793693434230412549209
9383713045947564900943542757640567788478
555189167506489702010569026122139444184
61497088559771576755545701932050279181
1446273745135301718380945952122211296
86292003252746691405350350182243429550
8349665238987812145765094489757289889509
2864313466792365530641468243927780226354
303028694205943780632182769602699103609
364698607748497972619681890174886222471
547514414788714511738866932899453161488
8139352723476818323246485222481679467969
285792725948607324673111890812775773596
380174141557161328947978815978427911659
7805660588312990532815431880632578056568
784613182300667253560079985848710268849
801441849345245583169975618315668527891
5944848825518587323753244356851333019228
175910364472407055726619504943988563835
364028752607844340285678956842757052962
5530264257308051337223056010365548235839
4123717263782006705066317719964665713
3121496864316762208394298293316848589212
756974982063870895640398849762890826163
8881776655396550491763107428731120576672
2723744974069588344128498639396198469706
328229473468162339655772698728986001
7522421295598780975873541917659741372134
587571514607458645497149644719758295004
3018567779961071546083739070449185503853
491978351745883660796881006864382692193
2175430417028932174919566469579132927035
9801363885929651693374143811277257254912
74032772280696622397889201315090932868
3643791858512002649487694950213834414449
69525174383340402360106757656002371678
89341778504267379985132494737920076836
994555868247773150724753592646385671760
72482558177331384129792429457422583911
222817631445728971838219869874734030
39559960355820999565494033879119681644
226781819186086296210364659811967165069
4661349223873958935294863277199839242
14968692741546172778214601690337521589
7879531465671819123784086676503119528970
3735968961557970447867743304504441849622
6123703225085731522753318989959417775
9563286929248854256412354135671762724801
18699541598117849537474819203971532340
54243745006834340694822023789044581743
3587180626995289356564167731904049046453
6057148051343000505480163659139749616467
7365667313085071863645054483212165762446
4965877317792504807194098824452281878381
29605609321680527145448995845522814121
72058492743241253395383197673192001435
4170186841153592445899269444640328064688
2077702711624543889593623319957792112397
7475176742165603405614511765516596345875
9599454808138547583823026534619995566
73191962247202017674945841725205344789
3852783541488584251317647270105193908153
9067354463671920244494214916857594627702
4874340124091756427595549125392296598688
980519044636460247006436770855168538958
3208306474269109551914148164623130911
955129606876562398023452994411258026758
39651698876051058174452899894388899611
6647749740745900270414699006655159424661
1272818092378822521949084118598835164211
8634537092445737292448346669597692827
7239223401904891626158016766075195751
356734482572173558258481541243127698693
1551429211787258276628384728948486042256
83102047120913537732470847473627216475
3884885430643345740721331117920323241343
979310855272908513798948975434937213883
7387767367932849269063294486641984913030
5877194138031522808790949223717028244576
889110283048070215437076129190724009010
619318791333217105390111516101236955797
590622692947947342685105213437136212427
928862015646365955838322788670371161543
3467694744033965440755904747432676565586
7938658992982619966932932034332296246
730712354881019160591052059752956157797
7344259522768881698533026326267743936224
1636422480692542505619451956907822382202
1369485245139863186565539578341101865
34833184298699227813044945914764852204
64996976760863438385895552912602262593
3805383646889978802313616804127839981335
8811441764786204681386895687375255439623
381728688115508280814534200369514635817
3859302021986642233637781701667847988947
84803410272544137050442758125461799555
403619238751956162574335819473861036302
711596931353930968906928425939922503761
7170154883102376985042124907763991983730
3717805777914955629122073452318778607401
388417691940319218333042297060973729305
8751236759475195114010638074528255067224
14796480108612077062286861280686641947
6595513424347818775451747059271415476
3531115635299614810327411023704432893356
528437040915444471195129993713760476215
903464364462607992340493213114129657261
8329870267868987494890734190253356015266
4904503400019357204661916872854959545
19508554571269659219136589838158619503
66820263932945848980268615184333715872
1937666678473719102866049399433884389389
754559544162275892091846479684823847569
1589477835833716723898287148896831279162
7983444782408447512579912193435368384197
65414679863258472578254144134627498448
9489395624035645813351515300360151695526
813310051331815584328841898693077349765
41038567959185285911541816628773751015
83601922779257918419314140621027032976
815126269334740571075434859291671767373
4144499981977491442868953790974013389003
42667454844001583271027663702479634762
606766674236244964133885100522524404217
16879197165587267327194857290371248342
344274773334409280064945069901776366454
529664353346536671198524621295719897808
328490213533887969329251640877263348686
3699645186245373268536256408521925827
26165647140972431473420134813639447143
414825433266500963732913455714479413038
24656770700699887052090110981198711420
532120468556554263529917972771646378860
6741794156673476344871641219053245619
9195237687448441975061211669559930524
6309182896698064139996673272430678452778
80431024977533645119182560597555731363
7757271484546525207995076155527478048569
596240485084450100441803270310319893360
911555939775307514985239203660874349451
731807584601391927684135357354186898
97485146305328249221256029119785442360
347285467698139862928933574861643259670
5295847067686152277391531007382776305956
108900036681856487141305957563524198185
1847907850759670526352868110167134437683
562712033400134886729250283133532792222
4803114098773756471979354126206638924
9619699617466517979369062780982030618694
144048748481442112343468910917955727201
698579826376719901594916045592957465
5258546026085888607714202109413234839
6640923062596280170883458361259326003603
44166847392504976285665712819914583897
7832226248727490930423935709234921957831
2807452464041387112890541543395813704129
3085688514444238668691099609380071768696
598263775952400372446946561275385089571
7032315813323819704062682026523880398064
15572038937468929320963484434193175899
837941390756647757715411887745443303917
937967007232437763864275386905179122390
2318195827943927396774896979112367966750
611338959168742063315859746819330118431
84145175417444311785768792387790638304
9353241808696704851954743534194788236
45359159735818746613386398172328636405
5961225143811116800528047965686375014921
8469297782704334756550480089221996992
3515227481345571463939696996411477343650
9010671338323882145659921688782864982055
369041302978139346875412327845178682280
4723654225777218456291942274202152921753
824269288781190103626086074487088122417
737513424781252442515752763806455179691
9113989937929541502076836179249472351746
2742655288973960134065686973868268855333
897973336908323765349584923085896049257
7444521330827421586192087139188713688344
7996471216515219551417389932232830607768
6348673058224849899257182234054691180
33829558683348811692330947635376823131
697678821202510347474173459144916342766
541265278847064894566089749633746081278
9460935159892931467745021261386415438
607360388015847298794874907113958561140
4294215150296844814154279019623463392465
638611655897704142936802936492612813842
63704672960034312230965816225954619015
211915169222964746529902750170539557319
875773897309384450720606319744978439854
813726150543838850846495940393994633006
4436832763196279646338916401801548026311
45011257377836969734376226708335272316
992999049698119273440705061994986246673
744185357266748022707825429739979119
8957806851052515172172623858630946554293
3592220132145605685522982681496921753213
17831003502670263436908065191881628101
1119220415837666493477586038586528841613
330929242021010337526618165715189055131
5194862720121894676746214362886660824122
2690752842294835620058146138481387671671
8504489967293677950634749550763848636182
496320118782021203990635480360699982928
18420634996712945766488485352274996980
124926839868297328277214693971187101052
16175840707371090569226472269636942630
7379421020501737824683937380139736192980
285687725716796573961848788150118245439
482432588546966540315961354374388764978
60191717229596264552618948765782748379
5756311344776984826846969292217018874924
553229147257314515759267035825263705213
31405134244278130159602302882815557159
2604672442704112359730857941998090977440
47678554194813994225158737403132718304
664960311460139516785242606371219202060
55945214196368751863345890408669652435
3215439744022352767297762948452518537
323210037885429868702009907149646047656
8118764310238725698556876565772090831636
566639167247170980989846865971303001
8379986554799843414176596681650987901857
475282862106509867017500230594976383521
685261287159859228803166446691221943765
5846199750607142801691357396622378874793
52296799505312084125322622637054747718
5496745825479397873361042790548857468160
183868031117559872991673659232251023402
2073720435207771504540923570744767454047
68766628531817246278793791622996556767
6048760143324008047411563556723094733
29954095197494733637633852158430688147
1759174613184309765297719785901061511
1957391672016718661237386455681369757
2835940631189050140919951630586051958031
848208751139507374680083240728228517798
92001087283572380866483534978271523865
697256763571867758111036155198054348840
9248327617542684981170634616100097435272
8555420425389346651593156035488978619641
51469084426202955867942381866032660
623164638741377102583782255263853592
52669519032921316424838286404791221934
8449968765756484484046427760427954641639
1732452912171594472957252406399162931579
2374512656875725693098228562665047455924
8072175790204365340789138198744932972533
7521625620529540480522543527573133106283
273112401636302173159413095362714624
5991981070217728449224789280162531818423
294844763980037855411864992213256106
82038893373656979615705416739452153302
7242204685649361793186022999383941567
320561496438997476150288986276243032500
30932996848778208072335529999610739465
220972728642679364543392275468677682811
5401418374421826595299978831741575372
745935904050379148452203613607449786307
4132194660679900312736351049350299427011
5879614179961011497663687513885433256850
4873377597422891407442357107885071228
127261864707877664249018681932896615007
862671836650311914182284914478731475427
6853200476286380433142514957316520154643
371142631453662786191687645780052882723
279697509267716721442327577644466586112
683948314393483140431172829689668438725
43779090104781437253399822406942869413
80392966133959853895771449563585063154
3711836147269726772779887277546893393
52515421473393692466970342878873493646
80104736667425638781104205605968234347
61101287472377309522510572557988315276
138750601596520993869804880671694504923
956574854800499440632863281563280598834
298468134564440784907456192712736293265
1247372379786345506646581283065353447
7197589971084796157467301224702553279518
6371790969959190811899793593602485081047
750082310124834906429262507627244036165
41458138502489957955371677853460502055
783927208629855224658174724297111777644
6428587732721554200332381922327432132992
9436820029001221728982221212798583436425
8361981553432188122799274593794386283748
6606816148656268782073357701641197852914
3370963818908781898268275717863651328200
6958861747025093129401505770202983479
99176426556811837431457841841258764273
1695426236743841993429441550778568843897
8318653523574207741459204452976379167497
3182202344226965388871835250872919588192
941768146081763989653626964915494808589
8485720020398831592816761565153549295186
9180677656854261385412912956515029201448
363967021930370687127471310827254989224
8116838952903350669829523925483611295493
4452749154428850508917523926173354659
8086463114154141666926302702948762125984
723499252824291726567601212445585564866
753725228581161236128048145937136738318
888630623213386392328339786396169793218
3716206536985154370143116413717244304
1736822720957030766038594991702291791922
58111866379290526237793390984727107426
2165977487177018841816893671435983712528
1635428735632344706446145846516396847596
90358112510327437263438094537362354619
3123702639648109865654775239461215293681
1911919243554039811523714503895916954762
660736098000962550115954704744742833341
928587611841735647869907791261127227008
5346684912654983776970207703793761129051
133848011236534928441723290932144708615
292885701815268291312257770013277544093
1162793397791668252632186842949092032564
786232212697644838491551387778886046511
154938624425943194934244150625732418668
649679934521394275195239334818153106249
746493406394458597345617649884142918048
5557139497121078544694138856425836276550
540977731697711514123473002375715788622
954744222074998291821113121782854123144
1373436326484979458934814632591533894
590794987681281699701005177442629468129
38903016270928588653555289436787730438
126070939214645860758821820449769954659
34647796153962049623390697664359382468
691770375737274747032975115686204148276
9206892365669030279036136200250449954743
5342292961403761882471539910632153354524
765389658590964479151629443434193675617
176996288786136173697610444637648347341
633884423808569181853856193771152498153
78641854355582276209491046519292402775
42494946588829788049815780060526611136
1251868780556204365998653913977132305840
985623735029992514985592920157471001401
5239271666443726188411657529548813228774
9526863694315721772289247555610089867415
1663671273956855599158467480493443627189
7921671317145464334488589612560087007637
52267351539917248732586582248418476549
75739240627189242812067845324774961310
159672377371862490719044104765883181576
951858749046828949385609933275494533509
2190770034836428853916081844197863759944
9930180483329336853522833505850358416532
72821575749774614165699494399294115752
666655713726119115740116872754142694584
201538267250651673059149100991334525488
1372180289139115831582725931419030276805
6995329319034826804363731599413282582773
512733791039278784598274723914889262250
511297056769978350053130177269034803775
3587715449174674481915205553740419087
211310910942969902992731433488127257270
6863897994777530475523375802298194178019
9948311676417192179497159315461246251175
71268676986973959113402975930226943
46503738104760813342693485528698479774
5636955223607967855476666332969391752712
3059313804014893372034664316768607490
344936943064548185880337970381990041159
227084777249826863326713507323217707182
9821278682359221219932869377144789800
84141324525479720020189011759910843815
55384451704495344917418224816724794593
13075657252364881455929037906726735856
5767401260032235954367382560227265804464
5829127222728501961014686492075098492
2347705969539501848057867364493071115982
69425837613949664404170389102798167517
8205743835318691734237021533358424663396
78569913559991626291003935867482940127
38759988391283125393118902726512798990
9285693543415605353698303945652229526924
8018269997024391343022941083307177444567
5877285144441789661958972158589557621908
9493161127105942582775007024643070856669
3386828760151391414514431200974566132206
1746788371327495577475563859559954713129
769714236383183463153411747906980312378
562086575394494189609660340999887143652
5698417535064332979172282731839360429756
13042899434469187661683923415152732859
1123108143874133326250528482633219897
9776409975645244967075344179595350899016
8671464670099914809034105501339567002646
26610859944147256746464995848914367391
2580718532378931963057536047488190791404
781621355384676175551549333258178964359
3574575987359331915247469462743974451899
714155356957628750538303109552947636365
3544693066313982751137757231631655801262
7711382840809486773170785660834546389845
1673197461269744976388357126310256534623
19788224577369106386488693618535674814
34061174871387804080314594479690425711
1945881169042822555068263949856315274123
9851933678472308244488292023813380666777
1547689623392791233785728485866626241515
3473558144701537486626933395603410160
403586252807177012094275584972777903871
486475346052859185864125138874046943433
66822679947070529664507981960526956901
9656597472734567750429785683806830884099
462874888434603608567318041560317548657
436355809466520569568406579628422656201
4326446516399951411112621023971911420
9695598060325708322137745345296399452178
564075081798621633014122498732849836587
35035445820478027319117979410972395701
169367501721154313438958216613611107143
1961908132185812176367328345564280078876
2506880987075089774087703895235417365905
69813055168214820842155332885695749585
1224926041308062711074711954258665516140
6470625056388185546763612400672418438094
527245291258213870717641975861163341607
27362679420951896376313034081558548727
8062913397377347298197233400896568954430
86731378273351485591183480306482847722
16392927996927881442350788793911129153
3439243151132262450599900730056545842
4508452179113915612331198182475141728752
504885695519329959665751803222276354472
394153638073979129124674458177276145924
63751734984333751947198768822375483693
408175573637841748746606566494773006159
708227492287747515040329924418525237176
21887031491411251404094820952124683274
124946011461339727731309590869336942990
922815797738845975441261649941240964181
914299020211922819233862481548043778033
7900236217688804325967749665629428667
61707727530495026769808111544442386874
7857260711583672362029249620893047903364
8180725218993016626118467798545262135881
82331799885850306131867269606816932782
441566971131793905554945343274970879239
2270515851856142797220861806603256932561
109236829658116815825113850836657059616
9885200759731033857870092542424174299105
79917193119597538371181691150558409575
7993681430465473733720232856932278801746
95074984974518666641941203936033497366
602234343451795698633836663421128971354
6806688529728326128987768487619721258299
54222104331353979506574930930247818557
30038616874851949554093969553755171851
49812639411956188137131296357059407758
68088838382628335995393384928830816299
5641327647926869368678501862127768144984
754572124697515035803888893112534006306
642097193504423270551341416415237396
911223608877410822689473277952329619
680776345624875180269344252654072294
3703281685323603818280263758538749064347
4238375860824914347039073373370054709485
2563696944732372619845623414324890682240
8404207955043758265518482094645589474851
92234959060927019871863644249661036985
1528185344928219113573464248018898
76976773614957141008210848589523222878
388839707271711184279055358570697691803
610965983323638433497691218822452911874
75978163571846136285051426964778684
90229513455380978175216381749731442735
966691379960922348933529461722317222342
8512873051127941680332109386572534141964
119496357439432139731771623699712816871
131568449602918310138784574139521381601
490750527310896983648115614925139326969
6246631864141948583382242670467139051880
8556312614103359750364529884333246517264
6400265484536346448064307353756545587305
2699982585486261840060363067736152061098
536656911364232581853825785210053312
4172955199492584826353083475760470042269
3930500562077559365416654641854057357976
207493481921945742264263740952235468999
254653388337922797799953549472037169293
620985318633802819425139283519969896
2963516878152958041863879633617535836
490243631323385572962311773701699214
382777567721405274212929366429285697703
841494564471615031444139839066853095028
773186183736989553085969422698295443565
1308141662742779994565398424655913878715
10208162819993357513203670728106561223
38818340560751228666673587480485175641
6736442298680678124791611618724025376
712687356502523518377449728415448754151
7826550858405304117659628849770822852146
321424289527912777018572202239242387437
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]