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
6974178350044251635095544218995499778557
5810245811336833969813513719229315487762
3974646283821706581060493944828740341668
7815828862928537717634987041884116679059
2897963166816414403090149127567341990
6974212251496902873174559699847749847589
312015608162211545453771914415848896
58851573856797334244848627746746451333
583930233858372452259756706136285147682
956411333365560403919194592942562353760
902551327321377901191432770440473977253
2258965575815183327512688774667378224355
838656654936554332998207896917284072042
395377019637780750123093957497057962206
1912109033742494181721928065168815293397
745498720418843514761478623159659513006
9044320058854214445858091546603687248612
124760652558112534096668787209936188467
394551375372154180612559140923860345097
4358181643767984892037798105127633837
4046192653464104248110244213378147922230
2903379089316528934060398174174065401198
91296451797884218964225286426630389396
2019400566455801260112211956418416905348
571778590910823986224032593019049853
85313906211011705671723848079615518343
2678301092368709673369943815304474121326
185099285735290187631874825259981378625
2087207360767912976285559565831361021734
819784491426828342369040934087381807132
5347662140237732460846573504255181255111
576012653843991171430809359278183174993
84822238039981787132872204248417075653
3722536911075657709810168278298599658918
173871783930320084191549682645421654409
91582804170353975399944453826229528311
159765429108300041583817137836693681552
9397338951972926417026376341952135127016
48126523207689585210146547413219871754
192831831952882163415866133325510369662
7939549621026545830277043073146375761607
4512194623633098292450798561250833448301
4973827217495998338578716491327514279010
5672566490606259812671583995227269749
617447157947991633782926585447896223745
66651062351562146065417501331694309702
872852347254147167532033293376784820
5016332723624950760243222685218711357143
7190293245388131266811428681729484511070
9537942935059613191441179846798180121932
493419466281936130590618640251892441134
5955366192765701551435588197192512582407
39313761930398798836597522923534925698
3371549481433362325555393191365759490
406449044335939754066893125391890617761
3409652424414940655152645821154117151236
13608626776298869659280735151197874935
7123801524816406604793724033395612903763
86315186413785693044833329897763679345
498520853655011520896731721494910839549
8755106112806249796939431688949469847491
8209907367905366329185547345656537448
7893956155597732321677013529820418072
229382114052145725111447271843614087406
940192404491782550211014896579370083514
398621635306773528832471073339247271740
1266600199035626862445161873769471169414
7791472121988876968130195861626622387426
926235647720448547232977041948421691829
615479138180614977155982177124733648918
2538662610973824317639868711503586136147
649851407565093278665413352511796660
965960256249726245954789736674282354392
7463234559495269089277605365690564894
393431981043246293313114531036315867063
1574390896239359341815651934034064378
91192870644282742605272467084451587141
403291925926296338091658068989690776560
884067733944964166631115525085756095426
754125189892388151335816896544773494
67942878938103055884765668628644348
898174734462793015628772509522562976191
8276456283370531950139846747525467559
40417949870541497472360122699986669122
491944977894634178979231489329918577088
9085419880988326946160573005174605160
253956299396232157558495681882693855535
782192696221359278008276533211387435435
871640526545306480266566629707058927982
58946467621574902695273730399767302003
4797586567229662946656599962856539102
221069586576938610220449218682662592
9614418149223702207568855695602671845832
2325147145286213608248073743925643071
7755826247147473990559881761148939701134
37034592388992745393629669952821264893
41435871840360781722385209969047816422
16436803081983138161177356460164679970
1885460697241993199669108457632173372
7453963228129627254974674118348295399386
753785936646044230797333720254487099280
3328454479173013304474857812368188665
706379877015800130879651677239712212547
5546814068682908686168851108734577694178
3068409371278844744791253430973273461573
5464912085408856875613442071778396227263
26777485083799526083460455153829726560
65487284581609978184466686254421734590
6794288338699928963156557081569297800
13344558649490321861172177261357812
117678256022311774989389335754180071035
41279441982580170936789860049368332806
751156096481292334615771545894197963209
1810687888472877235346447482389652951186
57018241875398733797604409029714646516
796119332233965328067111105967745885308
647159429071311763160895527145463321900
7834983020299311688869541567141587835219
527863004454429998042444003589574802865
4746866587489711678787187938329338438675
799423503533293661126877789170751392354
536179425134649965622565947483126093008
659299610180475288218914769710518605
630341626195303527953155599247643172590
3553360032279703272326553732338998619526
720217999007397470753351101547863123095
3394781313064566064788360431396733882
473590163201722435740181552694775952859
593780532457428194031739534734545761016
11163441283877357691045386446851113292
623649439605996491358789537954754586427
72941729721989051965258283069252480808
447514731329463636826281445738411781095
50603809322197908542487192361214915625
99566920815297432975424515013499817807
91268269515125849789909519219579412424
6218361852193872882859436065339842355463
53782148412872366047935475811901828329
539568045148699713552707339275750128907
4395264791972835889146679227292392158211
455196911779287224005400989330906267572
4327368590505338546038912134382542412178
92608638862548488533560359195015634830
68077705589957083415656204533540431453
4310898955336485936644017038317744364090
70602291977449727357478858420877276370
2758125494406888589575679614482787
164318503259894305089879863870456044073
7607114780464959360983707242867452265104
3190186235641989415165658121223699825773
14242834282109983272587326995632086201
3928920288759837877766583727248576546569
2645724748654417159092349940798947629344
8123363437903923336221626124306650514261
53508247828299767775529317158022528369
558896244562412692046372306113978722
33621187666164461962141683254787337154
475038048673542811181236913859326999883
7318209153373868826596584658273962561702
701879929511405682382846294774308338441
21482086985689655669633751858151346004
121917470012437258132959987723913307863
1351424724613668788455413500499597742574
167660639734723710484922511837334667613
131366595806390563099590781854013085412
4977849892365861572611807761697362114661
448295289132973345794880996098063106575
128076984889197224234770331681381498242
4450829049878157853118628569168996242653
309974327741524134219558792798947215199
5009411584029297808183133070994153209371
9324380520032876037199982959331709301
7247929941683179221424181717158736744571
99134026106594283629142113807077857694
1910660041217388767212112408743326717546
807324878536296455628530149406416798
4713307054397757865480541523467153945144
72642134284941045011564374120394038394
6634933234525442343265833737572261943424
53327414024157673912742515348315581192
90292341469315432094928575771361177278
309146053063148972562827400474221867188
677947436401762263397896475335969098044
1018987066041937318867619649076636479
15297550502481569514259874159082266826
63535852179036614732001359129465193689
348334682732201413521696592252727343673
52665591645348155712232403526986574481
721204561304702773633491489418817331414
825535102717157687249875112479179427890
27028242670681340627146235246243132159
877819981005331305281879862888479251762
321966128774263937054459577296119898106
9335283221141702433247139313139661655283
205554414026123347201431194450422791345
8544837810022773800855902936287928488176
627673858785229556444306467852764956304
275229968822230642345069735548514848708
3388793467073593385450991569998313311351
789266112932840536744359547104362737118
6529176379887110155102947307480945068
367592795395776970863864394555203973143
513172475419012288656438857876131762291
998724201521541328463814258312299348160
1047337280362847631475017913345276168
8686283370485621516270405714937579777767
3649161371382419126131115615115121976
931775002333434780225262147480053310568
754174479406580758784751578530368837097
5176642201399454491678695228593353237
6321841535517830756236111396531182479991
502087491458916392739771502934395027215
5528329017802707132293676368673183754889
7240113890076083522371421082295116073628
5540897703426998351255561957250318444
562065947679491836712421323383242939460
4032686631951050432314047346363922998723
9786159983854036197886462506481260114828
2600674651453210881924320818873265303
153359573493405980032039360123559326703
64975351250597752774846932365334866136
8682130985982953579382622833417943539434
6109267540983910830437539489909698925169
207690577352412268165457148276398887574
652547341785192279540564350989652796795
8917192146689188548497439936876866794351
2439614563499649683088242111658438314626
141014336468478999616891524341368483184
6582153534925966230672044163273954853088
236974841490453444969917796968011997381
4559107321258175735099554745765239546365
59347922184099622641367671616237128479
34126346501583230441888606134087778
662916433331336156194404167678213002771
725586652292977244392690666822359806393
59103325287443489106428290861091962758
855732886056301882565302456437892459662
3123794664374585389393943604834625992838
42016915210214373027077792442464162033
831679703341100263796574107263409192972
917024157380847071699048432828072991031
745964834015346917583988699512289824188
8335663443544936677780813784784244981308
226260123255150528860876937103617804742
401514103579827979015638573428544779415
7045608257635390616638587809155196162769
767548131708990580816328845908072872608
2538181433847880628939366730250094488431
690753665229518865193224041819396633969
488777066866465139118157353308981783051
5456837717914964308365559728246436393827
1636515585467643597380598334982444303692
280070978958364246933444555442823849947
636771139635023967637684223829046346207
7644167253059511887263477744104179159398
4412172343706962465123575017187480004157
364757586057137568716545381635673399179
1475512794285656114440863573443658343363
3159890417618089968367852361958215389
347746934433196333571487368991385716611
45398527295503049226197780114372459161
56653946236393549124201468328826327888
3679751197785871154296741942183779550
5687459746547997173433145847693877333
12864851862360145705148476267136877965
4819361175157071220590574728601218502359
2697966469694844394269455318950375436971
77518281696387438450254745590036732019
509983236111727308695104083162098514286
4861310912373394752478784728491964138541
641648941216681041088976914068369392365
4695636295721550500115168526921813164470
5974182862327112383070659061299418503005
4874517837592884669855369402125237990
7776917893716741157326257150534511836616
44523614269846118556086119897671695412
5062517535809587306498794450042926588
345911106367780760655395917637699429767
588746057409667724832764342124782693669
4035122073317046447671079217588584277670
6613855485244375878161886227814822443952
5564752937496657821603748735288415286
247678655082556562763242062760617477418
4114139990921783846069233426478426145580
307116641278211713724316996476949368222
602316797029480681217067897804113586
2730848233641512297093666136342137194597
12015753763347363329098128134799762733
4293898712112256442893412479699664377430
1773471056737137783298399202903166722732
38118271718018382347636876096898546659
6858422611386671513781007337446737043881
517889334444272873041767302968099864180
4164932766714083448233089001135459445653
7664259745899131762252216663780799427240
2006203050706960641317378989483991281874
551988649113223861279003766083239621619
8326190455734116245411975642127133071
382738569791430323018553418547315204715
960515338413758819192835694678229636158
30417588009644844459782027212032967582
3952287494608441968948829464126218063925
725289909155297560296357299619949069691
910040421126262441186206725239137904516
47253299302434123943998854669578500693
4527139444712810937331626934127732237
8772816061173085843715798270233337743
306356554383510764751792168481639273410
57285795173440123887436469225386770863
964931317993928736256137565548362869190
294426475759550389360669379974852643881
5808261917444412762443695450651149242391
2855668869884225870261228526145303776
303179165470857686684654740945253116978
3405516145388718780015851460363756162895
7608322764733560328246985887284145426942
46658902856475838361091204696658189891
829862966642720158317742632572545631215
114249267019472449720052303740384935806
6386575720224834196194964157635183499
50478081635747844204580133359376006206
264453739717492912044626895721770947795
3821912020976257825867259012590073569907
352145856813551971756263973665316306750
38164849567445956884101627468618264631
69079485244706359253341116598209577901
728965717851827452443624033998075043
2103523788213664382416176173461568837148
9153334390794155613068969380181998276475
4349439858837941869774246813312702998
2700847988376775326757282047922010477507
467875361865995876579721368252162148122
9157546686616834410065119929976190579958
61623779978138353526544421961665315385
529854648488809350233789800282011158711
73917810575013402466926123158718022187
7256202422233765346852315602926935941125
3544143757311679625019889471195043286517
54199282131779179223719997236874586379
1245603514045924975825922663501479644387
69903647690474963546305520191198082800
60485129145689386612317476626188603565
15318608954863109584364318812816419351
48263085346049572536639427567126218309
439569727516086235836131776960179269814
247528406643312246628920214541945901990
966825533831716994886389156361969339280
81548352301256487259661957700368328534
344299892313381133345288728541959601721
8675353466747064547560297310835612579582
238273732885856062863546232790235345216
2060986035027235392396525328570849197463
2726893694733272334465902063165090582482
542530932375580887238327335231652561032
603573858755361022639039680289002125535
920127896879104662251172304625766590
1968278517504879579481326026812527815443
250451409918755074687449307399516297178
3180805111487900393995341153101846481430
811222944123086798495952562506688835459
8084670387457745473295644164518296804478
853075283628252243622398166384439105633
52834893976868779124310166192330387543
1745265389841935117897099786588467522780
36674702156799057480622657616631185740
171357592847149291715678912038270879075
6543485534976663736485193737150159034162
94791333542557089870565729052509891215
938438247338664475473739655182622298898
811075982286329481812629057217716502492
6494426585167280922615972541260723498187
82282390970415570539772218239779847533
753034695063410950888483928644297199822
685167811349539946291743709314382447506
293837374480584836255885988866281705323
8198228924104312635817608218849429836041
984631099913683647261493514372053429847
964017754348481811509896691776764602684
4643366225861287380016054183943222153582
356041111357638477031524308435661443664
6962111454349405520999067577259253529700
6014878650239634933940405619176457932769
9392544590862471242351831528193287633716
884477171127139016342563434080872850695
77450064116851490463618866728849363125
493877203284944255577268780835791276416
7568676635336429683689838152287676943989
904247221503043812936697762943797121843
9085467061726896974768201608680122903
7119083511742731672907602736398432352
69798510135695568499423595084817201338
502526233879584105651095948681730148753
4818543139032118476682065030437553586600
4955171571911269547583319199565384792
766189382250477941847966126931418983
227654818168271258842451137354112671338
74837308340427245160599868610124652479
207982414966661437793657494571242819499
8421242683096542567514008004912534543399
2196147275995821942114878771601577981272
616256876784670672787077072260116815947
846552951273889357159054980333161221783
2448663054465378159545359585204992576404
5510477640654946606327989744792071729227
18003703145219784007694278757061778573
54388947485570516821333478188839095161
6063946827341272473380194544285978082530
3435155032578218183078789321356174423005
177161327427516630181495097145595976959
699884447585454286406126863769370162775
6109245619697339395983222948192431084436
504283479662268764621599559857460913476
120654683826409381272205477678764027350
74453317908608518075596914116977246877
2068715234146647938495727732736979411247
7972393372137819845155352391131118815
46461142465777013534444743125748679618
5104215332769739878347038640391350232
7641186763916172772883704202599131073769
7373897794792611633162797382964733886907
4899588364746502528374542570641634694312
9029494130457878732624724005421968942030
838356362571792147381102250544160455328
7589112092242926806877693729975794701525
2272318876482268195097519319478778431087
254698894493727765722278628423322503152
311566333828865749291321693275898665121
703269917779907585727464121794681477120
1899857664131569022612975930933073656
323065467866267325324803331905556749856
88134056618748755145843561358457169402
4336368588342802219214208116846427197183
7893809121162952228211830778023386554
573028546747246157536375528929742439901
2872871841433555437592808723699684223839
91457781931260850885299797046817104619
98759937125335773081314973121038647331
32266476705227415330773941780627806590
632245835037307963516371411188913473520
982898723683278984328986314999674595
976055915862683689914348142756972003323
3241344099211455720866981611221275827354
875563501342603197751071153462453527735
950262475417925224737545828479053658839
7631511949428327251955706063720464063166
2320209093561067237759044306121660638122
5698850777145800971322519589752685761933
7112875399259603880936876288201864945279
9082913426721967443613599939120285233556
5963668070383239618561076314598950794441
3190660815032882984815157774597442577806
489590578407129122720175728293979274509
870824383888472523871101055409551964711
8686446752188768153890539024369782595827
3981810772802252354080813525754251097358
9753962884194361845363381058650425729
9592131757252907949697444914914741508
91349176961928734171619930728748897972
587691825541545853446025839880999051746
9028484051599177863939303617778213048370
379454867064775733481891497627059334301
676476495125838353520618902139039757335
1099206638454079802833603318589693367388
624179999719466979947173174173641551
83613238950831937762191846971373604545
67199900232014341423813493435151575560
8347393221357514936481894861255691342578
23809803925544581232887101210995458544
1171800428842265455114235894677524231837
4130118250965515898384462739921514983855
549051813901592275091309063842878403880
126698307792847477574201677283677211587
4945610712922787231186465552574103437
5201307514398406679857926700893824224258
612996848289731379097735987388858258643
7916580051554718409744871421607767102101
411390752421330748884974831884212742
87358690688511243245783119885896268998
417126387149688580521484982248773869714
3530315320085714412333191539657157163850
382554915511640459322417786757614485387
445271047717211450872362554956333239663
140559324324297520454934908730867094685
651597384841311137838237285191496725
8019250263087663201293089882870279008653
40512917973967132457139898533011032052
36583255712979116173364788177016285463
1198161281045325942621151197505138504251
4488744781995956193153821247857395417
237832176551166492194054966570345496890
16327815557365430963257495185249395397
20476211003705598546658850822871728011
51023328995829177135748286662354073662
385363456768323712439014179993864191829
6668295723877576995655123186314758662
506523625064223025187755499880669921182
8325424711599401537230717198633740195981
759721249869297796122802146463808526636
127250276052787064804494998400573999442
5764134721202544423528612322765884005887
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]