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
510859581651419476452254523216820116807
5767185777519841786565816314077373354
271394856403647568901938248961411052766
653153976840476413228070915896555993892
4831960841986093350790406421178115388453
6096631946359040813282365215451435055048
6716739470661287509011019401792471543478
28718887397075639541073452713615523
37774012704921811814823610452933584420
509916409567750667381021154311075499672
785821467855851279678036752172217636026
4005375930244040987411513746167342944189
6977439118978290363334478015839830505181
10000471183572696305474636865606636891718
502457924359494703136694178280818072325
65035792980059979429910552575114752239
6138501117438885716127007577489187864894
22137849963825435726129325759784335101
21172648262087289178583136490557615559
4004204082653597319178775948111347108824
603193861840600411342344960284748957184
934177171707648125533585140264528174915
6739503782978547254347082882401518366234
711863871785683634438707514632584471928
298122994653763663041949442830160015404
5556302812417984698450252032918063401750
7096739250601217573296259289972249783546
601471404205197247071129901285173187411
8655722227465749605853599523935583335586
93514929450198950359897722238734637442
7499376614977451592241296278563286431000
299963721033476143460203582762475053610
30714145421228655085646802931812476400
45605734632185774818129670473186464951
743993927158583614434989410781241816695
204123323971693527383375262502811942047
3973786171562149784123186961218688958801
663330189881251441648784621568241718266
8577989757194485887941275931353518156159
97213160922677361392427657251760958803
2492967352611055822968798373995762398179
8971345786808526858330252812726288956892
8162688596957753265561064865502599887841
664631454614665981406298971932210456147
5413490235362621641250799646923072921851
82278721349983863992565281298749355046
39743296227767207454689110079085592612
7157465812319797161888208937267647774230
785542454405681358264977448227711237616
129948641601127466734605459109690157933
842434578762383296117734930761672641058
7636710636632434113760463973906637644005
43451667431734965767252136834375554506
51562465471381073723199509654458279723
3843928851068593289739475735594648819417
49854989609328344638243897544117948089
556725028801212654998894322977942371777
783738225537629461351194895329012727223
482217686355215837185619849339946975670
2026128215347891808976768695931927306
430349731927119910096039890921544808761
132633968957829925544462950620138505200
584451895304765054571610547529778315203
629834649236807592988368193634579519
16114888611768413175619358624583674174
207078816510381098653330705845642772447
9422100614012449647531220897528587083
10065760496871659529164872110783521216
70524789925910631687759680145316522484
8104372825312349297393044859300281539868
5024734483531612915121304497844179775720
907653872060170426677254498445714163675
51497541546811522023301457281443573697
701855932688827818470784480349568363516
5232184236104009162052227114322538006007
9218850314281887601388141185621910718971
6098755471589702156541644372575027088050
9737674518208385936442884537975349751
8165113566988060570632931529267395978923
16174124918455143395801520592391249264
192488001878390670863247963833622348661
273829723421481332167155063807875529960
997253715668917424983729041980088329759
800612729629106853768140181640686010182
9552530547777794291647284840762881537125
46806338900514952974446616131296765142
5841348843559316115419625696794238442
2547996941027439550437887877314610583520
76817470455563024938919929913865732380
548932988629914664123586605871215267635
7299518963992873471082454242726870249980
70252115820557614173323841715246676854
5422800168169947827223232832376468328587
908449483555706548173986618444928677568
878770459244449584497224289553562764020
26871413193604235099913650632862262
7187722176713099743335115824918914356
176332374519825049458406479556737713
7031667895310115591579862792481277438
932839268506171826259057037733626594670
754064705468743978078805802391434716985
6794899587688732315577342347868832701632
8428515855122796385795635113703184227529
4300488271401459684913099296678981051773
156115738832628326168095310666026722155
927640077204865778326517304069951607131
81142252120925987938267710473380059813
8728842071155192603050502923100422765772
91936289564157117737853392879461261070
5578810312357772119537535093219937418246
3470794059939653522919082148239131314900
1588394352194983576617820356298772445
4211283518522892896432598819877350216716
942161933788480708612738804531857038842
9856977839981431211632215079139317595074
357496633735263345197493126683720598885
230353309263694586738186051319112398942
405531982971987370198384118414749776857
814575393108458601645079740930146438583
1447279820831662686769724888281964415189
3052560239823286320243521595865917731631
995394447019816143113792186550675180938
9654618455182793656055398860339468257364
18494236128772147581478670510837568861
356818699554561506794003323958999354985
9796301011028250392759037341922955759434
8341370479259768612291569578297728774360
169395239334504580167628078661238574353
948970489049137295635381538630713895702
5291139894674575800267031615146258921214
892626852238768218464095991180923993998
459524184688448885212155138846356575958
39772262128659871815378963842228405511
3867175048922145347386838969476499166411
9606595470718387147094468060950638768830
387947973239662049319517443048613226
98306680792108861665963569835444635616
883929862951121591605875961324106077732
2491455080447464961285355673709358853114
437485842618512927058910431137876839632
97952674327757239752285606272015812567
9477948039701986113115215858728936524060
431883907644882582718975464314616952849
1800305176706694970958038223560267167322
582320933031218674502594291661554024543
564167371087607522178137307376270867345
505487924691422317058697560817165336426
4874738643467704353278273354354352768644
735290757775701338515815382899565776266
848742785761018470697305260178624221395
9455277575286802984961238132117371149896
55712785207851696325264877246186258547
8070924410012749252314491369594610564889
889614918476940516075924516756331104738
69553787980794323766231761787522926289
811316836334192680564136122934970259236
89312125644546876721620620579921173995
55141314952463110534524999522811957768
160115818227185412741496824159583285742
89439973874255068655010973847832952380
17456686506580430346504817622959651217
626937191017196387835494112816799667978
398526037462112348177505618643286796950
341299502197960661327473746969789959065
2015885062864335746381888817475548868623
393461259596803707691766205779988273467
303957919637370774226464119594265682622
14883834879169892247357700450414354256
268292152565183511711028905933679379832
4258200134892501361249214357030843894
14182404088368912094217814570164076721
745785443259830346071727955748141599293
3370844045878702158617390162053233065
2170905881903339959167209202217135302652
158644674609564230211759583835844166805
1413377633739929777366203512262963041
9128876030127426421658506132256666322432
25298977214751122574438989942830114581
199059465412141629766814211870414578855
9636534148736237497843006579453117814958
112726078943481237488632969650275988479
6794510676171637935794026468740616289573
1699490417412177299420123378309046914174
713788125992852930192132701456515898140
9048778297169600189970911409366366629437
92896192648854569326932346947431541966
3720980355209698701254276836223247588
957536359227238618784801638459792083588
6241995572652453508618622460764965996806
823828632142639423116046310912954049952
3186386857913955258240405295574712478019
9645817097891168151401982331322862164
20382965265931132040275258163203102311
832664949298864060022250982666457324533
375225357648441119346181122121484741627
9460605216815002253871345183955842922121
5300729875061876922902284413340274268
35114553762325183077295266171647144814
51036676793761715293419978939353484253
56489957886482932997926996696926876068
984083067208851952421208970616344536218
1262433463837677610856444077865827904175
727455018968687925290502560556830822949
98938058628482354611054750735548295108
3071974126039543615185769657647351245489
5122100648533998796056159766148313163734
682295336163787290169055063385818444489
45607865158332546252401790919422796037
814127447785273895653604004200315782345
6491730646509359947994177975793148466625
44849615561786534426887144679019961024
9124897029397889781930071763962994279599
892424256974764564913026409045218201886
5108996714252697346638639062535759153479
525451903896988111566924700680993951081
24341486399120549317573301800726989347
474042544493276445741530662693097165733
9989885193073423272514822754432693908
5111820932386880383616224561458464342
315231165368861351357137260814417996892
1937808974756420462392423583979098205022
5777850431767167915884695755721854588175
383426199050699832208199816397898484171
6159273475163003468193418262934534166935
2278320950551152587255281302283612174236
363365445199589230328007455847860685751
8290357276708459917984832585841914633108
8178151192891755621682287849541422598
1541879321665468191396416815778795718
15203729796301890663076886893798081704
9504435215714497484672845654487051006
463883396347263257916054693558033499349
3839594226207622660080383350473143478716
5650721792515766323880634344349335821862
180294009044291264793224718385858957715
946081404162657948472747322136303555770
2282538899782369856637477137436194551013
1543949876211840945777689591988795511816
21986900883374809969861673015741087458
4887356463604745824499839693376069868292
868460514969624631084895991667796349529
39423064308232117300111510831593345910
5447262106338783047383874982087876882
624576048623540271314814726751379556341
517314573191464060878718872327172117991
714711623608178549682654711763268013409
328281398811111484244008985841394528497
38425971550391672889165378602017311090
690577244630768763301572245567086346
538943822921744371851506876190763333400
115744374125000767715515181404277654151
588714283229971286102557245990417512589
89087450744633056054454330458559858589
2331831553799617258650822374103466805949
3414954737524150346889266247818219404716
9820629026452096911866599545108759214511
9932384727444895192175745465480333087473
604736053488119463214703150335214351870
614534854253877717581996358338483113297
500564655517355940821136926558942043869
441837146992928331088702363907762399956
8131990034932411847493939942776332714268
7189516221571212382967515648381591199
7102193320309671679684852287164648302
235016616224255141043743913969264279882
9180998063364521790330949019591953381331
54799294291040954744973049276756169479
6687530922833658516036123252896917323720
375387304475675111674495203015147353848
272952227246526161814433780381935776138
817042693585596917099542268232972968902
1999299666947349122012214230770598782091
6246744671967504743257469760393633713254
4675428829569640697060928044854432809023
5219593349043682729194831605426958291004
924895778235704625519014961587393644
50777396594573216527982425886069228127
6894242386918869077578893538138254496
43653867521537543386352112712569659943
481556392860655776007932519367845922783
534117746837817341929216435518683841389
131449469460396959631280540867333732
1490813767149317994962473272568343056885
859159339635765952938524549056324544931
264639727537238916872659594322564735583
2405583595568568489669816283222848721282
3158953998889859480467436523280469576272
462247294180153064181722350030886127205
44757727024706683768113849266466297988
985388154965400759855042980984738963258
1779733019036164468148249195878925787790
3490750387018768934836122193876647276511
535832139904370898269738498499621082082
471497276914486937305902366894158872588
683896347691518956234181067874421418565
741910773429470438738418495135789528466
3172585235984130544672012608612164496187
4799969537067757405680025761734722337980
2982477641741244687462718578749586368
774451865559346382888371747625917375
573363787759986963537376175794841217696
1051602516719187368592514183554692416493
4195909113837850809023581713225716922305
137756737205679916667019794215268997093
525389458816443487112193286486137846407
27047353191263791160792025898094584276
18311148587387764998760949838187273291
105457951172951130538581839882472075845
9329423321634045972469181445579241081
2121683714151810342931481032147915921751
3439849189438950417380415059743645183902
335517459868231928568955732508364427690
384028801442624747761233353535559487918
752840644771687137474579266253871778420
25666831266726694583558391166442744546
108533026253599235878085540178053023889
792674199225239791474107532355083253156
9255496757292900199711386732320349937660
430882599591163747841427380276666272899
806690367154305599561231091912976953748
9705414733492899613906794731560319473
499217217294673129849167461414637618781
267814616800946684883325201784214762260
877576692643838779415532370038817681971
155233539304664005248978923621319083
5172978696207133700970924887150811299948
784965131427305451497696477329712969231
87493094835135404921330521238726362633
6344716514664391763938660511189207301
88635652763722126152891736030874109521
8031400681427293938525149174591981963187
5222655863076010837976325274263963956170
3766873090787976549552817213479281165566
8345308429939656677319099237023597943
67977836505081557122504388170342381402
456891503899241867755575347622296582740
31159384617684924484852884276496549589
4093375558850416059153793158275214393
513228324655952666340037085416580931039
299675612211895507995713650709540401429
5440647688143445604338887919225911962491
184361112676921532388348102460983601193
223622114146701845254715399511275471107
18084195389213120294104078355250434256
8717701079638837465718067166185397640
180246217764704379839219156104725411869
8409288474002102945327788159199491828652
711371413092462460446583679440865599360
37351796936753683077051776660374507050
696338361992465390712543230421532904903
364064541673477665959135676362722858782
54612201945469744735876859761170043398
8528426421016590962680968170860612163349
9483647080149919116199217653727883301292
395121121072301380620704819146439191904
142230817656709375068193059770033204501
151023989685433066586485414613239053348
575840051828571230891419686219094666100
9531331271172661956732458947307345251813
8897625012450425359397518546519854870
42556914084334702441809912733969332794
7850711835634263843426878173889335994804
3724496018404268528814925671993730729003
6329835885131496691521035717438179661468
190192966213404119648909607598865159583
1069960268981285306580757096782646926786
87629356698357914039724769257936905552
82466556910973223959851258818897063366
7716923382937819327284020760268277303
21224902243121318932850245618173327104
948844067643502241050308031979984986037
44823509595296530077921218844821852927
804213302051419219718893434811052526751
9285769570728426291864696291643049172961
4850455384019720625294632325792741301335
8884994782619599870772422670292876844347
78586723848616432490505593444015417266
64977081876537392036276452787046692511
808451159105169190119174495831052463694
5076600189664394190534255222666996106647
8531717355069545616268592525226677415290
23653446509566514414452463935178979125
61618921464114322881844235767179348
126217061052973164012137383723217593596
961421129157916496590643911946362788455
148478001235523724385032795889323449138
706223783813755604598461314647966503843
379577231873704354715329891768247953389
835081274996459079139809304882417649840
9754626559795753540058135684657764879381
4587691690620942184411571464729275933
2415870828751979496194148585832532698156
6373514189712712128726080243914177695
2114150613347172814624964313545465498655
18785459873937437432066945614188902474
326816465812563291315454049385023482423
6447898941564315241346039303337698909817
7844411817232438952318778675545143792
318335692658675463158171746138911174307
962667067731550163481657143658130759647
8384324275697775936073999243590610995851
977762748096775297778647482505854337101
524055604903455964934924912979853731876
8939970331805242160315662870629896575486
85129777641154575372582467605736302052
93303916258717119839692043932547718652
36708802616462785358591946332173893321
7607629473708592318942445838412058798387
479694094804870161621365633475207162072
2598683166311468476479609561710477285201
9210606971072656846344694399346915242675
5800232760955362970724631518752229677509
888228216381382870099800788826781152997
20085120193912285637322509689024935665
982171218732338430103264151750386839236
5910463514449138487652122576191558922
41227350543610095094875385319158907950
4607929598364718238411994764956145136798
45453060833813081114318696608961321778
2628240313584136557274414450050022736
2756197320697773906252448052590488567
64073704659349184773447828146844779803
920411748885984135239360123510488783103
47536662484145798267846867449983608384
90242619794202334624681546394077481756
1080123534475575279132659926329048664042
814898646292366448570593495802880876379
225164401645358020834068469780494579895
7178130979521411191443432528623158809179
1866537567033454100040369934895471881466
3525319995599575471557879934813761987240
3983826646151074981629499901924310187911
1843191311009383743496829825166462492395
279587942983631583476353148597725034960
8573818169955729863584590906339968459
9216263484632474598213189824905925773
6325412285054799786161816988203310818941
920229634791105964261023624622126756687
820675575658433128393352061423193674217
922580532646515912453809359787854086660
643187791151608069438413885296993666803
8698965226147334498074826160267145225533
4268140711019248997943256048850835323546
1601570499388238115450906163864730627847
8759103487933835126590909430626560323703
7669939510499136844543193051949443575780
7466561147081124838461851390177112301
19014114270693119960972180135805323477
5070145625067613303852426754730704065
454168966089939703388458445422594921395
26137747567429299733957847650611046058
7679452791671520813647572618541286807030
8823821198555069146851326494609115447597
829539138287914421819193000193554073099
81289116674245838023342506525580426139
4719896023958455320087242132113139446691
3546410219376285369230683486310670858721
2086560335934886221483737468471260292790
768081782204194335665898344282363092276
500762703202419835691637953679311377463
813068731609533230273427689410671704895
2545553492112007163545322197668978214484
53171696781684854095160548813872957834
3367355599139388503275055158126277578055
5605559565859815803660047910131771225627
281268691868369393330166288955548888649
842494746919541674002168864479660633715
9716358113785688992798547252927266669695
28645265868190275161790545831888118007
4993654540966729517944955989759107950
820953383713115154943754297171172176538
70282482514468551613752280604925491507
4420836257153253177760842297659197124199
6479430573925809321332112917305251584807
269024556172234174553690690895951883183
7613562675127964382391962944302884322377
19963753868454193298619375761605806316
6632848439793850842914517518935993441871
489591142579921148884405968993923568289
317517493335645296017904605717117219716
5429485158546629443815951724384090378004
265398733169788123664485226927113051089
917878182261223339535896287668645652160
934373301927205374601484839292098766621
957190623473237171436879039937555745693
57622387667834992890655244403433479553
7789618895324022735439767446665041295289
900336829669304538558285574793168465757
8866689887747937691984128091269211214609
750091742374619750229447321721497953644
3034176737702108521697979619384223089492
80689666815691667439028988919272209632
30333315963373277511114349842399309725
959010967102069729078895433774058737296
3683216952948644943877222335438840903646
4302157538944392290197634591349734596124
8094509169351562117846319996272084159
6950237049217226778756705227211630781546
673246326952479342336532826119974343011
908921988511132648816590944133603146804
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]