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
6451718018509881655446313497378253504526
7753222174933656915559879476272863026486
749223937048259135832993853899832416069
204275059154136810674256813164122814878
618093738739332783368412592147284452532
60293776366385157230877557289181333928
3522240889259304672614998051876581954026
6838674976228139233181772346482483667
3582416284182757415142989900690131433480
941361391862051683561698781526047062249
2806459046037883243553042996501497372
5192636033503813813026502855959146844737
840135897699300335958742161708430143183
845378063527897371883121355730737262333
2698467992693976619777871677828046056710
225791566919071459931817753821231945096
1663842321298356614356596697199077874
303479402892751381814247405777280988513
5658829293479466793066522607776547093486
34458158373364848508052561890031884529
58283888219856631256289832300046801790
874338756903990453197184120615884728280
510219291120837470722424878656346343808
695749622084192257893137694738179569418
17816617453153649867762445253306022783
829620781265587077732529449865756013883
524729884201704426774108348128577707056
1394355539445501778128048449767867818321
6028289951901093089474731986612393793
45717439807149318475812887981925166197
84130103179185312168662454692053009126
4864909312988714285653601591648265565
5074225917519281174429817842316443773
48255599739680638717672304602664092280
988432361551555810238789610709332664116
3243771427345451137605919344564482735
336497635472278288408511839836380441903
12519544863320901041973423313640873844
855753738773890826198837905838287961095
43851656601451207405249659501936936033
97249893552371767992719719206021334994
1969377810868078860626317463241768123436
638354669475061127795121210323808726
211499507278734126293391563247080305670
7328741043458563126544881214844042998895
999787363342276800342568629305390395116
551968877696843876965819198478878497192
2601622659041593212939833148685879054
598172633997896729327037533971440654894
665239788567179053423944239152757741098
85911049328337269388510179017676232390
647547166754110530298421812115437929940
664839386658848782575291902831565222107
1737100955895879107618068951300154683149
102523856312639373363593892240188816917
8425842550171586745241333847836412418139
3052726595809782224344679678158637486025
706632328651197784733847709572688189687
551032632266543252512937304620261851129
1276965590976042971846559924136332219124
4679254759252163404067983235557834012173
489513743552622977469561540436414673821
882646322733970405718483645172439796940
438599398840682891579397679964392855978
57064523010875317609014187489311564627
4315195970149455116932399970854954017
466142784204592548384142156630196057981
8942967880637156385223406684227619746033
2451748026334916232131276376816869661548
343014183905948342180016385833715778826
824880998000541870018909712870406524861
658166737995530138269907955471826860
797140855464402037567002158790612515190
736265414048183589119009434593177216739
45594366843383153774626781776539681637
4845301857996576369637074137425688738565
30212635807616276565958457957396935283
456370296494844576805599056800057618131
59034104306816837294782141123430669642
943745621573611232155473929193624336234
135898679271706811311146474150794512504
76164818871359947865146481914882938829
4427667844933886714180676278820329385
117664887920874281784761417489437071933
587584647303630774971839255852585267941
217320151867346244122707370650116845652
5383117153174834264466502116164546123728
235916571045853837931739205316063953504
32968169402386714831189662127743579684
7745603922713230286874428428638018241
889811199627759911542802300653952047866
4328543472837949347692471087629184745454
715213309057375254754389684886519290799
6666624251123902222822897930314897841214
286876096931765963085305986820241674173
5009341427879517075775989952529028120
8334397394555201119297141066469993005550
4895373776752509432974325715396192744745
7235293080442884397725894638936941817571
4973404241944953562359951628930895170
539977111575074576620883258157967542639
187868929284910239003150739621736406993
43088469187339219344428670596784536573
117240341136628476891532974818548508074
17129215643345779161797039918130979490
10712923803237140937089722948170693100
46749368154558383468509557299179137424
1263551363969496298248779257581779579288
2337926164048618207086192497693013037450
456168221637589306363199599813543351250
1380184094194816107824221696481635558092
32154473860563029217762693146845836240
55485923458330238464223936044231150189
8215428842936789628028809719148984056197
9365712150957384097024774362699229396
55906101767255669442858640659226951299
6734248075695207101049133629494824442594
6549237885265449876192214767809582026893
70771877122341416876263798677569184635
2401862664091587914155972724653268062339
9812438429645358224049498199661246773788
123056907428983259616250844737861426792
1609244954112516526167421873708792071408
6795718260169751400844514833269940902633
2898693733097790550757658351694261577682
9885224456655162545272346643890888454
31633572789658340097965421805925423399
435239578551934189264334368165841985753
6621768745851715619138138496126542983538
216556743303190445680868166263984902576
2689947697456316655888709854154695877775
8291475886394995478957765319968719989018
6464939767679712113778932330302687958625
689653594448652595487949780710584316837
74701230535774036838440138954147091654
341712766660486313467512833497773828353
94126426124753466493667658151552684159
70497945874917672120880213455438525829
14567002380775983812170877411594816
590864726860777229092803785770845015589
540401342251921476552979192275124293635
321882719874195295799636436350877844855
2893091417830794484696834958386882668
6583436950041628362374016024234187684614
15599654779352420675768063793062592699
89039535308029889325846256154213079980
8632971666774292782239803145318729782942
3405361131605222889461453106845861625
315852506964539864582876443371262513232
1084164157885972465729086002506915542793
1843400849081877134317072695921775929593
309545025035178219313651722543177209206
8537123213772098212296941084849714945793
999577333098417451744839600291629185709
773435011162157978930696817744062799
7722188911961424909064284128677358845933
246397365331723848361954922158445108426
29323727844227391859425318650499339107
9340612969457879479816803548774872209439
196830282573222220383948458234961802332
8162370353171533657043612899547664005120
7806900668235130607214701490466160176949
247647635082170739174133812547042622430
866894634352240728274491528458423358537
9368372233342915985593523936545162887897
5001420149822581618630901546285162249173
3769909515579172907738084704425827527398
380999166717531945343236574363155753393
54705757877919375795117658633248509426
546576283845204715968135507767284412729
79551767278876994125513644411070911663
4651310914223678610179321227291169952052
1397265784521783952991304999150740267914
53972206279791408174449898326166134398
5465333998919928228271696862498485693
689068875670752390589506530732916241350
8479615778302982409575201406526959429506
8350405230633723960886671024959697828555
5905345133453919770073435109499073126813
444357903632790252594844750791655571072
596121012363467627594730468815635994192
6223583420346032842481391350213236425220
4458869662238603613645098672817414041299
4616982344671042255749644600628218612892
99879456978399959018688266133513437654
3084966175908846225325205059519083313052
8311408973535781837347443480456396989804
7614828432203844385474234422261814806561
6482347843235993741390298940444284012355
965048638718316215365709297554098455090
4574982612771899189154384700110732156871
180418899988725015831272426566789337841
8586931858991598868704195988092366065
343169615471950291684157575930913593770
309620871594887285290628126509874976318
317025512801660862662742629434448497717
634052295500500727673683202348737154890
311236489238673547335355261662516582474
2881481689769507905261757901656538977753
91879973141687588086269446125151906973
29161223335449606717188760558259279223
5453819815961779961221462329426056179515
1502806548153803262939942729814968217960
5235973353805522793025069048383850033517
865849403529791281334220502791239731566
9480279555378725303558225593116889328650
3806913425058596289720252734435661077461
385936269903267516738443698626221717
5486774450315096125559503921126560372288
710576558538620974407944493382644719675
108529933082199948326786925126671322183
7233274853586744746797675584967995277275
742074388213903376447434514468259113917
129521361380998742417172997973358955605
70224371292390232350275054262468568074
95366567826517932868002605328135274732
14901563329926853559305698379334667986
722221816477805905466928155263237342
7904111654409462545857143474160862651986
407366007219921733025119758615183505840
381388385584639113138531193564336995823
764328888922455932273646199252157378687
7770478974813995093332840391870561531
3895596888708068342832908903504937224039
4929310599301105642981094081608640456578
2450113611112028341390308631474410276
288684393123877882344429928888896201613
19386390853922358445798933200774349784
9940533376332876534471584434705481475526
99216627418631447326750023351223535519
441556438584155987868985390610414182405
7290175444793975332878964422194371911283
77399105817834844455812426301797768848
15167233316516483580963439041263776801
4182396584229535114252843908961346992055
7673433357361644546526024099955579182
3082458447428029986172204100244619479153
198925498880132857624519689469577018010
8573709551295472865474656492643227318152
5238567942603360294696471381779269266824
520017037190984577748646185542366039046
5044633563176870183575431204872625077865
426338485827092834159283130319348565298
528731027035996350170533285359533692816
4208295812588229154133712957263427793973
551591754891130860990426453452468008042
9571490899993438362154889941970840691771
52471384148857279421935517138854426736
2377983975558706406865236681615743482987
210115926120384212560201961791170011789
4907984949986098488613391258575831603189
339721922454975744961442297427887062927
832594898803720278908572726446180573939
343090567839154367598978477132647304281
94907386149795333344601188461768555672
876396045304995563131254295599358045364
311636365907590308326264772898131025525
581459059195618063258393487610445746780
3514786199841685180153315212945299665470
21643799873337220429202894883277036763
36043159667885445117372541055613877298
795489766338235725314552280579315064207
943766776465068117712698452994455003
7739750293991182981227416491915283413630
7829612991772379169667322423728781706316
669079933016864124575179038548095941819
25737371950834257380244025157256163275
1802717882379861406790155603768886118494
7943802454139006753309569991653945514
6302654362319144849326255247894717733927
825051485036466255251482379224041767309
621434674754067275111611569884140838916
94007539919954143736532872372522452411
4289832534616589475058393044561725082249
2203937239726036373382599347961375
516572547127402277216211096837261114993
4069611136023873956342716747437193364890
4290264959159112112891254530492021951702
97961058875346278456651849810823245038
813492767620664488258299710985392788273
96631589965384941063879865747828545133
5995823154406156517457632384808427676170
2213998441183740530764346719212361931050
7655184924544590826232008294692957671122
229036489487208235378195048919344181730
86001227329376593355909241408285217455
252519422225248173935161518807797689222
262592981018268324246429658463574509182
31828724963167813349757183903028797536
5800154146912056721461012602444569337667
3302179322716744841557396858469161764770
1400920229968551136244585352844940841031
89189262203850436684307753845499024401
63933403482903788002436148430594641995
4154970155231267668910698343184643603
54303612297643798424660514584806945963
6277301228571165555560958789614080824235
450364529581401035397809788434645423401
1860415839359902534951958491955364422135
8189272542956972336738628215312656145074
683130584335252347495616467131720915776
9661113466171545554254711728504117965489
1665614347755772570054966066724459797159
7677233884444558535890009654755188833485
508777766105912935040495502863250175788
6923446247591506921098188684322150853346
9510508896273352382980457711747812653750
866037933520190940184322245317014331262
7153206839235863894672575959981527017442
2095281735758303192471102944282939546019
8309201428664837369234193035588772129161
7601837723173271641778001661571162223347
8054845187383834811281387810563884223518
8675608598754213898591237957910355732450
2497634611348131512955417929636751333089
309112119662224051137342653420583267
7837582297019770394180452367749225006533
533327294682662956495566042800982889385
4873167217279149945735256635660212022821
3333729126722419478512847833162612333212
9495517570810520236205651233442563864
9200308688795633245891293499614151508
7368785562203329138665113761767989486652
232567296739104863141332194641302317107
4243681976834557238675163952774793611159
7877871030129112643239357102345936289902
4861997554039483344175604197332482951
37632012211929087125677269730263659873
747838734216573769528255485520348735906
17388280112037258494871364611680189728
27258947456858051308304913473674641968
1453567237535530452725296921540753806453
8911519615643011251139984734616353431594
2172278683827386568920134586916059472330
2962937961892350337734771373678169877849
770846064942825425761619282131126124596
7887538060683128211550192658892434025566
44009762553912263547206883614426227957
1455890482606610244637357670122276011467
481872787385810699229619627580516293964
9280840638161552198421713357819812322059
4970922345551929927078452000319573263142
370433824066739636606971330477369004936
1373122153365359456188413279983934421629
28403082727761211755078532808697518725
864584851121778142157726598734647612186
138620088213448235711823671917747082063
743350555682825340798555816118813458793
891195521852750906047539271267491673655
3215364432877474988310641846692274641637
166110317733403115299816843974882555040
551156274915615141641955039638956243818
533446059635991895579046252258895202973
508669548821291574844828653734720863565
5212410519477786151417211041865744934813
731235179871304689767656676544896499775
8861661917773174131798295645773826844560
1745602054127236617497954330294390186036
33381952267351796734801077199457844405
940981317240180820742552377875751277488
492866796711873699638371216390145537314
62514403044000370790637702849107926
6704212262845362152724145374881629308467
4736561264199019902216614125247118802
6220624799971465705009152592924641950
7386848798179438534945275907366578698
881537556457112097734054012752969889256
2763662631675604849178903497746920202517
300646169115372590027793155363536169159
534410157833807340664471184302889219673
75199530295846902383463416962985302723
3043699919236774206309395733646352360
956746611483235247618706075794122527454
341160218394233957014389529838787161858
2967316133602001115265867447296379245065
757437256532563314597444517173064366796
376233186188382421876514631316475432474
4945786277076063924480974792961935381586
464391241350317343082356724688886761264
948397295639572078127522096966812669928
589157537644819465715496565143168554906
4321217575297233650326413741495980097361
292054168424269066499772144410175485212
6986688212635358555915305066446434171800
280415947614306029051105208782322205758
43163423645462622912397692106877334897
3301251718658421465495891661395045576123
595689905601361759862079269448238661898
753586097658554984119704236618312278564
4563436359281665978170566264382859028383
510196667890897328303884587950883001010
3101340624493588909257840276663343480
6740779633682345891739182651509613466186
617332074055346936174733713054424426921
499229093425789466453092450791155287
66028035268570933673890354329093219184
51635041344588233453928810717019701097
702620526805211992075106723236890957052
254341498513456932291183881146008692947
945948533511311595470458272745228875246
1500719779619328851710011021894058186802
8660807339637260512889453912144869059722
705413573904186167621313971821967799055
679157873646541621061666661358465894466
7065425551198570463558505999249799309707
98926543263657262123124621740814385990
49406282735262482335931420878735486682
77048023388655368283830048510256030192
8329147931273486745996509877157851585330
875126018870853186837545895833748568902
77035506486745534689481121907842363038
2687844053319026190427875749861315338083
113457763524152678883459835940473598833
3572555449036090804666278126502125714704
969752491032656204985834477571189107313
166750651920112791913445351165150565486
810273487180905535351964784351332047766
8325563378272133790769638875653380879596
1315598863226154297259554283578982883884
539530708661917186091251748694304819169
4840550435401000059339465836815294019756
748934404557601872386402852240866935231
48428170945522576836923467282771818375
8347115529471967171367212502608136217568
8153146466121256582477927021596190414322
843942712432831467369015890152429654394
366768964076334720918613669163259021046
7248506417919614910762245119497651371494
95109107172841387533230633773592372605
2175334487126878242527174406124884228
25747762923990137629747377705239619043
77649366700362661198865698118437652930
1577211586272370717391538951148082656702
21268481787743166642208823654238158495
93237058213343852018847619872011591818
30426145237480793069732721446955709368
646031647968884987995929476603824241609
400368483313455334025720470896337753
591980082290420396999319277787618975056
64932936370914959777117384334493523419
332376028268730096059861287344962078131
258511554028738538272098646217397253643
2348586419575445964122754049674274395306
106226875244403788525113781781439241410
60866148452699898122847537392595159818
956021556893210619128178590281940369283
5431941392643098911168768577991219042208
578163453699682261520948225153256463017
417271639948139311081176398272524528508
7549727196318737615215292720812122327912
6581333780603240442584241136784419074709
6751714333723605290330058928494144811854
483845057416409071222661671734969162108
2907935028097392826821656134396555957431
659358160842496427934461696779032583558
202146867189840499258379771101834934640
9800522447513600905597537769320542887602
1440215325932927919197631028717319252
8949269833327346830994921631618717823071
355877787389497136454066092973453917558
4314852138497495389513465054433053634086
4955501772850606428975380266044343779
3704246383046145282586051338427953999
281330248468641276165168151845998372751
2997434737655331491192075378937464192326
957499173158150023528008453889490559394
766772218244597818067609717978051680310
608247664748328947650831051890110912895
3043762367788894676635495389985735033
94795033628982267093949404240268421821
636818794812218814412658478800367091119
40299896945545279056951319239410766631
81202709791415955102397171331498373131
35152324147530724177699751186248942469
32636064211760694727336600911582986955
167933652163285411579125815322569101
340651277727774456849223665989129428193
148449808654904743999335378203326933750
8904856218849284221776609671283452594406
6342841467667868118221727227159595131941
534963155458467636924383762871951936674
4523447764606791397589726388300439497277
4931207719044831720473917789728265854745
394083133288292453783928376841285314916
155518078917486773609565786340537811725
720382375607905747125055544261828235151
565093209976358954145600985682842844244
46006761326140327277524210909360834779
46089979218533318734832256752096118008
388964508036895892926525177518958501211
437159582540526590225833737827265626927
2323658154039028238033472757923788275172
3606164355737645641364219711189536923474
6479465585320649885914568943894166033
9089823493368039586233905609915347143939
5257861665295215272014752341439646537015
4537153278466671833540583187676885045307
400635266970569912833661077214980335307
766722242270886842581347401712517587929
4762282693852010856369853416251465341089
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]