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
36796872512760619113348550521884808334
805628360987178973323766388276088134
797449182747738934467249954208970031356
447152651630286437433007896872634834815
2352240522522691961870463934615256513035
397574297440115993614647486845928818228
91558663272300519044617719778490868274
74575052261221653892551148854333436573
2945577029807215514031243331573364271148
120155736853594479374000425388506101397
436472493053825230025729767892762215410
462855983177977120756886295210261508997
702467577642988927278989190113020669451
4229605335647892626387147914969178857535
802685085277624742388205477954246006623
360420457781075304346783330242704575
4815791870614807946161812759946794573
145181654327308394739162683142764009308
520380675505522787320462296509591088140
976465924858814520062463122923363581214
4872592568395870563620845518669788953594
431338966127368180049704698960321861400
5901296471698029984257929419937094549
8095490762686408248118827202129278494892
6257290426864461110011212696163336614631
395609299607962869746297650941257353129
14337981798549529232212675470163999146
9157519646449004917812691126889553102694
893010727522856648502032785964213644137
830663891037601321041951548744535667791
8591757548107267204265648021470135288536
8662998773364580573832196970139967536651
60555114740925867457663906685812292282
215369787221702139519395879399356983193
28381733545454497023444911264038381883
3878247753774674228012741187161991575074
417119026400133070347770109670325695892
860087501445010410998099400152016106359
6416882191417310296829818459600884096256
490359499284263544075848803313428809236
9928538560393152420112659357628237857659
418441296396714977870292939443894565683
240111806607520137736535883508470195365
489882433578541397112936336815436617810
3904574314157733511483795851815469253242
53222321350195155777551464992648291101
18330506912269775660372849535338947003
76624423479478863577597933402997424278
6953279741201312542570382929311453823920
555733509185318538932731300812123829285
200611876774116319277301059574518349821
1333282157382496057067698932368479714
3618897353523119658864977194379768092260
15151513437639853013375132728980576218
6503702963452381203385834388586653996158
4225528697864915458193237196648138623240
834343519489716606870974893411758979828
95727899571288464887030951963016869856
3490228234285601367220769582639143588678
52209293994677497038896288372021659332
90794718962785880551758566767157995416
36425877101786022998987592462302651937
8299287365468605358769544199275783689624
36277663115602736886912218862948806660
809834958822151584783627615964319216696
5181332148101062889244343943130468455
9026882515612183862640522598157144456006
5831411360736436399446786853645462321
3952869579421221363541912902387027616572
4777983365243625972074164122580010126878
4390760942627581681533725801154831276366
226429165344141363687656281317591745536
222915203299477139132626480533263086918
751313285331322161559983473011441663
600217898659247544861743617724431148
306248809961028642732561528401848638113
3618922668362037817563531765918832829499
10317792426921762075079719252551808709
767758982509372897969092216831118702947
4483944134126091829813382682335721203399
925324396773812687595746392977569163666
2595273865969226188694288410694738532266
22962677800791852512381512017129274606
47046912828850469982925105775555844204
343727956463270839828173784177425087531
4771842399351028803396035485254464487147
51313128319163206975508826284173698522
2157727864649043693428542895282666919407
9338862014055750728932992673486746793500
5656372869661149851577091129949858145756
3903609967167525982946326317365368479
763993558376211705021340758804821651
4555388046711968556412295920204985244658
965411548084418129733246007655210339988
7398105991831230159770909964129557156576
753447670375519726463206429161050769294
2293651414807651694585546422505843057705
180816113963810138112083801823115591373
795648757871630465416581999391441047336
7221901452989364717857244327627856632449
1518667490945517347808294388320918986
894161441554745278585609231760293376109
6272860774894904340937805404093076802
536865146529534995858446888202129022582
3992290121443858228043403644183120101206
7327379348975770666013981867425921535
69334653890165929820445678925475353543
3295478249923168989261268728855674384541
629441742192248876785341396058871996
7030657158656396759291426423853465556019
820527429801046747494255460193133062275
7878485520577474448742442826484013570
611917362633212916722570751565153534599
490846676313807840764832986343636014106
6022944524225442299037397018449938971578
98266517823038342322973867556016673281
823457739207145459067927657153384157526
3908645683016703200663228139481343583830
29121565258839158459355723501953441604
69764688366679679692131385322428713578
719774528776337689026324152661213876996
4823870150752218846426448215107688492588
699742476722638892055128720855886113418
3352583483885165192572532328330682335915
980830882607993678717927474738325975181
44645926674372795945283869797985625198
1979634851794948329584181261489127977904
74878459665254729931222599468518356518
8821640858968573426499479938205725549604
886991376314826725616773233420110774666
31002061130560649457853822154652429125
5085276947385164660767533538337813205424
6293202837574802207986750373372103
85084911530885879378410685948114889168
4043922440103021387220857419778553713014
336225568814665490355481376085231886704
628484303519974762592209550280890045505
62838205597983335424676658932337225411
4849142527458794215537654038996386448037
575525615859046339180517890707484739190
6769564057416991496982468884786896161100
408615063563909378258324995730378131112
44157259615537572823109058433246855873
141848305660354459804095531391252102450
4202320739463295320975112908379132859234
3225179186684589122653518843541649646142
5233764520587947757569821956409116623531
34195836718514814814417996751017672354
7874522239499510427540484469325379724
2281878947672270393993955988991113392145
9223634136145937923492044499888636425
9004806194819891567976936052947492549433
924936251193158280078414145587042134708
92038271656612478804841904411181634423
221357594872221042409043593259061825562
334652055560456027517617914725511823157
90734665041244484527343373473418364117
3059320269547053726953006056869711445187
3382216996185384202277468331528171496262
8216831328449239802124384802312353798056
331667424324769816243848741357897991487
26448547585929337732670328863236319644
581957856649793505278586347721674551615
510119358877981338102381739925523745449
296154735720738228819144724411020114177
7323346733106922560134095687879415514
504065656385756869556166697696464041989
901138706337022352258102510389013378241
4615894098311265392084569984866852973926
328084416849291969351136834972489613770
22725347498749983110441766384187259290
7803160442759016625208601924978095866
2666752610845219931230623153413218448026
2072516581341969534853423297105545397311
6297209078074732844870269656537553764761
85637417758238815742545260244834501758
1769141545175954308493472759652086482402
336129705416843857637596743953895518525
8515425371869428999113874863558118506165
143975837049883186233631502654184564805
758046372483466896170496008342692008083
233659985286190851795738293263644107477
670139476733561683324760955951165121712
381791218638413572942429542468325753246
980158561853342609630046833960533388445
6781102080302355868635774010464044514503
176656781587354915589480232670511459181
747133750371862206148874831956832905808
7229954217039562321175127483892081261282
3479499153341653260787172133394381069470
3813728016591229838449659630322870067132
2021266350318142972388889870867296275435
8535544683066926172084503406997566611569
72638162934799834333432936336722777061
6014585545770135945274080123693217282
4011762393112228143195755871300017037065
548397407744983650213977716566349706423
1153298337319236786916665874724251164664
3113396619944828473886147338504577611340
700454279632925739736369712875626714440
2287602274232319176648586381365589774103
11154229474868681738591519534057081968
58716080148612025383644030286691745222
970449795868080431055613906877120143610
242532456798362126961367775846976445054
1218976889891545999156785154495134088607
153912954953709273031079076166234768101
230739761011914662477602925180157684306
4507457773943475113186469838624817992922
5451617312513917441880302739540949821327
72679521118610950054904022311319063772
7461964151814665541371419171316014506361
1224621247821452430841018095533082532513
3380443732433526501913509967888977195356
1522568984518318263582939719529161452965
998716855643004609745109560721073752618
614868988908737730033298489635751352712
437539542288138561922789571999062842017
6867913928181467554228754080105557186868
8673552917439738455070351076813398243913
10157823361528517966991284467391621955
9169196855902941733290569591835859451134
3152162557182721232657095011508435144323
905577728183004581062916341645326318602
894296641483560114438837453275178855988
161835425917389887236509162307883724528
4332281546154821671956965685343581614882
7017507397386283787457728602143127779649
592196058205787025422968140977560341516
44209520524103187150823977537061011443
2908826136492251575187524360772390288588
104219182929306755886907186814564095193
8198121740779853475854149753679714039058
786426034569934016269614480320051065924
6688982532441562169232139371255778991
647058536165935015836393873924693815641
475887142318961131091233232167001643564
338437678722480935096530559625047085462
42859770403238823455019516844948653755
4462979953801100896446125630337087337301
12765048898999034479462140190316103747
6508686065204756130297623561624482708854
4385754092788042611358777910842198228624
9943916045207259488023756855591256257775
698635331585545885245733915920228555980
97957743409465154197560918150228577484
3717792033299446963422673870628778947675
2620853439481493821347332383384428085989
291896027512841668362943371922860009448
495670812477317155153129792871745596112
1757057512698946240669929686841596887
939789619059155814514825946310211015026
21598690690179712816328697218129487659
846119501525511699557513572570872628138
72012534360796112312552602864055596625
9418740290953467277878843281170716486909
618126734648707122471087188458835595717
3529146865543303985449464145305288603201
3663262840447492842131867981047909071
90369842963104728111164923991598297763
411078847387861648666141401732562838405
2561676635399223234335797122471343239031
8189514358834305489457852400363231227685
994830613495665022911101523140274322861
2772363488282397574152348545774433129617
7209519150597224886397099716234045394162
470584761941270913010676117538051306495
42247410425986557237392599509288243752
59553739145922569712499435107224142708
738363403057707103592398812386566888427
1813864662651996578984707531575812328336
5936705753028325564933025101820258756
919010667364855123313881707793213967027
413447928691688270781188126113017582761
300776559174077739880505702150885874807
6957149481057471469775794441860955035821
330869654434816533229343223221664357898
14315652900984327923516217596457608930
999318361249632598089658557452960375286
57445343296103596082675755865117738975
6160194049131121190940679199546338789910
690835767114688680145894519869840099655
337151854024732468574647217869665031269
98098397917356691744679800908453247442
173230256975179742016007282221855702771
999412928898477740951777819819201399510
437368744795620926486085565251263917620
9876704464388464175375572416918840895977
8897389581597241338018999691307494774835
536940895423560885828695784198443694916
2925822291059191746482258440449396995754
634265304268632981380765787672820052598
8874788597473727987753751243634059918338
9032709195623959307225727017641760464741
4245893646603653652476146288590767474148
8126606460611357527255749512486196483208
429486474648399969153888613165544322506
752171689181274682552371332912371547950
1654363712415468639993392842306482276449
6487742223603023437145720455685959382
592795397044745850158348443663568508
2784501288061201266389488794255313148610
77171551793822179638421984162247664204
915370092670601594816684497688178308905
952529489382582815812936571982044455106
1444834256938062157160303551742285744065
541170786110267113611376839892825804180
1056301359949060498376141513297289912363
3400766664074133376768255668803680586143
7395433955333387213497264699615839048467
1087290043549074412387817714877072728460
3610116479472983292548867419311116863139
5783674112185463814123291954657510124597
738724635975736791417373917346362376425
965024231574404882937526953649346171705
3418108728704163572625302583584111271078
7308836533331151635142602713690956371871
882311078688138082429705313333915027503
691123444716114574648165837639199105482
410593656081440399169260478590858071
810815892203351048666780442829918709439
177919554871575484963376616826452870484
666421581695866017486018256827227971689
396544923504029376892125424360949516464
529134174394466726024319618920241529699
611613624972883596697324278239361919562
2322409441932913991792129710087737211
358585304785741065571559701283956693616
8592606530360196544553708466416209459
257156215785427015922841641635261306772
97359026228164473569136083957771496942
2193878655792981523799743058424967123043
2084981112304641455078341961776917751441
2684107396205315848917432788676636773953
3912650262968591699928617761938156343373
101946593899118171884602617620229272409
59574516246188866637853547130007129631
7392187759529368905047459391127586352233
249686778643189099590908206494586025786
637129433197775239095496001312833315260
19951460855834137421792716947833782830
190942443789171952747090859973496017
9365442821034892906884704033644793678422
598011454716677247056315203423639728156
462136646201603239485919522219788072744
233255399296442734692921647129327157389
740156506311337486715885730277270352607
565457668904118341266875157305145786309
53341317547729284738123774259262629900
340780815408865380444476370222764503769
732946836780951650071617906333535394558
5162964402680194373296521807730114891
4400748351703186930211179883646134426176
864219763135456699255385484534229976
934380822825987667145350854341940248737
561995494816626381759441431354713374956
3812858614938833239247564808900873089937
793868657737195862228749677435283456385
446668673356712021707568332832863714386
8578944910974578657328059253247328234593
369193083871920041273352367744514516277
76107176269594194111586985130293765570
5604564527678751153479955311283179131979
4188528060992275690528583741308672245559
977061363402611053935716329173463510754
462426928707381259769632822996502806444
842657121796347775514662161020057498838
9654162417142718516151508649683455466068
2342151364868525381843499070647416860
93477280841204232978261397610241145409
3468664162057560250671246600885516059597
1079358311148320639914307211781358853664
632788164047222990352529950111067583112
1695881665825478453370816412589289706604
4922832214065178193102843666218021253
41927486541700384170569082329374489372
2126658457454600489814847168694472692732
785898590581371774371634349237167323621
5717151099836867539133048200201248193176
998379178900508270557048093602641193738
232146914291133732163893666436989522024
266463808344904024757888176886303410687
249755049432310335143027665735124676295
543558190711917150615819275826732723004
14948966858404711415054120940836091952
9347729022452043219081158319488711874604
281587046572559220016753606454355584264
466961699514829238083352267388063112000
543019081635597350933009884671862346580
776020967797507684562840620266968733832
13137097754427816794134763905356656537
5290870390128387139181095638551143433649
5637958718501986433254392898209122436807
32058491180411583784165111312571333390
428263627313128758418546519440436996646
8981249310472958574177415449915952845522
8092324653607605948525825530294287977818
6424190166067832529335764350169558525912
286803999191270948990411000159358913361
6593891757619540124476077611911093009464
5777100036329626162782679319178190662772
5802375588958519917350852940269260919704
2199837489943983488565689832585043984210
716760115955256989128337509244733861982
223291362363433840845526202568737982963
2476231932927392720890566096590731076896
20268278099997738749136250390438987964
8655432971418819789094135231031002001
2516967733405853392371059410249521579684
231287893841620333421565906625270555139
2943254821395160616619901495673396146320
3796989295647574906523459685552864322459
199870994818906282265282084595299955079
2178855736103997116230243396387675945023
7451561353844959292254299972111112098633
36679893423627086646924921555658447831
594214956395013620583284445459147597521
8764310055413200206673242142787772372545
6104113914193349630668709242169676799848
9339720828466266168883338877367624498223
13385557524495683995560492466021722688
685564119089920160195816661663380863411
775061624896772316386057494820773502820
690918652224576651953729663404518206214
6742559017373947642151193361940433368620
5619574424967439682131542537686154359
5186493471375527340145807168696968728172
230838809297216976068187822886656648
899444044248335624961063914150443894336
744624282112206212035169898552498101265
3144198695984726782281356051751046814153
12132652600728053431198328678199797140
625773037205341118567603601147583328530
263261976226187667479431716135919412840
869844150047952473462969285626627436
165237763347882331519337456126339034657
800525112887678401884193272206354435129
709646236195374549109801737362077582809
20002222788287023166581551015287890813
214333955914802822075508994873086549761
647372505243930951596075343730530834739
75343813995939588039327898555873648828
713961104500691524885864911172815123310
27947337746427976538387064407498891468
97779869173386591517163560689521190621
102414118245733407067449704294195668127
77021731250643198808469738947544226010
19433795524584765457256318142222704140
5387840324855408111621749738750919795798
14081834201254715151591506521871817430
837133405941055176943137603978443565950
231636833883889839147196589965961725
325118220647334468198069247604920213244
148744539152158598213999275112659317373
584567726550182561776112466530093342539
4119719586312616202045528557357154482942
910546398026515277153443560756396486236
449841767309922426044109705468415521882
555268419731436249659325791259227067142
3321107622851271483477122231670039396170
725020171861703450256150262221665772428
54144800519290267862793825127943274665
414942639263997743326108602400820756773
60063819331944526395143059566339305360
2162461672916000949391764458655037494342
4863574944666667398110718966838213204
1194879178543590569078312314978085281754
2092601837545198482058545919569854456405
7627981516626308620449514822645764863038
5553528430008001686432922814897513166108
9539295332386246439576105717944343135046
37155132692763251392333076252765537049
8795447445367051398085809244704748507
887336047591350921619313173705445604850
850121657525489109941911948499460852097
9397685954707438713479832499803146533352
906576042859293752857496773117833756070
1844693843504565785336247575968929679749
5358542527366260575576834920384376567114
7612552079166050987253302337783698688221
627055786357610039596372258055218835540
147817025586294233795468436238499225465
522854754859038585299396675894143643846
5623860420815814868956103637476728727996
2617451020557598527535469547716442448605
303346018288384349376039808132918147498
752387338510442030156336892279129598466
7162873238264439771873325287216451851856
198212049512892165605319259069885243956
627320336838711426871650672521219443943
502918338699721519342576244176761047650
41179999500856965072558430879344944673
1570817141738367702719996891516613726199
80538488272011456498854610336641756909
645337492904935931389308563383945432159
520480877705638681163662495597890138343
79672731286224328418814749683890815240
105970189243509219787246277492119339204
77798156780079513315614178226920709896
35551727671522691496942654602149176629
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]