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
9659987838871865522812129982536572600
541340223603585977063747244287088848517
181660003787213445488994813478189481998
3836226818227797188823039791550780194023
8979173618037025885772074219657940328026
734491219917742469713485922813272734636
8537659330421411853413729618301818478
7762301013434491365626268134610221789105
4020609317284947628245608467486535677873
42099829906466378842789167368364923753
507924164644140938266681256957132942596
5561610498775967879192469356537995254101
8059360462534112827987206370177209302
429510169183167817639104619240514134282
65098067636862926688559467547744968807
684355935883867165210135970495414477399
202649678879316321246369559544397309746
8461284474838652229034142199307154849741
264735016888573968981157867035559053
797684785034932578270588252704763389094
28921833289148362779143167439025738008
5510605420767909957699422744461154857170
7832170531041158772767593721996167007268
459992322568184820125797827876662696456
496758835429545250692568426301999424290
29788589260105012359096372806362576168
49277825990379142428988705689339632359
6487668540734703262450867702870153468312
114847713621676664188387115933071247437
949557473901527919514404502830834584036
505350466231384426939575491331415361343
143626273474133763947309667511347841982
9111802022661772556542892209393543458149
527576534545683830865445515669912006845
48847265491296354353659675588076401873
541057469345631986038547148312729485459
673872305753416630798157943917225712902
5640481114373767941618444146119538407154
807716766909477733262414324153978826145
6176835784647543239274852274624023469996
230367952782555584414329472041959510
859429633398713361698603221551732144464
8782713472274452312376687132302296373569
225451719495238673311137670848568789203
5615848545747700912110801329328450326142
2651102077822140277487451042331410099258
330757026136191943286958864189342053339
144646562703269828093655243097275118972
154722329889722986142230502180650372231
9862356787377676789029307172855824714314
9573737352812675149222543062843965298
1986440497524200691843212652607839203934
4662775840452296989563419207824852159024
11469728313990156308663557397858753910
16833759252317942363549970318856661971
23919690631387894056858787062768884668
792282079042450400463911940934617837247
629874314953165446393795552874571886626
38697775629946368286361063615931058380
19215447257577138693823232934363965751
14867906138719875932790683040796516423
385657482139449619101395779432665276318
136564319882201752322243180120638109467
203257765746371783955089116801656405927
8825964957997920444633522993236215943790
2632799011875108650217835073450767235509
9140161313244871899778440431478076438
8059820412174589607879294660769788379121
457587554890356051075807306038052064433
382915308915311093735612828990854421537
908564233721169262438047039200891935715
25258458702255599077697197088162514109
3833063428616480687299483848663814310
989974966677816402267436318505118785300
4705656272511276956895055382761710097093
930078574471797524547510199068665795394
4836654448515906390763475735734282374656
77442303595888131514677119833988304638
8347910256535906463721883752775415906885
88667214774070266269929918450573181196
83593377212815345973936347595091839166
62942097130968551731891422817817225141
67559026880167736704446740968883152664
1020457849496713175199844970706440257191
820613336202285175942671187837843322726
523737117219867681123179791137945239091
987821834779573606061716312654089415
93191670650388826997570792142250874711
761198668542910692387251790888740972788
817261354172343734506379734239924516209
306221312135650776320723211691219905072
566788826321578524896107181107842852242
4566256257108779529280563578855021044134
485799194139721760589099272306136431432
8289772529352337863163981254108965144778
327724407527392228246649310473628165550
5760653482536956969748095636748456275797
7792995322656996348190396130769355529589
4857394079246074414438841795622867917830
6008295433246809976330811775390038626939
2915325479762533253930816404760959812691
944511456267991365438007790667794387
8189994923676157231369119371458626591
3410674898052575646582964844501087976796
2468124673728479860256816128985618482420
5802314659849833085236105231770595079
5468218384518531611628663473916166588712
561187211486590382329636330618623973043
90923709857445160486047343884889136264
483696797625136649439807122829347076814
5710916285447319310027989183128903949
218325712557364313636431235568186057962
60818946611099609625537236866352918892
59314798500960520005923107943083222956
170323527606469510680406846452718023789
977814372890611224099098571660133517646
82361331474290957817458879709293168119
1189277430553362502088179046096647811
27816690183664494797123976934815775972
179783567365679943492986657494190112299
590834676002631422164497449626964169118
915370005411815250284778319188763928662
752790882327486523822587564749883727
41261094746557011638572236087770164379
676737457605685123160235449814211808979
57776071320780399075552261649672715756
104122272073580667026619954094081153456
558870834788943658878502746506583703271
4084463699467224253126044433392479305118
791530971121644215916194901383418611336
818960289826343664599888747197043119862
4856621560791921792819323982306132754077
482081012523115376593763989239096625012
56868419455640961660284390756535494687
3176586963289200876257219379168386194012
6959687413476851601937429666432476272
480092093573242360485286505565198849528
22724392818385761352173799963377755212
441419878349154643959354234734599533494
96728621615678685596239634782559416658
216878234602700311331666446319714132515
286765908959714034778264477913640861998
2022139773383523863399823718822758292881
413528207531967533661362433727622500700
54843244792708672549622025505895739783
4547905162859269836023634350415319996520
657986308382798827875392094765848905022
1458239868074024204822478328939670238581
1831847380095324413921374929319469496909
702669758060791790132495366866919256289
8244499675421131221227741388872467043902
9466249777566148124513546247439723635
7296311427444086710396583480312320694854
5883933619393659436980993124198424083702
20160083732418895661747611975436907330
131557687901327089139086257778144937893
303343389877983380205690402789269221239
9587868011041507554939604734935787226787
78483031296062177181016482143687029446
2777751549228800680230032601369433322123
4310828552618142859493756841538188462520
530932287150295269296953431494627278085
8805477978294348485569443892439620476151
759943267241176144812104428494676054601
718290496625288850322242307779976597415
592977175585689018872877720580966682633
797649983801562354096128802843214021200
3649348046033822126427918167745650705839
2024304776814494538783888827674287984376
8507755491039498656612675855449485871
782375173832800455747406953739349952690
9562987160675921349789922779172146702
9770576612736257725469967414630841118536
2293608861696406972381967914738938440
5671129076704228935498356068297686655984
8193659845915936991795415742866898059742
36021333748720431743799789885999819453
197089889414318084003955995086750359
66313467685165928130883470754719141494
904294356727582112438295692199247163767
495158588518504183187888846628625233648
1307184354237018439350543268757749537271
516753734738979421318950880812641592434
77359126683952947958775645341138042791
468121231661838191465581212374742502621
2647636830112718458239762422889414043
3827670669371637663969583399221481874625
2645191687511681549829398534946609974
5682946258716588216863979316152519982448
658930219866566982122649368832221922667
4274934560347327302993069312726380865626
1315745620584513554991897750533429941106
4992415850341296424756059565186725315469
125612397125998622336682603111775847861
289415984421336397956156689138156256896
3018785171049709433175086467504372991469
66675322235464547873947355769982759914
9093851553772555731429771690888441423583
7289497572732184428914892743887318297061
371224312818490229878046039929492828371
426047838696960272078209496201789544665
1972755812166278845370419137744419498770
7871378083335025494891868297991231804185
2918467855256128403241481700760139074939
44896854232314790828583445255687048737
4128567596568156324939512657292137074
963552933274160529605737877066061289100
3148954376320655111406633997774949802
49755502147410000791295986301290078064500
6338129598458949493696559917286442156009
812758978683772484631889048669696326005
24165451554162008842688209220896975571
180768743452953798818023870389556503234
693528336663351287185857181385222065759
767961697397043673915501727268119283148
67241947843126838397525731224587861015
8162721352647548915581095268833144209400
7294948871392272288675585583940362734309
6572933277634272286586877638453178563984
1668186214502651763669795795379155589
9765917069123475104376577711785798748566
872152938978341102672886095527048763890
52618067577718058655500941571410529958
4422578119733546595392439490161899809420
543998603564648211186591916194720071464
3117664570892695532637526015667137858
24459127183613759255338963255955729723
911112814768885560376240768835654921911
3337486667925361822119107653521463062050
979320606900144637922927727261855635777
881911232950170450579407899242269083834
413259166421359852148844564108498863
583352917427809159915103164848224275858
688769321392629117367919648121421819720
909640292549329462416131174641448416531
3315301383953352583367796074929610604819
139199334405912136203187071241840696604
6520644014021676398054448472190332672
1063466911517070286047251715657963981717
607724414464944679112028659438260537269
773986316354799286353263924105821435227
1573316131578668873920463091597469559237
9164116559404115995834251684761296445182
4188137284128681910832561693339272489214
5168128221205568299970683817225818448683
3556280750218334818275043137169630123142
51435101652404145303961145163777351217
8552335939586511401822455481223738898321
5016839913936560670740292188985012756203
2913522279902597806738686811235810114
7289396558109268216259449107472158411149
398358296208201555862361251919643624554
567421353041466877085315733750093803455
6056412048459098119422207534463366126829
214053523848388065397192488115574384314
317148675214204918285867621982026509
8830978176669901976317328467934795833
365818224238448661043978867577748423524
925658278586577105098694034536756289440
89164797186736301038321466293827226272
5108527740641699392574003546980987386589
54047303118598813294487230635333801203
1699241843186174424324229866438347262
373770238028742105770821249246371039
5059756590053907399935565360953845123139
6350643629597306887559069941792478957609
239333941255479461316282308402961267246
375155476821401112116234304811246217645
30253726181185778302429327178489874184
7098784089327423525837243478165778258308
3763550524563111921354441214900670063705
7206613734916668298454867202082902877
781930808297965586336564279845387193709
970427362994630097912962380191231411584
97934283535012592311054262983803458137
577172483539327552092914095763115864447
95677945861632641249616134571134226463
476277764693301449931703917561032419154
4180310521345853156553806129656014841659
325962921705647162018326380783447424870
581478279948195357803597893152151793
992247495982743492848656153364814673114
96778472171698617675554743877423671352
45735413333451089195174152595576239823
859942536662550475618755570881019185895
7162718279864024719755189235091057373
56405535096571341636148616414985788467
7677293328796169242020007047713692335561
933858493933220993137632294492494216384
495635497924761860137694351191889633768
4253921054964464635097503741698074143359
786827245789594417681713217971253332412
53671121430514079691199241382024754152
817939288783416750778444018270341632
4190501321379998211193973940748733704800
4181193324718189694162187865651966484081
6739371976608751206014307555661331532047
9884325053984612947379759838177714929549
9919179523895037304452781193189042936954
138878385814334982855523847802767816583
79989896860317395311428501455942454149
96999504922781449699950931987224911859
50151461392978578730986059160754967993
980511807702569760237123849985090105091
3226043672859543017186940501495629950
5994137085925386349960825359596898002747
732210981815293871310397334862915415482
31517871874836931682409329611277432403
7377279460173665148745048222881280498038
95043288720297648075695777467505661823
757186174850501417723395557322761159080
415959625162486189413436796984327599431
110389712624187655037179362917919776778
1060519022825503162886134780131332641942
806138343337905142927187431621332043587
45573181589043097620915766785884819625
2885505710404697392234538020482878505227
618714352463597269986448255187836793701
5309101952814277738457866092764986159865
959318785257280204754915131442042416735
447044602121943852114640490465871646156
501755213351823761283246557267344486
3392621338254561226252909427114965496759
112276647008796663922331125230487677549
5025548899623134476015097171256788374456
73137699580122676825440451794541485720
199461977385636850405033260593523132934
726161061969995638559798052395298097455
538735024832736247426691713775109601
928234259293237944779254477438725983519
7284868166719396503097426605469082073538
50013172433198559633894248403368874281
5166842348063198130141075162484951999081
64737620565875837517487185433055133605
680443453517251360835091912438943832777
497862698246926245037141056443614623364
8019341155731895806527621408118269523177
51383279986759468149452651691278723468
22373890523517784096066743144832312362
4542355536587301106513344248994872954394
16559072577686832683708731518162623894
6849731690274728915184629587798410281099
4023852199686714368091148802272918135938
802666693654363652085495104789744993665
736959146269998985929233420675264735713
9316497626314986512823315994484962825692
181866974244323032945145794465924543596
61865811301986444604354127688357965161
106683439191896575607642726306681994912
781310346713707801637844808645915269340
10572070642054555512220758030573060641
9892592283225278971356583383190045058403
1218977084814789934932275831554713427007
59585193520192878228644234076328173733
7267389230831041122508172358281295612
890111537501695736168886812461696317682
988091875183120138872639959866925221537
2330618359329301350247838752258878041035
278537198310688870197354756160156313795
314164307017038974868506762596535284488
29427579729185825725435476873039068066
75472851163240290357202557105097307436
96992047212345580658374304452597278806
646661922085324958351085215425191243778
6465144329997541937993733021182968073967
769966024386603183460356024126958037327
7161824978429893454257106331806291241462
344490337385904794938082112587922493527
1038743963082750816662693601822153863779
837831915580514694094029713078981464082
9729226128633824980199482359255348651370
6005995923264092974063141759586950908420
3106621853333628163428733818696186147
70664201516916259449263738917617473790
3831682717191354923012656253634905598
6073995926111587911927538946354029647645
863565676844846221410071203184736038521
4937328026975650522883118754286277566540
91883746852347042981133179254924601258
239188091149889755649041924136770552860
545024712622580939133768459345915136045
4256794821714009819611494359415147964353
596015546981433409990191150756752053653
465741366279833154124846573783683328826
14432187519969977070211934902181766175
46754547701758026728779666558632058768
717583072657629991367759290868523281536
68395399269831344684980811612928536679
549943211323107870591201969337149437805
6690451735975620592024364891936394483799
427630523326677295530901031616613682249
79637720261673461584703991690213274991
4726507414806232788094816891442756376230
73146971477416772387755128861356275078
6057631117608358884738698671904356941418
4287167980507864947382662750105268185013
60248393928067131276685881318328456559
224475788776189171170974398746997176872
684234261724616245946363429791946328966
1188398112551064624833831355107522558651
1833439475175117112284666683928574451919
1789855353699984205951815088759678421991
342357356905036687759954219892138151464
823381203167992277067399915262486771332
53346486493717632823723368633477632127
867889093726425859244096583148770709990
31131289194493352356546558116382768538
7647528657176915811203820918517302309
45838605625581831979630171141143154211
52731330459272919617767077353781397160
5293953191128857814875220821276514016
8971551020387919374432890934319305667
7091660156789741980759318426896763267148
8166531113163393420177671428206275562474
53635744798550384153903861502184875604
49866637782038186277431717650884693826
350664207687689733785345455110045862359
721359809020961868281157220217161224273
955736384474454934376433918315278536833
659370361386786777089738381282446367
84658889657205091867498164468624456778
1290561343694096816730980773586355052
932299471545522918093592733448644984524
4530812459692274126677722815479428337095
8259449738793198307952381516654611233784
3450935745647014907462128458310938582463
86935898200794078424224548872207811812
329060408799386199207735985919871598018
696898511248325241851706406447233924771
4181960080437422833070009340686161093322
1893587686891985197845493481918733665977
4857909486861788280559688497440738132000
15593837153077818463768158882473981801
6148242663142944127514883314452942178281
2865885587265080739876773336304123089593
15378557208425528558770257295683853364
46951456335812183157285913063954202722
7045285937589278817626058600922791722004
748819575320735783487418149121683902177
506315908346583577072754119864629594286
4414506948378249688385071946570246791179
7832868669809221997365057367822316785799
951529780722665695413644265217462700
1612171850918022108141915559677418541641
891677978666340757997094224986444864318
5496707280753505583789931834360258717562
86997385912649979315948300768382497324
3007900743795361936238856562500897831764
5656895224055196849990579484487141780
65515958827912696425511384476242115
198766677680781639857644929721877002083
299654543575513888301038787115166689529
110899837102698736349353902582311775608
2212728672614226272636206771681293983006
9393617071328042548616641562246721298448
40557084160455267204336495393291384850
809669379181831901159411515765376938273
8904855425432181517912539554516405218
965283066453564458577262301873635022332
7719715175999525263835325492813088933198
2998771898214279599471168204652184582175
3479990898242170543380913583112311421375
89248992638177232788944635929926671832
794220563471685238857544274453582169469
262140322987637675769192658308686471460
85496066498244589009974670518346762839
873626007133414132343549923382584412238
3662486591743055529498277626142488551521
967236051087165212111382306992871737385
13048338137730945740327574623837061717
105925266509834731132119243791466111911
9329293561621945027995663914641927790
6327758812372178521499457999702913427104
8989614891321301572879543246172026494335
541046897727287261419822043794626675762
8623546846503760148854655875996025353471
9916866571343389858010115277382393096594
6838269950347129436519406121416423601
9416831682136764644744624700478189096443
957384626762444181437434179816012726438
488383684883380330318349631594084243627
8146635762812715857998326946465062019534
747525526726256444188176445956320826401
5168215448917933121726586748928828282029
2901343285741204630758713627844733778100
90714514852521671322148307117233729621
298573377676246192799744637994327965040
94175294107807464986795079989267134643
15058303937832685671011746365073837929
5755459776588127759907757475316555963
814822092311888116094453452518158903898
598997332049696541565042349791692843
1678237390521628843186751343973312782013
878696628442526284205302671235591781
9576628042764718698627839217567626364982
1631697791560932660418152981014203330
189976673169483288728913560409790411965
4669830535902338637248528918459051995
1739841847523274587413869529749670153454
3587765615411884423235927552948203675
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]