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
6759713921345314126266013385122037875873
864382466822988820763360933353756088151
654319823360010628078918482111738972
8243335592061273256466662793198375789389
7094302114986365955384328993979181135062
693188081494702258580009639757223509589
9285235290583576411757826143937469949796
44228221378322727301641147842133573670
610729894219189617533313294121061957247
4278410311909174347260487727337147034585
9455157759533027929308539131616195531
59982704890137720253959855312892836870
1522575834943652931938665475753126512580
4868713145889346793098628181221999683200
7158591827829706769975792966566025037378
7257618668778179771472117240302085738528
725535359071631946235805338660975338835
6596543738294939153313977534975913942681
892488299249783649528311784678920717814
486429684010449122812072189551956125552
7636524410085039987312141624661198146459
302764093787949588772165524285610873573
5585701616159244745597334734801137391063
352243215155761133029549629705717454775
156090953164148039905862421615075729935
24412365497926381818987954385899321436
6813967412857696314759811557562426228
7538200040854910225918399269695339145875
172416355515800916934153925552731468694
953921174275945295331843799868954006415
26871057662365970275981297418013148552
620820601883676387094212638368297398033
1599405489872983723876664592398535462035
6809663441702803574444174917184088632185
5763372775719677310430138918341518874160
778667472684700674339298857235714696588
482688225287700987091015287185411482800
6075755466676938691116063938254292262285
56451919831497126581888399695212185394
364935277900891851587978449758770413321
591565474385389864861467206735304248424
91981892668008226980901006857955259385
7008947620439789770390092911463663906446
893183116844553748777448013712054071752
521077859771425053365415963556618965004
638723415710591898252857652531285383723
78169823277517022103205493968074743714
18554962959821188444898884161174912
2696947759846297954843111930769502599
66384392460140416654697718351277633442
8648536878126314658464545878145823501044
13251147942690095863863847769475253991
801064626927374864020747039756615582924
5509184890307383533245162439937018014086
69434924714387112851058993341016862453
306737381354490077457412121123066886057
41661789481512591319513863904982239975
682520788895630617342038741559913977381
780694892483509085781984951138129673449
1429967026405950213932455667876994782077
4639411448024258931100001059786379228632
508337503437439582124590113499891179666
502862222246367072343811932200695023296
2661302035704207661083618663704010914950
7292428291922114712293778626249242402
154611956075681353666579147980118162466
1845897727831444636147596292972556607636
666959845717774247397151527721980682813
8535811129416971175181136813267256389817
2481158438174708217318707734672719418648
755349521394202315635639169128576585611
207540884305679877064479589383966256239
6986277981346419531611914440363060256814
312857066775431949081988063925438015980
258771354415289429422142737886521336699
351657491355950661915622155773725537431
45705900333941472658343468348441141468
7895910393549511122076036290253353276386
2468864945308469533695735555526844432324
75526061813513569700115156161969520455
8147354654895466956213741103196845315186
6715588540602304470633146973475076163204
14752678940147362220616199803627529383
688443355269420631166580382524012601025
276966682584711980554654527377993021894
978553931127180141326307215182165574995
6069631032213635304344455905262841564596
450014081143653753160586497747081127251
959776744238807999412257161411274548479
680998009545695771166592490435838678670
1517186450062520763531284402655669595408
198924924180651119733563197929753011640
3557775474742306639634374078216121453770
773556857517616738653798881187656241596
23338586153440760136619918141298097143
8041984695138645470483172817909383927239
932835770642738354921882089752974313
409974172382931811435187646162546413912
849824737146659914225297129911178638940
3542865212873639687566508921808445268252
63011675474279914069794302510091797400
912906165999098398087708744244839841
7212957723654914431412519910846464048396
341398683492172759195276296473015097700
8674789012713350887578863207184615828813
1383412420282049958369375085753140459138
853694269667824896090158764278711599869
2338169475922018981580889288261326237448
634111203839651264812156923189078486674
212630945737043694535282440100133503746
13866143208333757435366817809656602341
486652858456718325291697858354290797869
96102762902962519534801300351419151688
813315084864247586984153172811952243889
29255640644689529626692728940863402938
5584754564619684364650781412572288646647
5463752515259022642064327290413439139896
931123934711996645487878517911106276761
2505870486794405369629092239933855264036
978176825835699365350734275612242793770
12733566945848116239541922576931254
78512474808775348228794100919283637662
506296603359369640792305892296712215725
8320733343705835699685771375597597723415
5526101543673698646919131882413611281577
2738770224881947894951118438845356083310
336973393821841902662833730856482928535
4992585471558618163331313407934039873567
2865678286528275317691998951405947333648
849177562274102884829296219680531433740
8674159067419093219728049407911363791419
3143162982555995965686715625729192619571
9327278788227634364915873210553757212552
7083900520639265241904757173226856706
22908415796314874742576836731536389385
5495561978829788709038355979244916758312
9354109170925433282847142630743043354608
8294802245748653579168113820227030903629
245312215244976664111225392662337719989
9529275416683638322951326193138114541326
269547157588239345238179452532568155939
68527970625340212254991983512941357465
1877993413883846554482079689482339193458
96243238509420079636752452885243711704
502739129544430989564424451393192771598
414534493394667595231826340997862022375
42822714465632648792859310458429644386
722773108504955183123491597711068484
202318069984929537984739065510455061420
3198726910854798697696452916132456347685
9441583370442792412968701024749680399348
3009464471569782993242496199448712713868
9081277876916180835540306364361540172131
9740545182907496378459233529606554184922
93619644160845287172624919608016560
699258358057760092857312542144920989582
1797903016832455938983048363888728592452
1067380868185785608996124870903122291348
60187988697298388371403506141273322692
215969686755940457101385887723662299868
8127932054273152374574718562356513707326
3137288756664841587015729177960187852063
16946812444323046259373253779385679843
2608750255837992926869129055437172401355
6783586173376402800592204999577791048457
6556746444064726322961436383804113986604
94864402865322420895845100769433307635
568357617824710471991148795951880834133
6384669393784545417154263993778221827646
8794536550738066970999969281569451242029
85446994026601942768040222430883927985
3036853566408706622871614827968428009490
48708694327283705336487845472859399579
792842382861839030011447482244320293632
199769621376782057993840535687135099405
118752674076627995014939903385544619052
749700994344972227172244998787343155752
2270452545134371141737264001122046364094
702233841691972044475812664642088136942
25469321624420187913498215391599147671
5534810080308960715518394440191688188649
30164817555165673129528192114196587987
894953463951174080174064181829213108998
18588975523267020078453992960938132044
588543352723491650463930975110731266
7847346219564723888669695051388759737
591640518730627413858245028374513062151
5814965383084596624828766077997625328325
4212511010123141580750194460119634014079
4327366019533462842897574776061349734
7698020534486171060561975912569086760
33686046595539965732469530916435133970
66905051356508405092788405185318422432
817296973714596455645823467482869380231
98779558847846211771691872895961761324
9423473093513285128768382897179382423280
1142700040417634371059798829597283044186
203797246567005722249285975473829222700
77562723318560572568051384670613389445
1957897111719721917931315719466894222317
328844657373866626267030542855303334203
8445349326282084555411187682109933025738
5091816988115524026121132579411541630
701087505195796492839554402759989984845
1541826774944848211386601951225896903330
936494274858822694173952457383157346546
85544952152249640351797219164768094541
7643820483083126338854058041356563664163
198397537401529598367633666210297546825
4339274263283258592342237748972570774061
315348162387497928037443588238846989
74551899357788865851543298837364068
3439922695255651983505520840189469235
460121488476465207031930378797039700
799678014360522155632891987255182565769
653778645008770825353693042134378404095
8057819623643865682566337759744799452
9005747913931370456082649758327454341182
2763627515415725107286243622729738223
8263210778428282849184633880243487303986
9746615522118204928245746211800733188539
9062528994379094904664503062279600543
653627562534189177190898650676748099041
132440395949553690132609863146640247355
903331481902419568988892021879487644005
3034595313871647582012787492551062981
29507137148531303620626742527833416152
908159938208565357234308525950316431681
199660784383245791350917566804938655252
7811350624052603881093226019973114478923
1186990238436120432444585468589745462380
5761834740004173801397937217632649797840
6571198520575242551957859766298296263953
19351503763988946102171912147546639603
6502745351344042238328974071556626674569
4500328277329743666670164352380954392574
8790179258391222820263050528535279423
2690893584615750191448569931439654761614
7401851914865959502264126780770615051010
854605938649076945866208558149182969982
7803923987542796181437858373111629448476
39243347681829658332708184249197392842
54878854507340922369785183462561895419
4748232120578934833419364766309663148528
5844244337771867320167243984852986613695
461029561308339833792279346769878931
6343150765163013155755318439972576391
30519641273056262751429767211569531249
209483662391764642556984575730157389191
747792572571289426156455938864746117549
765254453568068376122611237436487212134
1320595114412163934473942488982775673
1778175317427835013509856146407237690
25649564634536978282325710352549514583
385587876969589247887488609691188399262
9156994537018485366411702710473280874903
186779672227780273509351553725155983887
2147352828869701104077459385209811628347
9102401591843645709971268643885186576078
2342441917426422902265218940811548015741
630279732974369808252132785754154129223
866076066279615657319879793433395457873
46007386512472752575404326971788501011
8043119184027893383738245710475233366717
6412967295648190415065409450790666796509
645121166972403881544625475569544896529
1782959252907094347920126333452628391712
896812560763333926088094642856912156225
6783782542786131633940246978918952493301
650271031982193880161519897985732771333
3050408367651985416286751343208223749911
159937626257405134398461271939886512190
37298704653663689967918410672839881100
763658308534584140782568172873449139
8936844836569804132671046782448062995393
581177881992818378504549787991127073316
5161919621759890267299132617941349504448
1560755750647172252856082998714429295004
1352422987028049775869034374117132765053
8160488886932614109839568354370882349147
8361474946529000553359601745488559902122
887582726455665412259417902456152576247
697916677344127394596769191187118565192
37832109093409780879554701604332707583
818422758442186748473318207563499169156
93558782870870139807256212175078424377
302628931961549345166762849338589798498
488873047291948082298233171518891474968
6985528024292497837134701942164599511267
517554681305164103434805110759462865163
4703976892079357433858743732826237247
4962700130848035461333087377322117043710
743956552781356970739362898466893618024
102491266979397872212101168933185944659
445998145469522901757141315795148088524
721972097716725508157389162551289739365
907584552573108522172575635755797372462
3967489550789115974577929181342730827012
656848627739276625980031852357242246559
3065350858831171638046509431480738789
4064126154153648813098899258070541267
6892444839209234445747789129563494873108
14674439306396895575173565126063556562
5570688428552422809857816187102319815422
871777993277406587454552130901041806696
7874994971671499941013343933345464165544
2571293153977181564512833329132610518065
340989847743684728029993416072229245555
2473705668881909987574357580515739026506
819261494281894792054810202570670584651
97191452556851725624943341835122843368
8132336737625447447833886575885538112597
555070603217816810431952349917073944504
3626824229393770779182676713958485902984
9626966546325495906640337901973247475632
4637771581684115455233693889631108486
485756926185216543771133552934829222303
197623713888497113395808227748668006519
5096764347935865430121417054158864743008
220712250462423615360687023848931752638
580829364287528202594988093678137118207
5970297252292914246393947810316334209
490127811159557176088788815370425052547
499975867214445895636837252422572264067
31366523214951918398677035403986836988
7351829420915289230752436885427197622700
5120748674277024870496255818305331937824
993467455643325581031727912644811508943
3239154153281636363196643846592185288
132246764294329581424612264751857599116
64462751603822546785134149840264703492
18086766437525535636449686421710174249
8980165776919766136391816843888139592423
59371555278138720145477643769666562176
36397499583032636616346862575245984063
6756254063946859546119962740527564127
935133497095638414473318322585643285179
118943523693781888543741322735520314702
2097358543286691403966341078893079002324
301959065057171160495233528268454881255
22064627254472672209306876074014187042
8420611319437282505933868859207530375969
4331764011910969423045404771551539923
52348196775357989915992729491357282750
2935166411004410197476594984148440844141
32661660614877194815054432548978152902
543523528961300594291533659534392921893
502129769947617133733758271133537865351
83043746174644933737978477743122493582
9638798620684202917196741172737792968367
266990452607822737175997273215067568906
143912091086167911150957913481477881786
338737211199841145040713678140271986668
8936835596823946223932924744546516797185
8302580634266242545671339006423849865442
144055187361507243113658447764766342885
2665522224349972130086113693379337622417
1949102990162505156633959099567975446867
751384825764618445933217819437778448141
492419705754551458567587481114177884389
1074752735257870223077506192332330725135
10741783645388108530863058125631608816
1647903653035203746452754430882439153342
504011818208997325157480951721311135239
2183879547238531467734618106179713805754
4184889965656088402481773655130525455657
2247467177499347500821008712716953128575
771192213492146447026220882429197346697
8440866174961057466639333041760771049534
7712230869684504669977374902400691187203
62153447689949331821872216116376985180
568693778536851779985849748194272747622
565638078279382959833019958295994776386
74271169331343037448865631115538816258
9191538679639574436026818862263464619346
7507518035246334731458425457200197785341
827762423873322465860166907406878873355
45466418388894768963586458286979683588
6332915631334150320406627811756604499
395581021458685496343106223424736704639
75935273826587350420773584756477629065
9189480438278765415758726174971934829497
8068162777278315102229588083860057928026
518787647875215432835330294858886508278
891574811289904554565127390424913929045
475514854259652890014742480709843098450
548974114548917764389708023302054196955
4545383838361389473445911357387129713
3592489229513459990826041479516695585499
4722909645617849286075786494749245855877
4987845923318433954418582009804356477851
850658321664022681757422510135544104585
419947966568580294786589778438658684811
3325337031936695568756075996178742599053
309743024335817574076580580179434471414
651713298338961927358583966927884409847
759540064339651144790325454854278804090
774731352022658538980633772917341496089
174092124986264226104672854975479087361
1182154579938072960779262400746147906009
5802812497848677165434073195483222496970
308412042750237934233053884679029281430
5263116743679312440622187702323876486468
57313254262235182440054079402446315
274686026687875877147211516722565703130
88882422748760056563137070328928807896
2043793953643898277377726324930082601601
42139342318468025075687740333415777278
2742602714634719781130666818879366597240
8171586459316642463144225241807059481730
1506747977258509236727392892513062294012
69611786693176541024880695488270209818
64456459117685270079571019512495232604
2937363474357611361818601449264328499629
711780197337194749331762382549080861994
22028379916765014541737334438121439879
314666856592237690719242616877314766649
433845839608389079042077083146077711439
475139438182810811919811768149480487806
84888142022400651715208250968622969633
46917051427229086946310872498786950570
83392084702424774449402582495583643072
1380597798281787962984367587223391368350
59512736145126597671034695315547757225
899140290386201863230502784289371081251
97950605844126804487111448257155915
98691922318888091010885141466886418640
267731123928336550957573764546561679987
2048636266363946739746483910902442101593
8526960789027373803942185099368695298277
4368555339225220241861213257824363441576
1266523286547001497380388162148232718384
48878486234141167578648899130377996400
379910062729128197450515902817887409166
6511596912171156160687166765408431778577
82439671749230685749481976299255464216
85044612967738617498658629186254102082
867491304431553484454896521667952325275
1533748781939228389539385840233837472432
982242163646956175891729377224552422807
24603449478613146426621216871933244390
42437851345318999164665221736521608720
857299163635839160315277687762624317333
8710444575129794726124652872517869075
38991021399196789072822527565371432896
8422628934092929192921769676701467993360
808886817557937671097787780226381099587
6039112046396147222347139404496994422713
1241471712567710925765502166428697451655
403434532622210181191081581619798037416
8238200288108084419374632335432848806058
6628622596313536294752510531650602857
14781015155984976985203592220260002999
3761945905114354249973264998890141957
820878592810957096273477882290492626573
2802352226841859693971492527921826832674
6371660729211433365068067725330129819875
5267804660748514774912558001162421928554
9610183380603754340784486116444084725787
945287342696505708779417069720673884431
10858462118414539292585317146246605320
98819441420082068508419844164243826066
34893134646852535386935619556541553701
7629640173589933957438018889430728109584
7419566038511985544628228117175357456906
58931860976692924774418597438490329
640760516977596808089197523809723839400
8348510963383790108935278535554729418273
6481605251187288746715252585518212473187
573585757930642575186016765332067251129
2504850241667935675823069348341872108485
292693797614136735467614722269763878471
87727135533234651065531038527576072792
805163713284807734127716471730667698578
650995474776676211229839718496773641228
1230427212083801318442292705974987727884
59687704599356991793874889533058093451
760052171920677262874987203680187681515
1055813594952554866317444861838698212895
50238696313311731852070253345727661384
7060498937607343421634901708136576035885
675079799096353528609241418628477443101
2397465158012969500884276887677068627692
6454920266536506878227793916268867238661
25965163773510157878142565811075294295
986783893089468156311231698523324769426
664612771655909421766911148732345549284
49932717783851470262584834943493019830
4683426335139285824531632287092449131
9788246892668633246873034633265072064279
1429672022183817482975657315461990445711
2687810131996585410157977499761648992799
8208807732676735530452705512431022177504
137937959421681309152267391913275525242
91798867628522199029156008610758251260
3946172439462414886677758734985197511697
53475488974265939451599981263559920469
19025052863018197293918463754223073033
1017863722997241234216571359745282855851
2512264837826531672814069266663384819736
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]