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
9494469576514356112465232869866066485539
708130984675482638812347237157650316
833024753292580113511868443715963992635
781387483789995701387015294459867937
84921337712278562907429991746344345765
661522427674970014043074264188449506102
82315725353076282571422478251187600543
667780824171193831102316514167591536220
1881220550488305568494098543612662573700
6055623996642878850897229866492754713535
439428917398984252618529307984961437422
5468232267989243625410313172724035538353
81235177493895546261222295250386131138
52911763762918135980999779893946391071
117363429000857510518307465182935243860
2672797466761538233278289817210027880
2122953067086837785383811902487753265592
394698455901381015329771242222853165316
212627486488503916387418005395638771815
663772616682259223758652064802359645221
6314563286682108766059583558954761834156
502316255682531767481323227344424131405
717841397106336072659715023334959914018
5353600877115733372816704512442166439266
9051654882665782223455021803560446837527
811768124953201389543852250176897199310
226823304912841735518938262292042092232
120346196554128720172829269597414937215
6387582826421207485337062138832591522920
70113089292544042797257953534799446881
823923399653421886527863009847273484050
261557545692962543132917864001980458
2586288029261245619573455174674757741835
292140668016532753245175511847292181902
59452343751631672014661989740932478171
5730986455139276547843546426396181538165
9658400674638907186610183484607381856753
99754298561122676818617347212705159297
289033571754240194356141908266345225985
8187203620292135632752568339240439266887
4065849969997787925391310923276471548
329992806090311273992754082214438642779
701454561931300085078575849400463477083
7920270483668511272353803992365593863790
2638714944921056947835087263306892238597
176729867514283149097018577805656252882
956351184446235140255527706945462001637
50905593436391795192771186359784355509
36022589523941409407756949126021151311
85003395234873201456840110788459116643
393879158870805077307890166634614965731
30629277973661424287492454149518038027
4794871453599656787927062577266228877571
707773209393633151968599384344195184847
3238886220275515612283609281706668455338
12641381445939375078348550560867585642
757312927858533580916863201989813116079
1458753779517805533168376817979220251136
443124429192376271323503228455523750
131777422452371486102017418731216566798
310850119318129053425908646600026482447
2232564239098034509177973322824233185616
484179781248015817284863918530430588496
8555642146909193895811733229715575017925
4846199382248383865190199515883524567441
6864130789674722252032277592470236215515
2543921721115874959350061355790354753
1057706440004481647432711302411298898992
6365878021505134571394412956049466699
33739648943620798273706580647319821819
993350154775609164212070391373037538411
206273294115839216582327916667781307211
56071171921601958939173755510313004778
5248322780148512283207728113959134045
3533941578899964543445884235795279824258
7410235839738651663684672284817324164204
775276543763790251424794511514464587199
8770348528196243827292155896620791986235
541843547355968237679265477644434801503
7868639944667745438724431304610719429638
490326281714344249783251823725636197037
917054688319296957673715120981067938780
888358473292231532530477970505154482364
18339542147422283220181675219822808357
9006256393893079299423874318750569437177
1989685148756871789231822467376952534072
972253121239613274283838612514690994256
993963231562127952782067660509234507766
3067532230106268592222857459326121335568
7012384413955447514939311242210259722801
96119576467556988753841124966305486520
2999717984679458835591938597361450075912
509639571842936681399206940801450496327
457158878419763750651584356370642821607
810958507649567363853308939648579735507
2412385888133683249069686054469125125361
1130101841652014981928483904124327501071
1921790318731756398072141300363417758387
4998671258824360262466833045412187668664
4248683384539772890795531992136048662
748824841045379643215684839823511478667
474426756362173199235145524778861192884
6135339361551710257453156999123684632
6860826973219918882708573114813966174
2993879896022747175911245837980281526939
1325855268264347516280393536798059139
389327453234684048259522607982665855985
581815135343417559357645878923791187768
880327139338165136248163016525452385549
814776786589379674401433110151349232385
1308665918704348636815979472323475487709
142730134235553188363132617279783996283
160061739703218678386099295798364546497
996380679024465745451259506948421307629
3215100008335394578268249791657039468744
1900311351177492178698629165992683172237
9371451619772350653240734504416016081376
6533927535189853381297804946321670305048
1870944974673005781961883506882495543749
7603116966228846995856497061134070828172
36145526380734388379944591130965651143
5209754739353613193951263404316340145684
630581686951724790786012733959202308864
2548701378682696928973485622188128911189
80082195214921567179282750431897732565
9357695387152749741312131408405813383853
704657017215304676949538146140555555632
4303320932259234892330469785172029993614
3210376716418273517379149236964958915018
442627318146883619648934940170348134668
14315947618840233192330926838249804201
57903314462333061645343596175368612490
457753097673782555259546504965521749386
78896924606265467265611688335293592561
24442158601248089571128092551493102230
8699301472445095786672678010635347206621
637744394323661025542177983332190697854
287861824462435544488379736904799386017
41968411435524033556319966157398904052
17598001113635142258936120151315053813
1446275455157485754137736572650437697
66038545405229986733801161577091997103
538596849567937039852876908089045489845
931742072192055689374154898283324912999
4163826895264695497997132459273383526460
3263907937622938148677739011159722037791
653241417102698438078994237512052611495
40894888365645815323886861178192426557
74481997717141855988606766767635796862
1892708713665725732019398209160184829
819383492243446278236961772592045716594
128254053856311531604207786149748264375
944475214709366887160473693238796843210
8872713438019861136369814367617269357175
7948776634609710848549253614013973226
1953747323505742747864171971123247796190
921911916029427442219496398647031644251
459538881506737716087598873800856893
55762823162220566568208603370259872865
270271720663150288375513093298611428552
97743742482167716121382252936547585085
197032172259635543183495020161947767727
95893426917338578260604378406114227881
438226898381214650437400988726521753714
1222403682556233382437229570453612189694
7677417072095532483189006150851367913943
6396992911007901472189174152931079450
570883061867289318041377474590872798759
4223725948583145636072761680513944832601
8910448943826747966567872461920476857156
2451766686314306585039058049840012872928
22571534730965954221436501621806827986
4379219467737974176523102635883876369745
320454314765245492415488528912292293148
6844243085766803213747499114163633287708
1062723117926284709490871168766089757025
79750951454301576389912155760755291931
846840546942961977922185167207014997200
935376515319051688365714804257096367150
596587189392799278690486832326441228373
3985271228018597480071979076302379965574
9137893983459264926131123661793883745944
724548364197339858954754964577057848513
690480139889475355399966469785032709723
687588954182750247696520916898588139173
2970267857598230058241559885819401579
891427922564903578581747740365543261209
47721426936153056774589560523646759615
7774118786553532513231923862721455453665
652873693751622893945781978527349917906
49067241377677969615142237118757744965
2399710528546128154212634120556296059555
956633397269567517559686067851763303322
218231805848815224779021458184823782661
326612454379847712087566007805478545367
2812211719971813427619151987276980
183626188803994880447326976781634405260
462735312117867710583776012104964499681
7880816770981849864240023870042406698
297796724573153693787491672557139565120
1741450961766231478497259775445455881982
581646357036487776170996363608689968975
1335557765593837228675439451659541904612
52235791552132986332770734412408594177
110537471419282591433606115567263996449
74559662309486111009411760252285116171
7591554426131662798492662959627137539043
964254334317839571523915965050128597
4878473116071235862683067210572580653600
2704646141423641995907639268689199747
373131374998713953818487306742752301982
4526856830392919197905276165815084570
143424258508940444831073590562349008506
84802833480949305129238156490274377551
5255336914725525855392181663188527066341
4776776375764531167387949909169122751917
9141249692196283843912674264870926194239
549542285809568989467222912357025063183
5211148485803633297167563078912997564325
1596207479678113224191803072669171886319
27472917815987310101167488291369914663
583424485937585583451871943199522178383
221219016443384472219228340893928302628
89201436856557047798256870324627624169
5255811139296616359593425513098607801
5325458632095279800451052140219415743490
17594013823321905664155360817138195947
636055831846030391226112503563699118249
408583102026861119052853628792731844489
982720933322132910227882374860047519343
76164848675402572246766323434969412043
5647888943256279138064924372778429064809
159690833673292520152217617624620655041
17968334063006281018172689106360283786
43664786579389486412729648124936352999
245231326271726235295307696463847584996
304677452477316285068398157198495304054
324463715166547690575779115972961065167
5088253125914210185174662388031604144
56786263940856243028218399444451006875
63702271878056489557436867127688449329
30499282307316698079473156687289436
85532242383061965853715661415359971784
8726576699598127463271091454372278848218
177710652871816585492801925450115048864
239833369626778693795494536483543064
19354708195545995192594540238815918962
410166721967373104072753860619540651302
456551866481895983550962533742447582685
3916755871295373676854227558992207943
1100516357793465159594281223383722466397
5348778325561270998665959558903423316046
269450788925379819197672785856474731050
453890591313258755469088389327882728323
7398559830721144608425210415421868342
40135272928676454678310746933541384568
361744767907826408305123415766387548
4482638237359950651819274565420486088919
3742352592854703985894283056293039924774
553924811959834118037407732205660979887
936374252317244594968456985146251381750
62183068900836776034697318630226258661
91022651735258293687821719619656122189
70433698972477001745578222686542896139
1546365844592392339198319531273756133300
5521872515547427439440709413478570156241
1997550023533680862168441479215432347814
8375962453864715788088321162831564295562
581269668854710834519285312864789029778
7643653472443057929150374470493191968538
3827683328454896291466319830120463852351
2340888655593437455079145222949556848097
4838148858933001462731972940733507352
6695424560243325411334824254924639644446
47924724017987672850089212929555556015
87998872910877116910348192839984172491
255696843874878479467285083676877951425
1204203479488830874118365003632024751630
118168754174942478663435148598572061278
476392510818436696171281702157134039322
3321155412011770382768859147672799605038
7507807978904267352017263448479139586565
493424781811457953036583689336180067820
664890613711697881823966801218198978280
881967151958598233256541482259479357346
8355568528346754266197901752559839305777
5455952159034263121714514037897512886691
8133553529168642422711338826153476887777
8603939233725050746157844119928047663953
3489959761267313897294965196909713262
562019162261707976163702870515523696167
28999882991733324378802429189168244389
7143180218819024409045743152141168827151
22078901404643814366487132541975365291
850368272866936147385514940663616461345
129818636054670769523178400123824576059
259074874204492693106388915884348528786
584186613965519783919493187725229423251
5661767941663850658243101933392192266354
330852798837283441071394790566917760309
8403667087278622149913216517361073815960
9971565278404823714277219517846817105980
782594048616859248763328443930594425872
73278768920602412811491212295930475913
252612072539305456599943372973968655583
6920731015718809471912144417591006504
883659737544652310957361798104419162241
7778893283563385139229470834145063418
5515318766593551402786571582435164631298
1636817378512218361894122260602646369
5944475939138124402721369103267496242018
66729490606269188624672591585305983712
260273309817861146068626574162753927872
7488337688279980254911204802763841524658
13129036602551164399688852429923842136
8653564417373034365199714574677226757
948239921327214865318466828957846744880
7229856848443141113223163606965945701041
2483969249867715825511073535286695914
3488552134506482615260169694263096605759
3501418598567600288730011550734445413259
73339386461533621136707161304273164305
69514947089566450564071439629614481221
25345194445579458106485422788399606268
8207856087833997536572315544535979425
1995237426629742220836685199617225653474
4492772128081569737893068714831679077271
19603429325714778234980537792921746218
818977921373913874518698728504683083672
726779432483360880593981227828810352136
147252412936708024844240163998165343
950826751411324317016105239122143412593
353866430654514820910216632471864877
701835911580477947850666596676656059602
6967674646482968599327262733520167207520
884295422691536264389418310710842322518
71427797809661888594917768262191601996
9133615828364592105790989439287623022374
6374643653907299994288727749301645
5065949195241649284653491554873962566116
316999634675313314947196704673834952750
428391118573854386289152716856587482040
2908902199313987841461127236501847361957
6584515570818957602117479446557915657353
864411421630826635482128537679652650664
310320988930587969749232578731866879698
622366649686972559459071789530236909723
625155381126800167865140329710868031387
28781988696181153785023457234887527927
468852284083419823342940955512647847
850453061181675938628736601622241527710
6043468277817757541191593573797290977804
179192791713547747593964901214277555587
351265764104481976112399468517012655978
93341119677920616544809423465870365876
4724026133335841651952886858296475502
27846134869279569186391569766188378093
736977419133629413779798182038892223
604791993886144797630983383993410057788
1545293969164978401414579887561785911880
1056888687946187149876143874910958533501
263332731561946797884600237141956026953
8589610113024525537776837790295118066945
3778874798521164855638880451979665829
39251585201237335446991997781185052211
299011793639138173640406889626811348376
9396692474983224614171053585353997479520
7184231189419259193529130569166924766
31261098463637637192561845169863297217
545376419825611756973943529847882517738
7363698412558552233149791138438348073
6518798574521410110945687266559528388316
8649489735283107971712542448850257096066
820357976069528995508595866171695543699
1353392970812306108624099911610894564380
213385752969262928767921636750174599578
6557717677183399365128807999094864339
403572776594273964515492385292497257856
759742934431963063832341974824899450
319739748006492422603097150078039536342
6594696589272041928035311375326635248304
4265129083935839437945948173483094905675
5312648937199326522035536739297164479636
910260933955295510967343707522314862396
73114216797529481305875863954153243945
30984464223253283017998830701725493772
35972108749718351251825810518481619329
3713976370697958497391659834497036049
7703998676061046618125161513327232327
858574554655344660588761565814346096100
71288082890568991608498319542713503379
9404974976151214272012784883293236176311
76054575150661165689117413127518518290
25551261291754244944348605152422883522
7946776425827158894865498630359642089122
967511654897909095067283761142396682073
716113174384780659335703437599403577012
834560961661466399874876675163478998766
15207187621382366933182259502246877675
77016454426723931703544827636422903703
903888083032630893093555350320317638126
6546734438277340379005547879178235895
8885477150832129605725623316953211958464
623844013857917782588706352262763159026
82526812878506733715897165458118729520
272915273563262824254702930385279628500
8936967921076016326517548480995942374111
12928700538272177641726816232963852
99103642163710233971002826654033092985
58461572739946899293843275665394969622
3460819659165309612715794979527742994
8118396262075371403224236738952050846915
5668521865816315220774786451367334255111
72957755175476202571572530479530954710
45912136259336342421837359844687682688
3284428789264693179847626236322882134485
936130355816390960115373186541463184557
2908378062468375126354568449324227815635
23943635980993420415455610224781418113
79442217132282100238764977259399127499
3124441439242105735962411081595299049968
237173371931970562915935823238066969249
9075963043471854342127168572628932704257
3696371214355132368572261476531971266685
5442835558370962130306273977309927663
6772125438529869425687509797883133635808
6249947031141700379411228449408220605322
265627701255334493452266402948786031789
7689578590420072756510186417360322433
8079461534787109585646251544436923145850
286116077436860359299430990374829831262
739444980011851944277354812643376867267
3516653138069191427657102875747142413
1991325822827967845721401065545421223211
1800109668716596181039031492834766609463
606267514131999263139294851519935287992
744791298674543539512055024735689419343
7644231722982531725455849987820171749175
521487129570438916292061391882476175282
4697347788071670651551432438823248373991
89534765905236917083474866752066079965
715143213869303592732844766993956228123
633022051168235782591807820334538906522
278843526058360171942041601628449937187
9985593077658284827724938618263099484
444059109988685808395194616988536386052
4917294854534503633663269520162469134124
85566813625997388698541914424284628431
2963846977461988764970664005681476415781
4583690712508907249485537709923763241658
292386765798762394466776561657741776499
296481375453565877184208527086749799470
125268851712428513019461561436641277041
360989164126785135554876183507896624722
5011448177423340739033858941474594091585
4072238014521112381035333819995845692717
5146258832735730356628413507312115055613
80312019845600497655824566852729952773
9021917556071063978277964917129728205
700529124661812540565493948281799234676
8425468779209879678300564392975262349
16626238968157272435426634328655598765
9461733170751280991887142241904842101103
7351954992618659922644251518608484
1799463697545089837465929530219632516389
45038291636031584766041569355740259102
41909915125230739811156421287582518943
5471715777762316104436002644572984182645
72062954744757195542989273438047621366
16124242812296787144848214844148798178
967501398567994848776476592990630825557
6648649387918353719824889000749764846458
3234947194169072303746088180168143137554
939797169736651610081159976479528793495
485343368182305065631936055644464171213
4782218368137881935611282899765047413283
859169311501744450226552160859894127355
5301867716297224434349312111872850076831
4096200816388189631094925142956323111530
2981429429245697544963691659922466368
8946196317067698764468328365035937812
7475334224037864183277736527146571825
202754244863538081715316502538119431
74366755549810931114701799089666699251
2765429753254134831328248948524569198305
6349524896872228754120094021774525477588
4308259869163088746445967942979842562
1359577633967934907561743838325172125605
6595650891572139504499187066893461954194
737569027640380978309238816529969199521
9783341730363508476076766238305244534381
9444449896334540285724836445413342531352
7946140270447065304136852925691132520
998235587307902398094999406708361927457
71378078674818688273617155758075141898
3213914466393432159181146993881652023679
6939552318712734442012428255263965046534
611987299843473483903154640824382464944
317910376826382229663445056972261560
1254928646923926501740992486077423404
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]