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
2427631740648637513018463786128555425153
2654496960196295665158948866820515017283
7185219136101720245656828776881130324804
57183021566244345394213248575937145619
9846912365353927351230114524533169849271
8514540495173552188435627522493067392968
194969554270865174738358587055233549413
475438052069694430153785817878780955193
69281295878102576709903104865051141454
4647458137618483188481039350242031622112
3807239444725265580750926761712291164211
1427184278523671258140676061288759058467
7951346949056712948428577013290366256917
7237673793519536481670714824584747718531
2807895640791125414691972394208681021721
107432874464781676019884556962872002891
37821952407385957327282994633633603095
254590295223302854764949178388095818047
632945711248156253416165007921821771141
326761919679555307273019247653687071371
8354451629122572326876931937166425227472
9477885349878957279297878024482373550
408955433696746822583067037957274647346
2096853569544282951635886017619453475554
37374598724218892123912575158552392475
8286904637505652936223447783736359019589
893828348151854313408293291021684888592
6253671289206102234191157303832371338539
6562658788214038874742637507438739316856
9665751294792620587351732020315238245570
8706781316535036533196651854231926045170
1694196488543354249173464013240816641069
325654001882729697419005739653717588926
326546568219636727802642850330550895853
60596435313582696704233292998857414730
389915797871888818073947155973365214913
5971913749354943932423443865397887314171
164530259178326454625546801584475514290
2621998368776586129589103465592859374105
4499773939418482008978322479982874680
642112893221807261148972785646825376811
3797789284096189426993302349282938536803
2834433929819143482048726297674884533734
3922660155503741653088204208637813598205
1627543863934983156654523508128688231841
994773757685998158884524825948808461252
7855853234750689455843289698798018950
773454378433135299654436572773019651919
572779707887600745027768952176432238029
643698847694587990163417781570311783
86902347861429225144502318684502622660
7114535896196586469829531681443110767792
2292549956934980282168439018268329591449
1784793668689075547386018561993811523
1351953170002230924018427974138578863321
65243171760288733368227206615081924
5361339761933205221568215353724881557712
1448216262953913296668311055246515141578
23475953282119293605300034807989646658
73853883311663369219366587597052071896
758265035064536226118457840408552422379
819590164810560341976716993754083088861
5091332955888268201999932409599531889
5745183425229928127698581545319728129773
12942076890291662218958397941524114296
6934594021689439888999131019375539928311
442977425952885843645202971775553772141
4077956645326514987527259484921659696257
4837548171227431396472154626226712222110
2786840570985255731443059561769853839601
567446946594464186644143598636108258523
4803253469316057693417172319466511001689
930088214826761825734673406212037752396
56117021633514012456751141644733864503
78601461560593713912233906447896812243
280296317078406583700112880895112445
43453848382314436964538221670540025703
915247048491368821484775257353224851571
6788126802694476595719172871119243474
3948995570407875406542788329209067746652
683137149078492088398177676293922422484
9537315839539747795215726001732085945262
4962893554474694704416369953990038776526
2545253136535384430156207389774558755918
3195492067848869596098638560844626692559
44341515945837237279618367344478812201
700073392711881163620329157738072307816
9917423543667818611326526634206728009907
534323471043172183484238176898082511558
2942365853524480022129602565930694864
329880338945874306835803089176194219647
6936530373518974492217121264816176695
4210184738024098455538316100113764805792
1655791681305259633581633391214904
72488178893438159669145574825317657352
3946365770378211564110217345913156824
216893014479513245623755330874465779604
3575171053115942203210028928379568069403
1765229668561075549350321848897286471991
525870122483098333328868652122364413382
503324333172153275036868823387784594582
1764613652723329472447349250231980566490
500674973788463322201111925254875893568
816453762242707825951726017226362427609
503483031415259587753375893378269268
98026795568791112631177958297718901315
897473012694869984122026999511258452645
443890292858762976766226600286082374753
171015926391308513856933362466941059685
179286458585986868171164401605676159284
6458328667979858212589279064384158589248
234088436311533597498768418270762031138
473479629940045271879931018768716574
8364613367325397466032372322711085554740
100251244295112204146240815431793569
642798356765498457507844659302246188364
5178456365105935716981517526157642613181
764112822876354828155438967404016213063
801414821632276886024270905406976122567
727146448414464754665762480220587452619
4732495332727266864254002121532575842646
423192968045760351355638404411048593440
9124621817979813619833891645363248991
6491347960461914802917476848814773459381
7239955151156682434217377684622432575888
67188517722568144252686234418344445613
677311515179623818075358406345515869015
193712231138565038468719264346841908538
412921725655622943543998928346643704739
2754677611911009592314168443872317021620
899797692819886559658139620301018926884
139079393199308897913443249241839478663
54558913209757528653204334178634608
848585270795443262549143079215387657358
9092207148698137390094166827623688405356
282586597208863783965974635074088761843
89948177154477193138678532873211924121
20696405140447533244886551262073829620
8261959958169831236552968953955234412
3102461597338129636315662614762352771841
1342348390971088922977597600202256626961
38576734546128065752185320500385601584
160897246051602445652564065173794219365
407568246887951554804385284111918722502
7798297662313591113095976357498673804
2334903154859141818564675526947080515280
430848208808987290949220915557628788385
7241609881583417793341668868873857147269
7914477066857676194743615437573214264377
248680351711188412279534477775199345365
7531217311622929392478241557458393177295
8881172454552607061963670546243291871
50599695680873115725975615190225728390
3958912496168273006205361183921626992
9514101413531208444410638162043680892
483338414442632628834042634793312794762
652019506605481321866429228547785771710
64175280239631061819163571950017272109
1185822911729281472584921555758382731283
705566481627633464970238217519578447634
7192772132304158298550267889358891837703
56803829316642926702184117721669218096
48327537555939730819445397479135488053
1065542667993641495433337991517143302
1892720933474837049104798753451902187
291748253853775854280503879312078364447
1957485535219931001543151270753143292
1034154471391275884621192924509337081000
2765586734824105775068092454312432536329
291219274535240725379571671291623870
854712313133606246021051199256573337409
5326233419512723974420341397803965733334
942469485118158624623595664173772785569
18101346137954375851261527467834282640
793266295805583933897200494838776053468
634713923054394651154582090355730932074
440213921975458139568888439251156649
9249410532042672918216181478528268426636
2771844669618890955591648245938950193971
100510336720274467596941586680633094047
4837366718438543847186769273198535693951
4247157723554333153124037237438846548274
298944688619713178334364282733875396
72131798952680784217990777262715436477
5491852747764237230227944311721122377905
438281075626811436067645979193228831711
3820423086138154209893062994687228085921
92197746391132338735341374072686596865
6020658771045324979355021572222340066448
7712907736985580692766909618815424898789
666820575213857413388555194623615223622
312025618759758103317911188344646279295
4443317441872205698428431922705560945975
30785424915891431964144287513454136142
346970232757900795658094713558268294958
24162534607206467564506406560359931894
413987028961027452742941794275073258601
308254146066413179536809495259861486950
48593207647662169444121717033018787607
6982591889268096063702755269451582916
2628447186006026993219256868970470277710
6989157717625566907297467836626413028109
7838123692524156534825078974265199729218
523958042868811865895738794906473044470
2583190425287781018609631615585108797
5361134994781179460963539848976637861805
8349910736301263611979071449304288274249
42727787629368288763792965789144071247
416121267898217370270390480455949076
12376809573448721171911979878097254806
329173252423730748404312641331276329412
114212773579666318346606078286191044588
153292246941421928331644857887995909130
9561603081324505872533303089612673135783
14519697550710843245254372935398538895
734162150392573659951588558905682641844
59892556524866791738710436690358047004
2013172355937824606852684761938817954520
18936165674883130058928241494076161585
9790316042414218735018037820363246972173
92486373623577157031633964144261473298
5777638711851996577791943598560048773368
1969433346809556577745647641483119849
181214766010395953338581644581534865495
40982012444650730617012246915356852277
6280597151801481678072342080402763515882
6151559645999427398415867697994879174036
402455922166164800710911866891619493839
164112995242894410114999349515160492240
9815536951014291487649526673513813127928
53521676613954844937912459293484348410
84559220286676582945379326270525659071
971427518250269734881686628594555607231
1995798895951438624261754945915248984
85308052985376202694602420818765619492
446185922568400217787264724187535614961
7443909517675046736975185658565542486586
2632213760769375362285777435198089439288
606793728759545974633768764115243873697
403726043383322435192966282281084345065
500614543747172141964564001962115209381
5872356224547947177262227437266437999932
325029230765083772410753120202815614402
61551727606862749479393667156816353906
196178354206742164137832409178874963107
3753935744753259171757046291459749421
4074140666978902371562903034880193239716
878164556177400421055466619730929501321
661316380793871124583443410521761971459
12742349391269548853739537335870142010
2219207596555060181456035763420621558528
5911952027440813726631346453717664969
58768165290535671305117658911792657276
138781127650591530874426541547829545719
761167347918553824061403265315252608474
43912392427999451983299512957687169558
4332677512012088574608221993821284125
365466019296783743768434270716585199384
3113443904079529415593103668567124321
8033841391485113909885439490259334287
7411250933597515629850615176237228308
8666411934479123759035818429415020578386
961568538597453259727422493169014533274
345280039443432381412839478687491341045
548333734126858053832872764454869111189
7627513799487801670092907273669874119657
6939121249821437111840062897212979177191
50798259163739694160878959447567746775
6058490646252227407737915665633411286938
384431187203211079904390939002427446
7748242457435726671138119997327436169556
2180424312345517644391368086725729481749
993839389158296015047787119155540719846
87992839741364555682163776653979556662
9795992196245061236518485130300842254523
2993776129427634367645504900244069201070
362768859063592848815899408869168353
667590894864030453287897135951999906833
2623203227144067449421702342135132035346
9003616359774459796963613288369489219566
8222869039849332287743569425463533198659
26128926927326668101943187556312162353
6538713534346863624694696950107964346996
8020989013347445972265726368342580485826
9031973658216925970677368932406211355303
4822291534869853727647757721757536263201
92731942409824787492662749825275866959
59793238675663756616214917607875731586
873133788880543464425181375822448309891
7474972138162203390910116432384087319851
478658562142310145715003604998138982164
3256100618835825991735618400958318574526
8335249617644232466372172700260482642007
937750909897549968301452231562622299605
63573760943451029762364295131213023473
601064524970957974003495516867823258451
90953839863400153321939723799642652598
2196383576241951149885603827207834012462
88267315540586870612009141212286317543
27905468560443118824982956181524183870
355222223753433841051283540777165451557
610230212055749234052455678189163734843
969217551853047513199464657498361142960
398247176852665533946896779388010464354
6060230692487382204297580268488382942
4268413346933535182289554589932978396537
71187393352896089225391874584443621162
216518956095695428408571123783863797579
2598493448217099553447882764975314784237
85576340852664337155252909854976266081
7248827614915211255473419938891122228350
51866104674890259317844848840388532337
649618684847354481412241882768884716281
291611999564628797927506729925425515355
9483749542824074165510152049775770147827
96479548895355718207458688798955961156
2882919526203843318339355877853214978349
6057544044241411161867275435304995247
68092438159560366253736818252323533950
943664173606996272579973331509375006188
212474496961797373338925292388373426108
305754695837925348989387227865153401718
39253853763920137343066655367083127849
775395773183926841014688811369280633970
77536508762535887941113659118398908376
456783162853033161792076974164397243973
5111501628459210141199471463364662554580
2199802957584984845774474027540194627866
839332091354692792319523999244626973564
88291592106764931803678878036648935737
120565126745245264816797211697487304398
516753837394775633797617950176310545328
87612271565439415554030749267255716394
9841541898023934621226474685665224618184
864165759880715784255487179147754318808
4048833428965778705977441971166070034382
7168870016846590824669877814694368875262
72458982629439276669869635335946115134
7241444240233527575800714186449162992
483918086237824995485689554436122595778
9159597810681463394290307574234562707514
95409390673968205132137764921227178647
111065883122347753455193842443468459889
52065670757341048759859145935251739613
73478165227188466273708605659894002394
5352784684551853330157725364904711389
55248760949663937441601266339235349366
5170407823778946913459109783825550568234
215194757701541871909046950151597865129
4096601976903464629035602642416275561339
12691739301319891623145242377039108794
7297240667081946858089511257996276001774
292336022328981624523815640642143195
3719159424861404108187459492447581991
841544483653529585379276196644275815137
8183101741412080552291385615215230631172
939497149460850232848083847306947399321
412037621467864383669099891015826423896
862115265093426413923915544409435694137
54751966701878247928275770961535766803
25662568782368864605596358143155678908
62750651110507942838808937323676705666
2586650858765371348389339289262543212
9295488146865975909599854543549927476655
789341508476125865924487151377568482967
433519621569603904786645129668132036407
99159269291831675354429509256034268080
9608405523452068953824648098665584007259
112065542114458798890137640599780641594
9686604726633668903783701478639264116818
6374686217735091813727173292744367014706
794830515375729738377735218747580361524
85161848874085814944156472888211363253
5639664954133786890757759688769278852349
4035805792305478270218611510683501313
5122601268791864324134197857408948494962
8192337819186455623939224256648961776697
443841001930848229672492881497083515
9957768666218482569175593844568134350
270333444898772345064131459525928007679
45154383569443874003201815512755887850
4911330538572985656279526944486936013578
9129519718798174102358922835673384652616
439718443656516586083708693989425682484
979144684077984811968861446832287923331
438925914049256164835789242216816464846
471232945305451967412292412765754609856
57111821649335303922332734512054924604
23977469445147787109440982531061602252
165457674652915114390977258696193782141
55021828624335126696730645798579076832
4073277839731013353052147316172729192916
19823887788988840043531059221660028652
4089150552623678610427911592968349322
32586181541582076988817497515271089104
6381197883589662476662899665128094597350
588947655425468690386546225814460211048
650641798646390431713134548280635664611
8387689576395841143440017133278914751134
42932216234561683654916657417215271172
8089252931163869970897957645959738451529
57312771358125286276771565890884230
125494019613159012133414651431953626377
9062601323063428350243298405643214911537
9920541766823999997153014819324113693565
619260263999108082522378638400930394494
8388464856259275416559482636403399583723
992061273338134058947872503116935321141
974950774177830323835583016128062441863
8860202167952817881022602615558891924108
5355458514503185630278816982415021172578
2187854483901544354539077257533931898204
523857627118172497314061905772057134269
523344463684308674819755995294277847232
5044261718575706273469792040442799007987
174510005807100546629154317888469042655
7381852581807898254950250246331262627
7800569666504555869545971256237520236645
977084310172118608076759610744491813084
4947273619414152105952495225280774906781
877395296664950068848694713119893211771
932041962365239237346323274380264753710
3576625238084616363814151133926373789029
558147932752860513341269711427467925071
321766055436510983060582808804254665499
6484569421422968133529848731843753755302
125537835646978642262017643326994334979
309171223548562324864976296647974094847
8478586128192117470475745013562186929614
2487628355645718549915885695961639347260
9771433333058764863236724055528697308493
8694465988954216894262184841236033673971
6231764149228701759129484083375194694913
6315516117870556448291320423230572553
862582459082645382633764037368536901410
3638107686693009551589934910289579637524
2494777266489389606850589817177556496507
7291553234284486459737373773311057718960
21632344688923488283518549998418846
5000356631845248221265744186286854691413
8306973242022944568520887150409736412687
7684930216326300670161999358683862035289
728016824541809729263770825886084109533
6513984769828064563158994710805849329794
34174139748773606471699636455599206376
401794943624742466471595407196644974153
888426713783831753272341630073301707629
79631088105155488192481783719939641768
426061689539291085599623612868660922567
33553391271308050196350633788163821694
8158690420374095821756121494458253123
7338965939857693445189115848429606849
88035357435912311216525125694468576656
11162625822186033363968234736237483870
860274870305684361217885339253464575629
83536205596510000314015988209524164641159
6701150848016346552748582171477066106
674137565808541235307834620420060448209
778050853890231034321002947196999301237
9109322864735566692347267063237471064
5055985629851802309817619220335267425035
3695252679933822754991043439415537103
253399817463701086021696908075225598537
3184371119381068771667088078637070045723
613273144924111755213893921646356003791
50948475641841859314375816605726015250
38038538110150709294898432524832557768
7122593766334741917331662289517711002864
3310992813001215707360093630397594246
301221294156320232883813915612490648185
652652998600296212012419939840338571476
763124356357645046335780274536537034373
6173738849713726578457155670328952216694
12138971257649397187446596308325768644
75552359179255148617808632603998054705
584526950382656855989046815751958085625
691915803186111471542955915683215206416
50239413945871026486781983247151224241
479218166894455842821933134926211574505
697563592654311646568751270751252492763
191396556737569538581339482368438213093
819931698842909599194801940756578366770
95896315897488852383320142813617937321
2523761251064118360133108270471227445065
78326528803309622838678233968153084033
1116890248143694963285563297809326366113
622754742374266188448314237750112089137
648924511829786194859108860936723462188
831955524031020901741721307517934682944
5633926785315594169233871941169519885609
4307481417259139334150646645665269584391
14408118996850822470869377231399726702
8884780130053226712660066621737822799654
296297175503473244609010502554920054531
331199491268760499549184260997715837490
9216759198265232538212717373324246215448
8389050662441331871119896807953741503
2737568587227647133288344071176185363008
2086739236496163371969947986568481368078
80087903151312866668135835226979129017
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]