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
95121163338463288015105337456525585641
8601452362221431773954678985136121739263
136178183959751397739437281352736436702
590240229395613677428392586254592893475
2107917145191026144864369384592191304601
541871607058764270482303230374335837919
8867323740089089373479459133289825810
180631692456419068674169801966592405485
168284611491964753269045111293246351502
5355742384257859626092637750223419112887
409219472526724053221720613046742743688
30483443863930961009285186693636947851
1821148694032155919324431764201566657858
293381959074212512246657668655258285255
680625542031239568581207055781178872051
543295853664975553183511260987925162701
7715679778559095197969351238350739534691
874380695773998204538844297698299859922
426163775300638321462658770246891452356
5073207457034452897193518977244553069944
5465788793107795638650565993893762779348
8245380778767997614898867470853560283006
311277765614478867513692967859935036851
74176709547663703533290079628582845217
3046487683096077342253092693569858355962
863518793675191390820601937375312554894
5721491179046412521520757842288963568036
61915506509350992958854148866626755395
593350302941769113029982399893369551362
635018771056658923189334619113053009235
120626552661947498749913912578651851348
703060958197263414126394560766923379194
395898795442252470750403481470049716546
777313064522027844227268871690627067349
671250754471808739535903084839123274094
2977231979473032885366961098580642554331
5355550026121187688316824797118379573
685436064393201069835835863750049509159
3624854186367643243643915416693966016
94921201868747836122101410379969339830
992522734912487178874287977127688924860
8716573990482522163281493784792132689573
3128461691363257500576006040845598422843
3580528861538640238594614161659037528489
535438231979033407464986716342767884796
1712153767163215844192204152726015373
38732257167240914154013837884854843226
94467536897684924477285660136698969935
2788152898479429277733761975126681732891
723681008272679728724273985350895576215
2252154394327234900456328140073219268
639127446390816010234267687661350763699
5886907691348686688943806393335165808250
1267525770338752181126684135963346512088
7819950239118469498955497901222750947041
381884805516378404958093306413188887438
679557088481277650962462298759319975302
2908908658533704605291897688152649251812
2826599399546164357483934089914293996263
242624033106669838748712310115248692997
5268605119965747971779185678103550046575
5390512046538302546344019402984698043368
7415969351607522208159845838213518737315
101934153599925799058194790831883243886
9096684888624528227127318612486075611841
5058854953138918948343597234365558055845
9722589222364675400129226252744943899
37485962065549858047888880515567379765
638017839330283742092568576146880518798
9583370261856436653788168217485183881152
736698489962426485115630302890679831
527635134778970324048745805367838117157
916261807840790686532269592394641904358
542452456302546852668318656303191584808
784481910734067804943471384691797933
6138635987224865510250152923263819819423
666160886728304839469748366542559191178
2446561925709283834816986530240221746959
864992861461363634646022433273916548026
912816643256531928415601679552787017831
54052494886433672786910377639891716726
802717368127227628061095208199215452010
8684362777243405967391562697218213222693
52682669645796177221870943993394210
8286810670936156705582942472282554467069
583891149920482231987950430320241744182
16207623894164411737220167162625396615
58978197769072626315092561683403631317
7893650193955098979718215540863864181051
8438556858971738175571629698873492252822
1041785622852689506555331706829420191762
80153477442185724005396993927030154957
6337423647792456459782669737293314233883
7441415866264769275858162731766379332100
437280795983907257978626475912659045
2951372025686823337457327279861361677
2625643068352165458868639899412226292
925253175553348548747922727193791284726
646864343763540926486892940813666307129
200881986184540956084992317603956861858
5867659469681212697804119916095482382
5331681845872315553239024874987926592747
82727471755920649304179016881579149240
33931756197268297832699533436129975796
8198260180987955309292127290379555098865
579868799621180111439857906430542933267
96725516033401983613424750771586081475
52495866754571033929031550282112339221
950516289068340941229156707591972949262
50735388164169872118822866759870425149
57463723663211605635484682769034192266
856127933717594641742401644658604067223
5472774741692660271767939634854384438908
56687726193282015311164186773172926744
78625371773562844252595556725551138485
1360665413279035511308684502584181165
9138358242586399986487398618219429593654
3663844197837139294315404316230481451175
2010485368966927989011359010226853044456
999026053820114799821042435139273206317
32662716978383771877508913876634630122
531931318576804316272532520515480687591
36133279932816440835598877841479691374
2004602336127131275955637740589447914222
3735735585811860233029114432311721196315
529714833214346673575859619976294160135
14472110231664591023959979394948161965
55766636167014236504576305860299583127
5953301082339460254465103871197872518961
59405266435472039792032343978728954812
596369962941545324099032053857921117205
6613108196715508111184254273854311723
8928408273299783124932474863746712164274
2148918321479445431897838556684536600
466706086027203621940124822775377269544
8013926948303427162137354455361439021714
8403567476447069402357528141236233635030
670427361182229538568487473197821888726
541849293373881347891809485718112119984
734816767262990147417394736894370513412
116399212985862832556413699198072242787
68294702523162867058206891390880434252
2640927612952387811948984189739038233
939738934914460625846958698734893262355
787595242843104889263643312281675378929
469288678011940249541113464558911384589
175520193472211267974338160461995506628
686233959038940639952462121534748539714
593965639640477147544047287865685383754
5455506491981352987470646777989278827885
4722779271973580674543182386234212415892
7688811278762283478012102822821261035414
8207969824929585171278631362975840045237
570467898141388453829925217426602093279
49319820528563847153515757581063914926
26552951512135697319183236452092544134
8972781813318572907597909783923097923896
999562576557824015831438782210558306197
9511545085282781792811464966291866858935
7808927122884211505277041703512785612512
8819967626396941681117526777881819128951
9424856323367766836219203647951730348038
245091778066567944580492043785919825711
9056603127699879247335212678805551443518
5360529285254780168776509012668841994403
14694495228207893662435238997861021305
37318166816231963682722651977113379705
199948671238497986938061113670996813739
4945794433226306355731587719436920418020
4180253796399974212883022795684845691126
704973105565623655123574257150920104351
7474315042086376886937185734607483644415
77661638534716398077114663411142638195
891676134032886831019944369961079679619
283255755746617834931114499364836147826
998379223317191814817111849599930662945
5209587407556089139437198076190518581
354876545996637224067326726502734429709
167483971279634874793204628168423402214
9907066826461804380822664500731668175
2237803619828220994978138947970720818383
7152265418533619237848301661546237579462
9070295861056664109923219547286935848742
2612338530988349724140508895716346274995
16358657369150967338984054960392477021
477923015161232679155967643236324109082
931636435803712860685549949260217231893
274891033767807125344448126118879809206
1331742296442658190738811240710330077754
60642845168523885612906947398360441148
4554113111773034660049702489693610138126
286568441020739165285915215242232935670
5301652129376282157954123823254691836440
3351364052454376572338613928509036139347
8291268928798811879361241391117714376
768740708127924292025133252136364108
7176904939406507334374162431238988158219
9877378688989067843876219239788973103384
109331273037354474683367137722883198767
456121351884595887933909646873923820529
93814478237944353477364367271569939993
9229321992242539418270188015746196471904
354487172018984957914666900355244192503
3216871511056629582937318389214525622667
401839592078238643239522726980263655040
94362507204180252462640594607004434829
70742746293860931856819803576548112362
8717338553368760397734147087504069274247
762347629645050543246501634820414119744
9462931760563478739365081739881512821616
82031927903217872586215856392791067078
4625812637798974301553364753826136208239
8559430193708901850773936833163466780
169219473767178129918588180376676421734
274418918653740526582881626119238199960
8705920452367024799639568885310175846543
507717627222579248950632946758066082187
69059907202484777577176507410051446279
6321318783701140504632396747798870592653
7082836045657715488124716779638804831
562784954188471233827551128461417048752
71891972525151154410728998210537716192
95453667607589356544003104065510388098
7012227697223217503115652476932293740
343691671037650111288458595864743635114
6181511081367868600488356072326181875408
9168457444027149786481278919303798082799
1306735453634564322744039630930974157070
26983829788645239835433030970695274232
28318830590632465823039698522907116532
8208210327235296587260694116537583886746
8200418611243085286045414256866244087871
3333312614487852946325234307463689937390
1388731581604090698531679200408853609352
9826233358631639639695004605275749573370
6442615697389417977730078591816043476952
1465586157387012137337039745837348363493
7331760358286689578438957874813293898669
1089502541327167578655403094535078961
806118476365747630611362342307939097902
7666847951192726497962718727219462446131
2999891744369005167745224928369741887
2714619995076542362312303831881957156631
7016842392685351448448968554906670957309
76388872296863202514813781585339969581
3380437334004422686230061719479044726611
26243643320084345065831456664779222369
6702220548753902618189181943758966135603
232613658028581710697268916123262317992
7280785024124269106293838422610448631089
51736448186909825114514235550609335710
7181259423468135218423566385756951089061
2769950886744160594342162049395895911609
962766583183505706777575138731889488663
5648386740284132397311416150924729132481
3990993914557591359331517546781892081213
2559419586443811502545619652725360782739
34123096463059884775875319445623299908
61346838121252480699711546198560416249
68923869380247440569225283167608990201
13238078303679298417589138796364973502
145528398060811078586272273844133061255
6905722441343717246584122763074353228
853751864451712507312462648302864007356
734640771795010182069242147764817919946
974216554796499632918512306772322771512
3828225860815204366975976239735932417939
1136380521688514310549825270119350794886
4359718090395922829262808005145973261254
5446518573122648731255003016589284435768
9959968812561432691458505681368316253585
5668251790902139822027754663778545857
291047778662122377071675853761624332739
34841414500361866012752239805473219775
624628288699166496465058256961445032753
512237439465026812136975435249811503181
29395652705153929235465750409276417
682813749598168014833423505568018235828
6731889268438802212064581635931371631853
44633322448137588172347155335018203499
521084604169140791793398766876924748118
7115725583436952644837006495370995369756
10435945599956760156264114542310884853
894597385799405364093656208358648949778
95433098469241671621392297165283246352
5531667945568874849184971960120367969
2028125763947755928294835588695098689052
274375497443912168706088502847315946631
528817973107182753892123648313605123
72194819337801751394879149819753377588
8040367255456725710216764799798590766364
778257985687777483627785802996576534813
40057152897628871447694469859016179096
812535041318260019332038607015445517492
802795326069200691836215026731936666847
4066964427072590687226704819597749294686
625540963128223386662362342126357872881
4597518655066015846510131502365032968969
6945982297652725390791333334384792793178
924958567547928512106897348437865674663
186899269183138299906266394894086495938
2173268623262814764537835528838499299668
6606695338083921644424201424235835049829
5108256742141936412201583391137649618
35246944371026221057807778460844563303
909229599031649970176669181717285543653
643688714605475197371987498800620213054
31153981647431931184262360672542418359
637560092593844094071400894110517618358
89655389212114673886951139221158105538
775622134178271572320617534338093046383
154531589583921570167448886933131725781
759351917656917678411423064920250861200
4834888911295318825824289754913840655212
8037511040384504585872915590874314782010
98580929448922956549934110504429219938
133617357769576933732798663176623526924
9674938363814418861843232157228250424366
27118377787799204228756890225959265879
921616457183639317243532919128410676758
9258169383599466351396874100888361105364
36283431818924329120661718598099949923
1239289613437324596495588248093552476
89432227304484968979617036645669258942
9919419459226946431592414078315676546509
291562393459068594253863408377344901374
9703244991374960132432783882452050171799
403022344415986920026572613953671904443
592410972965247724831772665437989384722
2854885488576547433815309976387497235710
920957671778024202168265508826333741719
45259289634876372753088962699946281856
71085817855215862224398577541024010152
670332267720520377940624499766094686
415117148977258296463017377650009357542
908043638972753771449792507478468048824
926172737251892473728157816448379881001
5472220854362634275661939953254446319895
460708760555922933631284211735413211031
1578851388797106488455510573141414236
6802236368707760689952632483377725135569
699280688679932161603131960327175828016
444182003166645162185450777083913932748
721875949568172871956973168533910406
9396375026516531861193981644971897217022
82951159739917043134001545091476529405
644197897299256905129239550778933781278
72033022168135598659799991585628414486
1009266723053694650046345122423655132560
80057843553895924025400647275745362981
63136031323269857097851363019949754108
6923384490011733401754277733403638507805
3392854119628157973048934246991778613824
6200671764991955747956666336276151038680
570887076659914162399112245801374547649
28567645815752074949903134382322587607
791227695586886138606659548267221377147
540820579908244236717234544894314385403
460912107819603261787428609590439345557
6333343630944273122400947987965637989
947753651053841045303854263332659803319
4091685987316798157057085226579013185702
5791645685468755886144053927579211947468
347071248873744849067839385257717404554
7853597969634818628260759173668142881803
3616867368034523713572664242190471381026
54149066357961218704119650676027995218
81459982326654235925018920786321284540
187241422903185645915908317356622681864
364147109891646117975487087839736986916
847998431678625433295056193791849349061
63350705781718071149938980551117318194
114494871796318463393478923890958286463
9072436726236169154722849353660975791997
596785246172262583392127911850849528351
159448786084930561194517347621817195239
966562105015339619298097231690327992007
9260307477736064365356626921577935434139
5785169479382389188733386147915288564629
119860063414940473965234658619928883143
33167880835038348495964660582699062834
396879129749187727936038575561884079227
2411748340038442158354952887867593384816
613236568255591726615944782567363561913
94547547468372828947593681969451272495
2822352862351419298133695024691282351575
249947716416230707298798797979335856310
532565608643590249086695353472006804671
399716394886436258482488277849355287199
1174756151256722862817062541490828819390
3035357519313617398311307513960438248
504473483686963557885318227971947425677
1867622366223219167037366584523085408205
665495217248441189675902137962301848688
6353209419038602880887409555767072498
8713717150971893803854386486856875140
8087670989945399969832497056210499779099
433919055811555378373744405369164600
5779196939232470815272665381721786045722
317170001392754458471391520298577539991
45565202998635231693396217971165933402
147962389284040407919662309689756145698
810556150483156628670872001505188148503
893759871403155163137988241610416896319
689772663186400070168547735857472765468
604860619333092781413556559195479901655
89382919161305265845462785433885667756
996362217509114243986585218461596713313
2149420659011173094164684339407994027
811116154266565945144317583457554902880
241398063087131134193369322872688204140
13099423736025332218108779562933833260
1505130020244010211080235212920799271785
7724662358577834819552339260948498695101
8334390155497488365810922735916792259039
805395709544492753352324285805127458742
4671231853663509599587628328345427768267
900946706030767555416682835748572652398
8115122144633626876251857547421985465916
53299680820892791314546893856568227717
646890688570182529554947957196187579654
3540166873306041650571087965937355834111
947054019311703823878050238445442125692
3118255942401448234149367717849641678724
8417404696799257778354109530802068749159
77037383439270585917746102852340349
4545655150115568775189088579384738235530
358564819667134852931037959708986058009
628235267709762207222858101861032892335
240933885356322168286893470438843711642
62516886344465161383006371498757919269
85752894291327349838892747906585305478
74672882512573382413878421026662945752
2147398140567552694234654866734487604966
723942754585993786903275028751973919088
79369812360254713407175621984146202170
3152247397157628250226945739253142858281
278163749046618097158242819813868433668
321732725066100192960759822469953157527
516288386214276447522049927707122049901
3041690519273579706834382006793688787496
746011449889712198114731273306643755122
271023298351133921289372813967013005664
8446656259283883842586595937101955884934
740716518572642518517242771184288244868
238885857278508519939579942951224243230
6465647499478074556469831633532171789459
8639222097975444733763033430918718944755
605884984923382384427510383679688957743
9491267830654568525750899434666277324
25367616224215426506434854131093643914
3501659913116524928548679565564944987539
650438758410924979254116975312251715699
269017515466664955762685801614498256023
2925293850486465505378312482987329655299
5618341925223750512265098465787961141052
9249113890545262517886711122423615064099
9647867355789280393916319113514871674435
2143496747415608882861184283895712941722
4314148335574272568491884043261622441870
8320919492245945388925789544212444964748
939748173329478701225932732233581199253
6300393241728900782574489454398362841927
6509427015662817751120453073239181612164
269317719365554278923798104646737785462
689952633225331913748638102169257561749
425398824134139245118241717883920754454
38685883005810787530205684379315413091
8903973796791272380354906146268654163583
538554294431851664984309785974353755712
880463887023429867883313502797487648263
4216890602545179413333683693283476395
564719148818193011049617800680744517663
2918345014845237126579592102911116524639
60934805591267375152576921204225117443
8423401919591934255513065640621995448250
60979358513283863165977718361372627
353353573019643127616239149375194342
30351424300687267609790379778397854857
888647118347723989389369508459437504361
53996586175805664418379877914735321637
5822172977100915807378219399561186296
93234069861721119896426651220525511120
5987968660038780336740183474799253962183
266991374445108914353271579430134555475
8804661998443154508318195777573673309922
7434112023846366390036482814245912507872
369517364515900909473361456735990284457
793369245108113687098657893493877643579
931934375812488885286432372242886830
5086814261807978443572301057211426923282
99440415738946374213854346654528976648
94078089185291167428316624020855795612
2761731249946171189228180012824554226
9967231980478556488967422727162731865418
41819208796822057979261220617047467010
699155419591565110365124949736554856278
920393257685165523746994848271540465831
4572959649616171870492836378760183962234
84925740440372366491642371756464399690
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: QuanticaLabs@gmail.com