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
8072671639514978813851365978369433411970
950912731737789869768832676207589123107
5930762020088443996131685003303526686907
2945625442778794897570372747640427114628
333924753648661907584095529141484325901
32866381864473025666825145234272143816
4826175374687367202740372511447472623037
7578603627477371410728474061561017193178
335772532766393084851196622742075578
614044963297669771682151269910928218787
786154827463591686082059399440673053
52274085325192928291947715942620554256
46457274381653326569508498299522215457
92601128182123777419582846442603174544
135670238756446865364186749877434833234
8302563410744577226466422419266924338955
7053839441815999456289955875909465203049
848625622209614144289458484299056185917
979039783097952139022618096277569768983
497321047184967715741856714184321975171
1994884663848038306932362360089813275
4291875063062372990349705161788737387
3384540529071602957980423747386560733256
8142229025583967456019191095627815529026
40012527728633874015849580448790417698
8466606139773507525565412262642426135277
52845575546367526715884890311234999020
7847599585755638651471001480627665353933
704530289983309517827074232148649794666
458342004080832625191628823048591689300
6030406618987208221128591233930238521154
5529110992628247855683746454563213719603
763162706075148130995618410923077733975
895929728437437126715473861513143677732
8956631035045488284654096999698627429875
13292875680288146693094475815926245899
974230007491354336602177194988488801
114495983027703303240848119139965041373
3112667475872698390921282839376077334
8440750292217949960594803725602007775
690047938109676188721758112902668615164
1874645632769329587643001577238849697932
3533484186537969598024384019713519867566
224549683810499369959019016618383538754
201990061168224556566342092648666853118
496160673616609065339656244999628962148
767316185339317687525913915675950409441
2609987255761218624173109039999930485490
75821964391763580771502722984466339820
42647329676498490882201421407626673565
433334816258185675654707668176219322810
54652555354147379477741182935284368127
36115867178643938558437716901816917386
4428569922728273339408589998991944281
414350837174152863684659630779304638193
454427092835325448662426159212048573384
410791495886279415982586403913754839272
8729712457964273656343946426407657912914
418919855796805211696751521353943967047
652566364244349438710685362638568879145
4644267472497949879481586952913989981
772770469901368475477005197661425728667
8969421489961342601917716198764034383512
56793045266090514579663987770365687510
2918243761201306208130041807923943619916
63565436815904133811359207715391299622
25612164917901788324484958381592841219
66755316145767911014909888774797034136
359528835455251355951589861668214376561
659356612203577578707587633905050075836
6734250869356318318569256092832461599
240192076933871253651295530419895333849
41271819164851036966314374737722462723
34953886708256260632036868421457615012
956810683828534854361387524170418917909
955423221109492671776269012700463962486
260121685188376533727981141380528652955
855034899569679769164955986156836153801
30411340078623433247573472307188908710
71744646540093298522868102102619423430
2797934918801547986080765383756776799473
4120786826533640761321814958799721056224
4193675382527323863541147314867698455
59246883669233180222121738238626858465
819746953166164496687352131412043234864
87738436240313653625495724889753641036
860812004462976922897050336226159137177
7747515499985744432043589169453274956410
4964488319388565110349717129219227085615
171663451502610128498117855523589394062
98042153190215206132776597416319637385
111517114984640034765974760128729084183
8558727992704155707880865849812587293557
719316581171917984746348785189420833013
14719855935223706786982796473366434822
700646855138642367574147597771764203288
32902581870446435042145748220374915486
90523491284837123412012711964335918457
5034950073401289884134581604522951847238
6098654310267392806698735340770150935923
7677382897391354405856212689986525588890
6723336433792651918528398513493418069879
6183990547444293134769734284304087486297
2510317544344849349086509006266282111438
662312473095679940920352266634093859974
4867688850064198671432625512800987758339
70870689642088789753121002216970891549
194791916831525512485418303577654385671
6856540627331362651361924988240387928700
701226661672037480944807539272197781801
32254019918758229797585566641573802929
5619324291648564361323238101511791652132
5944578522435873857845178111376624263665
597343167989101710735886926594764474130
8329556210361645646598608006953665068259
8591414941121494695771712044644772281121
6341487161487012684520213037274753807741
3041100225934139731487632727321179018890
896623294670311930614209720995255233983
998973352310351320241059291749463862969
256072378802600571314844868021553296937
347022803953447566515848683878466364182
6529528628586694900684256032218123885907
6454348367218186121581061954665859173
578343256142263382216253805760298383161
525021847957928642683641188559524715422
7840964854058085110498988427565445888536
836321383785993233185983168986151265643
6467421482639218277783259116258658962
6901345447846692521222016582173670442513
8277304071175684628793439498630335645581
590835030941941819321942077762646262922
387843525470102117386550302494534645476
884246427324390472542294518297641033400
7218183127406797952297131923873548596759
1983249454958192593035254931821148958455
186038904159254639058030667358545831640
740071353079461918074573647345644591705
67998149210447169882248779421574612954
692557505707979331644256734467284579249
683584422439291852018458340193527621995
7483662422998776523611306087705949903230
897517113310132278329424781552528497613
3555460062425114455273571695089075321
4595246069367920221756888208339767615611
98725698722713634652752570099664564840
9653932965914600166033171357431750481753
83446018466589352430912784582078936945
623344967512735990356881335183553256865
335085802910552517533018373682821091337
12423509976132827198180956146643372357
79601479788940854187944464202480831105
8941639958579376352370696369862276819870
9997942548907710194217074246867436197086
53616478729249323971262599044885242173
91172062107546106327872091244568197715
795981164716511765499799092330767387393
6954250610897569585548626762355661541218
9395970590942231380679605422846729321209
95385788870519115338161163975439259583
10697513782789114083298955504458926723
1056302554231344226139459152245597809
6595691534766582671452891046231339846753
758397459686842429348982888196781268125
389933447635889493013679769391765637
1844873188197316211232947391215032762
75247303464144504616822372224479669068
8758255257518911669382737285757161457397
4754660698158245361011409505407364754028
88745672885953925284316705874197537107
288710489289300498346183385677436716781
5960330257754838753283423779629060172
50378079781782681747591540186079188689
378143065516766185591282905621064264574
1961778350419907497888066699685144031547
39636514675495076208043774931433629828
255675809016667231119632217571344936331
755864914959306437577746808290337391852
984553596721207332589506289762248959323
815225217645741522212595393677491769430
114435291601358778438146955245798954680
94774139363724636102500526959257862049
246594377272019688349179771995510289149
71613875805569458012853509972030705557
3736837988764766384673405813360885231
9138804856714231934844185811318141085622
81913063601210846807199165496850342586
987679652667561780837678632796277731
9206868414996596285393914026821560373226
141111167877685657069528667211556989072
311625258063991936398112531118526155833
4223927938567672550511763209529834638702
315480969431406739090117089924715388695
9156330713334394699038112191859532233
5463592489652140861098619036380564722624
8127568356059024401511319786682058802350
702394611849834225974318567239865389118
1700870882632409943813673577517365902858
144070104164480176591514784054953998121
92277711407779957380206937458101242680
251811302929146551422377313431040918119
814910093732599556398111861466758511669
374298345121982863098400448235859817531
2353295119318842966792419596349069741036
78846783309328771983945921815627616795
42642285708436475765850293486581881449
604543983995436816739250609449673220909
8532512642511785775932379372525287535923
6801136349613141381939148587161985255598
37934885754134227845593302950061847771
65869189461560088375158403317177545475
1473338236351549130138956647918657199
31037312766363358024646781691865081242
86090912571515831194674130586540753239
260081525466515010285490495693964672909
20444576337615541813629905423228899021
523860266211829134820176750264273661169
4305677544053780132149333004896615684285
961284503578456831352149598422330567093
792817321830766041091707811077717297763
7661442781496222040391249225084664474
79445675862243905408447504386764974982
724468291598291318176391653197514756617
61525343692874610274597693972698045131
4442531978824633440690943799936473996816
2153603328123189727917655570630942237851
651914794513605875943318734504768449330
4576829675968444232930218594745482514824
69231894814898553246444594923135414063
535777525103761445207725992393091471952
61418238602818001855202555651691420606
766847334106356743946635803273477953220
5760554322113665243890102472619654667961
3224492035219699845470031924757437972
8642651650467688028902861703256591378
129666822393496575122787441579281698470
88626593977618422855111374413884677715
7972125687114312753049065532930224358328
6801236979459819330462107697549475445172
134748748170251661459145841390339901938
243434755938030875574614530105041425098
89046830550918091243535376079423069489
4196119681908388715283931242616949922955
5046300012171289971537439422470743566680
5576582820261243408813815655603343314694
675614165054714404593671883792990381009
79132321404731417933024258783871558310
4058410487118193311181429329947091533504
3405823937849404274136783840180426085904
4518173550631378354973623118061847108
7424741493507266856510663481109044938495
9829240950221971930361138524112259797275
989358568389632432455365020847779786935
490443413552787623477939392311342033370
332976962831676580851551703648581382906
17496396060588241123543651434214494706
409072807233509788872291046232290912370
4338490952082434700268444438977830633669
93667421831736877352465060299784634345
2835168246001296715096383137588479085796
15044918739570037686789837893863481336
541379630305820702879081622986076191256
9907332579384810250359000257739058498
810714267622196701578593489765326826984
176056073895868583463534441225725762812
2142449144752929706371379640552511341587
9138965989201856371392451002609359404174
3331324373368684211774032588596898492229
5232311594986985756822763260509864809719
15945254859946215824852521446933457509
177160542939821699662515856272896733949
844397257484111905576283404444327479889
59284562286856558823376887789325387816
823927809287477196426324481966758887763
721551458280824241964163481118861061666
3149546327667351293658172343436833199074
645552595812722662430674115617922789
16434159862429237630880514440486120390
5997222287951232858566144723577376382600
2146750152207847402368326193710163305178
611033789055921178418819471465819207840
6259812561662662339441922194254371164
6444680927696516977681524440561044223
45694305944678895694211245423986376488
2929649498332231387383701211263728296947
51130173723508939426417702977413247715
483287815903238445574912105262513009378
8051246496533660326553072515548553416498
226992627969837797491737798841478341438
1328558278514946967958499015511478837742
490932382389498044928489595899078955764
19382627628045204270572323268094245797
3250732296165832911176063413335253924711
4485173983877910337785966225767384647664
158748686609317268510229422513869746120
6075445518603684758256148730129027375
212755322025374251284553952103012459187
6644551148542616692207415127344145581
7554176111026406225814214690819994786691
6858403711546564704521137967491469276
8166259998097029597690492620454034026295
1654908522069517482888699068120381896636
5945476645813196281333004889543634022015
2511758070592833258267693005913463686154
5813145416255963669598336610302637503456
75778463606559422978624349025446677814
7877690380025726649794842974801866006923
3737499495033374599959513690725194139
403933597789121929517134095849165608264
1775750538849181213189661943392624414050
579242972089932449314018920853969399401
684136603111857496454552556752735283059
6867567673524119733112367830116738089837
2037730192299083310292636102190028457933
73657374977097492694809367919978354103
6491954771556672829884084258365245992100
507757883543412794667163887155056169510
4248666987255609823148129345476188002522
25635485437207319904587663741277437616
9466694475028117518523724106812862781485
460906502310119640691193534915994749
8252889819287198402444143431237293444511
735819375959822786093794452121486635061
680549301344901981711072727842759782035
375796830106205442478378883221373404913
448198747886784251558148338809227519829
36708052223360999172674134784621731774
827220920173335597945796514302697744074
3557554545735339177773135394629843712166
9935745882669536257357699773354524962644
4709244762028190160053859477828352356674
55081157832800172888382943781626683104
9352857372931784307140952516492129598273
914369926700898040645528401143976112851
816110275136866668483049329999794547187
11221357526692253281935999228129658767
9585401445759187882441222975374269554937
51598331805743522213312449356914819469
75379590308876854339226399181581128987
5868805124553197940439571132284067332130
7757571938803112174131721235217754084703
898362389614331355818852531256222658987
1927938451291403753117795450661891334044
4047325744741215799978526899967896671534
842656356914576810042244898464280843358
9679391224535939987059074266641463402
9017607544548297378511296528228170309720
980641577479997037151534798738579943
297555164512302955858612754839323372979
1021762427162168100661984530193426427295
46555217736774383156387295362883758737
216570549789312982668298148940840058561
3504702829281795287955325545269052296534
26577965419569666239231550958211572
128477106945602033028890749355730684843
726663662875200747633300650582799561451
850268543731617679262402524709580254234
870396569437238659495148968670822005252
7561411787463507756257681651472046487086
1702276975781548902642222149399751876746
348698051287568264639425331522362566893
6214894911479190397151406159216585196023
8626884073548323587982911926448955864605
276281531826076748197447884325816405412
9240423984793625224985631450163416924405
4859250034467925558514596108787013729062
85330033166800481121794556426656473295
24832555424966766725871841587348578746
870174256183275255277376720385797313591
71608134819910163085477379394949379599
77456746862682812505438300659817671600
170231893937112843841619974964489736618
19417421985361426659259640645924464158
760588556744861973843426718584867529494
753020058271713556459521182063155991908
8415762875437978082902143537061763772
88365574496569233587250465631388006082
28113731759683549257364577414524518547
3860113374639096445586806967874541599863
809455785028231441642105012128518283888
563098241357410335342057137518684091211
654275527224571462654716269538244307
229446876967949687238838712777713729513
1974486016552351891519453273221721767733
5225262231936781509363304883872284716365
285588438405670183316253983130975436252
566230426159389114043398425537029082
3045501492369990535429439688358194968585
932625247252873578619463864375936732761
1249859421549842794660027958424537852992
2842259260278622435332231777628262071794
31634406523116264652801301101652448851
4587149798309327235539832202818437521706
572393097295520931194363326162032143939
168732483859667888623069133931778973430
306512756970992253724948953597418072985
1868350126752796772388359445460856635
56395829563523025047142559763861314432
7096363484679791302509642903577324184
2940196789378123471833834569194661289
3124155695731370668243101029150510803069
336082575523567616545433159479396346254
100657284539197733250893514382769617693
7613936251292786728445183234477158275624
8192295921604868716470845843762889238423
87431131395669217186572899888757532904
795039537780254776761583285871182611760
5542415991954161895590466461320918283509
528412992041161222683562775586516081808
98858978462498749622390262653147335304
6707773562528477608380432090302225697087
9787472096895538825153373178746636699850
611926714758892518723652328403528959000
97171714529117964711953882996516206266
7205390070716147307128401390607542288325
471554813038784431271801237222055294134
162235633527449885228927515060024586446
8342213613559615995658498031254794032780
20969114779559795230337879287659294621
6748264757747976152024205776209815588369
4710932827953576995289949575718059682857
8272726729937142578677502689102259639317
668097746862989724947617929730477533145
84422367606662097616064693826456006038
2347858155447205760034627380205126469280
443458947981691746818233543897543291163
7125447659557563908289454495125835121910
18622821878952054148476528966425592295
429850313123435271936498723123254935043
361188934378716369727794281540466829
36051640203273231867249687571233522471
8111801310259440235352902742183236872
166956308349732388498128679655872185
6452676268089413521398698954575039295157
3035220793201655700326029149249189304514
961053712985773349353076517260147866253
6698569431337878711125235284755028971
873197415034185553754612391213967484
667251605906664789796416845249715699215
625135477605236709674942313153549835945
781784749942784524835286211643296173995
3691845149335787464951393669743429235853
669224986416133464873844499780186387812
9227642285117885186986162731618671737
8102795096419678783676832551550938365
246838151634940638810324632176429352397
172411381714482294417275703569383047206
6284152223145422138428703470303123483131
9551473837511213843856028136668223102275
4198115490448278133111297719316486188535
728179466509096921530204530839923509511
8802471440707469327749416705809152343534
38858745222087902904767911431049831552
7723592721909750656564979715130297889470
1106559193612557572393701363345608856
91951402756028897529128023573638727349
142742279924729084575056155736578015
225743566794967568252847794844704517074
5071683192881781471614105774700389449581
8997143575446389850374798744385420223521
147442936758423180515335626581015954763
379584444199549720991813932805785829857
58754262604517681153711155926090922011
2309910996825620413172927785988313255081
8580277997623725202263162654764173368496
324428098308335471059268074250179077337
178257726390338036212906697978997155039
918131305126750989498664567415515843624
3620129342246423803814436800293788615863
7593980984233856437773869526823855602966
9289580244776952148270212824927534811318
671094097193832224592256768120840123098
20937719716450827348550497343591146896
7931840096973061730674621593688949851
980454721782959777919909166367143635621
755114608543158581662851582946292784644
8368274292928133517436165681022301479
97638728245247327125665454659360333664
97287371429999569924564698543842864515
7096181416398096608731082278485411128742
553036578304255659313829386114664558558
820319676363272572635036903083028904745
22870483744891350094941074941970438110
5939823893356442304666261990775757728667
35144226893127948887189760468421467844
588324531598141680253763065386875342319
821158546538911682977815661741743263863
5403101332261169294683026693084888682
982971114392882374069398898838582242746
9006960148379063954591175834221687262488
1544608949323571443052583059516874723664
203272682699564997825941351269485405499
5786981697894613922120861721431695125
5995868541543228222157705521321774852319
754470156387969657688994806998841206572
620160544502726396468996296513331226556
11792913797618026547150572325241453177
7991507459425907309170472653670473758559
291527459032300475727351505369656776621
2461998824043305420657287037318164077246
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]