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
13685821501956043691168540558105047539
846242430944328552252228805563712266791
340457896726918082494540665421677118792
7284787333858866766199667013383694901147
467352771048966153564377509229230668411
659677487780384537531348467494488419151
68409860686076695724031963330352558546
708915924903670946827585683054745489474
910262577019725965277157310567161504760
7605424719547499293860978529496315204530
614674381969415150459045515205724892656
6320102331921777723963822288839062989934
167208226927018140094615585836618452068
2764887530307262619269729826373184481704
5246140717147984162623306281445576248483
775587405687183614693139257344797252549
326049525803289552465312167927421078726
137743784161450351691116608270276987705
98069870106986932085069502924816275880
562567162363288425195142537988954841755
42314270912529879988792742585416702906
86816044778977254648850958966132629392
671273626719226365197506556629182244109
5547668318719104985049855550916764181304
762669576355410173385763734183380777840
62748564358856690598392070704228722509
1290585850766221780261044752873347146540
67453935316310722789513666375641272474
954325072188203879732661684950828604324
569227463397906169616452551255137155219
8390622696635538832371661971473396848
92129004398304863627318584111354893362
2976227158415784379626810193781332343
58797994586850627987680572498289774186
655199578045374111925044269317409243210
937380618489184042418158591171516786858
388875128220625250209380498785782278735
10477384424786811885647953313249743153
915599311124889052213092681659847112292
11162618491898887591265828420745168227
9840837220599834868461155182807542487
5963074298296278271497542138859162509
850763543154957358717102378439854960
26108247539354824198264413413811558209
383251574358957405140894899531221713415
5783455710003287919917868796920918932908
335670097314949296169376065500168814339
2501205059085300170191433845682361109755
879987272621244795032606353938365357910
50883452560438358405251297293466303222
715337827134950956650129798157432991344
712330968156117987231753701486077053285
337587035692813150948386815727760879207
279981639785353667085308343278583216895
37053735195666511269981677595083676762
756060325100529277594515716767054231170
595847962378497748302572213359795476812
96066658751250953285443674789653832989
1094706297236480813417958720456240993505
6862454751554741250028132587326357331267
4942214334372426620148674947154395992503
6330770181596655126147557610359513914987
64316181734408526382717187711989478461
426917426165584199441193242157251498985
276053764660846118565017457688840167
95921751075591329666178961257845501125
9602936579849976615462283722877379098734
891570883548642745448521751577356179958
3290636481487064602889317694915890491577
680839195967918070413862674917242652473
1297478189846553239993032187601851414866
57153033489527785681914902294375301626
693535933525299336465615423196037951282
6803725087964599400765025570296824364648
13653606529351620625995297405083999922
39733341517349984731979657223812858551
773275303699604599436048941759581031433
8761902367744132311137086876331315336
546329114314217791574486996562956376978
4581673564676387909438671733258119431408
323682567955832970066655162114836901118
79428308552004344016725850849472506764
8331951682099946555730211969427213221539
6876870111363039575162871322735256172883
639842998259031458839114698416477853838
8347550059134397239397598468504241595
448593767844741511059501100864659053343
5056775074873343839941151514381789826215
5198943674574993172512269014512247783
1497572188618074444150051185817172856642
29731298625680743181609468310313821081
6119509312158734084384468650633735478
7204763110874715289579677727854370855139
351495199106351857533927841126359184254
969019277292370785295421873393793998811
13837043446678684390171172790342885661
561484804654195128829602179796031452444
60188444228099129246897269695122462
6530404278023100976353529806148499472243
408961849096173874863254795740343342063
222641729814456899241611097265491226
35624409309468495889571311459793719550
2026269904158953448946999129627834719
412533733519638229004590419187197048063
2113974288723318031280710318637382828
2059182395585357728037156511252543579231
787253605236139769325552420306196121700
5546407439184536615551707691792841969014
535975104046595273330055899938999095719
439726622630739257929264473614126965693
401117591763288973696691609591434691158
9547964121535841996985447010850393307992
3652152917518004522027377424280393967016
225440199839202640022802412201985154824
92786040564556763407829508988799723391
442672251066615958325128515941073582830
63489522237943992459545086728244425032
3774221933217472989618572679865599419134
172031209424874914649501432700485189921
685890789166567215288725195545448538821
939277723976885157376567633874949079231
9608667725512080399495223387200027661100
212590116289154785615653645857544185666
80827890317807846167288515798515014149
6618859293116045634699708066401515795529
26565691124046591880333429257670081319
50089762372204402570517549579946747321
5290625773905366235119679450800356912066
519782544471282170905385873189787180
349269195962814070123423676311267678840
5010621221767323081503400261894893018
4090203770129166853559965827780249896190
3307539934538635664323115341399171621
8179770612646156955230837001699253015608
863414545642814172743906295786586901512
91615947273212837523688093179624947706
33962853254318126581567681110174031107
80946212483354851598581171358262037485
93439473454418670006777240238445195554
465798838829194227539386009439336165944
4164613398863390785497717641176763908949
554868305746815983137861654979561331
9069699081632178109448598594796566902310
1597623178462605949280696286881668065514
83245076133542565479644154435842087036
1278525789775289879675296908826794977339
5295641295058953706694713145867324541369
99794067441784603233012781519683355289
19253229908989524303251862076323005490
9050879634242701740229466337970772756410
7547713764253825368495736617598413318
4836806740641094285313769067119843137
712048469576923510523945920174686364223
2741661846219314672140915924478437005865
89055561069475346035662123596046223621
2864472594298999274810159325652060119629
376557848684107464265748193248217802687
5614116451642620156735461776449146102335
80583451884674867768436662952778300321
106070353328903656645370790219117947876
3667482047271971724661209961286093062908
223182275874177612868599268246589009733
25035137825880936792164644884231529041
1370988626971412118920603261947141417
3050572530459035569035876186804691803784
803439489376759158725266336859342556828
443252237693463149268933179841893264761
1318613691505694247137814739543323742366
885462539927357297444476789561188958513
422713625362623445872737923562384631425
6218707124619512725144894649593604900
9882873930873528773712188826697158935082
5234847673961490461050273249634431737725
7641380018562416632149497852759022618525
44705873855654217013829923063542722749
88696644484029953943375413832567495169
3897563856565928990047672552599445087531
8291399323555669644239337039574857406587
7088893575029563441326127688324993417
8318716678751487441619698632489821304419
82144593344513080276751811864585584213
124374178344200869185018228783374095625
5898463667625372901521295420121130857456
58434927024276589493568198044764682784
9799343315679415611658314864408511525947
894567165447131078315519148961825341230
734858072492439989684183749296791557839
1303144295816108187876213494461273868959
7510833223991156617839907097407983777867
456585612902952257716364607542212662620
67856313705866664354772262582347244809
34791986369324111545484549475619684240
4441423847566201563035916375142852025
155814854161731793592115118129564621448
7459722255605139693621901551875920219184
77636333887823071553702389436373457036
9545444254436978453316779827620005828
8501304300436324063272808812431809333
852136676079400589767195939590511864000
5099287523744914399252991473115788637124
2031800165697863207673701732995567356396
335837380227253715946132518593091581
300869786817922190882730704364745829792
382847048073625924935656057884598778540
3388138586469158804249908713778439955534
1947828579205626568011278271251787671777
371048553728791384581306453197324257915
295895092139450871598600243560296232920
147025948691049905712718707297366456154
452416547911425211485507341181843071535
854083446330236110451657400528663887052
9442522320519009814856036734391729189598
8964717050772179187063766800330971651488
6838109017051562411332394303571756354349
486510944284551889527442888849669571674
9889684513336933638474212803332046985500
501531847081901219426867875163323305986
302641002488137410438025387090831517125
97355250458282225033520363863359586432
142723179399781988637671410197951138006
4241996784849630306812159075228494379512
4557144461577166445168898032587935659696
6784633173745513835351708714691669536
5285576593339918850069937254957660401625
9843273466435527560294287987921471086739
2815836828371005779680509739636783963307
306375122110715639052245913550123904186
2802264529981032264230608187823026596903
670510755444254592528611023778136323616
830411651537263164056392614856398485941
37083888562413125687701708017824273858
359538995430736824717016233581904418333
8863619880275847669871443968262173852726
293487674022169719212456401120459021881
9269609687349076261339094366788012615920
554214253989279240919194336729668522029
3815600059448864803464523645370890781241
73813268165460486069227511516315387088
695319928565664200289584315115556326582
273549342424459918511415780332969439671
762762165159746920225467233238490603520
229291059266287236576106287665038902358
88054878489227512429543971791321384389
73911678606434342814348819414149148388
1334242751043530449016293605474958897773
52505174134703175299654602785473354066
464231442749663549150263103763745911614
6053615790358021226602019143954765284
325642128785119875926001250976733064690
39701612610186255504565855919713608922
3428845268741930317690403578253213706321
706097378939221842628364399539633061830
184381477901270924969459155548295721771
636510361968571751817627828500743141125
7697158468711320369572397168825564068004
184778233471486278509319176092098102743
933954247773875589921956649184227574583
4288138616708624382677536367341196274134
730586338872924448019454534797156437748
9900463379131494421386579748611596628053
6535918478313404137470436663677446693108
3774783713146023865612329182939199408980
5272355072675554399111788520911168678903
42056191286244356327476927340715979828
808276963585469454564953913372647036476
982879151865461427208525542790728392753
541991893353907119365092148651619486916
567183937548565446873472857449274227
8048160028959323158128297268472466563
334824598046638132497813595924301540337
49130006194897287171532666843885303079
96716107882470356993039628422088282446
1348688260655169492361698402317454232397
3522586193631283594827935595004595762
25553546696544782806709652108271637681
19845258746964230814445784866136867165
9860499460584052804662549933156746757052
494258469707552231215828050800988388626
23628789323955773438391571231167081144
474439851354824517627053988683495775826
6025162843318598443188157769315216411923
414528115510580315797373219406570058455
774741865012420135448416803812828157617
9239410357265782749983517363828917266911
383090722179286536611561966399417886626
994180056387800122315083029555797791341
9522338193098562136899457523676019843607
893792306232836244648375780430414466664
115448711269555373219341050659916797602
489588039751877556355896843579440015312
71948184479471586494023132743859196423
23194508578786680015258126428291032637
266747244484415605794938100353960014915
13842370699897877482831683172130102869
360538545662757410063061821599922691672
629575251300415284783208171429311932482
2816131329353880934196427781469176139762
3509704553868359739941988218821112611885
9795919054387072628566894407147192228521
765631365771154310582192417625345703960
8924225364317071353549528701554289763553
1599273864343815667732127979826569462
30165672827719026289173042299771166971
6504345460015618870181815065925238152
817984653783495426682326674865036626016
7597980576919431545376926952412349851718
4016889595099341615534595491211851217390
23872983942691744379933209218299498894
63187914870737041379912288249232376132
82855827976753874481066994072195754
8917449088641070836392742723251077948599
385891377162825594448202815697673534181
538564193879935395431138226115748634782
89588500556064566344523922702208121412
2860983494358650268740087353217540452887
1086860656328800401283426227402362282816
44141741126938619236139508960736232803
907272511025580794475809698658655245331
121130842611961367896125308640366738547
3131907581345582509299354835159953589931
65127089520326048454381550804938347065
7104411925822117267323924475773016046467
1835373465474112620196316657936733542556
714725488822449652575601562874651723
255888175919376785161831638597686996779
36355384470486529933687923261036584
63387961650375200585373315688183082305
335630195598945947445098172636934565472
2193872655258589973967961849209092664293
4674970862699847713045626575621221376032
42104674230169742262686954887989825728
7366259995664102468633941464866658758121
6912802966012768742263589159966981966806
431114563764620038047141243475693207115
1318835264566993214894235325427048535328
2629832417638032469661581625283266742124
35406557687016936307371791099404223984
342836946550663426099544675985921131049
2299892997281456635696917690421785056345
677310192447493735754194947138935344445
410656821085470898321689002192477828892
775944231556799696862811426348433125198
23283188211707961102130266933614287813
44292767655265711020107955182835339998
8669368315103271496878298759692063277
7854442257678956463823818877950765554328
18052132160169207031501110357759839152
1937182571714773443346338720312099884437
214758861255084741793048893768980789972
975658873011672340132677479416650432873
7412156031755549636672614830225366613288
652635823103681614160671113818537792583
935173301718800851439456012924853403784
9215730618384024922084986162717476643658
5973647738037201183393932253947495812
73693581677182247244903753698562186580
44428360138434247672416267005212232524
254693771014604471652276793230980307785
73618541421655939853778582050133505991
8814451386668214803260579193879845101849
5079668146886525363662902442902545216463
830217951683256129875909166261727194
14186327139561623917173324959716263755
594932775219149476406581831249054722965
869227915458862372836278877654482826576
7288301667482834426579225728478185159005
6421710450583696975566622404537413106928
233855541489280681899587727371076271014
145041753738308283735859402282665793931
95646551979467342433599928364225286452
7336731055988447249221725045294512237637
4092876314916046291717254232148428231044
344967640381416886141823031987515602
122177962401872963481995433312839685377
338447573807935987134346809147470565534
26698448469324228701694221229364410342
81138933725662084914365737688176916913
4816841569325525200037402713639570291688
6045434058441795445612892668293817171392
949714350377289236439745206301770972371
2383798921644752163911298231350866983749
1389390847159709651488219721963799161
68405160813187146805975863522710902743
1490749523448346804152352524918234637602
856673315980929771978568262548716985334
95163378311560412052330462625714665020
4325620026421317628997554450480864925399
62585445504751873062231259308036992847
60892243895602472242131771440814412354
173467132668124146996972306432571793318
93337402443534176959712125565516451118
553220317635927392670166757995697717015
2142598490722519106734227804253985154868
61332424879224119020585744725368956460
292810306383405709149493674725320973536
109880454955025526866297169706864144322
21196350609274489597934461845819993588
6867992318185985444171538374619620294676
58513432704129916930015969460829332928
234026497833672605312838401298623685044
652317313052156552435657463666456256507
451549155843182814908466638450214473994
66362393620699944453504876955903019933
908191844683535390242307138756976021610
266928021132556631334862543555525742093
93466559811576305980921603121173144942
242588932845140142256635738038493135521
6306712552602789890566367540992812105348
289862968962170322221214747762075246826
5946796864896539689379778055523028038460
37094412187840134013993238650256104381
26471549152432566825709839410775014674
3757704043181474466918383148876804378
254810541223402118632845714613086956662
326497385806677062167538564872570741681
39598839280824199943670623444936019396
714310454867311565233645992784284432621
597646783122686078847903892203163102007
2734603898422747656487204027799267002346
988155111865614796712748639730462146266
452870625081642447366984468994851524970
87485395392472975315895779374052267822
203640993116129695162568263597875672215
7489783129329500955950209930638556429559
4534527726959666148441949380246838347394
6753313522994173588032123251737262658080
6018177192701797580549203501339987944591
6438520163442258313777463259262831899
2733298084447768536366849797672473916866
702843726288240298530606801977220438300
6763981816827776976593251694145814084792
85232116251351969937144448315178056186
8121464192416394564306201667231285941
5952247638483197524325887878788959998220
3121491562343371137247695856127877732392
16719850463497047682614900640831646659
172926676003664277519576989746922041868
955579319426872110244004006911335608228
7012398929213566591131731162542206687
3080451194747238777325878539461963485679
963121417980921240315432110319317056330
6469512225397055388543941127948462965216
999017128177181883494448083467534261538
923032499802120395544807292740632785695
140822171945487136866506915697916725265
7355941140575586513338723749800359372607
52238567713258602057756158233421094291
818634665533574283809765403431179956464
2956643114327171616686561104184838383197
8965521074239794455435702066731593101556
734182686402416323822602131682055537315
2390577944623061663954375591935153838
97343565978519660054935261886550318750
6170770495039436102272759055504444066687
3244499128619567645468984791535415444329
456161556140603161692873745535321299603
458862379872498571886542662402136095099
38283414486982571595395854922205071883
39962429135575122476395578915895088347
91928737078053343819534906234969488919
4554970464562269266294387120320116347982
879175197480538791370011606394046774323
3794983282549958790387957154980540477455
17823362555444575314362837513405209
398323836443517616964006565787560765611
9557168265477796683093945472124366668049
428422871312240194334842283901464389616
15742166305319368268680531975072668582
8336743615671666590885226678550753470
781788369153128854471503305232484541553
28175780557737124944159812915798489700
7509142430301035946968603159614967047270
15251153739644377588393891465756868401
3172428242935190628150658303797836757235
6481328025392026152427185709440602083
4405582297821721641060247505554250176483
847043598515243471261901695600451403779
4459655083725966197107922602543901509
445575816332659557192392485091323216819
379717211133316176644546929073266974148
730538997474456787560405801825698962509
24917007168638563578358330170094723187
5221311649147352233695849049259918422457
46082253999483752844467738858507844
837746516968668912151925443643926769799
808527756388442326290863229618036194210
2269395098257172645274274985326450557946
563184908883544951034718152809230493743
125371149042770835757064771995886126557
1504193139041363541291498107128980539071
9090317767338642435769103946583857
421625733516636329089417296621420079634
368430388363160732397614247813450067120
1695414844299208836360721954122185374295
899789223058392667914585562135689311789
7811455320179133504683739896178788951276
853256713418336583922949448657621597559
6894111899999197644516444481604027948387
744814408275457158585953247870441167071
96718937456726828047485158661681187216
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]