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
892030595690947528272390965547731756196
524795659637290964397320326156544325383
442228619489329177858708029618721569
3765865014278349107345214751315731326038
6844183867651229732060828343074683450
9174823138185150474940468704712374353499
702557527846425937607923831132029107660
46712893265328313992565360206233747319
8944576739224983551814862314326656412568
266417991170111050115562381230145093524
6143399482855739676222627434595838372621
730072503478971679723888597551207363
112337342940866564075633819375999333986
150811827821423726940632785108654155255
979233024580937498996996833303792598872
4096501645955266630922988203537125061719
9130882058345137657471074980341725519777
7596363464341620304348087718440583771867
997629651827780658184227089221168473147
3726526524006164738085556945314592759226
2913735328773369608167798652830142136238
9473600475629654136494574669301494697248
2370768747799035189256876086828114615477
6074330790751530444822796222828669428
473093189102958347521132053167732238443
399014655340909054199896232199392847018
4685391647352083886324498940608490027698
55969511433123555035391248238093595636
98157652124935798332353101698923146129
5461480723058409207692895699829411579104
324214412649592044363733017854785146217
3533250880592750110241023414309956488176
8896283348253740310985833478947755091129
778117593977507040805358193889856154849
9103249885901966144070851006145541086992
8785141128339513609886796295677571529797
797431708512491192760342328497364736269
69778194533987169315396361888826055641
6745115069457231583277052883451436999125
24218526649556183876454926727219542509
831827283109257474633122115042349639983
3889214335583182360312361269774718689136
5371763286782256756263816024268474367903
2408839860968413793996339319603279375
136152488681219621738519958401089014286
62194290384556196948214262474915556640
208231534448676865758789185359778236198
304431873178511106174365834815784572646
68438968124937420218338788334192369919
4068902543917576962786435396895981368251
2986637640096432204639089371916994426063
985595166421488267337744908889612566663
7003125848325564976345821224911748475185
175084557557301905814948627725724332530
3501375177492813216076276368767110072105
4747306691448470333688483924951313244167
7740875810895342265061409614250046797038
5331953619591927765398121327395433795363
3607352738704767839580362510650846409287
4535565393837944400066505614304063191706
5811208538726038471724902802344231792
208414476536340575902455272533277687665
790657705503560500152203214567147159050
781143674280587480734717854813918458777
2648598186418899233273939229225524227875
4392181686637080573353259960392526345414
74483778393709828520477491462030144074
4948140286189141294379604522306771524333
4046304060656771191214174082164646795505
956824255573652180286090768613127497651
994758453126529982559266505594397843090
2886462784574177129692622076228291663333
56773517674060514058504332915042697953
4148750216195273255467772485447314701871
282274941952133578706173973437394572582
5698250369426632569488294306529839574603
17185407011558689319221067285990992914
6283752786818371822940038840807588635477
375261876447608985269407037187151894928
54782335158899006474652688531295275201
530226235017000464320247189356651907976
23049126904983994076653755712266633967
48765792519181665102743040699079519054
534570404244452278242427541792083406183
365162031874301676388849207457085562585
73234197442450846379614947827409182901
9671956759577573405384351689634983089470
479945745536945775194091720913831876551
37175331467709916127472150150371447937
8464842759836489351484582261344321557804
83226681195444144600501427813720335193
24729348025610068790249315801893829707
7343327920563182128950788859164211583720
623822761404824413273101691455639679034
926039567328364659935384838396755376907
9062339360272919520091993705451577841646
913241208255212024533645050925224870
245768748625805481899056793690788293816
716130012833443941231204092849497213
314458338802899177956837787999147502039
8102473890473418988413361633131076451632
2084582295023040912261144365379129672289
78939070604533116127262781486086643217
7446618853117116679265441995688918617868
382286169128459641711086616602717575884
7245701379716310334157573979311387054773
7139158071164796470031481776745813303592
98673646943121601411426631511339567444
6271368412640487576091282340264657933
842871995835657407249686933586597776592
617184752458360672635146897852659424648
6241964149915405970318282866749662162744
535864288464005776438012213348377784292
495490865385551352074024410222522818784
748764805232077818474832666559322542
419885895363901782423478873915922665739
2443312923801379186106945068392938718
6715417994753687874718407301254942776192
9378822998194403724254975682043054950
266217830088585112714020033097639358
8690464928844808919848268321406242387954
4922970432248805593349827800870221726724
79152842805763464071453591673864855789
171540409928864350732660960245311849629
2458783677181486919286946464537511235666
15597047917213651665982376360377225605
8800839266575720837174146665796820
2865749792813369181123884299450724251184
278644105183613548849824166230227758707
94105294511773436420814395243978893787
5940413625609930627244259822639124117145
4643229742812884181944056061457341168706
117971504365611242651773044517276453561
2674575742002998665740734625915630812198
1236960745255123871081159977525166176064
559274974234969512984507247503965502579
60048276550690867622680852758796077801
679685522832971680988453644641334376129
25363863789199253026732949155159206849
9092886874072287273589657388901107701
66242893362416929265188738737958287621
992597956377730631249962377576863072960
7177123182444535301639437682214483884127
48358707928625455325309091159842053685
29534312842939577268972663141811643156
662462877269991792815110000234298733529
530678654653624683318359411354178736543
6949239656041113742686513394367142771326
162636566109516954685771596309789813149
3930345670364422344059517623949479537029
271553625951414816885378794985656482523
46031461558396198029181253384041291960
1928930340972199344717630152327459164
3130875982429229648591296056396222806133
301593773031687822499217124638918771061
885113756622188488596742326931381526993
7071780435993308731018005295996481044264
9264280460342915711524775354225227066415
12715512660162262964752432842757637358
5243166312245436596438302617197210533136
525569611579860192782456015298848312037
7950718617614012909342466681277586372
3602941395678192940633211684226638641643
72708751267287832697605373585374094546
2702866531886510193358738095526134569898
852985481336170951759767543979998691550
657158522138334566387765177759343582
84657609665710216987165933843263815809
4210344857815276777671238649743361504213
397639671854827565090662304951092377414
1883661049931323216190495243424748407126
7378554333015557779328807309176451911258
153244235267952244711267908678325080465
424181135000910197230151358692236247847
18144298642946854993867908013072868837
2345786939902101455914334118635880665154
2905141427967915499591353047396855889071
8273623233145090524957111081333479639194
9823702989107477621776967508401036133106
7340335554502327592213235988941287012087
7616473991827971122641163087381943671386
579942181279474667305879859394623655069
7972117195613252265026067289297970346093
316621925901603336289663069799781456230
359239419909465931749884159940996743404
382841364649484729080515928052861923
588966310057705997317152101348232216
4792177755998938532755326593317272679710
6817669359431951712177952687751693720
6098969235487744295377860142475904830
400184897591366837361313816220406741445
2477741440437811350433919946667414368406
14126406567418169696745972551611284918
569637438279191639519138985342930025913
2795962711997099684417204440158688859793
55485885444319588317754948172931029935
84936922299713076327636223464135739231
9917525780621211791929363319939644015388
233243624134619607837700750674997761
84903321135471261167246066798583219313
594313629372853489812324955477918306937
5377713476068177750143654596100749846812
90496674678742837732435693488146776
2092575292637609165222655482695351483
812592374843375932328314731837587015569
9131330762306322438962738432563422379802
3707308762888244613446662862116090330
33454869594247857854311254953851926566
7604660169223556923459821771948420172213
6716136759237435264397214970137597034
19308515884377198822333752249197791210
259055756893330859531724112096504349730
354010848479647651997170816379224619265
7499229613881547205432048881983579801842
2987813626218219323766275763884182282384
8166977742217129986670395569349113321856
2700851855623767764283051538092214922
13121231956489826160220756283785184417
946095993893712866518351791682355699277
3134807048948734300557868961773413644999
361982022326883854681868141857775009233
3718354611898466594364405539529083007222
5367506092318391977714883534873888748000
63431093506520854285701922855283269718
996711699830567520083557198815469122121
746951825239348919627123592955252841682
9033112948188774165796782365632440164031
467819445016106643862031360016754144119
4870124442199048963557925796349081409766
653093176645289212827569696693642952070
7542828131165154751643616193091215976
6762397738268334379126288453298984536757
8031246228146069629528858521577026967977
46479578177676012986613071654093331386
67443757224974096138097661769606426244
4514797724404213256476895696604978734476
14565732854396604968157607731678065907
5250413625389650579976271216329180936902
9827775224552495528431130968666872515
29658615406547978336934463534075701343
115679750377447395617868356202180994460
139886022978677070004249202164805569327
601187472034394937819099650611082001899
812466782961337168854434814450218596396
7852124597274624927849193634186465099840
393815430139852258999984706888611081778
8946984433808671545581046256713922562
671263307767104765453792492268845329013
32057721618490752521333558200022614281
8385288662425976374306628582112619265
6713781692619686465681690086407962810
9787492293943572344676038698269619888297
7907940726632391397096687983187431565694
693795949941425425422453732302332574458
75017189972857818849837715487739194365
25462983576699803929530538732025166179
8632950344391777122217753044457332998537
9944656470734656601725844491280114935937
2110380548987591801976264937946535239863
305954523411961592756104005885753237607
63401707478371417319598862436597058496
74725437157993510752893851951384322117
144696534062783160603005049985762354532
209088732592291259988994376769456209814
7003600243079495136439738774198319126957
3154951787975767990771041515532075716279
820072611118377824870004580316115149841
7114281580014537382430025380129290895579
5011675574112436395916253524517745111804
56969396253800281745755896136743804750
702715152042676310262007105880655313760
709537225285821757771422447864898194768
71548623864170810358629946812241958437
9833557498276523854155088697750020084653
7485452911756664363337191871787532964524
589593517074565661702235305317468218472
4403116071485926848062722010237513581917
4518271218024933594057777417034438151
4884764245762195512682724718287673012870
5588897724222925864441334366704244741045
95392784360328839959158274277654521479
434258316432453581704802130843036617903
4131929136144897587859292538388959901087
932228214719955362079885493258164173419
4202356468946615727835606791265438963634
600526941244288376571663889692467236654
495898954397529488855428621134961298711
500608517554901772729817929223814123330
501844781335422875224305798150324063809
87477884014476925477982604737469208713
29723434485167307988552267690287171
9044525914462661584488363027428195274294
5507874646785644164974888662569642075937
9621994137357745037394712652772416508
85979429144793624234165295161729171812
27799193612261465048143998264719822307
805912936401827568348946829453930358485
60834027869231446478393342806181962
742562186432795614526693515169016052068
4697341751552235790548794769821648093181
5403186686669481538001304564183408641
4161371215978985585741973101473092597368
217615359876817834065929484257544235914
42625305250409836841385744931582523344
915134186904619147634507351880746936
2355150452607543940034143895175441372251
9473303336932272828224577882943010942216
83939872468588658845491157803206793687
111575086927769318278217311202823183466
268294138164214116847004701101637168930
6894184525773717623375289229942416086433
90332945707276558889921465619982587183
6435378632764431579082865269674693983036
9335455467164932982091146168981486421668
4985540431569185968250339491824523364282
5798285414515811716678034199045688389
991464599574518649728122581686561045706
259195798107860971954855959921809113243
7644170223431628789986265522427311659788
2521422221423038563871957787867446642151
955242304731505590176996734792671235
7338538134794831124324014498532085156530
26037281386274319613993081396868255162
584386764006770438729779056753774916174
467964714807297793653578717096187566793
201978502198400219169986392473758932929
463624434102134898189176049484216523742
692580529833806712093396769871188269851
77676032984242982113332336101045226727
931753848082971740090614523469761378313
24842792878143360994554312230874177056
809840131146816470181671101633255869279
647283007503298675159660804214057412689
333567767806293924885936423555886524189
4686959179672641133594808608238570835183
33901351705185573027776285287691178386
109017050201613151249121060934232361392
9601622337329026319930256434843660746606
820582408457488588077981488953488226521
9234429238441440677244926144872875574206
7256144119939400237391691129543213418805
16062375102904922899334623993487665674
4741303542808039226024995022422290945541
792926838168170277197235308151630222966
377738601950630123277027744591701058858
59771426136619645515542786427674659345
82885081695512386466919680286583906440
460016424163633727004641813414119364514
30801727501380096408981559383917614956
1244350247638040508654632698661255087696
48838905408895235171890457499982548844
1971583684561447897486243606813899021087
7072943737813699335258157502621146396828
8384604609335119568145756134829429613
977615557761552149536198092948663377870
20851152539318515067930797484776282027
481391421400152493195337176927691936151
708646483555900976416717435925961125488
5742871098304622933293408749798338172085
502868546923420092002634398966399865148
63144715664072520825305021428457525377
572224620119825871941643155931748700
294676563254279728811801836747934342959
71648789680492631821567196393107531927
7485469963135665277673483422499244064583
39757131403873787840372591825313521292
50785228794367753220784383965420184756
44213328695483479708248737531066344431
3026914480553400645230592531508773947623
3285149529006474284330505435228034152103
3454853791383479432527127701707569826607
1077154850201336713275978717762854992910
51283226262239150599064752439632858025
240468768500361040466761794254127822534
300099616043904843537753879903095662383
164988495587773820564895760940582081433
964748603340646334254441256644318862479
3133850212419523733474907913404982662552
3094490762483749486749627391484851096
186736975629023174896521547960024251060
5762821586986938980341654857549167284025
5675633030257503958083301398272342049009
4070913986703755191145392572132035528339
46384363601134087779440511940016708754
8605720175289996634044487118518751081403
3375993311789421866876952530014522316
3478603971367231673637511007971187257087
39103939604250954642008422410976962562
7258650220471305669669358788941486802932
6608491935658866476011768951451869498459
180621744778797829677462335089316187238
4781231444898653816381786689265736604685
12872256388105290025182016103975875751
386777989871563840153239850535759224144
2672218146707489345289413507263486329704
631332947726704629148629740736031081919
6653342494252739400176639945942934312100
324933026846465262782585080859017999197
47179132190358481333884141545837252738
3566294839467071310313062400982352566924
794486457283261185874058228723787126304
51858403860907136633358878737404582761
25277013731949980479381916361586104488
2649342859313937647628542354955786649087
7932911658628050656997176400510731546973
6144519836617972499182264428520451628635
358897881451931199074812918777929744263
33069515465115661361525663648868776820
3040262558582374882138379524549232816428
523735928433918840861558399440343541415
444921243833603248264076864932079128139
8612761282527369363699767234839287462223
278948456962568887683329868473786593000
22563731413652470909424738956577393050
421348662615626839886373492995465367213
7001580754712390251539449082679432729056
980922533178318372852432829699730626251
635388739207472390387228683271450643750
25061845121721760590887364817398019
695184552513984388929499368225811721
92118575838649715958198983913691591828
51338117043400398353702751767058553014
139455869900527461998431414617438993355
96216198624265945055535150782621274484
727275808566394136032837439662083991702
6504921213686255817948101057657421044015
62176727479163093446311943223545849531
5063693789522687738388503857516736941136
5567212776672758143714153082675994962764
235247801720214859828638172771273751899
213154015783458237854174886854347169
976787117753012609952823462771712452834
945531369778734788753852743340874132776
3223141555065838109017762931544712264212
960915389530591721426228402317492776661
195563139959687735185748437782759771022
96015587659925070224705223092263776704
98639288930417498011220343323320754894
378034232250449250045821738501684342848
250455339044339749978475893754066022939
936780859951441406032117595999652528428
6599641274327236741315414822205320812274
95922320216326168657206302417661935725
909030044783156188412833568956559003188
975455711431614689998838232162358428035
1366789000847194840815057155967076555
7994601526171735664263343674449893097994
76694739979730344381225318698954661389
60519857723728760792347628217796643761
916377206427975918457118251775628208522
726861516383983933438825990528307105927
979760505167955797587088507570658772297
43259229613096326182124825855092636502
7200164925564108899455845296208166925688
7442677170960752310990421116901126667
16083193729635615142421905594712345925
491340520339999279238601891994794278974
129687261657306270705178871976782335682
469939165102120772975932259882252019557
2238823534539202305345926156790716962932
8986973021423111641553732628365150015549
9305549462423917474790011508994625143358
434699303464665627045314387733955917
99803636842989635516639405123681824406
74345407681870383936588102754381198717
6241259581778691847071164874436998725827
1925926669889090374869822302400940425549
5837545892223012537172766671851625843862
4299197923008587928175901295920489833401
6773699422442320912770934481184052138717
8793131715674784364369606781219463509174
85282440637967243473154635526505979191
9546696454643454961127657417879856897647
233021337122706880382809742837039655207
914048166543852616369284365234454532813
5349847552605327249178129605782917233710
7914559234481560995636527479242257275805
6499441483098399385794921234747318942823
573357715414342175849357215918293686712
72156330710129561446748303142497343272
4204273286449123591445142723477846318732
3632692840453828239653552837832524063563
2566187792131604724814291136428398455764
85933361841910571981904734416959846945
65286988156910647584898048325263203089
4310571738368674631610296441878331896638
1749219966044055136393245327733141956
3901319169175836612440162443259075164677
43223975083400668506220494126497354675
63191419271461469266958236329685365721
90533626770795812552685285763859607636
184815779661959479140501092965224612552
897966472863414449628846670170734334156
3869983280678546292257602559833962423386
278913886666491968620744511688258265374
4205100950341145375599373608495870539928
8000549396927115644151223259155083586679
3219356603212058123682960263880889310000
17612543956137143856202530507237564519
49294790500725190953518739786643167791
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]