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
65161509440382340818745586768828485954
928345054240233638678518942977165953997
864279835452579166547613468751344484742
104347342973239557410388753326735485654
135549164836330156068591370682539363116
2823223500496482244660540792948663952
78125290522620180730624211128054516743
69825141994803594332572801143114708765
3537982913240033409373942688433128175
405915808390616961848721879257675659133
5582659123324313134786245153230239096176
9234573331796906752622292815107320894154
9963643452105985361275126370945756826
67522195668236593952060684644475195691
48588514908634144903691160754286431148
1593807646344740721591176385920834684889
5041728824269406422777312051762028217325
2868740721765789855769081653693661617641
1889673079903652875973707076150180232965
5777438135116569403174683566887138676475
41594671503055094534528295471266355203
72988216890518107692475942319289374545
7181520386353416369662326718625451551
45489779253722222864939652701972766925
4124817184505629564182633316797510462504
2448946126982815739952202551593053932
9554684053563653787648363042466964736116
1737384388822180339127926755119025239647
980832565873505084753107734268219839078
449120068218604170617091821883291702027
92237176249440951713659419974713118214
472734041092899533667124675642955475928
2784696615805832546818661870181811567915
4114568992457771136560815981850180271425
82354569800660628264739213813973552660
411225884491307640475018451188204151
660561736124395297578174116349796882217
66328181989561313617199852485364358019
3464377584916919315367591597449619233
52825089556736242720477694706825293682
3573738482591597589689107198383950502920
1695946130151818801531797166886837344356
808017183195822932259155442881028433909
4794266879854696364734380870950804497
542661153704914495569174811294569654859
4601670599835406680268953101879559157396
7279704046841897281485196241163123379736
90572968256327235270914373218689139072
1062216328231582407231484021532687109938
4687254223809592236713466603827281625468
483564584302324715976342691001583382
2829703696386643439610021069859496923662
8285497668019273290136286993990621706245
83029508067850575772007827681983075535
841279308451480699788946090768899417827
7764708786678678439653751842378948008894
293837993301920724118419401876797018301
643298396574288657179054476636176923262
2681615027721800279047448022982171752562
157411308735332578654339790121298223239
5389137962121700446195149958325840466840
79897268373025145446320864771378137200
3936641549753946193700979482511031593
692772569783914476471376215444168626319
527739256595194681878754779806710501870
751397909712887620786819058793063034137
1370709740238649742711185253854836324834
6204409684302309761431081017543850676563
73854870257932747081964802153851642935
852233849227322458191900725661023491157
24388926296571185739590689011068198
862177382740847339745273802344048844
660612454573377714326019627551289298737
3278514152424201418190802092291027992428
78684606551913923404468472723891502152
33223191660855792161773121364282772138
303655552934503725615446446072775997
3533838238364661902185635680901637972146
5263775922398710416965049627873618953801
463234578444181629325308280331935238512
2711352680997161252214161634153259
321649836261269563570024171327349373952
38323283713664984426006234845962976326
36795929114157638480269552123619245388
5133282834089739779999188509367259164355
64957315916839936126469220831719759645
99651792892654305783522771517878649174
549630078433386112094403992598787847656
7188265998811262659864471484274967626319
32208165938991505551807096239519743381
9912368278843875949689617621653027947159
7864496844297085133276624822285537419418
3629113577951688751257495971408026486510
2587633031161888514936295479729345834480
9450355413614922737419717383501641423415
6982311553347112153616644703263829212104
411669025063685211555486681817021933299
772559662732781202269369571556466277258
506592009099464863975582057102233406132
344321458762410637431017926295598945392
905116186632204534599949205668294861252
489037689999363395137624644485178658277
712460703738246342944142449409445335029
69929242498645677370559259497130311953
770154879046250253953329606066616753725
6637298481478357608988463322437992741826
972518935736523525372176188208956256478
3882685815728613547939711964258476012818
59267017149776430132535713640544422268
6990568912389111750746746615397644415921
83083506198011447323179042286649776623
1671268367796198368359597543014569912
8669289730917242204848959052219717332887
1774630736893284364572986160161369304293
8277723935259178835529594102308099233888
303739999502235239944963501772285613852
2186495470779938810514044589096258350
543748582025501269542234996964324923517
6736959797329127391084839260925916718362
985914020427053684540215613805725681854
2485111432154601320933526741768324376286
51521608538025074014258948006716403253
6385190070092767139812623300438211318018
451920876734713962179862115291975631261
8173317478343416279287518532063104022
4159559852329893302814182664497235396
7742302672758758933351423995519249596464
7756470146009183622589244932554573824427
936347845514980580102517614421429200310
4921922761707253547334378349039816635
368575961406860076825686602567761975595
847586892336015276616448075760391018165
99997364853976912753950443132217182815
828682919824836774151052478027838455
609339385455845369361008463709221901759
9642440248103310757420739112354657552848
384679620602315633990931572933663578328
3863798074916204572825737542471831715
8969527485357863941288701727470171014852
8983834898395616215530039921321966801241
92301886274091978087369535135497428
8599703551563327337810378134983079957044
9115775252979291394192722286569071112494
14408270105432912177177224135734272090
5109552955867633966457818810156176694265
209634023617114166545217180395840467737
1123268232945832990357897778137179592814
3192585154365228310570157212766076415
158258201554363096146183151737488899346
22424475705136636377985865269595085565
9412292417901906119129166486569417074192
3996405561698182137433126722077887186
1093138330489251757658018741789728595770
9381809829388330961649204069735733972433
796373238660463305994795273310334992617
892762013154839044271743782530161252864
456173331054250338734203825805450005342
3038700447803258280620152336722149449382
19805753191696546493098693411341768918
566685246581890188847731976240426814717
38627046337726618932543041368834018863
7999665832986151882494254175588723749132
2804697473805285799295163157957686983271
267071405982313479983456309518168701087
36735174102965975530156286799981291514
2706310559437051982541306733724723202
5472524812439256279684714077194411295215
7366683191637881500034045975241382388951
8844900528971072244716547497395294589138
957269274794880630384816381176556906383
4409305057134177239692193898290537598831
5198985382343690339275673664148429908103
330836306735628598585703654017781137414
664198935104926294469329705964237509107
16808260240634752635337843804649053534
6845291381313015673890082014446337026213
4057663184562884523640526465458223599093
3667184864778553541414355523548783048136
371405769494080239999558776480889753373
7422471135171960415520042433365935505456
1502920054608885998762797170404117122543
5130721990611808722291249775949086576407
2792366996826834610141591396351647314754
7362519296428620487784992003801239208360
26613090796655716520068915394262978555
996815253881312368862695510578470036949
9801745860022595413474069218036418926
454485530165591433716163626259598244802
35919668539129111758425358647820476228
2422184940601303405993419804525897146
123680771528176158446026734379755348616
54696497741177248576998141892298671620
4332921576697336622113421968546335056284
591570964028542912811623171351087748900
475235753270852049592444893172553936458
1899875818592088274322199597304796864172
754285761805649523836969772525043942422
86389842339443191179704272625326677972
9451331634692196561629179419500334442008
3285471633744315728554813184583186166107
1010468029689858331654807405640768721147
7066163637202596831110013895718438984689
8562199694704387845177144295465710632079
903844718455170953841133649498406893467
41838164807921169713595695177371957118
286576314342074741138552784736566277248
59795535774368187397289714811564952156
415392622087733151716843144947146388242
417744205044106895942812726822467716
7774304664995686526150238730353628503321
5502995569481367196019036574704993828139
5578517098271817465713758206936656309797
479907850891009730397686762813484455606
88944250583660193421815244855001277304
725536463000980275679633598418931767246
4326957151138574347218392221748818651
66737054630419599942527278541478314836
572951199605596368014062175582217891443
5885312910486016182144608950590527902113
3144230021141551169125714733802742688677
8042127646747122614676094562200110835723
262416208737112076466866779349641996910
33265822544736177432541238684527643073
2886895191961357788546062374880693306201
1730540286919797507559396805850360899840
555383047575344032176384219752493551506
655364782734787478266364946399710298487
79671035061928829264728964340113921499
882333581916165498101251905015417314886
134855953166109111138976822380927118813
827356395702630418540179310370284782133
99525257198148267098359546894539999687
528744997340793530839518870316189038933
363717712701696226502097859357990423
165371646665727037263716902039043498674
3814564995765205907818778423902248519992
57193107333193925703129354903036474610
320295794559878165044271955936863274842
548084759825542888294435840242039851025
21949096808458494113004504845324235944
17125259507723712505616425926038669577
298971172514388803133565512105567812928
167434796181293851062444503325779761031
28051055165893699828055465531248445125
4844421935229383222427672178855020408786
778442799644280541082120643720162938801
52554887291262019528732394370218153353
5994638552091436995774017509237142106873
927974416892155286687172399283984656890
425838417528993580065409872730871982235
6344745581051997886330671366236872287553
933541838884294178995131073960914896986
796924468584749736816541789113272534385
206996052726830446307953630411916816206
2846475979452019713178688323539567935824
879024297667851366460153000752425344676
4846214910882761510049434381192677914906
1370833060464122798813375025947916336400
932353859019854022815448843997347478049
8393129557117355036831846933649821348
25508823149638649115469194640997569907
12848669805774169783484366665754233963
9571168072147211828822584991344524869
312742146221904442912691481743792836682
207588555574156494728115235722574407751
6330608072927831107871789700658627468771
9244385830217177168493453002234164143019
5627158052143538780891823683300137218712
853284478745120662014173641164252076483
9731235253153167420111998538357312575016
1276929720358592975632961686165368578
3629692245509964146333952175359562131900
353039689519398864242297114054463773366
291016736718847229182198902388895394283
729520553002535835528236030398198116687
5371960839785143750169013413921432247145
26734164196543193989489696796024574838
677963233375606816479621075700528264890
259273327463616526236496733189131726458
8755689263585725056110601571318534
708789354221410740455922724607998939627
8111403110094531233584676304101668223357
70379485271710798781733202323936569008
5990400937844844786549234036938914831018
580798843888446414147681603538923443354
820989742807298947428661357176545075265
6258756843697205652792541310530136945146
125181442109717167032804137487434925195
2708955365642805472518965689125850484639
6288638493999248164627582345911737533
7803525018919292403582226501735020418921
892914468798407880814176710839150798527
194626281976502788696008516838698492271
539552628512755373859842746344177755645
8293120526975223395045739246166173492150
967383796910131126566690427676243662
450268113594838191246239550320483162483
6144844682389595812468331789328796342357
9470934466395964559899496687128891498624
70843571026524579771387456898336649399
9406303138425215963591899266369519665001
1281548329587348544876907137322634699146
520762645226194948909519345118454697174
8999264963836396116686899052958436190
7273420464371400845974258704157243726109
87789952783559778052872368827076729442
959890555505739999099335875053387755476
3901254941877272154277002452634588979
31269257214185641977209236189010071710
27288340935640137292734549273204328007
268967548959641116781777397618071319333
8512189983225559898586075976218215546810
933324945543353276857472519404223006924
412450638949286207811931290669323633611
161378877118325513451151742264442045716
756442379300693173143561516991017604862
53741560230874283608002516167328029125
6916763560896490419074347746855448198999
304760706628306677614963640184387326899
2740689265241014614767605159663167586940
954855659968623283075623794747720965715
645064627480971495795360962381792831860
5316733270895591860567762209303188329491
1945739752058519110736457243693391437
2371550743413808425288726477455672576980
6563378836714393506329222002273032845861
3589507830403150479816527737186790427061
4799899815312406712128095377244099839370
4401719194263459399546821464797804469
90856189262224641959727644594689105872
531677758436475025874782596727852631903
3944516563406653881217447748629531916092
7736477495217504026558737077218513
5152719564928544808759349983424611454
2463462853797278671694546723684169409046
974410881359377225424364276734639864713
2145844739494073823126277803414523457473
548781269536208973387452208644570405312
1190708453827196430876003687978590225633
55347138897049339485356853026663198092
1861280919772764427127023097482377208895
59406023105649522530627568493389876177
63866686817919700814564083237089584644
65771516631822524156465562711807929921
9734807261647497286736895718571056245005
207195965408867254571211842552784243606
1511940553825323211934537690278579949813
405757284593235772578615840192522707261
298797824499712081083783327245770678919
5589499576559765234960819594575812766819
57656363511793703197611549693954666791
93248786435152930722453294711297551038
72871049975517850526902138783745026690
2262379035538042813926435932957694941575
213640046201039978710466419627461204573
71834967183851191523147860321461534907
160774452590347174238895351180663645688
9210715986642676728917727237028731832
2834587365968123837419629905911129352838
484273404276770484884435703263915922892
3216533738179744423754782688354741171954
781158447508147590023748861289667742986
67278951679885885958610716331939222506
98145389877193907886934511162124888598
6958923528461183640430438639888969706335
1297221240346344896090629573191239761437
1959399086498082928672032721278843470
3723436830609405734169676305825965269680
76964912064664359522159944093296301155
722745898271635543843593609958951228603
848994796080505243726213405262885145382
9432497564846478388172519343464755394218
6306642712183117676484531657295568102
77727805198473371695636677241271333270
50487789815048835864440197640553508138
56346297174586152576500607831646242217
617293451419627643672441531533941596798
313250663514135712889733591365827408790
293172597899107610733536224139928441
2861745458687911602947668128280460755641
206276601617890288610261903346419397633
54102718807264126743533960012388718665
861547468413137950541932842537165681197
77901349234642013551570534831923657858
6054126678576118539579331974890813838
537245269827194980382460818153003360825
99323841823019732928366430968031461230
672264042634942581067570482236760085933
45619332774382041754617698826748607553
331813137479927657228537691330057940543
18137672522662449536479773987117083505
3066224984381796816311135831412016377935
640234294650568869468810315270894284914
946246035767697960214851656700380856096
41568757508788094075716238225650907117
66001142459177187067559942271939814307
1453754292016507465341248503167157716639
8323595254352546152516972156571728320
5007705594802318755756196146481136572586
4937704213479251434062017989469151519144
30981710316375938136314000930232398003
807073479855138212460808471558740858856
74688789344218927121128753893885296194
7798241194296389599954334826622570674695
526485358681592782233374399287367486
1026751139692355224551013374311694919026
804873502978189483214653752415380951885
441675268903845652528967911870694807742
48228391653456943659932383548244155890
3034659545353591484922543262684318365554
72588492285918078901082914431441339752
61705257553836894748072702862050377986
21998642065549556095728311595192589967
746857069010849473733694834107319740
734920669712923150110985898996719696279
293582231090780553031399336628480393832
390910124809455123875358109744556121732
2649584692902261890368399811705179021625
2506674443057082234558359581453098053176
827388846523304476344421306118761245901
441921419062385415626342101666999969454
21152287261380394901513972956002652395
8308172990236839340327939322508128066702
765333662436872574734637405651109309
64055922672223016624895756320539570451
64439314143327770868476810722872998071
37934848996717369751863039321662024124
4698812418002695272410957762822547002170
2279483198707984701116942808432323521607
9115359142063447864429762875265637424218
7055768571752874623082228274088897886
981167644803263123181787336148390472540
4405464220441869238829583684512943438727
8709009778891414256327268442841293868
728190634765838169928971494923886773810
9582558958621455375037695810557334377956
742080172526711468221663059383640742647
53318687697864628312877573531174523140
4183661624453426607669508162867975511
1235356230047773110956482875439221371274
6615330575245783934222442431511747598
257378028688679122579241201790935861507
93463317327564291169808912286981379015
8137375955362081289796098562886924223612
6599220526697908928094661167223070008822
3331603760541139680971840021463746229
3418262973369648340223973378716851116758
624436951365926660188583614057561461
728724911339616695639804638947765628784
7775337962952522745424647083489421724181
510861976357200350935694052278040424208
789168948407248439069292118159526231006
8156777719051807747635318905392053961312
815951247444314112795244387453302505
291966683691963300279022068537041958435
4612156867622348455096205401450156133087
4042964475397954215335369256321638435758
174539286967822424317541248103645695
581117719535145651701183400991146577762
281966448120576327269296215961424415106
7549254512943235966284155608149699576278
7032844670984475667856364163492473978000
5912969952259184855849338940706614860
372254197419158360030432854784067068573
9321104674522931447735916062866736205608
954407326084485313990525509587218573507
2471254613755101662061686430272853916593
723940565796314067766186659965921119887
55141625417640707917923408341113968583
6163513642852573866863656471918589537063
9608989843201065863477292949290451639531
57707523750231360759119988857607031352
2584160344861759832855385792623286585
87826869866236041436431446114152572
57682955127751981045186479921579418849
66679415965906982113291872282772776719
6206156328618190216910428898649124563419
9867837489131209364387397942850473038984
1000052035092654879151192734337582208930
103179063314459830173841607843349948850
6539617639276910855958055651374799897067
26704858140402039293562891798479656948
7835119356291978746343188114783208658
500021608123795949312825863587662284217
282835582488349567496026076145795749106
1950255056905558943525872060682867662941
1374387174677223222558562024941270994761
27217116662321149596441435474079832225
90564971256560148400345428841182574825
81981059146469027572971555659360203064
747738830521005714134646011908055217913
7627843671412778151627176178883290684936
79493454701143704322213349747078471870
706693663351573314954759390325232706890
992058416473301476583493519143890175472
9298442525564856105276199142411158872260
961050017047379072349289092629917918903
9540766820651230633212492330995870432676
3031351987227628107499516482410845091645
3771169421256905269135521763892025107670
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]