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
5729155566105869674250973059670753246885
4588584123996516369773344264357993765147
3023482560743006612655616050846935323461
344804215986121150291137909321970023184
778623564942155837192316186819138779598
3586964383535231226832847138716653492088
969354168912368656621699166610029717535
8369547465146314257946637466425462402
8727977587935852894194011612493777892893
5279469622879248978381874307548360351502
23375274974406273152636944448382135052
368883775604421956914056016152115465778
15873961722957371309522545039826425268
3502821519603704230158821513985555308375
2734320190383772775642164024597753997926
53374744755230948460498357524565443418
94872080924751784577515857382113419
1353726192511246312841422298707267954234
7369161497676547295198185076758891421426
78856987433472906087243692796141345265
43051692472230059285354136165267477181
25557084771982755577986665170455768316
5215415698427804713492918322753152607509
98328183931342567433586967879407857885
15997038968285597268932669847785505327
295123658486401256754497598291576589314
3369228910825671553169959797196001230
64574316911263516598402666925731764867
48513105822701036472956965853612618843
2074809213076308343029277282514089141939
66647088580291613146408989775605720208
98619130879073615262541762892202051263
1743755540444808649416661016420554756824
3141476113142437260615543217168397597584
46761623392830785413566924661351516826
2124685514674382729855438556990840996531
601820082656601592213664249945794268973
294843221877684694686386412906429262239
20799531422021213532980267418891277468
761654954526163757150185385377733278132
2440797921424537722373882875653829636894
887028252895356854762413702341855632147
5456211267432246996636443813446880626842
257641167942702760311506276666418319255
428262082803506044337682627814784748855
750864163070883687738263454797873886030
160297868786439836279727961871149669109
82003770960371063294380097568164711850
8844844789931012369772850578698238868
378424974037290656126245362590036528167
387763908390900335643475705533152604322
133650442643378771055724584354066304600
39016822379743615868864836940206064356
644595514402577330749946604327689486228
6229754825169369986390712927581556025341
261198922614197228186767669824591926620
355230530922489759374572463976594299172
8480999117741972209278685661454197943970
9220186876035993438687414298880775189063
4606632556039278901427813582861433198
54933322513306725574409925308537793258
144212522424653183174764048721677585285
45555409746891305618306620449527645632
6738636558681839613817947919147492692001
692489932467640010397197631790273188541
4353461717485711668580081772233573071132
5687587973726626388723558728769594476046
55361475819631960396425087608917043197
104811925819524079186079957530958865916
6065903520033679202980407756572716585769
129201734888118864191884125528945982038
4349269616354617255840461533711251845508
2601864863885833428034345252381109336
6835106398701259549542966282248422461
5671188773497421659829959065585237455405
4235589014281938766528306464356059192688
5200196956934471337986448706672362234525
388335674347753072456608576955657499593
171552495444318991086555367698971184053
2736378548612760192642066543985263889
8095821745565670655290908144757247009641
743317854895101964876646719433039513212
5947375232153696285533959259578385294488
2988627496536467702913548054960272732182
4163393190674295432227429120138374094628
958569384535339829783795620692526517247
480987752368800151049506303796699595076
462225392187605617414279637919238171
43883593466883822684551615464978131718
6703803639913972818375641422838797808214
3598536711952318207145586737210953052794
580554378475495291442606714425739261198
782615190874213694033739570220175366259
4572120124872567568284319581420481464583
3905128444698560648529674248952059444446
398947424680548124345782709556263366058
102978201757763084302202931765575668432
874447529998872397979013752627819528526
467378475033885161038408437338224134002
626776715407423396651865755647687086103
1508331982764685993485434255815195467117
736350628396622618632145827475287423076
13903181173091822685978739434057363918
3408247995390453624718531163618572852
660149851403612286313204761182550611858
796837271663481354073506441168675769361
892585396608980844155169235033124067734
731761188665228482573778553242438773788
9074117387392052256146811325309474256986
5924448176688297613264348375139895836259
495862628058211140632224330986244918953
4587818982027965381358249053729412068
3162331978397057445695173946386295188787
918578223857771149633602418457681667878
985571968157270250188597191347243077913
3486387129275247618113667203345760305774
955310371158118792976077286187994174981
496416286802867022461027690547199358100
21488768981244875848713363767914659743
9123769837294924374254008331071825648
144575012889471313879721964627152223572
4537932620707760821640121321484168159201
79466791660020882796691898969015334622
2971535460406442342895319868443882665093
5460981614357090469815558278289640378121
6077524766192068706472357349471698082641
68757546702267635108957447436939746763
531831209005780963386747713520699771514
2256297825667465977494985472374233833
452860553752559638804393602062217226305
34153197451319387442850759295341570
9493867577215568191723639016107941672
72959079922849832525364003152048657260
7439107818035094419115589047608821742367
550318977369118304411652395699675092718
558599872892940228269263532336345897367
989184862539554792680432309595390432556
2875317756515067447881936706143270585633
6433643683944244251140134470556957322874
7513647687282965934753108772898932061596
7165626059768250276793542511274518331952
639895056952937899329595143642352199373
40764803652842619476318255314225218574
7270415114394995465943805579307780539506
1665259664368809352665128414141213546958
87787565350164670886275391809089378488
293799586508231534568096531901539516195
6214680037839192951314675510815411564850
39332727907745119268593468811028197248
849298437743952390515301746241363676023
162349554228741512549702318816036034499
8189448339525420857256088620829742868785
749684769495910801323247878977996717676
84572011307544078705702976478341223415
8642129839659579930011956773576955361549
7021535588652346666865942773770746642879
9350112848917836512517374294951632862031
446560136199766750255749116185523855282
6055744437765260705274203380573506438
909887235075826949317984259822634616498
21686324923147240941701147901039149633
9353541727975570491560885833626540485190
7622140764413339630018835710866828087693
4459379728887189982484252384412373586
4410164615353462496781474536804085884713
69967189664250897037249709420917376063
488471137559998569243835985943672451585
686815946342649926331262912463293031
4287983233189683733609374308052563226
8295981056426700848429702143639361662221
9526156413811797731587277252414368706425
8643784897614500907057618671949813455224
27973262460466012801978986082557321160
942618957735189115592725457163732675155
991326216127541215046674464625937537192
5334275119642025347248495233743029567925
87758143923647296598255584312823348795
75249711756558711065624811533226153128
615118881211697962483007590588351658133
3830377840033924654337195251927925734615
71363380334557464026043499282276294172
3814279943807016987149158864340763944284
5460284171816721389825083821341173320
2276207087556031581665047331412119981587
35792422699059657980889491445713549674
67628350661555903071722663673085994619
3276700411105495554857734441889376478856
3655917878285699391588794487318136343898
474166344289794793410775355661758191433
933523857625708956475388424619253492
6519153574578576480732168521884892684389
8031278543743073863650383191548039784
26161398183824883062552564683491095207
7126670190232542756935762821247860445360
288332726800819989125632566384062036988
87722539547129129632634749626732593228
248484924853266959075101727904543842040
247965261454637980479420949176077878162
916051726960287622126824273880168041378
333610468208092381627188900136595371439
853638688699707281871409662292890671101
7285758096842412449893464069195948683826
49564833126735667607620832465257768172
3390158251619751755064878603981858352291
3358857798023706323696325372976840545381
8376437572185137247937863212642731804157
989567673615258465250921166155586507786
131768747919605913842106854417647035858
8557772415407530987933998862744949998860
895074814812771746511507890843509815294
12498953884295320872303992868398004126
777837116374842731226139050594628338452
1183489188419374414429143267293458196052
60104890916325745934691399647128506130
78860824898803253703673601348211225034
909987644397446511485148761797870147617
50627687661623352599385487571080427810
863449297982352716692531920585141801670
221763899321293822996439918251830803015
9820430689362719890114584755371862233270
96749521232522113674283326787780456590
5514341676196142385255776920447252075
2394758909931489920842846430197819
29582142127748437447728438654168447075
401785958076874236426067482213917907319
432986418802459126736663858371153742096
205667494338654276988060615376756283070
7340922387622970459672058421608798148697
1597175554187247180630507270379952889731
4180497675973023236957945333617453879527
2396997430467605325342395449221572259142
7075561737589520574398456543164161226343
8010239375078766933071502740600838048963
129758452602834857938753610753673012100
954178301865482539053277154780975063656
91363808601663025025263590580130331767
5614803188748563998195112477502522861877
2218558081894484476727341979342856336
6977983455249808107262684604900988521058
4158108186548259640675238800657768479529
2895318387684674382492956653927334375
24376896384269411191067026347997439696
5718244545941899573382813768198451739699
650866541291335882057264704556183957581
159973259713331012480909971478843299565
422927410479702896410818536569798073823
319977118618390887288852165558773774003
5900821256836117411362826849598482535524
5115315676613924596861323625253457663883
280051072781247715689432958587288470
961566256194257243121246822617337490
97776547878735846217905257286492343478
47238997227456053735397326944177688844
7567231799387194906296407566990585529537
3232715486637650254975515986690274378066
5015689914952833591066897698684823268
356857762638553346882527981785264456655
9789904544878171734552489102860110421655
5451187672781689274316504819329529754000
796031713521166142809172695391978801239
185238237745159911944072728919471947640
416660986507897631759271131587821803060
3614731172401238252592502513301923949
73592987185228066676749071864394869493
5454683964276020490345231241698554366300
6758157156208310376953459844224367212989
502067828787674693824207207493674978613
79077162491660297072306318435990677476
637298256367746232696167215816660598775
276662258373639641376974184720193735209
90398669222744683593719447784722604583
539742728098712193613314575462134913537
603665924587618947354964122561111104077
1701369915362737180362667389879392869822
2609260886056475394935484078486543333479
9635794211577423415344666933368483903125
411691639489918180819049262179779211871
6781856541427566971260373454577582974585
52289294848962392218924353294269641903
4295357322956188189545884523956019427444
926341395256906877944397155495060463276
81879972329132480916087476848061423489
893988002596683704761106309264559153661
948382678982437822396204468674822952518
92382109982784419336047984979967257255
510651914594037662455481030117175038086
7296503528958093292999635813195292641047
288588261086868333964177716589564497248
368924470608846404152504304123776209134
3081482585994009321912958086754275439916
7306479694726025122479638798366610855462
3664857927519821923004914409814722000
139588711834909596392385411369891182500
192295902491970623312405755638035956509
837614243169997563903059722620584630
502215658246553676283376318296989076919
21914456742240635233101925368523444806
69118504971650127431594203629122742002
86415538110040939461479024783127508169
1864777373294521601358720375874343083
734321232312795109962571421643755284582
7110857654396758403683554928932221680
4942101120987423155733605620327647862615
2654726076602718820992027352178114853491
647444066074342298012034130787649915776
2132583749794940844859241271517337454891
1657838894176296499952324184719356786
7336784225273520576780635940525938519140
43046339085386015707975951873117632421
2880252775832536555169543278251712801404
5952892816043417409342736914834999579228
504291577469679323188886456442786437782
1052903616827232250774895083770617437560
39926313148759662268124890042212701888
99811336524166219416529242885702585038
9955683250765324638253935529471210882953
589548647341173850033798342053762097224
8819484753092232790086192049349676223337
55906262466993693883254896320235795079
9088656470322247810263741518275360301847
78989206847380047926699815736197496545
3252655428877418675185115797892472898131
4722542299787235724877381195261735717939
1321607027196413356356408819551025823294
64742399645190802118876325686151597469
755589385855831425699245251875361271671
88137583661686079543150150694312612118
138629342076678577692872180739234915461
1159299969482091576035438482168326241679
36399529208565638319636483865851181627
456265162990981719786342785722395947602
6856581184689315870297787951626715659255
74720986913955456594403538820750793320
17319023543529682740784614910236946620
2924369053445932662819219152471298635295
276599559774964883889400717882223238986
5206805678514260525616869186507987604457
6798152942931632350579213872161943508048
95096712295081235530694930384211832875
999652266400275146476160245098456202508
64857672236920461193562889964264336850
5816199137178618372350733518444783761498
102381648723885449340174863126470349871
8137271736252902831477091207656461232623
8928351370024949237650235789879865435546
292227464519664510267637240889683937
7726496874268343909188361197206942838667
9588930880167531636973047630246172891213
2354415779061520300267047358582625905472
38577959324902066185807247824624257548
26235248185305271092781185273510339355
200719793639554091706712495882656761494
929420615776932252108110384425277879129
1184174381309558358271727779336358726820
86395632338528675735437952914648152270
78046268110358617953380904113454464241
6416272451255560644389304740504570201301
14233648354771536285016081132740272351
984190073040387468747137538214169454597
698822269326335060030133559819579924959
669932061366597471421503110124955105811
82112096759567923583904316636276122400
1787591512305245991466479118571780762472
8130394548499965513491263496909244578105
823310545761624996239628910847184114876
7257418845748692319621638632727352637478
383255763448549318489111235372847494672
4319846193155627518457189562334220547933
1433718742472326477080737558879824975736
1590378924526987196389467897103515649702
200228072543466578157845887246545171780
7688911511314828846185265208493285806
3650332490462238130339976204267077082051
9195570060183032776388706788923028069
820395743411237586422855547148280267283
2924640092281271988791631330833668559213
515108248049781366545262909325758519206
952717869985222937198551323175102655
2183979881094388626211717345967335381
64134696292081728847471427314661613922
7963827036356542270641312968221753714073
6134187622795273327904854577471281688
830317569573706168202018655250531331599
6022273153394255996543421130780008337
29339895905555098481221210613421366392
3786814176486471637338337624892880974704
3412489083584879398441729501802692606390
3369598824248442810215238705704860932214
2986880472845464551132605741210626762106
6700395933268858728176784153538417361984
5568200273336592939532064823961881894954
2423724876699681641750444830532412596480
923582759471864360967746960533765254685
6446117888942472464042828661635656846
516222354433057252177607948399914022931
4196548831053132716985128118835290913297
655211386410621885644271766780299119
251818184490828131539438712172363549184
7095731236169881363434843545504099319531
212059685891106249786901251968773839337
303142521642293456803899180154038646
4580453579035051208170276089561190047402
1734878057559460622593902193737640551600
32352969898444612407643718684493663468
21733699932613236474754950257021251205
318456471981961825049899423147070564394
4384555558184676804520119051517891814100
5024443331379423609420226825517026703270
426796555410551172504118211784019988851
5640755831134960435897661100473887061190
20583253808128159667283846663623064
6373266144022743376861296346767349611819
2073669997639831825849812189814432463377
7626785648734010556362305880777146459978
410776266375503733237120704524251452899
16677303353680784296806445162369589787
6341521942379050788984473197550940975757
451382773380663927665070821275868058571
916211652643864982824044607305840377550
3434957536824943350985247736220388674599
6732700854669741487381105422146892586902
4927634630891695193346522221277820702694
715435927326242631774263672351580885723
9655904797701866472192254376385536666886
118914137932744515073089141105031735981
3328474486994324254551872782439116281486
22691488708419196646823948661483647272
103041888117388378255936306466019742968
4123666368645704647963068472678912602
76544085973697713516493254996647514746
199432237642659874011353431418985986847
44857633816905610397634681207890526805
2959439300775353385129581213385439215
40563978259496259206381968344699584918
885250184935865582145637029858990112621
792023488487214842674685402895449932581
6806753055286861998577215494546134203555
4899808811391550193258996341244162597648
5733254021511225493907394552304754891
4357694933029418710792296625801466457265
684043084473231180490399140199558307382
2318690532665241845596386850173785699844
91438117969301347366194806163889488724
584199364473910673147233422557117856074
8862880866297218436645449605496068166807
3720135255009237838756408280975648178
9214436910778228324599936525696997256142
7096795757804763900345076719941048824058
1582715763019890530931195671913583718475
553611692602259408987347511359737941116
197932641798600228282973305275820325131
767644211504521988128871621957992128926
269278626254889434128362443235964189971
3489712526094396515517241164123757890
3570778541252414802373811063930095632316
336948668646593253842105637251530701524
1069666125853393874385927275316519399308
1203869333872533713780172721177672743261
76019638686563079094250373811032277122
4782847575868298915813000879532129
7591642918074510641869088344126416932220
46648557374169837885873519911748382102
7894413887566781517123107872999826093003
626226048949333699497014536170475319656
24723386781463299907871568550430337223
9871658141943885375144955523920088078195
2766316631161977887349051268532366848969
3336336774296119971010402919579641685956
787221543002399364468355322552240731283
40846111599065284037232215582956684687
5024353426825441173542885743580569347440
2038512334066098295272505747937191374043
710582281760600255314899939585063721808
6783722677855410337762623383226316492458
4373765681653067108282835528738101315
225484404475519263127222246848877791483
9437275782578609447224019487590274106446
468544804814815357270103334187918826606
26902805928633072216030295919511451067
51465471621217968134743153519976315024
7371478478177153727781629259464668780
824173537974837162904192576856150129144
750554813991986919542657865153475703104
67225981171244189868635612463358408502
65573581494828345296232951191593947026
6964754286277767850129229203224762201878
1917837727626477345873255683579518121383
9701425831396254222586090738811882025
6840599383281736649960480923351355762
601374886413368748066255500399777203069
8291959214437637818918001989888474158430
9668728374138415648197933335626826735408
10645569782242582142164764046837118
182540812196624483012553154543332938795
44281524320892609387256963804947268943
583635579542546633102612411862607214
614273064347457717282311821641017448308
864724367513719525936834266871635164506
41513329936543362123571075921362904064
7414790343073495419443357630513472817039
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]