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
7137585036923220165337521605630595001
45761420456197307043947461589055669393
582530163601681416075276174840351615
48419388422887516345418886583647373818
796743971710862916781436109030278390271
7069146568049520608021064129898553684000
887694792704487289889852288763469382737
559501180391371597830888783306358874251
947919480221496235717013296898676249029
309177324533627381462346406353019932234
785293726104474500746992335955562735693
40431946664472245005771717642666355352
174044854426789733206305672413805931561
18695869488659227587923960881291719
342159038865747276295090759228889799435
457959631354538174544509012434734391462
77221054460849498067194237791682115631
680427822081732099433793604384772029510
8944967329179130237544852853656160539754
207833567268915390772814103470874977860
21034112198401617227846836778172122272
4632286779113153691601669812287674927
9097219785051548130776589986447941467071
9373948269212253372319219785746688134911
174190122741337016114579041945356056963
847035805392328762377101456540954877106
118954316057442597397828262450653794854
5835398267271122580783443178403532781768
89276125251644597087091810698871866462
481196228958254942893584954376357715604
9812148534903397212540101691650071039432
785629819332290856037274980174004996921
897791243259064443451733627571094059416
6415588187346349395219997557617246814371
8101194815404781109663424358966577046254
988170730171680350776181099113722679593
874727759916483534026501222885902770
971559175817389490207163808758859569056
13973793362821139509201405397554781592
754667891154560671494063327654512828478
605635306431270897933515845964360487423
721383111514744728638497832460060838163
81935855608214827442081969401986850
3245385532483691428145172514883897886
5727832342751221826874546049432512585346
142590807078966210529779519525050803537
3479745627713683340035865368762369552213
2878784226959398408676530305287116796
119639460631824574272430930820986244
9241888052868295239147359941412150331962
6220439392815838437777833124534186008338
402714652606923346673997668515492481477
11009733540844054921589735953053166135
4550713411697242450572323946125792095419
333446721172227027064151804478563933514
4679526028622482983458784600919646357710
74942183588574937975279070992426014231
7055345628383528871679402673425985844
9584338816358509482320908908252833427484
35744379857267113210768468709029718584
339196456986755037202659230547943522217
641578816213878974382799338896344779692
52549787309012197903111348002922326160
694812789863711391854692644255783335667
35348322530633572781821035279530921536
6274986876266579741935498622589040632636
9497559729306818735745317977881011706477
84201784536611466399113257234162124039
4309322299602218155925397677813990448314
10809582916485030108887115147724827539
8915895145318273706366343547459821665
20592337504110980436150167495560112255
3141757142382175455813033584940662567050
3831483091419026691189845028517706652
1739257856747747841333468680652448766088
77345820068312588969068159610765731314
6835596327766242788830268328393578052379
4733276570316232922816616653584235334635
11964484167457202604205991942670913433
24396672992741143187653087963418196914
670147478966361737663218555143922764187
19443675127849382492016474173096321169
3986623016776485211676968091206568449208
399889123451265036488211457140186491634
11055257663229425520451091413860898766
970851315986823581022205607356629729607
7387900499397565281066004351250238251
784359132068602948495044350982461318457
70303582619773365160008413464836452963
84229322637964591393169459301165932261
8895753399466941233564181970626404754
836956508842883464139898609190516726452
2228211644431332062984343399748212581
7565455070798632969798773837887944083453
702534297649954239921918866549605174052
898020307384950937154497453720157569625
2252273040646727027911920194244882721
90689412973977485884065170144554669003
338188533763526973661722877525583333412
9148759032064737651392958459306823053001
36031717354416355765526602916756323817
9508802077735686552657003368651342564880
6478566343871945194151664485641399883518
402842399292850813259612043128948969585
4731456854789915901724787173935845544
981115294587574022505127742365633674180
9174302731317983677551802922719929161
68851741129417483715816342659968967695
124380351719989653386925460529742667604
3694556273999014581851654919651215175765
94529934932949979547469122338838729263
148911352601314184761367238906549301527
9208878482959012560939639403788148822053
11439575524712890971146687393059682990
3661862235577372903649669141719394655346
8781562212538966866944527116478211717123
802076746805509496375372795817149368219
791476448552193436027028924237585001768
3876303529216823353199489514966663357805
551512745189646797548851519292926697453
3937430377507275613616326817302228852233
1790248529933954123368063530211082532237
6442581441954768540234151439301739469142
9378936242633665645524314905236036881
3456327690815427641235956590315575145910
924017552756442168784026401432242536910
7897285134818764467812991958239310726945
986288643939517157618968860913432757335
790884544519457523672322874692525252393
5673494225792016907784448184996975464277
4119125773508733952412814174823031033268
1138841130106592737572691588253774149
897729539287190704329571739820595439626
3432491420547860277886192135809363967963
92411733026747935956521691260471355407
696918975549970764694782878885043172
668214273814756547735766395716498478943
641949768036802965634644209846381214421
9841233624978330572782678601805340604146
925828121799328080584984255633686098807
527711197937767966656609174412325788618
945493803288284779765908340827085767697
5471678861939178361733205129703364757632
328827233712450864378702690591187290891
916522596828962379095112240562157090480
6156351931528670604362901997344019271270
710064717971715696541638169544098816954
973922522809901172626536416776434761237
4354675641517696913587336964390977888846
9516799075738538426331091509369073833085
195235016620618727325118575892516415402
765970323855786041999495713598001028
110059326720425542876891566256020348
1054611595640099918234593352771082133
7382385562505222697348063763201951843486
6596469929257868352451781701725965989730
36848993264678590261077186333414127240
636316176175106071651511723751271595369
7594494367985908335731252741053343401
28117593358081735005612775843433565341
5712379070327616513294237877867697833003
417697837646175222432089341368311151633
232157176365872958678660885921984616419
5642991544145448629463622061318301273
52687486057842459676100101764996257754
9511709083644143488977959709111955191392
8782641686673916138241375569815397933701
12843199627907273979449668528847898431
869952875099266359875749456824942508652
467926325510890613523033836011756055506
3348335286049319998043145851153035349310
26541896571149221885468449794476665717
445658832478679821485294922277578662111
149078183211361902211525065664579104842
58508393609784379255868892995373123452
605986367282470474265151439552512074688
61104989440369714441216499569972387801
13043237579730160967387992805480304728
68119953325605645298672713834932598216
8847551423529985803757579377413239393665
566298380394347979492253941658793061122
644764577616229724440651788989225863776
8978948376159871783441242027978989075721
358982576714568387841096915942454834238
82276451874473631824381512178048125816
416950475117253342239220711017551727642
15368180114132427498330556870165647016
54954251659827325989682466117850983144
450478085482220260016424433882449381298
107711837242546086991949158724418796551
25450936767198313011341523507838033032
436632879399732703775209656169085545298
7131428078459019961932975275120337641019
1676544293714455968117954141928553529
63113490756722792084417865983502446395
183761125702227229851842517772431072031
9974151596656833903351096212818218657118
674875788121630540843441153432991373625
500756077566915277127948230942778608409
29841814945749333935422220156215678446
2128187635086268977359754884770861749133
32073124714441986766194906788081485423
52075466896119306866565750447884926263
14127624627715157920769392930659599632
653923872615361880840785009514004340
8438643984752218740317751262954623464972
4967137790129979205078128300238316793622
815268365760794411183087471569570368860
4656695712411455732225802404537178809794
81123399462598468604828117217821162327
613016512080681477344975282044562757742
28114632607350543101845991840490744168
427836226829055272084567826163276361716
7105755944517503571630115057899045913837
284930327490317654837457892720991751158
14563702688411616665086252328381850557
9894572231556948494556741315163480693896
622277144808245295342605630881599679789
627434579907852672450662374504120877401
386695899287115150544999784775087556213
9306801034845938183124049534022362644
8021339138009587933782519816489938507647
7929502221039063397755674929240067628731
40361327701579797721344202123366537696
182168437520119269425920988479025537798
85286223315977028502471523340116436284
973847128604433901638525229476873571781
6666358643213000168022045094807328103309
992437286951927089743336390215784007303
49337305978990196059766162369842269723
711188225999007302074736373302984882752
456366356352399191819659816690636904594
763268140917417773836055307212739157056
18425231469476968734203281243147742549
705343657124805940467654346187468456047
9042679247281438535893152376814095078096
271987740667578881361233128074055877
184338298923147330650166297488405090
7195738522603090115450524565148295533233
57456298373113463296152635166018085198
6257704399661650367589961849760359935878
6454559965946091944155006895812768211
79614335276383804053488747654783338963
500331161585179376164406225552718657141
3489879730535168522945624502355590631354
2713430241474135601981765578369873771357
408353646881248082471823700692453051657
774337878424715545634932309937217542192
3599272352938387045949236399568
829459919644013533442067732478139246
5901280651219424455112336928178449671603
98402421613436913634567980640881308204
27511543848766390054999606944315304188
6416466935011747612636924971939250979697
321414834076596361014455647466249812576
6981870291828434565059018951168426839398
1528345365891659788540755199161612471930
8814475923306595792344937622545276985137
15896882262390500747275331473819392280
308951361533832666294457720203290135168
7350841627591234738852829738492179526919
1788334219385268959340129156288556514
8169183988903233407170454663339394169005
5416310014708871845824617590116669413399
289433063312771317891266490220437306389
856484055890541550198362415345353224328
2327102094084913226943483508209246572157
386693411547257667451974895482524073508
597571568626337663446522899944367543943
74761580255370217169131221104221917522
230148501203207722612441855252842519853
633690416829030534188399315113182902761
82066558194983288050662571526207523802
753644854219194762151745580321876248476
75481452812430126964197956129354816540
94667782380183972832139091353171141367
5858468981155228987495472515709920431162
180885733981262745269818724886566636254
1009626189239726307035114565477194807030
816673166235670412024251420358822142028
7780282223132932540415073755339789742910
20936000271572289167602174861368584724
440356471791483539550215868532811942116
71075436456840874525480284312582658910
49856725781108245356373282234908853530
4022187016334772405646194554852245237284
8454752183638234659675314137484134763
4301902789661552911779999934570467599070
4429131245276310480878828896932598821824
3479611129163572603460241566275316359642
8414707429564721107488486362349815598530
9068177878686574715651491728424652852
26986115220313256878724836912551559803
9400405448142955286485184532226971056
249437236013535953228672358541564119573
2753462753541924983268638091723779601503
569895448535105170633234237906818862781
778151572976777899639448495990229215517
7332774292117364414835829729495659848104
51982783472091436671751877444063434789
303622989702743087544504292400999529416
3817954982817885562712079402644873769
279678082911601480020628279709872759867
287372579394620365446403967290827010562
5634732253282509751025545352807474279231
4917630043048887455342782404867930816165
4845554914657258101961035842319810773552
230462039522450692574318970597558885416
160876526934205304391849256622764257879
840933205888736895322853652767044869195
7788723636742584861659164588642934338230
738742019288063206330864983109533378888
568680470343365840529018372122043327334
506097851892140189889578991468663597522
1446270063444382482140939764769149735231
37251091199018753050494413635418086237
3723877423912116924084796178611182089897
56908340514137246243729494616603851333
1839244775879494200468005229589066412169
90933517740558658342265272925797719898
71983341239250165344858166936069383
4483980174382663805951202399858186663276
920594333731186299548375775030592236994
87156523482370428489722116169486647614
6859626237254996687499441220867885774196
531832149661601478572877613678240818196
444831368244448592740043942910824378
27968638895588827243539673728522388678
182137426183352143718132863612859755264
4038367369125744774023714512343364112
345565497002954475244557054913658598679
377756915939452446149101615941534639840
467121474647592688137677709613898532579
1514222145937377214754595581345821702813
970714436872990374513149379883473917677
82707578817362113049622469642842284644
852423609185489967504174531717503201352
894915707309974053865681846323576539329
267097161839241812866779779181189924636
4003117959439093957565285895483286914827
947300932734593820514029446195516609900
8881267923353650974713644923731498042077
7602277415801049976759603748209073831081
76985907234041199052888442040771047980
4837524936357589348884468455409811455
562933944624707751923929464861282127800
25915765373887760022020125357788847249
429089127743243094342535537372654992504
494533463033431784197757010619773132654
393416677296432821988594056956557035200
1874809168209104945421458659721880267843
9088207972161717484640049849261040512746
88644184869514021326494458544360899895
394197443230246165255382782642180447455
511442981480378526087782978722447075541
65667247638331982099975765741150433434
78039595552650689763209656774012200243
715682305799497947877127500966149576386
9855790238435341533675976481633779397860
8685831646325316325518241934671076639
6637918667712323243484951787883455798191
7360961028286158187884276494199646055378
298049841128914170460006602594235089657
760994065677279875768604444762016459205
65315252107019443191860801716683636710
171635623302635985985324633915696544
1745152910436681493596084137965576494695
668219688524501457739264060839958087755
1012198051816925227750769166093249034
10622250318522596314851372181179013370
52427261687163778157517244597114837411
79898339715115854026733182186983817147
482574406858335896926456402510246623754
3457294014692259824744493659867485637500
9788182175213617552364795277906864922
8556421409434091327457021543243366106
6357533611126969796998249368663384693296
323936562034272663874999678038107208
1726328256424245378365881551214026025
69452346484249782536657356933477795733
165458062964818253854018435965753715500
19511670710263462478691577544770184152
9764827332125848723356591593717021383495
5035749667528272907565468521686988607
6025956964393503773346324541144359828001
257524961028916176633812075205397585657
4507401619395225603133509524976461807593
68254458253955085555734222960872306530
321095553172523128728107480185039492081
3226616218973715372753179797835222768555
17854827179177819141135222395590394402
6367430221086118191828742418273675153941
7943156585031387763819345664882169119369
817966075525225387312716382630617315711
349568782751848572724931868374587719743
483927034587274558029366232264248087173
45577657139216769416457169854114395880
5304477862417541744968849845480152956334
45709926665445277820707049845036415931
3870234513463810413365667993315842502597
614656459875222841848619365157279617745
927511814876269084508776688470697724597
532810081922445404452819268453589786
5173564658922905186550459683930420892591
141331492836782504245365526733072085505
18904827846538080515561492115483508024
9911292268116667418393886215423410072166
8373370336364391636420634062796579919308
50194228387666444599956795634983055589
81743398371249886122806556120033402076
714533637594628510621405355255554866058
4578472074513270994194711385374961121222
435699274445695400148087167763784059362
882836829845879923112175131990652462860
12956864481897372296071820415929861684
960707931298462388479987101957059328670
9542475777803263914283033680200417822570
819765394067699389427118756198127354733
181126812548603534379041218942149217206
7307233537422517363432447579722377294580
1386707113949980670517912502628118254844
563554649270231720751841861124277911459
81217444719567477954948786411062886242
4597595357525830764732374205684112791237
36613624360269592668738427453765553958
202631274572327460987093239846216764058
753212099060372318192536843051913908703
67025129154690006524596459420250573633
105145598333245932036937777658915933090
371823281522670795017536831328455449765
5083423848987819622739797060315627593745
873742146338267223224574549666820174192
401899015223231627961136232590075099113
765656419934435272826932421182623664913
235682352770232297447780233402069518509
6357222619966932899413008498972729115480
26541223937601369072870279341792821263
3974257730493309180682151322466968193947
7883513142117634846793790032882571527
14119619992131195557775151408691449706
9458343173723983599950156776814432684121
7261786537046471145943683076779220243138
205180494053641035529602465548285434820
7649683937338014323116691754194177271324
5157950224156088764483994398822419
5646904389091514825546176287388296438306
222417896438498119109316842823859884950
448612945143525062173073189664573603117
85165712761958936067647733029014096201
6290396571553051180451042438806771025869
92858661932977785961682614338336281899
1511849235778506522339965659354217197479
909030672393544055575324214610712017833
1292907659412648816080431721054943011
184840619240747939869591663955112149437
55078717421550510025507175778529036435
216127434922579997486571548647942017220
206532734626136575394931196336666764544
44380478154691581356405631996421043425
8958329867243779805750086136724039761453
348016036535481918014923398551597067762
925195923527986195581578751982896925027
247548076269576621682116135429598264645
4261513192107677504475097374333677352332
59301751607618269879189997515753802763
7778162388358537924967557344964441137761
497793866518627538625027673682456074189
810401331137111329795697601382568926320
38628458278841981507912634385974145240
7339572140646253498164863943962479696324
3852111779085408396984943709333681692
1840320628299625597513431696571212521358
2771143781368495617957236801238044651597
596949546576837366050327256963998172230
7045830068609667125463138664976538846611
51907912747388971774227283077416479858
8117850616996030561192133183586678306192
83655283560897161845828624686660726964
1455204664925500130115709179905852033673
6742368688746284373025845918431330209493
83374208128367791679203902180837624454
93294074432850267790699211373440226
7478557794101337612092249624617202354
157673709695722860359922734907314767250
2165320338025724360070531516630935669901
23222523174339412649967502063547329923
89293409933589314609476651506459081646
79235290923324182628461875559443473775
64429278885954713074222910518163372272
4345961214245231503497424208295845076247
9584300889245435926834950355034509533
5046511885882851939067653014264160701232
3045339723833537660934561273702827893934
789819136443088540412423705702358123818
982847445745322517019129597912739953522
45283711541737732975867763413314949705
10755546991768860990241070325512326042
46838841179802020844967203374254705531
47623831283855242179889260456616349719
34769075687361085712127077390294432775
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]