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
413363261951980279306808707918429427704
87340369007523327546651744408119458314
4032919582741473124640539655164227031544
685214244288800373772932082247518614277
844029441601654380607851529603132732098
3471262661527328518888004296522886636
95087003396529256948446140081573012965
4954795125858196999384827081485186444899
16545155428796028967472986657166169114
5986758860283189178182134353751069225066
943790118118568446891712953670717217344
106796624205447520466199035428185806839
3281411483014607979243837501241372707442
5435975837362905784930263361447070307071
85328342656697733745663548441366437159
8644939690925898189832435350300498936472
729837351979549181450724301573869514618
255535129023221379379178738152569703775
2291563541558708540329209955541129956393
24673630941663891271270469238563085486
57691315708360946381277170443119506335
183017364517462382911736509594329558700
665397631385685996527714499739851316
749852014873148561798932312764198813097
7316424285751427564583203096766766489912
338893862234137376726319340480468444456
6939453806141825554602960952902998699
49210348610566053872157342489564235553
438690084794863271564325273866446507015
1659232171006742379086744094260767913347
2094112979045359065778805437226131180
576659736034964829021828819264781308409
91203131282315823052116843423917497699
4127339675431295882521908239274323973032
618762907568576890385762546873574556489
460248083269228704228020171687629927
2704977883852991707867633588310912328077
618319446159896811951652649206963456443
2612470252574564822179859402938259486239
6384304973919214181142750136882603530
5031793509458823773685274963424622835
1050294032596898303477979702131868434068
82938476192269972641743969131463138304
9709682430829183991287134442269972247383
77294538023862236492400911161063058420
272956598211176467924596307399448449886
63493796575777235174158428301525263906
54683885854066146305476867639335655282
338084234840182813741061279653890163545
6020998666979940852529924514758132696704
66479121893051953686261285672761196905
7461211020966601853588967477624627104845
30351632100097640218245330412930145102
324022311717935922371181361304558669749
5080088969325240482537624431161682848
574490607337753443525962611771412974631
387785996757428727162933883069271832365
622296886172921505814176080483246218452
5033449566602186553420905729631447932256
2463531169238853549588559216250418413485
6963338391355394035519353889025292978
125347338068167512857827113921828134733
1466996718254392214248999456760499237416
10736681464581817245048427691402854212
9683281091657774557496444716939432975
9450435349364171646952365157591954863364
612623531489938490869066888871521264209
30411022202129878763548548431163035112
4814527950938823883556291335254381896905
141365417326516625861901426176977293960
18145890454677704074737820785888424940
8024133115643868169595232187451719661026
2163378171526270684469811961192557735380
1989701640742127306856778466983026885833
1312305411941859704061124382967589036392
369789187709208200429658728267476656139
9898691883122177666836963372239455958620
9879752496363416468832089083329316923349
775157158504944908215631380399943773417
786411878102775304957686952920141947033
8247546874741697406623838817552545993873
10000594697603077285799376199690258859571
329677235605660786991406516188264419945
1091737021496765177310106147200246802909
534862054801516817582299347721390603318
6335355743379930254392598157179593728322
747586125573523887128525185488230077136
459597690608178873849684459374703385
787752505143153565671318836139234531882
78959191246760621984511815475586214687
68964366787755345744887806525693157126
362515367569686929538483388982244423276
5048734963435645603072712786972061239316
336559218951348497162885874949494865286
26142258211118662429606663846859213981
1416586591937823020246699726734928597
7466617291938500975653175124997781643815
50588538394691856492454284234315046547
5992860197461834569767172543557210693217
97586181164132093468476162647378319930
451423353359990516824718173945461075366
685629455046348744284946826613712092959
79255992626745071673421766772355844140
14014896972005892040822629233566756861
981866576129509713724845120932719384771
74338275932034964770210988004116652652
448389465472951631335370433651883258353
1982180495131957592037136360317531775905
152732690284019692510561328127118809800
183865141380989042535702591557156145861
94621151474669263292891201539349321904
678247857396892744411454312673514686610
9092323749929211355766949738808613761121
615273471781609425078813224402985965008
84447211556259245056498587711226342516
691650043331271519332933190354117767969
3086232233769876044725852481544651867
8003430217116160713484284489230584753
8838334975963106792149331899544837925216
7881737079886662535671044266312894049658
948703668927996788376812907962013639
220034387848941577767346268327439028464
26350935127114757752366979669962615320
6355876366133007793954497279591059576771
3119841484124357829889115376796050988
447994034303994135385432583459472390374
7541741516062024977922375606261615069138
31583695850629767048081483528095135726
733971688752887316611523990532025885402
319925085939447416024312345770518308598
33263821728447547644231684516790733388
940840775324836867794972565435416979161
5776497971095996830510736566170159546456
8730500917211928389178896536418216982511
5051486194197244976494929924278079321975
613072172921747434326094323844677945936
60827062894752550350935934412456723160
9769319427129829289096495444292050325985
4767482850516530514582919711775858191761
97565607948062422860678836268531133849
684945169925283017447836325922526754343
420843346021260719936253842858570108582
452614265134133434749218585687854681266
271395142979980575347618950283149894206
1438311319415758235124259912662778635
5412336655924501880070125084896125028489
273162043830468085346056104165417051733
33668237962211347063056313735439161643
122080224875664141833148791875225738382
49879399843085660645272832423132454992
487778901597703064117825525868686253756
1477448636737072749853636447722621259
2875853827817041338973228542446821294263
5922611580197656510967881552826355438460
13019742414278047094417443533054882385
801453305736555613674533687472113884504
838026126489716818071301206611585313353
56664502168913652973153189327117461330
8595818688048880835196144979287166143394
679697641931975110283327524470736982084
56758894821048288767825475939438595028
8871581073984059256375219961703158433669
38733056126000220841063472227134403772
3578104647125176699875852978072117414
2575648337395850666438574354852526933465
863319521082797121829671521388557353325
884746613468254874677105305250334619613
7252628184855054903822429812572264175403
1161783011072605584974683507346423423660
824160385466564416932458341164073615611
252525233837563858568219766144063478250
9018313959082723197640233739561811192050
2044250695614450192485295960899874039893
7424203033331746585280675433606571505287
3015604165131525280416459184621052981061
839285632721762540318893619706263878818
2484607039443785870317097863556991043401
4038751029613752250338995996296999958033
6499190232054752282131380377583772427
9058275425036180894216088911135523161667
289665429148550412484035482628885565946
40775564468211837836706259887798946790
3664718620382152481369806332853987472045
783370394086580184191613099639043453847
243421749518708899279147954321053171940
488810441353119120039925350635128076767
7855312624651943130019546236551876057437
16389619863492846403699587116257241814
1476216765513014228283472148643246748239
586791721307605632909930969577145010267
40432612142231525229013494555619133651
9144147650415080547494293581416384607029
82338666360152278080428195630995427835
1553502061767600640351312442288344103811
2209703627697083730794699097241962437232
4246814957405212327520337665104125491791
2284204566862226104737647562941540044916
6696715162045523276611301926116079198592
8694125483926167778431029145410335682
1562700436582543861616805630343132066655
245372628572599769279885006167111979313
6738932573581793677247218450484915624056
5846360265252810718276057586256832732624
417994777205942669267815793134866468912
5640780236005105140611083793350950204590
44023465325588891281382717480859246393
60756724575417514614763637930174637386
9781796154832752950133785542348207748
95393422874159892992941610673923132903
8477598259643642643683748028675710757964
2681381440827398513332228794769901247
991481813262768852634712386149639871539
49505607538663299684742483475725519827
627930085756758149044014385438355315732
224771245607764657427324840802747493115
7032925877589955217055051937820580326487
3186201045218058401829269941826642484912
322380873944665752123019650840260999612
747613002732134665219136318316824048640
984470173659768186124959492812611083680
79907376236676693136827513313850692181
817320482491295761334970343934754197612
5302487786738711853789761628429313171207
9699209035465722201572428214462976874245
957596454931341015602676470692761633806
68857412785866217517941736991117121160
6051145969504918632559127426446512547663
6674411326401339966295354241626899449480
569374097784853321411278661767765358362
354471383590564445286149387339018007325
6616923665499834926296623210153197094
61921272647271966776237563095797885827
7606710481320188961840754792800457457
994813932838607599875914367465010988502
13315308821052049805430656503333743245
1880779445861418681879273724194660458653
86811803269199884472612958776741749588
817695052712373252472495790053586003356
3913324255771567085315883278556294238
5314333976651466258324521604510648361
875391431146747396355914384705082987872
3757225190115427638779952276323037461684
2144134471575365369330084840243623347038
77016888837989093293995156873617455569
9308484298567429872532783634896117914
499982822881313092758863344944761839861
8052454650487169884764578703742526027
6889155281862159998968849698465984028628
8737523954754630178398203254950752881662
1311480677944684646235780110248085035
98812765702178522545947124182692109751
9225315770366889729527690752545603511
8856512723323004768960724412512217407275
5229365145224855385684564720967870915707
70361466497421914145633531744679134254
5537371661089151326271746335634464225306
335988913775495312733963970347689275028
1908648879659454408076513050211589051667
256273718620453674353192548308450426328
9999187557784713672492310870516011382
916085858102811266931732900983471854742
548835072627425891223425039163587775901
220897421365106083122999473784659372888
2703299167244627504389324523405417722494
2747700967934624514001689795523688940
13378276118586320467808614863526933007
937122483264161565126330878418965432008
1054748586634744265915378003325679358909
834295018997951643724925411152564906009
3936218840236250412117376067625154101669
69943367313425981361839696982267722385
3671801366288858972758342566283370984461
9895651384272334347258363330593845351039
873876911604180358123556253722923358147
7586550189572836731283179687669288177814
33228053612220643178599544435170843675
801960333384515078692414877616789873510
787947775411479718662988665153654312134
25713216609016448207387329217642867744
2129717595784797263781558372802250077357
915157511723215413132742924545711053353
6268126634823415253727841952161878567534
2618977532115241452768169023591182140
4749160347145605899594857353103872945173
46287199074712144399920727473666895291
740378352505758570785728048569451112562
862219765347103989996917281438999616152
432393478484208574962515827437535475986
9612285827829092552739357311606415848303
1758205166953086344481319578784531681
8672215335083912962582556047191390242794
9392872359244289899616538224141322561477
364483626642141786760226250625909791
992718176163721618072532930903580205693
370387947829514960266613891219286142332
8379965776699582281811187582181926234479
3699456019711704990269934378857122352
320966746104718527062383604136265535952
45134062097473775334107106479714608415
867435053476345141038013362727345615
62680791651844294409622587764334974389
1601197232558776473543913082566383854470
4169449083993881863477019910870633239829
8301468249325918330925577860664970492896
50226173441671117118543606861319636497
4340189460664678516952901053272737192868
42785632984943248614607781769842368398
8985550227141501772494328485323944387334
2043716749303776528564314189566351521108
35793875506184836658881017343078327033
6273910691996351174521898503685314733994
36784574351434912511972684119059657463
81339476685054259802562427350054048803
974375757432255646363136166360532152114
907379290953886993135237647736157324183
606846565442972752030738067534213406395
71526219231586811986729938672396416456
2724498344118655678115436423690371244042
4464151792240515378455177637686087197
934672285987538201584806472893534857733
9290984961515504113395766552576122887
357422458945473488568147340574470774372
276794666593138642743377226790419038842
17924606192748674869011588669049293709
2868520185994819661874848072675473906269
640143974921625282428707778208879832683
512175753565807993857425199788987318929
5368724179282002879239403076878651062
139138147978453665812765955657945275170
48187842936518135187018582668471275083
36615775859621049196951795666487377970
7997781014376616165665402363748853087961
383314035896410928282186476404782163689
86329055608215985696635982374391402357
3126331396827359392058962054826997992584
7185555821907403350558411263763350724029
115563141213276415716295924955956422099
41784853966685409888893680946398073134
5378634127459983529612961234313530114348
292420355709764673265118263372371649471
461757907535399056432373873763367991331
5455148533260436696553901769099660782
4968741897463163111547545124895195325
82511639166013458702802778385212381377
604728773786776084831542321896971524469
13706594269460591786968817510116881309
6850554038379417512217498934993631701230
98206495046180497926482421618052808301
26148612035659592684410784411934513
75782700565797535019848168846856062505
4376609494815472412495936023835336794610
90430872239296365562474310586811904409
7164765143362222915479996538500071478023
3319775473963482982827442470509874603606
659023286956605813666662288854436880161
4150601810294554920697611449470453984392
660625062514347062275492697192321845528
16339839401559731197533565425409456662
174962853254588093341583985294998402211
5827495823394513479073239838547776133335
44039415954462592238207123405659296913
5610802891828077407965229102716930351707
4417919082957803367576597423888745697
51440785908663546916462218940863936904
818992329328110839635609613759381094963
93054519378980430080523314531339315014
2785635572068031693222144005606685089027
9569574399946398302885643597458115202016
741177641539184934545904315292570329111
9745431714775797025801812567118323740
6191819711071989796361131142669143484
634434842744327619977806814045242115035
743242886665511806538058283124629661378
5219833655078976884433661874538780709697
20802962817371142381828894669443277201
178769373573804586566397239151726613917
16189123109778313940681275446043157469
187376293616558624452757817118652199163
9088289526536308460393114813537558734565
557367698793345041085653294691471063101
5879967115742881952514653311428453538202
988597419759145513754211590279099574228
162590254622832110870644576184244317627
99203077552925071348132338668774832877
762666698728920155456202485477395915346
840315237159456498817689774145444543361
889492786569483057610689847780623947810
33449917647507550275307267776668662252
968014709265603115265150385947872558597
773116028197200346278031972860371094312
566585220684260959071003044417452246123
8461566159919957158683824216590268346216
647672902472436299071129310044681214956
7954335547515768447727684754715726181180
660293162685389805262546553714646611664
8983786051536222309598864083147157638870
960825524642613456164422151495377714488
259650417318448855615049803928625606086
3408165878023811229220425781325978309388
676588483299659523496632942592734277300
393924133279873616188245570224638884276
7994220716174404573682402717186275332719
11605353100761021378527611622232916395
549497934188329362656716174648892594567
717422533101482564795815371756215261625
195181265242488465191070515585845021317
447710810413916382239046326239429834
488034598595164243470403682994020405439
297514367278783576337911238340445982759
771016593893803508988929183287135062858
122377748113619814416987121340718489057
878084523226393991581781155329829697715
59495911504185450675521652433687288726
5498512412254904961034129065430888577298
831456479206561583770781194761560827157
31031259665075588979737145838203661410
3771959493432371360781983968944185795292
4506412210093836581169209792122378652091
36672578581031741768536887877987123321
373562004345824294311889311919522099272
1723447689185529432087691840366117247
3168756597408650876650564207278875806910
51039470700130112027915583986618806563
1092967624087973918829393946396727341265
406749475431177614778454650680441178430
1402891627877278775842865595650183309069
4492218632166417529110954740966517816476
769369055478279676475371087375549289426
908370188687745846316870595251472369130
271759899506930960637681453674351944119
437872397514876899839244871879248062814
8824387262603573450752542354584562568261
995369057497387216629952351927001399777
3674486664192776317272616261593334529252
3058664065574026263165405564868497234306
18515797316732039520228993199716176545
704638514494819720889022656509819923128
734251331064281289449988870137091804635
1698979626754467601737771896496714309243
232317723124492384562835963482392065923
772484002492279344046359712279243303153
5941900769125930414379289263769477471576
93778015138757781266521761208486157345
5345972822437813993487948846900254238267
3439779496378586883818898267315688796
7746904156471388827724373879980939824497
8823948410000754812092108613398885726782
1147691325064050420027141876501792062100
8178764048178215230470117278804378873137
7383322710012162893570965850505422696504
8595865427733527876512184310563162764788
14652950872666184102892077515514465873
4745279499405520872666998667394385046061
4823274736177014187292337004464039034953
182813156602429921228923763514785013146
716842988544647453458897293314760547169
616422831301708538433728848173679874710
75451825158520140495391667762426922862
5908361173033184241792469180101163617218
9250573750504700374158648056271814631045
808157364080394129904618392927271887457
9786026113175891155908173356888003940
61083632452892484817038893813029811695
42605738472416640418123376928476853604
4489996933842674858614323993654697918822
794771921683198287184358645989289291262
436086403107883584517026784024049807158
58065330595986501654909528468065382608
50103024764098362552406630535398926415
7083349412424025382450814670736661923393
7864987014008590344071427591325185317027
5391618065964365967370093837498615654437
317299479694981062622171635177451667996
214731153618389654969348681207174659
906812245502226237781921241054852045011
492395367451951325783365139112057757793
4892873528132889648174533160240185847
648197414277179687892508145272231796099
4200572032587202838181275381271251273913
2873712519725032108595276157498733882
232968184007509421321775672219069051802
452321076123794051368555816339775238607
59744757339432049966347181541553562847
801537325320278039195277811885596415861
4462335863119103269329362036751149696
4040126343291290673612517331515576288755
596394438103299592821098769612730762365
81623803632818328825870220957986373670
9803711344838421326097699035737842818121
7252597429746026836453525202445915691865
491797563304343817517039573767792197334
8235725661856325803964174181731982454740
7891744059082741342039723244202500459
7147107172638224644469359174777076589570
7309975395904953136563073082384182608
3048419340864062638833145141850596058256
3418813412392112425268322283496921163007
863687876426592261344273466819368969244
4264385555044187975211699555214147324290
2409357828196749702547885403359348991
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]