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
3853530885128971474038617509132250256310
94995498771662034287725772299893969240
32844210151760435751085113680850878804
59868203992499534445607639894547859785
1928170636212469522290547789281374853603
31320917752628523891076913679946017519
7903343084287879345398345383185168901498
339616571618704415929088560894591263608
8228906470108676163173777216698225406988
78734211405571811485266273226028769349
5947776399638122700044073250675795167227
1347691551882504115810935577222217500
394783696617803249487011454801796076881
9935996710446485735836104282023587404
69033385100158208655125119835640529852
7245371070832637734976532025914018737128
206622921738667719677395897382440413071
7719577279451826752811697629881963321364
1791655619126851489683056675341233613
336255772263084672959239025285893632523
217891754096796508266729098398515232
3649783762212544985716231964144728652819
9258531270716265312185398135641443802880
6536650324778291768020632982764054735792
2371758976335781962695022882190766095387
439575784215556276523029608225738363416
33603307328871499765750977849313569357
95218207685114320454368261825066045026
4358736699693451129759295445573925045083
635361555414585396451432600721019040427
255848087151445445606039641074493664406
94991541606842545743147299737376392996
7136636776888640278743316876630489031727
938584836609412640304839637814263697852
5266518873476995280610866845062938781
61987235090226314277428417172367478903
5517629356978523570139911775293377002843
91925935275976427544406399673089332585
34764734705475427194425435228522392190
5549899216432477810277076818430473292275
5958236092662615278232327643292253777895
306791164803986945446671929171822093592
80346527091487961498923675353448062777
9678596191154979196788389623939716473
593220795961228276948627581169991047611
233419444548361455573232447988332991515
1686707963219184839713461508265587219249
6077928160279742325337663637536854446603
9317884843748103893753388874273988049
804136448621505559220442205728269079665
93645491547980074664766885465383295048
15716350734399888141891837935695197137
5041383573407564753938358297526867514744
1245918631266467174625155230527067929976
856691522233165966083082413621075149942
19348995240622520485213242440761899950
556256521897993131483275874884049747
788230065917892691865981997617617261798
344029026934242070388727768211451302354
15248826932919802829334417434073258920
347791985779370350173645054678595424541
1449378146745351551374968018886499309619
7307477571496288698889416828996933893508
8732290656041212834948946793067999718
38274527309362037174336122055937099646
6703492534654641279585246481244230034253
6116350485590679227473407729731615232
68689171054419850118258312393087665032
330446481366729631778397185506328681528
5046134478767661454093037816251385204095
628682853894825760585728224553444594001
567254148459385188297396196559372769585
159539387293198631611287228338767524317
574349199815377102073865391430617868039
976670831753258625664606245875976919834
5895153356643440153018216283325652458821
94144359211344298179570542880855211519
9012713833977266964351979500255757014662
14858474485884064908938341697723308068
294993684911699466152869793237573022476
9610828563677090333270672546764027992849
6709962299371194222898433928104565637507
6419770553647065253584459417869351497
326186409854625828354121908165811428364
140160815635628699396841859371896379311
93361545885614915461619742415759729432
31197654490918562586333180266297125668
4766122137223981746639172176703970616073
21284272344633555372369318011637853591
1698551395433009345298907704730568627764
5206703727905020349755933883626495678152
8597143147282421198618868033306635263068
559790989117439440132813699926411175562
2395352234651830392792188895916970365508
4156287346133428661296305355634539417652
5994157290106803977735814023245950229926
1822408998415509169471013220906180558600
542835655716316486794532000820247173214
8688688469923221285313237258521759702979
925676763881535974178334559279693037028
1935777217848176855992618468822469308539
4947531711511671633584512389053819200
895046179546428294825912360657491249840
4937984194192966296194392633260729798163
516668972280513573812657237719562003
5395741717499212962489258615375374494743
487475665110607637372189767920859403110
187689901132152178343791140090835316995
177290820999733239113468366173182375
28482001899721792258599664444774332288
2911110024976962990888709178121284903540
184226452413709433211915876406640322504
87354728916704218361943182552594242826
9752550533196557702615407619347809793
630473222237395580012085292233097096312
379222144091914212203678329507826885147
57835679712335317023564756026915874509
6387568356037984289971636129415054171543
946441844033330373472822520506287497565
6515307491260235512291943632553657608
611275355771100786795298194890086516778
6715461270213543675221694481184211875009
707242892637745436681273106698693102
44371497714498468142018536826646539531
993938958693560149452716299931054948514
547884302642565462056491884576163242511
7926563131512601588601039489821404251
595945957274782916729880142780385073934
976223915629527359396375929829980588245
60318273439349989012374557459659481689
847421567774158254435208081100169838304
72942521164929972424889212818338601196
79406537155535316833127535677316624565
36133285658720922962653750145213326649
19666915814293422699509869813697894773
3619317193548314787841292940934928715965
99704681856963595343472230059734224376
925136899918364786158296694377697853593
3908169343835271166511311082366224507733
2922147089966061992015488589855086889929
8713852091618093251137022660350461065713
783884985559528248641047182935167327477
287714172272357725989994376174487667314
4456582381661273184790575262218198462462
281499016347181261515952303306123572794
605516445289483471291849980318313488981
4975990166335601165492553842266352894664
479895313739274355957993480073223179040
7745258924817933487751631144196432194936
384095422581109045952156031621951605264
16807164717312192386132719376510983814
633036924578814051253126013388841483075
3474116725124982567287656982656128151
2584245146884803358850111896906320887229
8391928881565983890684873137303452674537
6217921682789045990963997319563927655725
4539548170484214901313236049232746242230
548497232904374336333698424767258484620
240241582906597980006589651656528582396
523323184702754825376452429493048207661
28649757269242995978795161845814857695
72479495457422899586076922340297491379
91741422572992749764455438073075768726
3493421075644488178882025203857878423419
8986591963833482417826201507452258522310
829752103942538320899367752127403908
133916043232941332747668904408877186176
240702650147207951479863988569110599227
930778513860787548055186660557637603049
64471145234572788852410869928587841924
958121975030839949146879474349955113010
761152440163237449442632280228312483993
48131241416588955425409236535140002151
154871165349209740884171559662149324192
35497387157749989573811328541366117473
62695844263106657220014415541151538518
7061222991254459836758657159233365327501
910332889135580572565694588726220656207
9171121183949723750398056130768323751408
184178124967371035360323532614349888663
674461769361969149553629218353435696959
29422133199385953848371831553883948771
9376299868503110169438906339479272597104
469846982328669653487323461624373979027
72694361251772994198038939714914734641
986947146532388617558825324594315494486
249987649553239677498715228948270813986
74089496488993880125669763193517622331
440837044035280187164061160774763924662
1335537599058180992058794156220076512161
6794657584298004657876849124467538329
5442619698261119887421296362458989717574
30312979695742294939274444471839942359
140464717833936633886090276616196974802
133114462856854470973122716969127725837
480943015390197483882971292989151982067
643020597931257372798050738690468237921
3122762941762417146877455954589675954538
117722067841660634863498301566379003443
63263582692511482769620746345313525236
171929667128884786227041369325979077961
6204149427926414589641297037303870676685
182726122788675105089903465809981201880
1003276537351602908169015288665429847658
1683263065931525723982588169972584966009
76592978717319421886983121800219267058
7772881764433211560631052993346119562873
404144696785938211329537531635657582503
3953532206243212217125125843266152121
97377204613294311370583993431250041570
44375956764005227123853010349881284007
658340027440129652289030453870953032865
834244523022818169221618490923155722329
6856568144784454998966111711325766371
2311109366444848338787845185845521784865
147924632262845086784773072425736917388
185412259573957522605230198835004428737
7884179277227129271447062045110574373775
27497414321103368672106945127339528861
5391310343905639480340549329235131307081
313522506538831548451189308764315967901
6889516721784089844475212174623069734023
50844026569080928449766358820812798521
482374069006763584735651680544251912427
5067369210642495721423788951403341307656
5134146110646018437928778646827732424947
876988912150169415916824016428480522475
3253174480830799867019260988677238157
5836932934423113560775997124401872523834
7023784066817368518026574445808616151235
921274492353370397452476146169725517775
67556171178798053656361616285347106525
1395296530505820626160323714860452753296
552643587493742144593504374478599284127
5467173865806027255631646482773415586636
2505311191746964138866068244244311078347
5806142605875316441937959470698325786
10255248780683438239028734284664806111
157943678043347537591143345113748111754
236495764478715449387597935747253185942
778052757037451123261428177949269683698
61564166521153955974215161316459949046
4265490852519652394104246158593824238
2392923056834418405341852234109153997472
4247614359637108876990066393377414663458
361494454557011496089458902253444833559
661611491301753577942519614787628574656
5195599913706219900249663888874360012334
760164051121436645034831230716568559765
770971588154936965356363504719676588
2166987543206550423350867598765699893863
973506583837663851721425994168048206835
9795824183157399520790178201403259677333
269371905119992913695007410821237093767
2283136152430170510949559639315983314
2710936434147406846038528904425344358450
55747432625028299107662599657188127985
31002960419722468265116452318306654903
7095732692025780658336927812736687957
916697071334201471572959991865335181256
101050344175909535979680195389164513517
375752444847440790591492922662317213498
58318928450934315151570915311687043327
4929639173556246758990606923139732151438
5678811718806443304792884752192187766594
4105637226663393622161193816373729984861
546664886837943194494475342253333419514
91451707533780285024002532478004388097
741633226043688672574759524900339031971
20421414825867573664485883547233955619
7204298693383116526768625049675760309885
1884672762355738438792312650948185306
306874673460349139732152659632830609869
5070630441852635740925173937947621999938
7705152761588776459019031188804910812455
98851272133526638686931157480990577158
1577102977058354938384012887613888394742
666596949217292825923844635711145023930
1186472050752108351374909472431994184957
467271996996292322539355967874776858
14779071524217653174703662626824544392
3356870632615748620630394295290156752
58573079603699939894909997266672075092
676158084318667717711359425390115397044
26647338411294993623322617986521032326
85003762445732883966800297924231344337
9242301189279566605094504899364097604508
8806715219287018661526138051799510002104
669583343926405162137317330858099304445
4309669586929817643558138061929919957
11665424931823769763508031644855608763
66945706773209599014674328565691227811
35834280853431944303339375297607386949
578051912488533571026855443596111079499
2655708722732294804890332664250087856727
2323433942687830406543335871849655466182
6625660622427080154130917542936126094227
20445285565055463248499426349407079532
4294103037219609732161381610562052386933
212461129905341962551708706554471498436
673960399259432227948942988170027617319
2435543440155676732965562209497694595
1506734026987754667841885068817585611627
5713862011408109443283662633407065972673
9631736043144961504488921080243444202663
238374093782141130241024750106825981033
998465338527738031833038392868612927265
78174879724827808866934780606315285142
5035141656588066742262839357261965759
4375979713209971735871255447484444083052
881396858197208694668097259369251172686
6953133617736701286234568905979367352318
466244914428795216440288207402084414724
31568727073563546032434466686174224088
576672894372934070856934664693213503515
59057509778813394705624419154312552856
4342711709780435894460226029203194319
590824538279133709056324320348261193115
191593641987522586099085521573798282074
8579987995633428643989172549983270002203
253189055585909957659722287298469716
3362170612183208351272013224941978386437
6061840492709973428955527431377842278134
3550673082627704852671284405396014826651
45852929954434655394114879524012709120
5469885099246186183528565435310760147729
21208401436211998292968150595665545562
3945546628973665256895122627683010289034
869671116575175076176810643916248664
78813101158773022254132210840939484523
7696880615408688205325497754402325280
86198401505433772008156437150090234910
73053901971776266140691033191846041311
725548659052121173898865337250433626802
2263739394326586295069811316038346057
523821016628632237097405554143411212788
465557156815482075488531114132277084245
557063702315411540595221579116739007027
91972689175286955537008572182215595011
75008255798160009699746150161710501764
4327148389629391813283017975415840967133
891767552327341571034951185405715171315
4524133223724525356733310239467372541
157632991252562568227203055440956252970
5351112655393317463613205241407597428917
8548792976659019421321282706677852181715
4181509334792111200111075232879848192179
932819262022537263945672377912005304970
888861727409514225867999840280886762808
6240773627087190680845489705084404081
279674751431543837618876597423089364344
538957901018838590905846991867352851936
68017222433696126511312358878032237850
74268533871783176497141674782684505375
8968490562037432492972314764123660305519
8487651828166028148237079746914343258044
7414362581755850343692784322653698987517
208906130692430529022371334195688821829
2946831498348350515523784290905626065415
568215818891552141265710740417321409649
941685364197923685812011549652111007979
47805651168607894911862750747867918830
84511528841591311631272275946352443246
521687947114864780811009566233998858036
9015248684968052630470044823718727235374
4513299099209651746433388120914174925047
69848522185652375223652514938264871274
452763758587988656981966764239386624111
8289721531253595508821346223159885552141
982643629888862873864619087798875715481
343326431273909944756979185970845628537
274936879081265159496671381014779665
8895969829096980575963739008692789885887
628295228751371210952777980640957429006
7958508543984126519971115517777044622255
316576798057833655167127850682559825685
2745795440577575380470904832310022212465
2147771160275240946137075428676863525425
6954234875857066833697433509166183493908
261930077475265540127442230998726147119
45352867547513144321602317652114078541
513674104028999636156364370145968082262
394395969949772409535169745854763175958
3863982582855146799527897184933925577823
8573637621749929345919532859245234111107
86098406445720937032738908688945301520
197583663128252911897238791288492521289
966663669462149741969490199274958029233
557285853023130606059688930207989049758
788930729431751744545916110112938198360
2631293763356154528059772510884221078236
91934404364137625257446230317375317684
75296046156625442146425671079921281440
1842399026236362101381538981109339037716
41256816571395321141212826587388020702
433863402856263175413727671727986153590
910098659915990522133323468760234318303
2956453391545654173617387698853522926022
447848972389941824257473951347318664129
489672846711483789409301209280459077
81158797300188558194868420318162525048
26206987201921136072885923656911741332
4971832225029547848176235511436829275345
654110742318180247466871685329295946457
8323182733275613902621781374247869632075
3451508010326751534580519166457343568091
9581545041251730688631684140934147207308
213948172865093336070081096911222959337
809939995464134560042942334851267502919
3747922317691248329144181390861187418
18512287194277794541381541542727225124
68718388647675257069007279152479659622
5494561948195570678774785199443083737154
7074150972929650604695074537867416904881
38083138886907174836721581752277293869
54611170955347947032994703752722643830
467118696167481672012464515748269949729
7526626161485896417729389435297647683
16548092624178138215476699787148671513
3688434437662240781958052350266061977358
6864437635751744914456904235923783958855
20025169347749261612155565555915083565
2500425095851719445218198813878489195
447642347943728767343587857373368113423
2832117232528338517476991678550219208
2770250235497929823695442353545689211352
372237132138340700918151290105932031265
7200583470212200668955175952292451014006
547682266126386993659961307986815528438
1553260733983553870753272208391159577787
528341945328354513970294055359982107718
54299035401188181568284677820546465
53234299683871352873371837575422832046
8829782720406809505776895109990444856267
6398561188505369984818318371219270648474
1590458197977131601675292583491487479883
4610830696424003590510738351622566919128
4625886858893203402069078119846678640
545778466276238852773173408277757155728
10217750124279224979444868900448542910
4700342641163166476996477651123347211399
459873119015258117185012524486173369074
980836706995101852377053274296331895707
5885207780552923315279396804666321649981
87534676753652238838975131169914027685
238397223951877733083421450572777865187
496236725820966548359378134078733807817
40975011687267413872884225947562449102
81867411628533110344917486470246253120
9680843331759363207416325055715671867543
7057540960576297636351201992576072541327
4915917983381001624557444765624413377179
58312850846923962121051218333330949623
3412260937427026265793274668185084421301
39955732594023555881863511378859066237
1587300599455628215941543724559022075509
69226351189666732901952958093433553462
63817005560552211057776233845336322763
30175039443576693531707260695289522794
9308221285482359033168982358026050287
370680377386612814751509420953552158683
814513101713431638177287390147334351892
3281698856543676210181611337126875979747
912030138881754293274272330422162191787
934671196155796589234162669781289096559
237887474287217831888065826172187123259
2030506635259922731317598307681533254630
49139407782862585518480457957298463155
60936327803897669867277751593570323086
5614670883566287932151194122157499797927
988754227604935723755415472834359313939
6761114541634635777580797374716492972898
316758048028736140224548692901711259788
441882326258252356007201927494699482352
9489712656212624829282339617366654179128
3739589649983049893823554953320759404709
91704570793554382898119599916782126587
8011778161231798105535783677624475408238
94685661144425349755295398664358241619
4042839789607711193265568556923322259324
8188721948727616309468036644882684154171
6041641171619039304280448429489653582
858617999231114642063754991851442774594
3277223390064471786120271012326844004363
359845937394153432646846590307383014830
75056417896843283128288805289883575915
5237722275355302530199224807136637042637
68467156110751236686512168741941499970
745340129544830525041348629382768519767
838436761157463110671592437495853745551
63936261451547122418938523821881437536
877857148376561824873413908356267929210
3105553682386574589084201491376460043370
14219204587530554239263685364111243611
415612857822787141119203619304520871100
703088875109786312573818562727643913211
591968196397948581098998452762833767
901175923335235597050737337474979499332
3106929283122112781537721732962429238922
81564772665627910070276602113833604071
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]