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
1067803468852919994356307114410123008024
4420656275603387134258747904816789395688
6050239658855898997034751288902817821709
784541901093978065159164649866651855718
215880893543719487476736463244868748718
309545637832260294773251934296078622653
3155394135336320870893471062173654103191
77753907822724271875891742541954822446
67926909411254137955834896662781725979
964654239191180161431941799309011083134
247225384417844633084512699930588679322
9571121331501798362951687833980944216203
7411909217212288814849834618639126673497
3943886677798479423092274495764980797231
471727312728512811902022109811253139475
99383454868974342978375590420562252827
7763989152723711878847035505103292304615
846973856208155258767314364732977619851
4014906111396150733221840953711761810
653443526873453530843838604323540837244
4259825295399616486851233338212598357
202917706190196075073548445301469655875
3864889246189744115916542276644711272478
229575991348804430447139714186510591978
1027833879571934542011791786139654078227
3538833928725531511842047728344416001735
4195727936357546943069642342874616225070
20683295943336823533387464769899815041
9476883876587283357630168725511410655
4442427839764192238746317198562427178435
98042831216739942812446529661821222670
21407958796796925637702049677686016530
3061542170101501573394268123882996713278
575518994127889823257825115031422309
435225184032432613144782651732456743436
8842274465986583846283619916017587852
5669844254723425490745998834359310995920
50303630617897058825212669752356324842
7807823280835394972757196449915846377603
744697415634798699888662354475060966491
947892378950227246602183867679366571
813180222844326676209758436597696996165
82304436623275945797926000239074185554
1067592693612462936055082696603538188027
1112932062454170940294472220821817109264
5845985379867505668840158308233985365526
3799669310541786258541337833977618244388
1132908833228337063449530357761240976
802665038059115946585601692574574867555
6022771749994842600046717623719748353656
5504270831562693631841305895287434695411
816052699034808566507260842266778425065
4926819758996734446539018197556243878929
48831950230955189831193779281322311395
3984718538538958567157196682555936952
870436431759299734898419566629407173989
61986651667797881073837631346934764232
4249355827541126368754682288087151939
606914521600798336097784489447753919646
902164140955318844532683632970017448426
3574760773626308566327034218155859447300
212825793111376423215537931078144904
748111268702720333798508148787333439999
732757323416465035569275847214128544477
7135797976629365636365008794723659724839
98603673367960749752202873140289157490
868281884299653217437174251065291084999
2233441796011865795573909261845544995415
2303384627569216108276532678677492394197
8904987800185515476639381614658572547
283947247845287536131201476538671948809
10743685258647310342143134940037868984
6719995049743382118068033620556729112296
3503467160175801442266843769302715872
6162868380011222482060105165312052692380
141060432121259397915952465854577810331
219732443890383820429600912040083896361
4106773923261355532222806947637232207879
396612517212634668577029979610419494
4516923287723926882125241599506952535656
6810797148345477633892282232348319348413
313611211988381311981012885536393256165
574567937274897875253713640853226362994
25554292338795733374985899463510472676
6297649353588714887817532736806259545186
38160581909387673147085357032430049376
7985210152012873263228067690120032336
30913604360217524769629930208435865165
271706175393085158467748118622053274556
217779289225597540372604570877236897941
7570237961402014979392137765352349774787
2323504721691103177150796752338991792583
4095643933896682604170739481134971751759
430912407840294461359966378664261006799
287510732576398163655727534242848835108
82464449812450242785607367275988327684
68824585422757583134178870932659776694
4646556890939287372086848750987075516163
886764438217458778871185916773784013037
818893439088862531778834906374619724254
21283387700451252309774299176787068369
68417258334477336914824536148879388816
85257379723917363062276812762037422904
5262517211287476315883692503242883569839
9261388147543916493569987227904528304843
1094774764982371767030175220680446098104
31322180718211505178166797315422929004
5007680890201073554329118836419965884921
59385383831157705725483122024829111491
305991149993692915121743911745172259845
64695918525255550192812498058389581840
3106168517569988128871155846321766079977
49209093582036297766390239052488572527
5080698676335792787062433510991657949692
108144919350474076904928306056928112911
9202126817997923879781606378119167599070
39748896492715169245787028295736995906
18804353712393905297414895817465221687
36581318646798933228595679926208723627
5288165648604592213798842305576737113606
863453087222712774229838472834594252350
4872620531525743436174560076677354100
65099676461146988512441363752959285810
666322656705965392332247716141056011987
8860969895306297872591806287696650676394
7324568731341551161047049391547481204921
76762170134899283795818494426692766180
7564952347579953640160613707950076419840
7898910962153358691335520203815537513
491527255773321069530158962627199186531
2019871326901804865754151459682071041368
173045636068980850978139767566989752763
3587265331147914148414945475114724950
62688034918122447109740526828898104
617316886378363429338296054553827543170
68818514823345033772552667839162144073
9421996430627604512395892016557118964514
16045189784313368319031644497727338118
6059977772792574842079071732104489116847
44844225741324965354185617220836782424
1381133164594714294311282815678029267331
7242157990369923798881943095229461326686
4699781099312377181755634476890293507785
642593827392782654598812021184562769
48782413846795896006684561223837998358
272917162693911227795515406497191239868
9953604848579097583696705089853231965378
711978665813113363760102699448822011143
692687974749043966344057534303380696494
564032529865612789281425098482062895458
42439502468859459118991418916035556401
861151481203657866904257580915694283710
90404828843418331557092342021154727198
20908716475859141182565133705835514875
72931253797819500376655702948715371802
4430334957444398114660207003889375301936
851634897927118526342392179790798185868
873680758665205715164311511806723262326
67966522152590664554675260413641254108
150520878305964616024811861799792788942
627956627109661937938856528199292487017
62041646204460813591906547439920242956
428161696066145629670835088576919306299
973881647203686628711261719829446366493
1255287475225898996027418744844307719
904125738709803614273681664515240249817
163529877565456978093004180813678558273
6536764364864985681773913861268174934542
1403895155356840894375462608392818752293
480119883377201119527813014944425889500
5419011192228376042504462445850465542
205819144434524869087661909826953842859
1527221960673891511679118015512188824
7678304170911132967840808448349337308
45819962466234337125841810393511774022
3048484979361481235886086224418276221374
698785243533577040481373713679335209946
813555305406271987085929330835091308885
276946383659922322075439120810053455
38378252472385142434112245244173365725
5013567554654575941660666460685322712767
1334486685913959622169118199731894019158
6644903259498581604526147267532142478762
136510512496680760365565613800980447617
60915952634464006782541361769505111836
9195529730157327592973959568385136329413
606894882825347915435183343135915424
35921232870590061735477048751272739965
16594565727469812893414292650492511816
55196375680991762348338174885147014370
3566185261407872733337542072403319205432
852443223795397377786593547294074437585
261876813191297721022045093480960622747
99596226550949964705757087493617626259
3072114586678112704118839276935594144202
9693968111814970636877503339759764887
9585210437877108466689317091198537748763
5597219274640676987197864916598935775
84955744885226964679005881472085559574
925439017211049890627069577875015627129
4204133581486253613431838065204350399467
144866564741787087028791506658713302873
1388219867188485047674528390520065886
62655195608172998798468101155822638432
5287166893836127416728169918175697309994
66487011330199319719221492578826223145
5398388453116272872134184211433513302
4125592419283137941679922887566329532452
440495918218603589262351439183354123929
765475395509338319142531524272978884940
3115605758111827469758906815613782345957
579154842869859397313066246776874405282
17686516530457053588743725462827027443
42580648181433064849159518631429619364
3340147678522070431285082610361651499632
130158671109945443286391776237913612259
2810368121092402201985699253634983883189
8587844416743708296457065227672455176073
895912707894696680276603588375926549794
1398429475572410826317044255175376041985
4903845567897717565071816740664662687
855677863940697061335936253325968144279
1181517973378453559421867450506582984192
3006271930954832124265506025964713922714
341143379486680977285575156376781127627
57981658366137812702585029645149615452
1095809968073189104349335815892655528552
312295533755748400133113144277332295250
36219995784531566354900033487626142419
586115845750706891066285156224471101213
131938419894786360562317613074352343190
64496398558877275702606441120330316911
964312578650858162926152366224873128189
4348197934961526276749657309740853086
5811199778348102271111243233676528175892
88686925341298716793594814179314795049
9035334092919759985160608573884342436100
262777121269931608135563733842722626679
1279862645221086726820163832545860282262
13959396449325806988629482880296744066
1839114554627342127821991728963961494439
937373645743914335104405149614738531956
3323564952541089278847977523204991506452
1249705744711895662176088571271930541224
80997246459632844452468702957177656571
7683323621518855511938465011676316217878
44525187459635857072120326143933887560
2286129094646118924820214728593534409709
278974174914747227119254315603370473726
247735961166325788821120480486712526340
962091677308303774635617903480649791604
7777923259499892845781222669691070535139
5084197364819074760320584568624525795
457820054565750662211760534540030163712
41193373828549022864536892761080626382
6150717762906426692459907105914794038567
7462133429896103798985794526153478247711
8575282640529810883763124503781675377919
16974506480521989425668907650919077572
8692707440663299194460674220343119035973
879780079365809940364552379588683983944
83869056885636712413732873446166601322
1134299391864607709352052722825879153785
6701963189268876405969823282419661218344
91866372496219834872123199198910623195
1390712813903243311234935548754911839
928830148116773815052069813405185012587
71418018667241078285615682253473018214
40644329216910676529785418049192754249
4827635186866429246330674652470923577852
79593754805848645624247266101410189018
47534926647020825557783893515515432371
496360794705726163781449559469155949032
1202699187118705364121775896843632938773
770871857902873329502412687443786743128
973023821142556970599227106887697895509
729598857202150532930392252363780981140
975154651012956021815341498853694945676
1830600812348846917217539994359210866827
880876615005307943179634480131663451660
1738944262612725767639272687347714421
603876086388402889072464505766326925472
9662634974654966124938152793878776168
778951932266516305168474613315149901710
57822152068455764512757165524016654282
1885631240838148394292616432331858989308
6026592390343654547621262540120165743129
3948462814314805691464499619763920427016
16427830715013393047248499558138044343
1412609361848236186798365011721360122552
32026283814889044664748742288344281389
1677182825849622346254064561213371343039
8454443190525578266794279821429920191
436714944875133118083202348370447673533
5162385493797168351595205893415127092217
855890304351859463871760419931771214221
6649236840664446218958056936838978831861
7774457713715482684855702133560162772053
5292979326535125825950504678526979353068
5295673754464458685186495867376251911763
877823210914466987246336105889571276284
23472445857221497356110646679812344058
5360320156203788708770987229571919221821
897511955489786932556568790784064046668
514235225770967247997809489542843472323
10544174997429273963457931555774879547
37202575691738431000020245140565013385522
488872681553786837666948999458327963148
872995317786263796297596214111472333945
186923561643121147894213534632596691734
523020446107514426751090587344807021289
706888927419414388899333224223936176491
257254869761526201940907852615965294915
68019026234684125421237136769189498976
783271745297988251126725610128027916
3391286768261255493198858360831177301207
636065397677422098628354760902095939240
685212463588521664199581738125837261400
52132596979143651173103397655715038539
6120513442616968310172884780927878694434
7322937576044359577141247347249275918
9550436020975575874836583615552628778926
1521888281124120773946857912561428215453
5041516327167429150245921322778051476751
643094263504981384773349887457249871846
6398840873042801556992116775279975428383
681422117448657546639091051108980034084
4596463130633252859583709019454514444826
726874924431994517697148215474579927246
6924224792209064266819781676858825354231
4700958580178151339996208907245642111566
31418429833932039378401125245817188255
4972896124159025724560043675226866887
710865420659825694755236280821897237845
37521878332363436174289855786666743889
823142312967106191475583956869984157586
35637847587796956084136249249348744226
722836648835432586828392308199433369785
8876931333269332170289332657684094515852
384215449061217286875451297981980142317
4178297050789313781911695018353038656482
235116799657762810027761494758644866562
3876715851758345148069511718232884477161
989946347361591176652871746319327841
130549494634931241761985482217237831235
7240879921027508130895622778591951833601
48045709829193418197282379765972211692
466281218200414181501069387255287573706
565380121875313980380885944520799402
841281778899256072318055331318322503083
46348947763391913651213726924572405825
789699160108114110162731407456258729335
114190124504863992565756908930276285551
8620928450374855796750261707519943995199
563417580769371747727217114770682674337
2448463043502836390354195491296394375139
32152070768483996075852934656549711310
1831915891949254733278563603177572878572
588733508256444777012788117534369935108
7937851681728359985916704930826572442941
5778603227582674878681587903201329282132
3658481090453732118683621412496517466981
108035549808122378866175644665383799516
91084501124429326196590583295821859583
621652953803737801164497198557907660
935699749751536163038848409535726024822
30702655321790664127606257751913379988
673828780194512684227195598364607863
535954722608906558928007143346758862931
306381333134596671787219788748231915780
56599781996710954603378507359874849842
46203983404455197741236082951078155929
89397359982888607029336161662852489419
7948801352205780352562074674934675687956
992217793343306232148895893737550903633
117877004546164152704378709647848306457
954452241404381614792648821777915667700
660630205109388363267486512244960265338
510922175534687924886018352452849869892
701029353294271469937107636843317703445
4640853539499541891236624237488219713
26897436376985609457216264770782688645
7294986762299737874741081742452718878564
2173721531845825306160295685528780733089
9713902965973131786999817882379633067
786678168376811133980767037806596134722
965118508318443513296911237341928837995
7524104590916334386871654214861267307904
1326617049814354612623437717166332766229
2546757949797949154577478508849485
8213577129499337693659420069086238860
5058940761450586906995248745292218993
2386519384025947652934459434455545959338
657676139761812538086933938481868251
1009616098456347206980946330801273507524
255016245948965297508803351012076039591
18374858382719257654226391340375357944
11439792966563171980964794626364327517
9442822572094742809861709562165735108756
75473646990715097492237955101599958415
9487166466991754380323442605774113773590
17251816609393279828250526424257414962
247074065986974467137069757648131352872
4742133899382655731385558568635001213
3450981354301270247399477949264850893630
357553754095036798968148275596044179742
3571492675978349419674893788122931213724
5325475118856390323731242464601115225509
404928461218231934583743193420616431275
2553250461799049400513043285157970705048
297968122899245972675773926061495332218
48306119694938058355383388299255448063
1860958092533809497481613677571177864632
6092159915893771472156663074231625546
6751405520294587157786965855665156357600
671976344744265472085062028282337854369
562031233221514752463778650809764667834
14246768740241184206798327617815161078
5835596725132149656594802042395299771530
17529739805586953296652912859438574090
859647727655319147372693278144636517991
168405965885789748510084495591797769915
59636925526883903367960814860272541830
20978053399971374496082921884422277018
32068411772890785345188458495776335898
78145411585667077919389799911866692908
7260974651036593210441295879417942633986
404268142541607270309213632876125388712
93961560986629341399357427329828131113
73454717901745887875523892626332518449
70024869727153882953890560180538818252
8828154818882718130177288102917692133079
6780808573148759432314793959218446232938
305790557187659571064704233439107631287
40896008247855553005839417928101441635
7349392715777319259640591783478877616216
4639286016651645712820027275572191777254
6574414086816549446998575743682875097649
33061068561777593545814791566835546445
6040114225024342653368136875179050986550
8347104960369197495102876053642728261
2281495968237746475635522941561423707795
3713687133937975631446117533977294759063
1643709465495667446668746487554051768972
486846927708539276422173978976399307955
1223873535462026907986261071520341237600
1493837861983131670821516389048878821
75557079138491558540597343205899425329
5499481980291460650449775553194978005945
5008619453775484225991164524481289566229
1201883365721775758762249851959733094470
8414707894712613939784804617775952652
879731791341905814833389904441812128759
6208320872876277539050241692754356403292
76257370302986113142147660940734759362
2359672341816489717338825800871732239651
10542363629730282763940777337435293656
57609727564870561902859849535671283960
2656521096571667961250669184974976276559
4083558131601624774842255404888737398466
7016429983267402632167226117322573876867
2670627648342707233247839059236892686920
981260977237366674101094450998853069122
2714301643624458342584668460148219659657
2880420788848050664389506838932750937582
8272178751631863936653325483618371872407
67266254454059113704867842242071284051
614561707119968152437035758390676307336
8277672122837834698233016332569244535400
9608297633688648310436751651355923864468
5381657698352080473987789409173491695565
33257506504582154524826393554630128202
615976306258228423547247519755189017769
7993716034819191480133197397173676594198
2269971649786718498327398433201818026831
485761212766620696477510380754239114420
8621490367975139668930765357586863036775
439449149916369785363595129473397931786
845875262299972267814912507933413077750
86111734234111671810214440563523295989
57977352416746948446298715414837964541
5478284262404731301669468686383862047078
167526214535984784311568204677059595154
899056084017168878341732393450210008594
26722603463697335537298442989715136723
8118382268635784592216652601918568442703
640350958750746025121008279585743069499
145914287948266635468202372100371955947
6594371884393206437434772842047404772
811178499678143682649622947480551736196
9758338577533588506896168880279739279239
1699233045162012332570954450940950665225
846276437951569896265524161693795389352
9411959542333572228561698265918758264
357169852600928188207161964794892175237
671341935924368790389533452819552128821
5414386467013604685031967758259811285901
5787620571495584175045687170144884692001
151935121605682839449175196669684569751
16609256587193310165607888119989753511
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]