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
603493628594853089736348570418656431452
184610671387805693716094517331371482460
5817608332150936101205823884603331141
673554948922283148164477057835841537
3109618090421194103376611150211047380
7783582679723147205614586258466086026824
377988432809065766537546814428895016064
1173566419533673658343524292929150232447
98818006158509557089114154851599945109
422517617761443717635593277973399523377
4329287610707335375081078488252955628157
178410581093708029461385473177168825155
428769920872765605150047748148789814840
269575522968924113362576685664374632873
259020921506126648377638144556843759438
8523146168451628832543654980311289155057
7270182964102131430659901033503221741311
226970646647522440833408110322638619141
492420784821615424880655920664634099631
34939161926759227268201611129372038476
715748921069243372520866676831044741945
3665203465769636247922715154703292724872
25343091802358627913371443920188049723
981444249616834243727599384879744541436
80701261229777289582323153966098854801
4716330058481534731895155875706625132545
77093479083191112272120911449022776
5037412668671523372350739983381745173377
6618722859035898957627203294124541566932
6043417147763701865996167631945648767820
2043915646392013185217768383348155374346
9882803126092672470210092578273287411
84301693909687358046827267811071979303
3250701773288049773812584864871974894281
250945852155768793811069965824257921027
49006162184655083892091355199689678960
2196252275529374815169694390175352062373
3494642332763577594751807018112525572097
60237739972189172431621049794813874647
8034211894207066310039037388529469395483
381207755858423347179121034411779715023
368176275548857327773239037697029506750
8734230585144150639482343146978033199664
6149410724139621375274216308135795369
6596451919652918375256546052832330623584
591637545578977383335527975756878885906
77442878075235287581283366002544381
1396467417722166432038044757385622544472
683562626721558497862861204867595889589
2578872435558261381968369368899678008209
655728329711632633013199334854322913872
2319204113446817453457132509857842473113
620644582980759540060715657908045717924
377566742016791598418905540395556364184
7679541158531214835080566758435172118
8966928185687932195169296605427244063
436094933731175473290298543834793433247
681769694667470111688312609135243864564
9745191326921651340324753165111321301362
20097655917103063211336135161829636985
35136114558729473000382145620219695884
558496781183562351281902010267675068413
62220187391599820957475039496645818579
587765322613188835882346752344492786806
7791534659939517203096421880867095572818
7841876797215233923959638706725567781952
3593349376055729371875585815871920057645
393321789046884471603079312783553619225
17405043690285033992375546572940768294
9251916029784030602542407350843242233207
5041157240005388910493068330819261423856
7009293264085388880481422231067302883
478928333002769388953612071427469728097
653315730867983788499435909247177132669
9502755263071994226377218733609161639023
397357671953470487653192921078345289281
958140152133314447718595204702015783033
714024499558404496162598173813421997017
478019536610537560834492419978030832686
1909112412477101243288301275838754217356
185513815238337192399788776923697442668
97391043791074595606744660111198864907
9386228274482551803370349939916970883549
9533171871932943178568833306826295379069
37213568106550689735337137520510615514
688075148909362853162347846032313764
4194299439442989514830252513710816075469
5377315913264450300256358432242263862388
862782455352418298437754505616182224743
484411056068108465512625796655379572984
7034533012345143225177669386366826409432
333527249301686847314372441358299357482
570229721733459590786072892657811454473
8357663871745590858870324016954287621294
96005896169234057836304711464039414345
41435996396880465214427747743623467857
2882475890332211765191486123890945749114
235657025823489785394572239294019786400
775435678242001539054563078900478833925
272344645010109247783752791963899182311
571335818854932025421541378945585451143
6536353052983675667136048369999277344866
479345293081839989595928018912996769389
55637258082634819912232991376263354216
435454484702838129064652803633614417504
13675692360635834504920448192858256655
7792536682649705168512248964916043632481
145930809712392415520296882222567739504
764424006302220748233654267088382346946
659562020199528859624368038810062156966
9045603315761615718068163785497220532318
362894236221523632451850467409385138106
970467715443388691801750885844861096323
96442676459910555335529367412623464242
65153256413764824726655834477567008095
9339707065531472850439026536989687364964
97841263498937896061543050988212616390
8041170696814731907942283380727971623538
27696556571434761737545007865991394724
2568686234254218191479598476980658814308
8035392720887946471364414493528724091
1711139780286537898427232461703179863610
195478673355771074673848491921154104809
5736379481849405933990835297410233417351
8759734102528161782789733095697405520
61122479061216197698048694123664504679
4423157851746891157965347901610629136244
7532509880128464758968879096267351774646
875830483530556354483660515736378267553
4796134281472886690793342670308764212614
1689230531091683923870718501270292208372
312085125662393073580983426707926469
967170882447371106386343972223943206588
20869566529381395985291957968120752338
190113078287267860864123778642533813106
164388477269113182149081629673661528830
7214756236687215948762187704252859007329
7878503949648223646331342633485226898973
159236753874185115931814225161717381837
23621952662236857199071449710193236089
8213633139429577955373181029580258015287
581673811801282732352125939785079325919
441918683423502126076643577402774647257
93073012292465992231994780874152234432
373272463104405251111539111473259597575
812567666323437990029128906328128332818
56781324690778107525308092614350517024
1497734046022931416317849655115890714557
105850487067752976349141550738732138438
329981947971836077659332214721113533153
33445154993951196527277283789097043473
7922771374484326968076586375529551131134
84508742500227446329112605969220434782
8581960792979517590495911843555432912237
1912691969307466870729642656267708314
2696357968765593841287185882779192945434
232223873618288752182477058964880986539
8520676031859962508065193224983479652910
871823499429661431452036313611516131727
9985381161142981307137633925944470375311
644648233743517826519656516802088258933
914346617678659734638037361916995869032
673512354750443269639472333990190546234
4541615386542435698146305522799624299678
3260233574064454120996213151915752633438
3631720450587712252241398627405673238817
469648335635643194023644117392133311350
3755941394426013403310564922611187578073
9517822825376088879400932261308119080
9032642184848510584225405605347744244959
640893396186099793374132131378362592462
367065686913936526723321689473122707438
143593309268044494682234608567027162417
9482700062378060385521024646463939585241
393560143092147096996304239160418727
47278518521257521983828159561871425950
880032293319873386897439302461540002917
550613359430349444004003421424734265430
5370658656751691138410591601710114919837
3414983147232277333097975983392562131
5874726952106258784929485196987036027483
87012794016480365655307834223691967950
5156216195976376278427089306360886399910
72012148534358676895698705257119919209
4235181981149848633067317101629713608364
32576005177662765274456939887370543743
2898670959038712643636004594869579434953
777371124647801108770186251859792573940
9315355634068991146074483052212744228694
39710856670240861649839727026914168968
643413469481842510598155744831245143757
14258862147665663169726990198217978864
5232371824757152514099413189500436917684
9726401228884126961775505876875639235116
10365126390982381517758624767198623368
1510162794715194363741118082311839212544
543616601766139468731436823921672897311
4051226901010123992322464039799501342
325592736357787297635906669770221721407
104457659045666999169208026328114362544
473232741207892075763833391812688688841
857752029319116434032136764467462242706
4200978413222213542562616672231716973561
734744499537232648852868475427642815365
936742459520964993163618786142949357128
473762367328537095277586644720240222891
228272963072601251976919729291614075240
6824996733951934955549614785491949892464
1379676181472939739458538768393996719543
7901336920627935553421009209130441658541
131291184769883698986799601427188659250
51246211301608046107152941225363232423
340691481416806624021196456206328696170
40449759971102419179113963517129519652
276326893104641877764141045802610809789
3164385942353182517785508239616536466969
78222144201499916033102776911596584770
44903022399470488297479568967683455038
7707234854207283214215241633021303337
431683946500670457538763013982170536166
1205250868188382726659833680324455139246
498679848037655869027562756688617136519
840677793103966895927300760170483072203
470884833890519176963956481526231464283
5832702936566687473315848416326915663069
3040364451862249757181322462914431239523
432526132193572167681824950310427149181
21097238998382516709776114269347622694
90671303248964362123666116777276963425
1696905819550695916340644810982247631
2220222880319191717864538978165171384071
519291282615439723033456788480728974686
9605610486815515516154769108899078847577
5248459236795403927257599259757550242974
285678844631491273969229395404450171039
30392134157759969711607324884615277361
31194035674136786692664779675832412618
50035650960131626747743200617596475931
7186742187346464539269966290859238603093
7995480814486202865194666014310184827992
4344852045001027752167323923506342667373
879042266269268665775872725261375538843
3629320541344334295836622570484577224297
510445425170989682904314492244562063060
12325844822103341225252354613839931900
93633975333490863975826789726396657002
8645272993187303967460477462578788848734
9710467424769714598033798572279711489615
768794615105195813223544827490317307206
2138164936786576907339443392770035701034
1195622187542877171818135372425692661215
5541544696689305476812462064104165068661
6488470825123011579712855845412958356
9612441920399428140217349988715128442765
8770408872254656735446221357863280886807
6945979035969865858248881362625620464428
167530581948829687514459570851518316925
18875538704340858160578874336955044529
302069249019684882636067867123101682911
6683172546513107875315813727728618034156
3912311387738739335666754367464491577998
2853474642623405640754082388435333918083
79617867296656859760851901391869505978
87334197417609423796559152570369174741
6156197319612849790749031405380896458359
918660471672691875045495523225159847303
713069455301598688126318580734401748313
6118361357249917869162003068933960942345
750650646944577988813693197896314663769
2801994920289006441879309745830251784513
554048952053513286123012789968860702799
50173165417842132512434642922926629927
7673953246654407713797559003358526593676
747976973286629961275686253721632812612
433416712727821164468841561132138666726
32526685271336244835170919412981453516
20612427213029992239600900375608061244
3634427346611549673779273093345044995055
9725125675709257880787754707629794161647
5493906739569633506246923242453780415294
8442214849507976138801102255438922463
299543171311558257383563199300513047479
7465387511152959742250884963360040888212
26878550333818721186168855901698487638
6219299846879229653416223679369126653522
48828447920630443344537153347468728
5709415972881082811437362213618625305171
10919803470802495971004698391162939082
7020601412958373474833767579588091237774
48865753640155587715647485417149253975
1786533099033462370137862002893716412720
510422615975427033519608613945034394091
55694186469865398641272468165235228991
6601355320847230678292261969737032336
274943215738568496483316590999468726
5564687415704010134003157837515925779
5862272031278642251659305238561558706
6319228248252277184884674919353755744
4281686549392151821150646213355078403
9287580693922200220637518847901766256405
90723563240832296514254562604963406671
122846868186169780358438711436524824948
1615898331938340830179704673210494768948
159310576022596366633481190720635578988
402381099918323239119154167880851045299
75789265370147734066508372577298336680
953599811879829525594506040799126517845
41444350194023927685032243536616642996
8536833286186543651624997852675798568678
490583774377955227087457406885864266082
5285332747503819843584375559167175873733
5143696071705648489922424955686066471819
54037031823967841509323526876737891333
212993384890171679035898497279916095204
6072946591223715512777495948817048818197
1214401610095780449914987463153810671829
2130247737461000598441863861377095738253
21567851677253095647869392355599198131
77082810398796925999995938586216077859
8603715074616583241384318596828322683232
2090757885295334260653325410584346302801
121750229453216989821277037955886574987
94923755081496808840702548144699405493
689271109760416990546355886719086237815
862787544535454887758672375718943055544
496223462861807796112278606775133687016
5994347283436381253084972488244960454789
62025001714711584730272235938148776145
526737146154242101750717325114519996
928558221103067558388234224145732096740
972478759607695545127258748037236137479
1094145692868351156433901499545883848918
351590372569368642108786321608466339594
572022007888593395546028981778470351435
2311349191894191520848476351792364039
360023436253761224889999146619902781566
305764991294869721216181842099083264979
576946001522476405673737964958591158471
668948084169203391577941648363865464080
50198248208485128068127229592175794725
941769081335623328574743179828891348016
6047807632973364593473727004186915823935
606378930768467757453527594140868109567
9166751351003200822219372559427319417
674226868234486701558573781835165204
7148807023049826749043756893449458255412
2696708056178721811182763847604994958
1035387774723090367332008772505414254772
968426955502413997648188227174483096312
4422720345109568151438482706528230189923
446044724888968294467574666358190133478
1970164443668296627690018174654316495
59382791264107701566573644804746553
7200955075207809314926229146180293150
53313694854592221630343612186178041462
400584957368457368056993699955672472789
70515249504906843375348255527255691
88143588661327304978523544719813269219
819580496728817088869080477190918660
363533639603659484168996371742733654239
74580654662779123514017675594345147752
5549514399413574673632901555583772957342
954311161184611472425579773356773399313
309413219995935574376016504162066422559
456698473393367698534560256957756305805
9202685941519129719295767753595341758
58684312044710795557947572476231819125
33598468803339226577166452360171734322
6156370549186879643522117804733534037392
751596284578950346544382508971193926365
74083138279395083369377367416504457615
286135824707191158214803783863711002945
23655152452458437724039797593751357
599744955472483051998725728217275245732
7727544175276528484522323798526115299634
9578407234088336614859592776631155673898
21795675913280110415818521227661811969
7566347250037180313613497798551728317080
124260414843660263191812250736419223781
547364365535972975577350231284892139616
9946760938387730669626434877843861122029
8724307638189937319112388972701795256902
8934367626421996530489483717239581270
811580283852734987446391499465424069396
4345213248815514547174029604684739849489
9333264439098862539260122109389126432628
9276440447639049631839345117478340779080
3821281461610139894281449481099696309
589056345559532162167737898919818732570
1908412360293294508959305961750721398911
289324738439346286587649255117879701706
3734157735042598541993389514434053489145
678749373477675658483568372972635362406
9385019359651445734101781565020623911
562547222938378776047968279638988691330
761377882065473428755212884918219958452
580651431957873448867519409543186549812
8747319632077962532166744576252038238873
222862035464398717959388238439651618749
71260916442243481072811473138510308340
607923149635125479050074136835586634272
98948427679608467345104272718171423817
9857344750657738716496027309902067915135
680314846280846781103775535312483995954
96597943536031552961335606485690881912
2398168054223661807558822262434944251848
340231741027596293624039318199042987697
9695108733758073166798282114685577262099
6496958786174956485252523753467586089172
9945376493984526119730469311282884302171
1703432423942363547698402411222491512213
44734376455498917011415905437626174408
5046378614851585377569607070569188747790
55941439931937942326997612096423532198
3197232730082001571238926917333019585
5590185156318809728683887474766377424744
87985391244441736079603900821453195519
778386049510981681839746935482427969835
266177665564269253993858221351044428092
51036386531604855044412697960262031248
38991214639038368742216343495777012117
3968245347216331771477142192183356058202
4905251626669846558971586693229436686759
9919279758664385016105214116881403936
50424792174562678336113830258938775934
9491326889006057257832932843484159989195
5237612470727668340466345083880870514141
71456109458356546684558211447080068955
241385085920395199148949730576025260
240525781766585777896549094699623782294
471019331060890700612318223680822133444
965144545356364814181113859861034699535
1009761532358782223359112269204948408150
101817012393749921599716019366316771156
4946582691249265534237147208571123447754
78346950761147747486963826157516621990
139972622983168843648042576961172669353
6448316084092549867758002629173968933393
6162285268778224268317392886613256065619
3740473033767105509113656630723752065597
844228715853125013066835502368041312797
118427873456871682242702875711381310
7094182234532579590748219260884623316389
825390367541330786113433589941260982344
8393211927006683482069248058510397885449
44933178497729264412329401690871347
8474472588652818813966944673760273885627
8830727697986713259547657328385937711
863724156905471949829824448888781924790
66969248159765185554993482372669188057
6444729577346405770738432465746957087
910840027461595153605763127092433006989
8811870067527695885753537330643557336851
853330349288804958205067974944235046857
31003445105870329329199321575277752274
167485054892856855126205170504538805195
9447709956133227143898130820625663148
5688503269758140230103174388830373639
4394285041289949302274194031157124459043
88645823836746297673055754858525025614
163765843011919584964755802993551642575
375731996400971284475234280414349593261
8669698019052404700586079664574052696197
7572883562665134459517148276191481518417
6889464988757466651693709014119094889111
2744834368687483151431586987320210467969
6590810940884103996912713831297693988603
32853509308775271325686263246684163352
8580925881198876299519067864980052172994
963499737328834469465369963880942745510
780868516523756474729456790809497156634
692343480067699428778809930376730101188
745311573169164919262328769039327735847
5594618961286887395176292435559681782945
52638254794120893881744468546061205107
2727991716837767995329721771315342864
343058345815593232887562564431788417901
3568658237758814285165913440239121775
407189099870649531891405230642544961453
9340821711054132719777268155526378953335
94971391989342370557161777733857532752
70878127615501525622025972221327791731
6247388161671132242037462427238457621968
128416266759137573825046575605837783905
7585466310784249449154865241767239616727
45241827649591039341125169654570558240
408906119017774817762644165603420855787
96991572416388376453661566344820918554
8184431385558602475584928088713283162489
732827432515883055444163541366252406876
55785868324311598298089851047859342817
916371288466353611198487683909798662375
68156349904516528888763839475395423395
6609697829393013194639039682396420165162
6255766578912456648275902495620910997658
609524919027205239777993548142485104722
89652598927783228126507659287263759240
275335146840927017393799972681373929350
1654861292827898466950593227750637069
7938863073275688542026563743726070102394
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]