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
2974409546752285991540366788501739686
928399156596438479056545490858591463969
1004333549784428720046998070397139956078
3502849065422580668059919927289174804850
8604678352173049784913344316050837199
3261128940911752154226339577742765712
4688824959414926709489406305447388083049
34848251787626777296400312543042945
1712625646754755427681026040916616696976
660374929171654789992687799754073711484
7316703374957677958716612777768603193
844811718316198614962625393903475077899
194728341335781131997097362424643242266
1134498986072187301931414652216297173212
7721216528334584487744824461346683870
705752683379808139754158708690878074897
408924533132299913643708669738179733483
144156668094251867482949943366193455
11949072993168460414115442278632097515
7934514387252831136435664408141069786559
4173326327161056441876751661315941522563
764736071382949487043238903542281278407
138989905102457475139694956135889855313
93596306736426461711645884207322019867
339492477066323797587577349731495952372
806838866553729033438859346389867807357
864852964624531685547757285829244427640
5257291566513679184355313258486386806434
51504018737250577257494119845653338049
837649940068221361342798377785854038
7107730418454446843890861083804973794829
1314152765683480852943909736844722175299
7456423914523311171792751326132396147197
420394052731245013708793732977856797
899610947440887592597992942386852653333
450743041738885803772049365857629981006
94154303582688233884765980836231370
557393065907413380173419171190539601898
6625175781115237662620034078384113029809
99663632895949170413147520165225293295
865960193899254808937861534812722187454
90115101613474652299189775990260202538
8423535193232498784629058762607334302016
7807779535893595408926002079722845659107
6624308844193583823978668616815763617
947535964655357566967808291383069999478
230695663675191688753384193616393885544
876460748433177958157447179387886474404
467948739943774455718163203289934775575
6021655195769847354467595701619742419
257090889323399375158440596448318605063
3829910123661278174934222173652281039217
663693778815288668349622750365689526026
95974840653255370208112735549891539519
995725435303719268328323774950069047179
841727843364625627469370884460258862
4523418896057792882639072859925739129855
2988427584561352843043686631296385173424
8715993460744797100329588618237663295316
231272348989312379783994510221538262553
767120133194541900822478335214587413465
625718102865213488511965214723245384327
3021288573579784345877838973383249941815
2160118529964069157827575426982045883077
324183155134588853353081399783642578933
8814949394952122530868707685164569782825
7268742365405557448255641074530119755460
340551108364709398623485772418951995805
3402844674643093295254637518516719633899
3044144918532013257777175946651060857755
4229535981968430755885944671998435431058
412155639439473269675697641138591829467
630113554446223997723034033581616306213
9286381410624171656487628494187850351315
5795489018007146630253654216213893366385
6075466125375253672829626574665592102293
82567709736719277297396914982924269987
9408644626828811862999075690598677937
520663711487560540228764185590133677
2784172748774701336340884788387418851496
656376829467137684813481277752293379191
7895274299804258795926824169579119643395
68427428878150937614933968243661205429
91895231409968161168547982354212688720
8931507664817236116146026218897019223296
2745926874615829302485523388627686611
18774440352340487086177069495720341969
142496376956121580285174613124435572636
923343346675939019628773306974652421699
126300303958627515927878816273576699
4190882255234685887547992396127397417237
825317208939711546051909835389926397018
917774643914404197717345657577651823735
741221195612549287747161916716573552298
669523119198344271863906552686376548508
59586267500059399698968026183730194011
414569589349723730732743393627615902022
9941228389963079644344727391835272442
338762082042363526123262642478892983702
223735951053404984379860718941567114550
973790085063844079466545291775827031228
13774165878067022102982735813071421550
8625147798747563654412988341173241658173
21464057306412582312388967485745224213
11481034377728952209141770886133604831
819328555660453449219793895634132113
449099541595807146314613694792152522497
345465537869745650332482365922640921261
5783394799711729600766335150112599528296
254191549196978965473593026330255779340
1989706746523344086324295212150728389
134433946652497777136984415937367276835
1891538868117024626360724882450015718188
488559576553820488858368202249280346931
9313961065597159685794162811918570811471
861137482354984027945129397768843332857
3609848896073607722595745302214934819034
1215441495503479328674429524320560069435
385894486694620274434507480671154518492
155233467579047752673269712902054407136
4521524043732150378571121883363311203948
8908573641282944376291371902219603889
38134048395861593308838301478174513807
3340625419836417495018605432665121924636
573562142787119678690965476807771896568
418241751394904765847033608976768025217
5888162185998122788733966360155520997153
564783207950222672392505991118048915917
7911449089947569197030368888691041737748
338384014384848776593723205277486866474
664771635009914856075074499492545432189
8724693093712852491364079231297772142217
6777442310282130214057031363863283826263
366854168831388950649573525310036065285
391395246709540426438588477734686679682
784368389317714674408560487746603479792
280684961591070428857863263895293642419
308331438601607340983306216810620891132
48573707918563666601965579710023606336
6835816770157886008825119964458646710
77460793382734316738891123119853692271
909965634339531510255317394318957529027
5435239662483571137273639388512699954472
587521113604361786169053814104797618770
360467215748937565272260799294192381747
356523275342419630236425444982131651723
695348883325738640686667984966268646723
9380801225726466773258065894335442415881
7205145814705587168720967927843722941239
270688611282810197577702300668295569217
4406576338959553957959059950728192641540
3309944097499402871115385396825320012674
1333432593592346906214606826970942539217
5724501174346079737838968952300091479766
3425228359048850779949963540750291974577
534222787980982529547804080732278334731
905894417294037812983418794469957222911
8736713498245998711433834544480014859360
648063141131681822168244810455346351317
8285195429221950688315313265522125136821
4503565263262129456029717370625912654623
988556636570541655028766236815403591407
45345542037544634612712587190492877
8778773770162031621043078445403799751998
39805636489893245227168266289670544662
476217797531768690492985627483587957837
60836773084489411032048334532371734979
561633333890887841583203166802092831599
504980828471813861236200171812157657416
176747192957379557555228881722673787
595870627434932766217913525090051592931
5827255992497662529973913474167042225110
83876433570826279410556680228841359
3305751957646234748382702725796465424
8404981482602487921814953565634740487611
38419535636668559235608732788021454067
867534013745980242472639581041876815327
750261098553512793465021697622511888732
637274746259902124799421666967095156962
467931231856960251066031900346691554009
12483106479630925454901162797618276836
21382125317992730161364353532273159736
893997205696574567297698790876231024251
10270041998228552396743402422517011764
1725297416876077275663772525744992243059
500633372174146786398437699185429492164
361597525388682219551663666534943569720
6690427442597285828991535224164878131
49182542161528543818741975763099407886
364915231805930964975559718573215624763
604547849153782051499208766037613615626
5070313942075652522459246206699486763
400246407833363655277567125612127609043
4157686543075250663976651110529188266801
3805955422215407853552695413246424697084
193397886047298870848931636195969685104
5924698741677544818362365448882727174442
419085729528335696463539312483445344604
6386335078979782956696115790714278546044
967942389181955195686246248363502791879
99584354534126571624068124352662873326
440122505475552833379339571938981386897
86947871187796071544888798224837208529
84993414984815599016500155476748737043
9848113945225535795614790418148679197
691955433385786106979168441384826653422
97662778796113388764195293356458701135
739803256391928219491544977254971087623
5285133849474549951219115291739562591878
925513919585493779901924515925563565771
4820363938148587243494734791299982626528
70287239298235246379247584892502156893
76720531204428071862048649956111561120
799080693606379398846182173242052077362
5608506442791145966312130259315253472
1531954255494797398770592754409231223568
541554848166279483844481893409569194066
81388155052861871727104408992615601748
21507998718381532863827291372856818316
1218218328237766563894702468243038683427
3285743479666977404713624198149569542459
4371464884169716115120449268923899798378
330669835082615084015967105771256796161
9666736921769575309186258162994351373254
5480492582819018535055517882461532191490
295506663056969859424268492662073172914
2929402923334775932988915949933381501146
685446452267330355671632836965668592873
48989295455325232659154469758800112969
8439659918496358191638704570361759875464
32153518339948697494557375907573878724
759028556875264802336547433392183757736
1888594241534877536325623275573337259364
7616573426326091452510736337862757524172
8803142341196723642819777545526527713543
450982697396733622986993377438526199498
172023472092571158896534467165958343804
4342694449343568943360217275240026424980
977589323584624908813984439197753106767
286272143967391286510231676263233214937
17778333970214202911665532343057533178
8605840422132174990913275411551541199206
17010047726145333979464830159550102679
612515712337399221956983290927512978506
248258662337456185688439580418161335921
67931940326475863972239379832028123593
9855513242206725422221751058476663553024
817440426482745677082548476370998714169
8227251395144295932497353875132781952513
1789648195846572733978918635153819805071
323386058968814298187884580288766882407
1369396240325786544128048753287299985487
965330497253947724996944911259282275442
7879835120126574139437046484959043283053
796257468537406255179763377688955634632
32336822799917598378792393128651899152
7267602051926365960454583543997431334167
3770737870589109940720596054438675778288
60428476179995654799819755957277112003
456268805030309525806674128985280845916
894681022255407441197271421437489951238
831729029583294189370851851057897914
479924991403328114178526448566467457273
6323446160715601631716771256991845163715
61395603054653493526937536350575396452
90402861384144482437066554754821253164
7547425494067905348189068024650263687778
721311766515589236363012287168340217952
121531843979786845582733327761974027969
240940815876270438198259146631032146992
5312235411231385272893384407810338625661
820177157362755618888814523352247605869
8661277742211575223607938950410466465
45156314380755109015949259752577153435
40058891148594844781663183015683537642
662440539233926514203696395391146001739
667756431692260402770526957293938749686
155771977520578588465879526711482336234
567578882257643292094368308809111078337
978212488086296619619333391446226601756
584941055166227280272804050500819305382
813399279041708190585602945296363951175
52783181831440327965103220587616741250
837825482286625158455164539436246499731
547759412914176869141318371873870539645
66648690642857484524553541304419453642
46041931590722634964145747614926339416
9878886187476303463611029111903470752742
476798781610388070956408871424608559215
389630825445719981316374080934040274246
608519662311929314585316493079024984636
5690434677197571494337761245842384757835
994079282483985316515660881282157515476
5231752195824240776377856190542611492074
79242085723718084839738557165491726680
447197323368119379321026404140072987890
142990324878993178442412448372048589328
5215666193592408814043759654833279594216
9166649028842038742620262231117454067895
5161831862271729133294747133512158281353
20666007960257082526435919555689497918
29329831176405074758587000884295085579
80368501419964311384690737753944223
5133608896474941874675554489565335411191
861716935768709238592775788154193054858
1996653614705036238017108394908466284929
2122394230318373607017115655458281528818
933130752139706656917639059642346381753
72888325840905157411584845067443408611
1769928222409854344251027198895820907457
987416238403655543145257191800566288032
86103248683288522919239400131821182765
5577791591107836903715006678966799354189
844648239901685272988923454399861378618
12289945153848978296973902812827209744
8573549425971973806713468617885161984278
2334672037941389232584824474405945975554
95649934181345431628701890351899094166
8404315814352807649635965070288115495653
3143649324026850733147503278889172116928
39014065865829793437936748171546988020
7695366622718610690952949198691796998180
329279865799808021507734852729961584281
836685691503183504527233552978831408961
3603242792276208801456899067406126308642
98915209997985812667618625712860957380
958353492171893073155786540631537909125
41386863943375699436677113525176853691
3431713274127372357398943023124608567
104481817769232114567459583429767115132
5314416677729791437250442626210489561718
148649823167696336312025797116554638400
3469946963864889207791826458511075041967
604012080541656275045447940341645187834
5175777427422234327042279462814541639743
712271463753812195213636944794585352602
127215528551977617989003649947070935849
764511295529888180829664613377655754054
248450635108639420485818294277192864163
7036573365725787542983958071989577213802
536119615125561266158062777908922287778
165610733445892622553762699721753046575
43763413044252933734178289538732362421
60629033829572126096933461779513360733
156451026986684517374065396456972597451
7847380471471898661749910289859649155
9154762453457571599672615295938584205748
59757699983199089623535720788188071163
5875748680529021187371306744688098668190
49271729847492821424327574551137733744
8756304360863483344768706566853178622926
633613249161381583836405110221057523973
88745413637045422903550520149868812875
5176462419671225848814599780447796971
63562928316536255378846993485421396671
956390907485746631823783188525614219560
9127329834707800709339225771462251518754
5999456613162264830906371189795603451
682836417279195784777266074701288982933
177786525505902481542200157670424175649
361382765796058163892644465213077562
944174237874183787163771577354504991856
7080779340943315949677935711760319295060
5962175376445520936931803410819437076431
2332854822569077229492884140455210359079
5745985789827076580924074265983136366334
5327778170355361840235338107684323541337
240848445230282153362366251572440285393
9354714117251335334174633335476460762153
9919479189628732594128220783582936013
305608190750048279729149767825372033
6168801786252680509217092239812275518005
84197966051941295662149631803374973161
5610787829337641854914509260666363406839
670230547200590267421632045591847319022
4117722298841846165238257584930238644913
62772255776965689997912096864519394790
766718572758307411638757894353922277936
673038712387831696355108961151284102986
764958468864581632563314266275970873074
783116686734543327583707609523167491355
574547678035604982004613716048215081208
5656984053511885942120705093660854767394
978198739444416596313842344323126425359
4217988337674171602454952755781934701832
174920257599922036494422314628892581141
526468878454190013475232125786911865870
582393871609832660368387716405655231879
7260526567088339271447571650447986986548
4426657867706037087652524607771144179
8594224023545790105817302504848915553106
9631461914271134513147828311265892645677
465624627605192891099224502898875657738
5949565111743650217229417420230218935
189924229882343352036401184755838083277
7957919924316371704120187869886257797063
566196714306633040948965793323121541901
6926833951884283332220159815190695566962
688945513535973244869738161371429688400
810143991813594222992646003518495316159
783213511507121235169607779953833706269
862773908905232242158090356370441335171
2722493617679016654974807850633494449252
54362967224498544111534514974977098163
6369778452934415494964734140296436251419
9863961683612470782881153423827399076585
725494541915521181778340799580969499760
2612997353422425287058992096168010797367
488647462577905670959801915862089232313
2291736244492928301680803100424780622070
302540845798189716578494229903567827079
2292394921359706758329911482498215698
2430518678821865350241206297470179719052
585344634292005993729841128510828703507
6495580266685289600843676205142637741375
4042818788191051487554696123930808917
14893282983230052162676388573947455052
81634954416394574758427185294836484916
36329294914239344352985495599929331442
907851781121374413791212252669404604
2454862784358562694411705360251793983764
2254151527657637231579486845861948731178
3696537886384683977292471329675054932029
893293276711642925694295780593349731279
309598386015823212048561798616173513602
74557168184582656009803834030591516241
843584128044820580321511396619794279945
5084462044308649918694011079474691132923
5183892485375865759547351411241508425
3440249279864119921007754456192543466
500363929098143243671994080926832048440
792355465304967761956173603997826671442
4948728143378413156197272833681366327434
1443413376812508890841688666730691798933
833152734700363381491861793526155867052
170634871932678054593003692396376542272
9617140689495754669925069355917417693947
911598941817769833867243444832323364
5212850820972454998625552380399022035253
86418623187549892288837460644949183443
5699994635084744465701929564099251614
1279145141822584754984623017585589408115
20286345810564950595109428913498634572
5271296613103031955448324270122221973847
2239318492576928617243537905678494410
363487665853460054747037367661355981733
672177244081538714909384243275288309278
374753568091591326069728816957926854949
47683922161787066488176348579089885961
5819597261104656445495125148387416029481
88967201747225189672895519446142629242
125631602097419238162433483899976686620
7765664285238386627774883848143686995915
4291523528877663402424461169686071832754
466318346044998616287852699667937549451
9298835238882509211527942368369143472502
5255579834458873502515795853875963002220
417079949425703050984729926287208646493
6689302279774042431554715709389949425213
862785312136696364255618986698951113816
61839377994989001622166232211111283358
29417553735813282418869390277481518968
757352003625915456533919890761421018610
7138967609579876185670874754573211457
962132463072593894264091755353476359333
1964663652625501402723903496362670644897
84094947460308020782207145336454576353
251825253649551151560432931620351479437
252039707168974053165571575424083452508
77172887535126523108944488228685285539
6438575254236093314715117694742468895036
8123774625194409576636694410236078465687
674224152877343931565331945929369763066
19522733172096832572514993994819043904
6019192499740865821931682550081798601
520421818903553718652591155080551131422
305828775695751794288122634485773837
2944603577178950167982342674157558129448
252469078294280312972859785429878014519
672986173112778183449005050795330509973
461196789384542125818934480295504205949
332950408275324696510396796209214779177
18065777941844741409206569163540427902
469970617806169829138737051417190975328
91396914679684501566857297418661662911
16986702210273333262811451894998842429
1610186875872643545128676530988873857009
693265493246449321171859152131064555615
372432582486735747281255469548929524290
730496487384352288856835208265687732
86584420984547593316758752598038009947
22965370109237775781807579391293938270
69856015154483678351218122927080931763
1614958696992293771692097549266825107530
9536111997582825839838144930974345031776
941434523475942244488914935122944586342
4017673443041163077949991715907811825
20349205162915662843105847451743294224
227361275239539465296899011670724816580
6064486149050398539714911295578324731
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]