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
615550343993694533388566594978153668194
1540412751033918919523747066971856167836
2811202479976570935884035216408446912967
18819697551649571719602645267564392601
86839429144984645445548327779740856025
818897466122829556131883704131249519215
3691489950530007369743327989068165028
2983514081575714334558336826398834952243
897187644063541712497199230657551093067
79668231534592045256658781570472234993
904592322379695053441525964602972321819
9330710128105775697217764054817436266937
901013193453745543653984398709913088494
963054906119657377230631943416253006073
372500748704713447084669415519381624673
6575852693412150957454303112147369985285
212262699661187306088907734288927307429
88739986643531452777670392044614596963
63994523251869440565992330976502658302
3674318543334342932419457226411120453518
9922329686789153900742914697244548684488
868326217969570881417932281364875837567
934310317677417222677444213654475097673
236690871148005654548614619403076922826
7577536486145759838860611416435146296279
102162672877438285618045950896698597856
414988836089513156892834766322358414324
1086395688946416487092144620547364077784
7668650870312280911734582644703087425276
2644626181684850303121103487467395922381
1467995570649116107871328145772539331492
483595838145310322947873853903496518813
5480148746012298124663181607601037347871
886690826355385188225304176265497734138
366536386061385766395561065826840094599
890626349836523368255355377924621049053
310159396902700627175444332985747452444
977866826477266779276925664551225242774
611416239288569727862569390431064745310
286447876656005528622347253611824382910
4629679988062154219786209842707698462971
8180497742219093747744998941101749046460
8461253070142357102350953148645565891863
373747086049688671936725282373539553292
3895577126244045700843127906209116154934
279516949411357279349396852131884135
5853113845259267793152028749764786092818
4357704237387318577379374398553598255507
1229280679585539905696618928991355627034
299729687803356619746095307795332878144
553093812797607656579807256174024064555
9593397921616796106216384599499364111126
451125719916921587843628345101692379281
7597219728013036711440416992233293962055
70115267215686655778905422313913255156
964483468522338727133065509656737523569
7320682940549053922559212694147645255144
7475309177651409533949377014776614407680
82911427499015383055160345396142574447
117115699932951032355255165374297525255
657913747062101966281908193548579884119
1121706389564196523131882435677328930
9309115847615336456112872016418996515415
960912927894681121095709600285339182476
2696632513370947262966207849731047503
4538173127107336138762347623712911578
6210289995641632680752075679506663615909
7798364873427599180944359786890339904592
4426545616623457112491895707909445498280
1034358176197536532636996473475716639502
665327407991752076671858932693117034823
897895787599248947182644467388715554986
4055145287162127814777257659400287141426
532741979467752761556064340172275564534
4953853031207696423907792130837267571
964329374805548723893815137720896376051
662785782060238693777999178843487222401
4055586422377140394462183741952171310
2971845379784728525780758943733191787
10365105106760214321665762656418831976
8318196648759717607630460541361582773
7959138268893787447984378592218289451
439924987814831830976115605122722891687
985941265308244260745883585360650636892
5931793297242878956632277346728848937
857311861361389113519674292587481088855
7977322898326757676937823972911943597
5460581667027497439827877761236953758125
2039851915619461356649728262215785984
9947664687096075383572526180311424
9947551779947622832869275439871135937886
3507526731921486435556454267431539745202
491656355496422188028167373495411669590
24353033557099995694031279626924238573
7047466271251394796322586216744281474
4567735869728303700689985773939184132510
72919782476715912565693166212730294424
587250109137981951981808345737437174523
81107672835422658104561826080766243342
6408888192673033733496298981983196778241
9223892270265494929056784073610376706743
335318035833257629675794509538755265760
3513747648734249585119665204675591597973
17304242848390467784460608251163579411
89178099870971237423031178751722398956
6821357393824960440178452372662876381458
795860380392693537221914483912166696014
5123940877298721471780822245677017982866
405665369767762674685957115894255894177
2253121932874780158086657575121110065043
2023295051247009361776422451858842467129
8676535669261531721491835485725638245
716937513286158915667902796365285681052
59541384784688997254966414350774164832
209874613667860454995155859534571605664
790733515949317793936241953204757632681
732184649452796865973352399752554415125
9452737789021671575977921220274360458409
918444686518844854773558317934188419255
734787578265670923484823919556977908697
6492942891701682800084737608912291667323
967056539119146314265797468306040391801
9761814032431822551510539029185017294508
21462731824662089234912300995779435341
969753449904185485647137551860557283
16353677853234125038204819825712388627
522460023661138948891778465615316316950
2847304211262087879162109235203437377427
6247120814733913689476845112815934972
10625008258752234519206865700550946175
7070502085561757299271627005886066243979
875957139249840744682178285436732537443
3310128978048475700485309618942576379610
9435700893097955507547487656664048603132
6240339389768381576353472580622174007691
684928534189599365609862613276054436485
609168339983527243024509939131466897761
472191008459672183395544412840825705996
350636762889557789719306984170610007726
4814784791459608721647371924175406983
931276954772696762527614119886975759466
1563211515037644726940074871119174635935
8337855552164861133144078110207851015804
820258497349737892712361496056771783963
9174645416194735672894813977687236875430
76097696271495413485211197418444756199
211798108239481169488119383521539465160
2795121227376775584081139478235502239
546313623197997522410479661609857854038
27076338248700908742867736109348737963
56187791526849673938481940761137771426
297986884168263665089689613625687613202
246328962101690867092895906555149036509
75752426898516929685335367947782386652
5761703622481614854397566470678257517886
64654176527497656095157769216838937456
15685353645890608232474148041059682036
823031989845223564417549064746152571083
46241815558692091732374140452571752342
401984483405535556128877318485689899038
180187684730646036045016340411668332110
521036243617456728472787115863862889281
392550446839604515499871736123991116686
58043072354522361148332874357698319402
91302876573143374229358393143496643000
613363321043487565977921933653345659264
9961428354481130339243209234359179275
962532608684988567313331283561577914651
8973837827160836011597149906179226293
386189729119450059102390292017233546459
7818952156373335392797870725346087234
114837169469659528960887245176263078328
161913213121471280625784672094354477393
46365931905192278943926535960013519504
477092854049799945761425914470498946680
1490270736303394130393808152830059855333
381442268078431347671638786497191772566
4671227131775095577236207852963490801878
2672291822911413916597296682978825123393
5593954922954483818125824443837839528
8404507781373592247625347403243068551620
3679560155295568375479101737106572945333
574928787611139902027817877334449412756
1183270684461023770955303253100970023908
2736610978976680385014345502707733602303
266194445445268229173782792123230586608
771693601324475690738536591764971131993
2239729058459943708427605468948442092266
90873531556638193000912683860814603206
36094994333346773332972834075134772303
710483269564783579441373658283198142329
290296725768924970388061562436418206751
219478156072393731211576404631016505130
805514655117837163841333291370160595177
9185892548359305657175998119748378629
70932707456554018111342392954201122237
58025620205377180610432894911733212429
5987609569114262459177201389404420889
2107362977865770197526499358444856984
977661523491338679227615462290014489832
9461508755459094653077962071714756253066
407614511941209075995216606722352035631
35418308366547968523455521486571864994
443881152218859345396127267314523001960
532925419632426118932161107987789645859
6023469895599798407047514020817365444424
3024369336184530581260347038671465708376
7799918556628976172749589018347496771283
4944481723575653924245464783207326511222
6462654639592454244462218619811921812962
858015707662847560526675903452981255739
926449038553652150642166960597451512054
1572771272165626163291872766620742563847
1648123649286004167753282905112968227
55589811756125583397166655476869839739
9024701897903246546877952926703736601527
6860418725663248390151928760180158886
312781176616223676662970772825876558921
1678878234646800468035731635449948154872
7119811032648019238243068520978195963626
33684678786219857226645164493116147585
129585112613832462853604311395270125185
749810674498811110356270831615622676542
639527648791263539561192553023493949
523787206442604132030282055847631807693
799463862861911856118724037108265446461
9269146824749227123390399178990590638403
4314452069907711646786097676736735164279
8452531878153038562020724861639646997473
937559295898351492266729621198935526252
8676376797341694268238439951411646202676
3247713879075996532717981496012138320
698375278973673982649305981306036679103
788483331929715525662181837292513087153
722212721991162231617598887331043871694
55648722467191322935788431727457971589
29560737856444461625582440872145588540
586078562343408550042503967237054711420
8023287821255977779838243364229338553233
34076024960861683129358301565360797238
2981610682364542600064518512625533859
39250685219846486043778945843250624774
8382115297452200671718166729493885766564
84333049657196424349988818994933898088
69015834531657551092614220265912820140
4778741375999038075169644355741692091
2165169173192977609109195559971047138
656141175224154699995436449142797601804
64761709677323455002424920455312854704
8632238424026419316426065703158639891040
56968119303404186367606584131944875455
184791743164835683621585691010034152
646473285455864969503406884802059155687
840846667956999711810392813511633851
86744753907133214933299228236274697032
5254116344014732803526431450865740899755
801192465806982087118050302685064912987
2629626922326159879856744352827620053788
9709960142823796754690405338733831993850
97341166772276306486567679567622552701
542915296332944834614105648273159949336
1477643137642212442728671197209391207243
471936411940585583681483326102643758872
4103947945030267912721851880049418348
867166864041817726227647256147781019500
5349532736253299205484599803413228317365
574288651371978685494507155580082315200
8077316014298491584187642601119039221037
4659754452298931247632045390263494837
845347325406940503589373266504832573466
8535738076697540713253774976695613912104
81951480941746573288381463260889472814
678320742753273335878628413134343761790
21393340253018501194100990733632239454
26991760786745268669923494672326354471
1801283139787700394578322630954276493497
645028968431512312802592600363502579270
43507266609432938493456200799026434918
524889257159559279661650628493728869768
3047496969486442257649548215262158547
521853218201779025599018854716526294348
218793578279451278064154452164655968855
75225681924042932182959361849412185520
998171151447782138127646885167530917981
7688837853752729429486547816532925364
7671861025991888110285236731983098347526
3158165310203527849624827486680316989655
1263307661642955317518218290163073616045
549127351686790564596933647266353838771
9250310486253484410275528318851578511497
5050302278971803175520032658228983839210
92993084258834147187743151796330914223
6386869976635687905412528166810719425272
86943335664467444345487859612679012258
7032860652161080160878226323827092314050
470288464421544061573558518828095095640
9268791884733632357243587001442968465456
2688490584525483472551703810347690922075
12386734002728745321228254177267749
429696538571381249087655527483373587215
8526432753443981399956070729524999343
318478362178614467075552620434799156244
29659811540865447323898164713151103847
6682585010282033510575735312103353875014
9766733747061610473286307303951545749139
122348993363906114865888941022705060
5098512163969453368783748958737252228670
3914509270946828251047933474575938358499
2814105426881350742295925822157851283905
649558427788446866157544610350364865386
1132760930657394233496288562829125625811
540410024787853214932845658299351839466
3741668112026502902751445038162347141016
3253866241587247532165493810195883521210
295973598241293320423091219149938326516
883928061230859172936863117861554542389
57324451934734679909397238297943015894
861667296386131665834425440333647122860
5436981534265831489059847342709020808504
361617548694463685851751493292374794093
899398772868369163585022052116934482
1406691624217413854136183348102353677875
713021391769407986981162116194690081544
8037231576148895621555545345651990588544
4914114122685216216534123265500573536801
944251683124056699131204351319823301481
211283261345331387669077366211434529672
4995456738265915995149575621728970532368
495488325522272385341381920458293377744
979688762479178251913717002326284871604
802958929074985629352998933210077522
701928107283326013966012310215796317824
6770549052461349921318047941163397326057
418320528674124137238987883187952115
7124472561883218309830344766207019451069
913641859613750106745888354935339160
94962317605258265788304641728398949219
705275506221654557053292632191760568611
4740539987316557288867549682272131411768
21168887349223064814650700803766076389
6755765430865266385762886495095795452
372460734031518542193212954883176318988
742086911950362676588936159655611876
50932068979166308269937335128941739637
9719475904832296171684831463394406572
378965185974554439278888545625366006115
649416857439720077726935270421230215671
73453977867926423706654216382876643830
1812691984829838758635266704341988149
53859679744174581326079553983081479904
222980714056451397420408066246269544785
73181949224622481055765937618722608308
2174730750693204994765123157485590139409
688985587307504326182461996911467511232
7721882967241916927745097495866325336
9761295675809757578245189500850066542793
1558628358176914109763058715935263921290
726788213823567072584307045967531096212
5965807733028610804311085074677575249546
2140329584405725543999371048435708399
2320544180664590720541747254687456375308
871350032645980057428878305168324454636
196195103064821171134776148509742183775
3251397532023184326476097622239389252265
946940486771599363386381540736184055178
195060066352900477798727636113349819675
828812787034394294441619821982725424574
847883378232447159353117490050086711722
5447295561609348928456664023276892466468
725377697189686899133265631916439508797
9600216712971347278794561566740766128043
4001521387017578674392593891822772745485
43581514770180044790377076387458794604
9508826851116846522667529847750838477524
451012803634559612744524896125313998723
7839213754007609183334219867565591946330
7733826024837567392771479046521910217764
4894147263916210555592618060574866346807
1534137062143700770528244268673289734390
7999908574953551970998234605354977985948
285620621290230569078733107970173722522
1367267663303554760786512204837292291566
960362147688781434691409769266960227843
533189184988964894126589741879379313032
94298570214547305324388044403671918996
3594234626322314685898163955282922440
50104666104912286566941556747161748
6872934776791120601266599074615365581667
248242054679153917654953092543262105769
8509692335656056825288655908191645547497
927735565214786172929067951397977619151
7635475066278279808189952019483957822189
7327528462404446094433004482669243428
2604493264075338425567658080184495996124
191418396284287586914116504357878889299
213983414808441185721307771580387564314
3666442043835673247820459283444362133530
1991252130842751127281364745297882454
9430520992634211767610966565784553332290
2356435540558335726643458160225811658
215022391027100946966666888917294371870
679718115351580360941294973777235837993
6991742830498035172124741593358247598
1827765788122898585946977587718184827
17795959749443344078104307860451079108
7030967216275632166415285605665234718802
73594172326582169029819313229759447944
760442934798381770895175290924152331540
89459875549552908603835134148762848040
11169033852175162894574144111994513118
442394903184550236773106269853269559137
632638584935425778263638167195017332538
489458107592936301813717256479430219917
34772242543935526789952757125380392759
1021182931037451960086287315540722388716
9345721645148090411663187798868981984668
55413609078422462874631825911437435991
812710598684378749353178170191220067635
91013239545299543784703417348598431473
394573731278936640812271497638375496066
8972584279987968724166131876576187880
661331745379681581973192081675143307464
6941608325571059874495392844458265171915
357823045832110734881538132448661724280
7144756181731865805420892706519953308665
2098222755831208110921725951240560309405
827976690673526292681348370505060469528
136983089848834861253556271148566218690
440611814927909216990085936517569629366
680744433732326539645819265153475719620
635381879945868989371156172764041891890
1237474897914725222335258148808228719857
9223719759332618804480043746347899949499
63779180777192749631513805318027556582
916970519392564441669941164239131427596
557326215385579271553275239638982068159
391117306797747871113612083773176462294
7005564391782502468947391935831586449573
766363476286266576890765166866254134069
791127276974661234189147595534943955620
5386595692996749328014335982794049507318
9091729257923203509243753834605735883590
8683242941487884208131802658409924997773
801329999900493451521977047335568025724
3293852431718096647676661841232357930
42881948695098506368533762987633748461
62001002726435615027192541747648522295
9832970155941582749297749467360024019212
89159642467775483422148901474024139012
264014199377484053586549833562672327293
7421405377325600597350563801911327565181
7300660254363442997519237056104219389400
148260623739733498332116490934056153976
7177559458401099125630386632644416947
2090872867029092158499041406641446031454
3008174370501382504463949077978478117665
273391413329292687202203996488599498682
4055705470135757722592743416201321212797
7675679213647075773559994014393980969002
2257251681246292519482399353764720632848
9377408580197160784193102186825520514055
3183856054989210346611782776444732516717
2160306672615884770716488568170964616035
23207159233312948399707889231570532161
2112428511162699573431494598376928455
5169203617946621393145703079285127251290
262810664502942323955089973719853641187
593196878948356851349407009597546865470
84705007860075019999271789986688256458
747230881311167045778095471574777628642
9726689623256279271495526802653175532
425249848104578060112447789419490865677
1250874693197351889469283386925211384790
4091191198477096457928265245576898895355
244032124049934413289694651216558609241
993628693828880266543307464368081638524
249294565462626032339366219156681583
553319475159183469061542380670034916490
994918794338787801982081171479036767955
35366931502824423229584888149354306091
594613917379371387197943589207728234310
7433400387741453425673758164827615493947
65779749992139052445694919737394068276
81252150461651488654114710408920778442
4068744868611544502083177501196557676966
2409480484663131958862313030629072802138
32278916729187673237194925782329564554
6979726746118130923546373919419510001255
386124974693210212165073354235696588418
5149605271225497042484664421587099829
7374798061607594733142362247232978395802
5359977846072824792977489315596358474998
707578292067995811888262043863053439639
4694676420883822203250217283932167241762
6928989622668706655993146138690299651198
7289569818487089155796235656193396479414
67051054910242080328634879870936622933
593471085189135178409857419783932416961
7243815065465256853819756033729216226301
27227997904300132605515295164895228847
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: QuanticaLabs@gmail.com