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
416464677058486591361646134118345046311
2386997598338606911233076369462099247682
5855106382882423613004120452729535201
8481339174287422405529148954360878752784
1084359571955623872149601585963467146010
455660409709229114952308943270712382055
54121808085209522559794305215161268874
3398869158151070262014493171699690934710
60671603925787851222798820798915845360
169424137634943831097354125838582629283
1553842287385883574882185669418373425
6821719677162708653286563962690524484576
437956858941882874536709763986680638258
9497256268001208627476698168817558369652
122221011215588540322196761464995944728
779197396135679989354497141713982856246
1821527220038998190127625003579824479949
784892171756273619523603398308175454694
2508929780221849936839591125906858021023
6121386369015490776742099242243125657756
11595988993182937477383249938188524175
6432304622327586664763568014892499212
7526503546932647931248609796927018222082
5628101245378012427376807968901784062258
7368647837791627666964835298108544091219
9599418123797453271987873122342141394794
59581407958440376192264538461456437150
4141233219963378867097889354340590962812
4472726393624718985169671672276160836828
809929248309867493889691329771291682377
205015347205328570864356818218617979459
4034941552964009408998183471440639198150
4147706591253736382579865575823583714171
527235265703268212419336619282542848244
7763767123106629710398446450619377329130
551298232982930543138896125408157015398
571361608477709855645325969801965103649
11946571956777615414971516949365372989
976193393569491061104755923335727295255
7366423112597318818740241657537272629935
3610113744687336688963447614939429042366
6893715412382636408624935007225140627
679425912589893915316173100044884763637
71964153420110438897793843877591087671
24459361300834266196358862872099849996
3734137513342147964716269625909897656364
13182286386565944720035430656863518257
712190887682701550262151749715618152900
5155642231228120701035374266722658879798
9546773707718736277282140552563053294
78821520844146927564577269166968547255
3619507697669226789753647311817546939834
7586724391685665905215412525583924312384
2742156236379314430118214248120731959167
5113427047577240119546137891315186496839
2603586541776335458173665182786698244168
41223107518384475399131464229768125011
3996329024741501453847468562516785037
1180478985803616698996056158904444422792
6281141535388122680027889637698098013575
8430639965283189367418128641193199673116
87436907416492122876001260742480651220
1019807364279293561563237976445589576
82256504991728907125666958867747269717
18262722309156727254205772480160775228
3796241626246809539562862027369655948159
413232818649944760785298377949689537426
763977456449713038757064147379043254271
45922269873180733160999375963439791402
9881103218196710420787264403238688298998
7987405315322361691476446580747331633822
3756192556887812956921141133562593259462
517456473052133302494291157772885433144
67417829571512622113685286376575583474
24658433867125519632354552529872747
4879100001188617105782194335439941917197
5435777062417677988167265427377491102836
9313565536151727177311426670753910899673
47199785461844111325496476048282900884
9809678071868690681072141420114407507
54416757943744973281626132825764413183
6809821911416710809910065268284283348383
388959675376769430896241415214148236808
770978937937545198076584611147427147149
13116672229398223110541256776874746753
4184939620376825786363161767514497271
640812207191932959989038085707383806255
1380131134736041541668079215449555375394
21683962327448158193055986688918249114
2601888564238441140998199387436864536893
5317696389751490744021814752736458259966
5275482825242482229091887950998451494905
56117209218706192120465127126022921354
5150935963012388536622388992521733447019
5241232909124051234662965219874555628
95512841156683115248622157901984760670
5760574997978295235865684451856426134496
3262632257673654236714457367208470828999
262429404412510305047356495853991798040
8793956942776264372236218099789612829321
30023828749945818963999237488348258286
86017351266126605183664723134870924001
21759237924547912733929533322439727018
8324924351321406921856647897192651458558
39409306186255168109637540075268025856
3827329351121391783913201549280530889124
2347534293236617930620081112368554777598
1888738429691533931663016017377030898472
109432096507753147425884271149393913143
85831711413592421394123756993074182222
47228959417406368898172132522374926718
2618770752259702118792049053838025752553
7580687828771051969765527902684465144899
9548127411066197198770679503738259276109
9214433792496443772597971317767059057014
476668837476238243219545653592244890
7867322796654454581370543765290282775966
92224305200784118261335457784467862286
1314340831305918400821947241860017147983
564780347367542684518734882583674129557
612779324812305208574416659537294896
79083652560321127673211175092811198795
422347298296695577333041892883311332058
5636503334773912968079878651190851165915
20609161340682092881472588997338303494
960985928415396585948241851166694353860
8453276518542926164487172688336024320
632344636109651772962263933339847227101
611615511069975092787363614749487299864
686217047109202881168759008313098605412
41922538121556202634467409418288951544
285813789257261926922021163864968392493
394759305660871650318797603900238502824
6690376984684250400449124044475684136329
826286506551604862173280477458318309
599767446084684697388943550330134792136
6079913787883387633757642982568488167571
4123905553875831197308367213353012473
369645642023805173441073226795153958703
7793570520454675194850309451398967749944
533791713800915957489541179563228709673
561172468474911254477602784386585141336
498881348824926297022831969936594644332
40217990398573720037602999373271036149
20675231216750738524392566685215998293
4888617513298250188790327142237284691058
66088213667733268761324967393317081167
467383473465696832682928173320241672502
5781598284012224407271291473289519398226
2463951417335482583050557455655826461808
2716378726069435277952263325166929308229
7601525952585993775954792958269184646647
617489174005204751959639597662175439881
1859313080112752282723823068128338381582
886214759721984962713509765573848623694
204445343168756527091517793957472481689
49318811327184146160058330590835743671
6124565047818357222922428606741114503
6947610369794090556419617508607917103905
900835834341921512650085373587199756130
477496959797287499356081770742070821268
950381171586150893261402201567582799377
5253332667218681379680768999206225132990
1896390588588731146886532213320115634147
60506029712595439081028187849358016854
15966854641792810364287523366773377679
384293276215103853312127089771353922987
79257117345734335423255339337728055154
8861969923686918661969604561727556162586
36583479666525316321356280281724926095
50771401242465984269677516315653413948
434763931400552593875829856218464933776
56681282754433555986067363358737021518
2846748448217464899913802378286640088274
733689786902768438288157869800592546647
20989390297419548885603803647973582443
9819766782274557971268351723925995953
9277257732145164164435695479983328525695
1766706484965188985668919277395668242942
79371286788068144670841221202390029551
10176383482956563413556601182469373390
24354658466181266178408523554971097846
2172590916155299445121437491889630332305
13286467528343409267600028604799137602
415605371627126364499357989984783087646
53873809400208648469532520644887794018
194928277499554819254919731307886598817
289442129581368252434743274773965073265
148755388426323101590067815648915433682
61131642702774953444159318806696038364
4658585785487633395095051816601638622542
832974461954125525177246584118676512006
612362706290136877516556676191183891373
387737597673382172739447628902448586493
1636627993316244513914653317106780526967
4532691956448220908119929945690744149
803461572486852931118134594251151182478
3940869755867548927092189587925088221016
917863911283692630735738192722851002594
9623671746581689213723815730859464422342
671737186480532241893042612844641949600
88422375774472596782674523633352853776
915299952248732696556586072783510239759
59492319744761873476156631749394114425
7224629077898358932624374358838063583294
6607774724175936986155754478705782356063
901886986888177385516639404164332229663
4694429991322708939650463596784632452332
819065253398826724346087802939898173532
1514448345046421507269362101119526263519
3924814789132444491399332450272127743963
8757596119125705208213591647664682702371
3934502885585663465046933910683096897461
357080427325742918589826322428532266888
61765166683065593980593863011200821658
300037348214584874877529251922134843294
3127116863887071529435501966149966478967
598692706981479869899736657741643361093
35702638170592944642585391253143964223
3052188123101974295717014942840561018831
5709252296174473586982439716118236485303
946112828384652757049758123785124986457
371278914576661207162388270992846521546
55084487826673691561762130436522815466
3156626798136297497028333959150186124315
2022102741101321886094451889976115689
2919995973475777780151747258151132731386
9536448330494984776724107343220639764364
9879030194111556258458507121454621288
545847473863476508745827680417085762025
2710370752521018899896664959651917846065
8662192566644537239124085875297040134723
6912453989344621194585658858669464063637
14553193664651514496293567037282893717
9698483039243680388287972775934872495221
178438121262557060845667990627843860217
2365312181627978102255536091077292368
6883495161724849376299801141442595146119
108160254967066115660843485560074023904
4447377172937747519593257066556473352751
311244903511773343063046286866368038521
1891964813298532726058915149689650171374
325230066209733627028734974556431437993
1784378540362666926959045656334493931744
3036889284978053608018702104468378185600
934624028164459896593463455530835790831
8740320812253233584498338981726319342
223513742482239968374788789657684313217
1052881253482017507893602125996087022168
951657951895049812899088077537260461648
149549216894406492613807852954782348423
6528426139877629101037273955513330524350
59125894836991133205709431698050641406
2383409039601842820551129888153330175622
508970077186980867996528356743774557379
378115595717494880177195438595389234013
5598145495016057704187941911299865651766
5010955434063988352225991501367731925
32382595390296826029728401262685164787
7247162591865385625677385129853929053080
8032798468159671414182175117895191642829
196844145677557220614252694443459793
7517959618825647179012348246791938807233
6818181473465762388570421791586928187808
2306482018163027844215791081503332841
39272879897564492782699422665836486928
7435696931771579143791215459425816073347
127238764012473615407358380385543763151
65231779574638422434883175639258406032
66081530507782892216636920668988846559
279795262915278025717218424856733179019
698539594115611904036405324188924917190
8395739674707506947069784967746481387409
33839096561332892294436288068155747545
955532111815353227083104639444225748727
222387443551378622990468196234130112072
659817875511104948319429001606324917274
2992664066915533685227073050875151634657
43717675434690114498279619179424278954
846555807818590730657773101756124737135
4762558098552410880546428876187042159434
26659993388558754502700152118102444863
960997785758274489773911599690743909062
318891991920286274137842413384838391495
6231361476007987892627561509792123753557
242950455338752848037323675291711236829
6479111492536982992311638523744377392063
3992581160052351913875387061157351588226
74581487438485912121892965771000082186042
326913094245610057894208540569034449480
7835796015063891719826967927420848448718
71926629764513413397683771177848083886
773335921021818773216472481186618798040
6392664177152945394325739239743660409186
246856357865820651422346838533883971346
97296774835306750831417882436693635373
4777433240393791548841584694475116882323
2176680519759948252389108214841142339512
5699194693431450115861878262625931784342
5658880727023844255446104807288094221852
4226362874169650906429243731306685866417
318180279133822390337699715912715285609
9552134246931365302493852533495568548189
7355943714795785593581374050333012223542
361375538742292637963091574702716785438
558497239920432151426617624555836224264
6002990455832768182031207999416074678349
2060415675188659232979966344344423
737126517058169264050194569468783891182
371038008838979011664108241947685129219
99043049413933068031054509824458799973
293160621148315831991839052581234515663
4557156839327196719345741733258547688
339113256691178954014947617200662647312
69747431468210739125267096537153589416
41716312733483887258414194249577856749
2017591713058801256652312463553495916493
8610404296653089307531637856510916694126
5130377612716919064224044863729257402
693184515237342154478062552693233783922
751894899295970843855354119276842601746
48941274220185183860705017811750517963
3525488821476306856683562810734348034129
6605537490994682797367908943853018044588
325377438805928399325366245777366172003
836695142229237368288282257925212474505
2418704786833420314376213844545776727265
33929284653885267351495423938301055385
4774525979759476455682384877389721056
9862440554101452559794694881123151218
19008676501288884883054726516097902049
340584562956377580304187701159214462547
9934858431152021969896562815970826334504
756979296349212993959125049012597512
9844238815167649422088485192932301771
6442603613529926417343904120904716423572
358560461485719504615143053783543128719
53597697562206754923933894724548644126
92357428814340634443004682255275288873
490234893784119111762066542133876382469
484142575176294894431831679814632312547
76364017949034936156939548554260658840
19705922864263160214898170529880247992
2480858894335942967265676340889229178628
344094686435130469349207305901419811537
341974760024625914444287632452480481849
21946910587078326528443580580924139605
6226224869227305638679032640564774504152
787846394665807813644538356538718895291
4082820871499277542111580893919137486
41638571883845335961502318498403364432
738271238755362797965306995161317524191
3133311411613352702526325364748715958838
432416239447664681746971276345081865460
174513313947738425044731340946442233572
98297972762116058671112907775535539522
5240101529931275506878538932539587787
181180627949583868326767499945655713368
6726963349052207450788279698355249243489
230653112842004335589438937614369722395
7650243123125665134232293149729197208095
58527416222443730847416989687782948112
9802852972495164263274064071436462208272
640199453759469986191943124766953999372
35974703230437224408240351393222573923
147228090307955254015098592417060852294
360313381855925423542224525459753855658
2953124324478228906552934531621865251608
748223873490103782921000682168873351873
378479895925954720227629581220173065200
736821564262558458661958209566575693829
6270338079732279057746338621727366802
8319502629716644716662914918255530585961
2388617598424884499275017765956056688189
619693595574326984236344774126687897
4231334324491181634021662585282086246
8257851475394178735426822226728399898600
4054569196275757602479473434495272552633
5275830628187655362650488115568651324320
909728137824915574458914137781226831391
378025544146436659275047848722774414738
5762246362412680139952268242615244624
8305809076321025807921644002271952991259
2733285210978632234185266007417599924596
3183979855435333459830901704435310902959
38962634732297208658944662791075829712
7886104725432603105220551922378321541676
1651320820218688422022571750174359112874
7598412356486578581573815963409597234869
3734400582631887551164812313196916562056
72442775405551270508467234598986031176
18835061727864748996602489499293932403
83352731576103327840464463302841558
177933768341712118542467408535701197755
4551468358846093120080736640854545491402
214770132147062369188744316390532777
914410643144594158644863621685654415492
806440877657508564426226183856898055876
7833955470406298328432459533519574934556
1960660810984084614223356429750829058357
1621644541408560506123923108531125006649
431543559604697989721671730691349273
6421934236335981738852734544249213419396
5973149444161951844240988872712188191232
82146477875296369086088473544616566788
98099760831245281104366785813033302321
4788387317592027485940229066150037324639
5554833318846569797173528313190459937742
571638047226152795557476959559655131647
525528751705134223057041257833351926525
4455568719529231798074606281660663915913
853752916506305992369737105526666561640
2939125693577789837124702242547845768477
994353916707410596321389196936335078026
743988988651143932880776112605696968324
464753438268314271856049259193727719804
6795988233671917916219667363992353627722
3965472136911775615830602049527219626
72231295062132619071496200855988286878
2224289142636513815538962190385874180
828708586223881861398937463372226182590
78230772369034542677147685998347033
3188999496174415157151082306748443256588
1336604251144329110356444070792222412925
408198063436058306413542800780167073029
1326922666461005670451563191542888924300
43773039998129351441474773749743698785
8692692577936439313233038304640859849831
614320377129865434571377150815938702244
3179573575812792567291774053449133828677
562954972448771551120284591769553666516
56874055944909862302231882369945573157
69558476201910423952687660662621436157
82547551114725897351614588194074527191
997435091209979274241029921575679892227
149644559267417674579666251710695329217
397075351629341427939312848414769394743
105937388395976132903902969414607768
2114355573663565650282667495585374654609
189255263091466990677385190446439869311
6672264080778030374028454711958376533106
3461795147537950581067275978629359895258
5273914421873712315288967783965434102674
293797127606250436379771460532388558810
16593259696337529911250882259584805572
4375156945605794630924445549382576440
654643708816158293318824882167864268
369593643814750297071753634411854137105
35908435771478069451893238399526446874
124858828362265157741841548857459746802
443071079569837095567870861828913333669
165774698143669184736938855428585427620
10119192159888186836003758339924506118
9215463842575392518118862657827746128339
311381158963451220693095747922672205155
3588326691795168416150492622665062029979
14240919533345149964144013504742962314
539777419529176128122266178376805951863
831461063725423135924578299898239379482
601673867039818340384048292154309387940
8384290918243197578893694269755939044615
8616821187968767582961233366550473026505
458524738310950149615831614524865355563
9319222675912055016867579475947945531
28573439160017292411835436610367498059
211614136518231175765729490353749834
3199702771898662299415862651513635814414
36021287669924845181617876487633754286
8531548239334297949385908977479544583
1636687136516671264718078896477016232226
315032107701420734251173286182978854922
8972899439342444692473265884782811136585
4008715247076502451697365869720802302
5361591488942305453618233462437263255751
1963140265452897926069306734427312596718
4424144350845085203891871477533031327423
4882720510185889766670056319068983271
910924265596624898885898625810687602376
7982669040549832246834347076552766901130
726934003441845253672128586256334977609
613017027911501967868501886573799836520
722798521259577929994508768473379874154
94831468878462251790971439138997477630
21809868411624968133585989835724941900
7108919673117249157825501662753638964318
338877936886472188460327128373233552794
1672255028721986652132287748221155831700
9977178398065975362579699350730474078897
723640533553228140583853524667264277798
8160444189355634368394943936767662322154
8063489498548292145337857921938642400
111510736044275157298737678968542624447
11474465871381586103357537810082089499
9358992865791900549562659764960176969885
997993163782789070095029440411474815500
3772830595176502159562701001827254602975
1399581690779733574160723118351698329032
47251838633918232123698327363004694622
553752934771263676624336515941571859732
2901808671756708134987831873367463442
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]