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
6646935799342139295756595433595451656241
923645753127609718911762905237462354411
39808921974323432026665911437474918151
7534721534124256987220237315412712051446
391343689483411698271259315204928011183
1053308554519465183043329659821259039591
66959522892555462615578813642094132883
103881714971854776891376158910935913285
896221755291374524128153705770769414801
5082666390132037260822304108118942924
9252928694007778633750876948387681844200
642547293433912911756327332767262714240
829320782999540941983273959654075901687
293142502829996971799597684237431606082
1142170884084502723222079133183721889853
2641253230364519882320551026411340159032
3696463823422727828386383333335453725311
2973628593365125502024625821339873056736
696926736806111820355484949733982769484
2351319525136913818756323871203368835338
273318494647888587777834390505655522038
7640513528285481926630961611185530157824
64149909506359946655894721447895158779
995365607004283244989951232645190838747
834035681187946429633492975775475127139
6719932624586524521079714143502316912731
9215564546807257739847584907946372699591
708630538667341330572846911985042927075
9829478816398739879486446512289271865657
6245802950083263827949556885938185884047
1158715024539878067635467811898555066
8505592280117361304771978414249273633621
4025817486535104368397029013689773861089
103627937682103225017983026222948785979
4239922794345801418683625539578116022265
74978753673065318418333919340490486858
131876636689528953851816008917492109974
1673140347776377321769295223942121766171
1963557572988738621411388368799373198811
20987850554257353907565722546951372930
34075017411103784046036745313348268
4070403763454409240617365334648988757797
3791165028592336213422357973909898494159
888824263505499684698254405789693903810
9322588151872749210639774733336077016978
1153398783787279294512469140190087289129
894731885041352645550186927887840574869
7786906097295073571964678341564293303
665057926450999510638122314964486573255
5772169215817314212878312024839591111297
278313096469430190359564524889378922805
9724902475404814924494703777435084775705
9701198931251213690185812407920788906868
459910922852968402995504282368373693356
993472323373204592299103846902495719165
42855177308795815743382166184015014742
47352543959314058615541835741867565838
24459614288791812347001697012539892212
87293541725292155529032291938712636426
68527347151164371612935092546746189034
6679672868216209279117836626296049356623
2723760684659686834366285483799547645054
5667506085641634810548246978399852903949
11536715293227924293991493608387549
6506371150482485887589812035651315132214
777411194874619884147983552735383777845
17186017731583930778413557192778329589
90476601269227936487162723152185727546
7270909895175240382262833076880932772890
5155192036705512669826215588356960624131
1098636731789483602973065522216656221
3847465842634116678575013369212766523895
7192805461982190732694903835119259379374
4149880816796964793395494747947734745247
999873596299207840103539497455703439643
50998147313441852339643748548043685313
8355127563744333236374329671987881338430
644032989093714412875356755425443056783
4044848189781225974988142622644978606747
2001110220526737709071544389859358628385
979180731486169676886844431244884921648
566373924659025434980082500372296445295
74027421121769297922346065058014839976
78832555943118196672916842143969101
280386235889666611945558459339626224439
938118444826271763314268787530375405481
575243325245626185084038951792295479920
244786502287974596867577107606094486868
849772323965246147350119897808911544034
633726253707938082259831617554546879230
3706835255929927892392639522128297253768
590214159008926864593605603487442787361
922858711726324212985424162443967435159
6144591938156364967986039861830149202106
369316554994799857215923715296324316693
4244521339882430154075818346774227344856
6143852959062775736837417785287096706853
32068552915103589334702960564316228040
3061602469667812884041499649086843479
298668239887029938063956070957755578730
527139147259892344858345477935889328351
140161508846812497457614995117751994174
8123263184426037879587571029786373725845
257496066607250531764133220670841268495
1062260615575025447192653641268529716045
1293560886738714744442933785587140823750
866722753494641548274029403844198143502
2060212257779649714647159946763016311
2541187022558061632893904475619093486
7775424287389294178970752024745190602123
11321736095359573636047576042007921678
134379342361238141494714808106016687729
95063222532384530564638652918951099299
641419931861812578369839163381784815271
488286407339720344180271918729921693630
6413967962706636928828867530985444561750
5992498544692059363327604415387317081
8363604282799660627588285670773871371739
53595697154267542044233358261996059978
9979774585994153449382514779628226747895
121300371773939422166195638442070746022
70722721407379176002875115747174832
416529112714405498893956209492413336013
7058446751142117798045393235981525685669
401744537959269615076898487940043216728
4548618533596831653802113746517141976
337469795472283983192689833459754933686
170376257469869456054469961695849869202
216867983736806590478810985147938677374
23218004896218772918814725110951965608
78431678769813171325312540311493322192
365351865248575349190741004277512612970
13668139097942699563522322752314402428
2622594241779684642117735575987182657
87437253667966529156732889859654941505
8519723532118158800507321793188564395
216267731353424132511428594332956246839
18942599206952423120628040746012878131
994917982439813699197557989452152626373
767139381318114319628449427099748785461
2478471416869681431403706361505976260
279845655127278427684551326259375822539
129943345741847940277682404217212721859
4247202279691038790430663461591263317
913341377845960683853564146510833969698
9207970923636648612055302334715048245260
5455230355972794190292396921883957446853
690011933795190156848703834434468159070
703694552422591510035963846798371885674
7509381140431839458660089742348795976097
47821115172995358765475747667292175811
979659952181767086945033081449378735923
261172772674376763218543724530348345
1854396518569120360136053007761251298239
8723754533088294506854237116594865953286
28877127234755131879234959560488109407
2439425489544805648402956537710686394
2560291332987608395069854213857256294326
8602189897214779953583125969096634054
92128025222066554613494368825401015978
2102440232726347641373790991918195253
91852486364654726460585643741161807647
138033598203323709577838504407396208033
8584596933278171435752558918745928499246
430727596373797381875554994836927272927
21642943843957165947088354295754595679
231916709155651415189328000515836811050
526286671888767117826476292806320498451
8607256319538931614827099566363252168740
587541711435807968940369668822847008504
915444366144227195649354259497089172674
758517678090962464825458052661998574444
376798734750946265191852336338445074631
685452655981902179526933839474466297951
6980548898964384328018575680554916181373
647930620238936520073803266363737505499
9709561835662348777267683586642889267433
26161482878766641635428933179639562394
191732203220280895768427911310526864577
2414708835598806724636947693923514753952
254373847046513478409935390963052217936
840820124482633596062591774121627437634
520264088995479714526592757751727641712
773812427679140565378566961183192248137
72067880573531373331767286435235818111
62959778341855640662835531785432389930
17051920653247988235099206538755487555
9674768963184367605518352157774931207469
923154335042244462499925452723711499658
1449692571498317504829573217665122876863
93415987459708450329914731920037603467
6666486990256266939694488333233316662252
7619820757640421302339978018685604926
5874523868854822478670762414835039325312
166822451662715829542357651113489057114
1359649826721246111649816713137634925
71292229819181997963562314772031495283
60237094313350585302727873932667168966
5963285484924547427840428974423948389141
627197786650223675672738344430380225799
8050320324899093678075152381858598052077
198933627137709861618048681611922449990
4619791776768225365637054692944699655
892295535488348350143664497429389879116
2941211043415651390638124405428023697121
963395499179450413169273763160074447093
75076660470179593243138453888427992205
846068503969352346120669139588555609808
58989420681344395103544897737580197232
9860256490539727993451767126833169674157
2656760299379894417236123100456053816857
362687035723749212953425082372728133919
63843224005593055348441892362336354873
5264979780206908888783934070549924925082
4211922767463701862282590941185985497
1466921725457807945186935968770989854954
906897770331851632769514679213387027724
65929980501293603277231962695389296372
446174494323841422404260611835389318146
10061207249905093333004426758236345400
474925057315137333426606331353539803028
975256371861242234253267338159810027546
256642671396269625056644983112681566218
4004645848114246686853184065740468026920
15729847942939532309450397593742065439
9911357789644239194390493713839453667149
9079965388457057799934582756406165614215
78038813574644282665063318531224685893
97222681390939434863047821159966179205
132843048867437638269593287845646208598
4341194724455477509546041585491104964
5591424336951854511841844895782533558953
516444163081698447866422708284223246447
172966154583581939838094086975950098364
599138788216940314218592738860130994040
500721756063664292428276687775114831800
894923741416487848163793597488436123195
794472325246331357213802601700837361746
59988012545119224152332693717259921718
407710215599943409779564549843896887769
227759331835211508047989202243499198771
398837572336961925885752638446923773163
66314517542470273533885489568291434898
633562498959352196497847028637342139335
275632978589166255494672194078547131910
77452259322572978672821854565798074627
3826232296648564112782111701254627714948
430338778551584227178953567746497738727
2000850437392940383673357309231848913123
859265075673996301673751607482070147226
695383407797857863034713541586852311418
1188184836817905775589589604808631275359
961790641781271887625197776366545467080
1363746898813430948940695654473817447149
7415409752481350099444145297372374832
771234767216618482866342716733340069952
480559217110209829230699746568666693571
9422457479659774106849878553990468815232
4365973640164012496732621266726530355370
115581922347434515290023519190038119616
173928771814155523704508388617344458
6391344726246631423276231759671422833693
199393063988666829808652317686686664891
129174259368259222965315066331776533494
250410411473775502418923167598116989036
19474123635214544326967608567744668424
54663561534932498852726043622069909154
338160245049567826719338808767862632273
131743565510280512033532495120556792527
3462829168776556559521202423686076331441
232689379671962766997963197946928996627
25928054413995298236121766939256396946
912387713171875734697863689888918935
9023349631213199475782737533339476856
36026922950894186368219438597983686831
3510570180276470280992423332556087082757
401477568253300666982322386072021144807
637944207772979250217272196220376682898
39987360848490169181455037338113906899
4785856495091659381431065103592932621033
5659592531069475529430256383797525193953
66989719944249042673337641654099939412
6112048387325468294172969554247876973
919995686368646582745306662955295937933
2226809148197684242774492979652991339912
6493066633416293832018328541177041495
775614018671723939793394326994826849438
4131162916746676856928058043393955258681
770245403023383783448308985619554146319
748193942259354795959438806539485374431
231927101914810208482587439917826227046
641217206565122665816749816505569969097
2042537290681499255388197560650622865146
8429783248128028333627542014681351483187
5442867369392455748951111015808292701160
6922918284211856591167978958851558712828
1837624781154709748296445454565834998866
290397757026358696843712520915464457217
5651449119983665465054951609415351924534
272750188332009601998861110470761806351
8643488850051458793293536115984362061095
5818204980353692692571451074330696111
3755663031884618905263368863357852964552
72609667309744629611795384299701617425
209928148887907450189577520228084202031
514080109238466790838073980125186459384
7493169311548256852528236001768594864043
584556933171624023259251972515749979981
621391251193742771254984044716889336119
555224008186122522132747810976077318015
614396473970850796515196230685471577940
1480684978451945522969844491524013448
200025665178845767243623838261069229054
434821692479127624897152138962213295765
603079274448716551321475440511029111361
2468677052358979355092192904364014596817
641280913746904236282128402389133685007
655555213113146381634170269495236992237
8091377380781158820118519304501482237
3901867524566743394795678317841752854425
1036887176161895854954769772745575577160
566370988118265404229612987347430344618
403172307362606854546178167573725651767
371571457981379891229203507151377845562
859067486169584126299446282308480498258
23211375147452389823020528316045282928
999293446803372251724871502380463362750
723095142675654452850826975111325104889
46591719353653481618758117914199159680
875496631576663206227509822639834707529
446512912930273237630355566390152872
563916017845863729078786834889962075079
957189503677945728616595366240283371967
654679533918713224970286986841657046668
898283946969173613941172578835815707943
93695051968149433234679969824047925969
9736122132998185716605571200036866395
8520167991114483756485639526396631605107
45456712609770287169462985312799134796
278576238768370652619094250065904912119
28868010448610293892861633092911597131
9482907759021819799154616055474556532372
8092417075502956597081984110158664367047
2706235960145511695073832753643487588090
80794773283565495867595965254218475300
72284611149936171199512877419982269192
784231139614753584052607079188777516078
80447262468803993439585684102141942313
3649676381685835753857374244946975109
148477627576585365548595511187879878950
754936529685654816818795393482282009721
802092036231807015718250888897794127357
4960463953463271675534617668205759549290
98010368392991173297125911398719699875
2426542592316342840250847948633985417581
50211825227596503237599253794970387251
1516488895942862144859826376603772432966
380692282736430692957863917028851340361
841894852287514959270445731381361057395
1140595313054841359136818090993569421545
7126203929614695550547887828845056895391
795134164011466668114529828972121106021
485751338126927580474040476355363387627
608427468857138370248923369778190728930
5530122068128293931283426902322285154075
841783141398143383410708647659158105580
85567840715164172298387397711576955620
672257526640994724564646117555094312384
984671669261345296038255172200684425438
8537807416326891996929795385597858198
493978114418742386564371325732734717778
9744283094505819642911689234839914224357
925398044885465222128072170804223249789
9331607235355543315448984696545527073581
237590999849836423165964097181626072
383234731480566979235882462956775845479
560510543392682548711676461263415995280
508912038870937370835110770916254998281
1730294567362792840085069692137737410
459875143007887394639832456952423409867
9258387598702016111983143353920420097831
868157753306494761706453648566848219035
76999966874644299735635246852316282271
4636954955133808076227762890717226150
259692515001593642597507740647472266889
208154582283138165791336613142126256480
686780917267330188395467830211591152662
53615197211568277006592135439672329378
841486398637570835453767283266851815357
623787626586729616706362649873188514208
5202570652801171422286625205370844233496
7389351755146136605318932397720713684776
409844915162619413978499483547759579255
763890105782715999744903611532167908085
66732293978347556220364467424055314923
528085852259762087073941804548798740833
914183201556271148338502937633783863479
5437134192121636444014198672426458131974
21279309372152437771734100222529771437
89237281853242235746863446473553771976
19652543239088817229876154979487720992
773985992248588168621001647163721313452
6177191740143941758573103736183829666329
227869665566560348341017271841244526997
416773741052588921977459576906919396324
7901769993436911568555869658590478955566
137764271103237647520932899782433669122
7257190954329796363392076711240116272934
440825741641898124932604291672026864182
9441264498527477619291704532052176
90114593977283398002603743928662536512
884414852529426023701958751478578064473
8120392149076952940083321761368839457613
5497481709918575791049851257563493891
9006266084022720998590461188878268701700
959120549995843151998924787996195984380
171573374238288725632232727673937159106
842752633165006982475003499213281399141
977477591727950270661911936530347477756
783916782300138312541664714154508652528
604158988975303159336129455628344899298
53891249717781868937617082692413542186
7302110574709879884563262699546454716908
894323923586689324613421443449231132716
243785853727493288136667968642252974655
7733875321175112556549544209770724379754
7946799142133520991230934117351897357285
7886142343285286232882603624326737635
869852916625982128455726531819037063624
153428764464161030097653197747199934886
540467504424842684533293755837206376598
5785905347785036785695241179742375682578
462366054758976166196491361209527947157
267514974494334144847723989249189984
5434492875821359271590288264115182732818
406272824735828868925042138409376013582
921796713311940538852347230777637199945
358296447157569926778153825838847461381
47443215744108663817093971736888064998
2060567872162574350191475856327246182
39242542615746894297335411173045184913
4216146744985043909257455834753181175529
8032917260047172774833238254504716125223
5641234444032381600645665523933182127647
567926475229463977108781667159946336534
21894815549725057202937781104676557937
7861735934689820489277599311086806782
4778555212628017115575553165181522166
278948634399901425433819898830715576017
618953019121239144811077558717274952334
5356224870281612832286789225686253908098
9806849597834586671927817838215496841013
6138169981628749732655451857820945685118
7516833156261616931431629579561711886319
6842242823215047122347496262248367222192
980735476813259067858784649029032045412
225374591575929572882347309157485912834
8467242692626280184266601616372910842955
4858707045591948159488665587297581255315
44566395429425128354307270404912494681
278273946447890927213374889740940377125
9971450534782411123577213401171746061790
3877106034284940864036386385345183284376
240114779262620316162101338871418827681
1682474634986951619438786229763292049717
160178246368267436438699594957148985807
9202374052741667624560601601159324353019
270312971601948592109945953580947425281
181372034195920623182776367580753625351
917610965489094182677442514875899593463
5881774047452910372376416280593476373500
49645990154653929120959207122555326433
683541539031756132688212717030584498271
309357749493495818135781926931898562879
9776236260549481725959891352580167426061
696428333082430366864295811342108351789
101554834683397825613783886547356733890
604754852832832823977140893574968292
35381449904029346039279519007181274862
125261435178854719459253555879472678754
517389952330464585623997444430549015680
88086278325443989754238322621702137937
6317706415859633784119135464523823258141
317843481183286808613911256575294752039
955234706067558815585767085217852436200
819063167100358653171083158065059003624
48676683925531993112394960893438786914
328145153986448244377080303021410500
522283705986293269791313581637332347
926296138368875106847465403404146431847
5186538028751647707435229815773937321157
2144569313876190259040686653522341487103
642531565204316813235585793612177338562
92398279352962394374799789716784824310
50996694073661785277916352213473438509
4147680865679066314870976785805579514229
263798829597623060488007355879526832306
788482473895697956139031593544844316377
19772086506433332063485363611447751267
3685315195112920488182563491551616137277
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]