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
6399495767691015571849284207895309652
100054903242251196579468254490599733918
2087714516036019296757168526721176145152
228575173439026956617444041947237705595
723257277749709569449292084164630774573
799585275039972296022646718112425047481
931755891777381555588918253129588522282
542031941328443749012338711893969579776
92429740341638017146781339559885841944
507943263725232972974566051232628539
862186169168661782573150852950033147746
73686222891495734519609448599840594026
759624188084207997376066618938057686251
226325163492270126851423329425840159579
12055622634936367887650047567803461708
891718133185824791623447288538237774998
564898287436544276708433325462579633829
793012701218662625687801624578668084557
613941997330645856847989526969739285544
732977498616212489635809775852998439991
3760208921199988733860684530989721436746
3115459377789083788519721429823553267985
898699283078792491313156937234776022461
340434692996783189603905509372068437620
953545517244300146679225716550574126007
3636638323435570295684755862612629765312
802254791102951282244068970431413952132
210260244077720549876531179287178163412
7600209226273334146711825480324671658561
9329791134157592566221815862504235138
6332921821175829208765275607218157935571
668683936561012676122988934468031157013
7163200854921681317248233396287984088756
373943239799149418851115255590597799335
27449518665125023617901156293972563194
53248639844514471532265019740459091188
109642109671128732401053137145728986101
690982099611629241476839402858351722823
293124630863047407597789037138636444578
313114706747534111192042900886934646386
6115631485927119285314916595545278045837
841045643133671853338084482555068021843
106239187201236417185440121971629807200
7676265417893440484787505507950018987440
635637827579896625370102255806337981368
939262778208176806062322960897513056601
477814679416374416368277990404120684446
6572111180065395505874029725700116619338
4215695072647344456733422208213351392339
2289911026173699383482702923763858856
4744463814573563904233092715661094466518
113651073888635611169589421851333405105
711157348701374323774909509480157823141
7075645064465125869648742640305612147600
8892360125290014369204804580611083173
4084765419526073559355244093479250901282
4021719575779062390786599780774746971459
6566394663026895909222196509499259389
691018821643517436974965304780750603946
560366631150399998548207866566917005938
209087158098752499442764511422726268675
601793333409525777017469264681033149122
9784665116963994113090749453651234068135
126757223238224682232448199993163976725
3175967739396894622765362785538437452854
3551841965495274142134666809794319257037
32186917816210455839610760321347307825
5222784634264411983029843948655323186449
8825534661784693545252812601357950589187
26692293457359960470212879818971472749
7077507816438414662636446688670053955705
6570390635621744616521859365550883779572
4810771039394869102573012276704310095487
501193126707584653880786732135675984531
541796675661130386438160667661887250145
58654809108981186524019865235467712663
461711453083535916597254393666343686566
2911980264633018818451601070798545009879
10268236729230077820825675642786929564
5427459385474616661387318152449473918
74169341865499458241113711621714494464
1504492327247163927660415143825236006975
839960775345716823325715853927003723344
2487828950602214912422283859209992231368
8643986332377214427339308098589513949090
24936647149672931167513120023080158389
395031043856109458382940697233167335059
1045579162182959338651859043858322751574
4995440565275628748686498993499746457420
32231651233658146803174230623457455820
5283108398549089896026623821533260999694
36014849656242478885854767592638266873
64128511457802432986453516146641148367
8324823653263000240578175350744555264308
310797798245592124791396662787879987771
970283886520922599532368234275669955856
379051061739472157473134906777919735708
6369141558688904280264554657499081033143
380320943121588158137060529481743015339
50121498434413588763278165415979377250
881297579842710929159905155938447224321
76424987463121778787933098328570511336
302978422918618522269641615157411607735
969269184110212491879003133368235198163
7557613171275125889930361000544967241033
3143862941141951909796523354138723239642
697321303399440731512273430882436138665
1289471869724920954523278274481958887098
753786442010809855771492452555286277028
754789635864993087022453346327231213541
1498243982687555118374153835719122927
4519189376526229883653076275897241034169
351763158277163190485200295763675246737
162012225117446622997693458757998959358
4307516224391005622328315723496835417926
6581539091592278363117346840349461394655
960224843882964451515945441986428359518
2612630249484499898650988421623510016271
901016788834561208590753330911878201030
421284208584764124021885450735369675169
401522084470136885329335690631749389553
193160566920444280526802575434713339777
4096667242454106607427775712317387093569
714872889856247192465787741266546509547
144581457311186483136423862442553546452
34932385356138656089739750295660972663
928226193819806334635987133167998795611
8214138528419991344624654406693588974254
4880920270146058763168863960188498398085
346281881222913412630925318748744291123
61741219452378298905645237534423635
2612254968768926902772563733967189573128
3616321152511281201072545522350361777804
241140922855553134856717422727645374141
2293708151316396585482415520445866306431
90462954893268722930319493772650767753
9460442275646852836622263597584464036470
6117715555233765309488624304935215951739
7609480241753983529633321264842194661357
306848533193690189089281866334132600964
71606235949454934755243853972811886342
53708503699363258611146219719523856190
338666203431393373389171038125946324899
629828353707558941776471308815866245469
75691537125451587252569019547463256
585723912155923315422328596322975441470
5650695064989433380298524031247252756587
616887726323160521922334369692449131599
310129335394842995974547330199485846387
822416885813341715878644333377350997995
55336474222334320862806782344920742626
9601238467769392808552635897658512621657
9353936968468967256412588336172426463163
96916111456955766316382882210781592877
1946295949164897530318282977662052583850
3593625946835052805609397069282461339
6604464577848022476841799842635448755558
198166008457998840545187854809450303719
606867555794979384312379188191232912752
12738740384960139090442563314449255846
863091381851329785193179452995593512609
1361202942959215261899388309339546382903
5534938687943893463242995718399792804
59549906414930420209681647493488652127
231315678638407385733094536059940966
97799829274496240623323076744624314911
584874715473724291975651657310428539893
9977135832413808054215233159630523210
99606659906025648899288622399596705815
264933158422680353901496100815311111468
763717897072606213321131982512023813568
393415417736648809351631993359481161785
767670261630486725412587315828470712157
63572904589451459957983632682579624181
1624290197958550194414251917904881501222
320790810872317868461370728188098430
14759301200594092001600563803735253266
310508365411746354267765337153519517592
4500390119111274631558469025103343686669
32935468356318261089418976895728266851
1268516944289683188087964913507135387747
573293291097715111662503437980187818764
120233922920657243457082836444896946131
279341313106105185681631568579031962274
4778211178607458845514565269274921839309
3756461211908719386425715550347679681402
8421855231592151778181418295699948746568
31956619867682254055874368743288853658
35681576687172582831593549144041697936
1859747380284609349706206182176750306
7389680315251765408018078120185316813256
984873519576401556103011787576074823704
594165761973117478931582217972167069170
43181954115860248035821236968212242690
8068721556135885954773927119600720565673
1755932396141378420458998106896726041279
232880277742721800245013501907048781260
6358466026969739267056777487362258398187
89596702680462778982623748313227056482
937654355738608131573114175248218963155
558527551907944996437107486242061975820
872783456399152237926512894441266870927
729119925100260963605197131465737923
1357583766877165443443171269539738122102
55957445900932374608535948870717102934
327227380466839585328096836728267932092
465989239720317294924246196536695284248
3292529199747134478741054386616959353223
173128458746186151713081631439844701140
92784569313898721639922514169216212977
80908685223299298755315138731725946245
456853393447369226032379131748536786548
6817179571632854543434425667683850399692
206424791612614195758861973743165883651
4935541890972546713269558492661073904365
7674116911549135245037349285933813358517
11266806879374615918238267935383436553
828502365235856261320994331109948762591
1288170690224388626640815331266012171574
737630359568112894995216581729299518711
5686796628946290111794253577852916624994
918977653088558451611479754173215463720
119862062678246677372810637272309873
729727284681274937818247622778349771561
77619482443402126449570489836092580670
6357138424689126369818138471422117157
25972878238429668398578167726132244643
6544476817525483445069356771180537493525
3416418332792080275880985071272058215796
172552025271150591491143995759487770634
876918752817769760982212597743324266895
7861193522758761268336461879021784990
6561724613449847318878600650590499827
89711524881357625694406192485082218136
3369708459537570218911649286419248345801
4190665474483487418459213823383565604211
6564815162106626145013106349880860545399
534369993085388130121239294731884403559
159445216449359992925166275965536665106
5380822240157574383373857400422185433304
34913728901082563312946286131968467447
8200590545479923394623716234146381272213
1445105438671002470055046917648664683046
5950651985003443325822298597141621009348
4852184110957657616670926889669175845903
5400605771594937139967228895564561135117
167079079824301144852356160864238163333
48881739537480545644816705659756558144
287810626512841537942513344855799864239
24618028853183389285387421475029403291
4644329594487398130186551869112646661240
20416978562894986514140339572748138471
1006818739632473895944108148553344487635
595380759208588521878601933224181851142
2186650275941557648176882760696156635
808263765571978464742418115049608068547
9740722476651095644865448029389947423958
2897983946918892589036268626644882866009
603857154881626699788878436270445032298
95053787888909193358089479280816786209
7191913995996350654896674293139696147577
78065622799165303273443648940271298189
201582809379849447396958309649715053341
871720082749956534608522870343678966619
429390152875867895039480186733396738568
546343678798200621934161722214674514867
938516732483973520083047410232968258504
54525623965929265174274465734586612546
4015386524593206210819355415680937695391
5049403324429888875278207369286113523948
3488294583506888918228447224507347255623
32574470962941663424727993969912266998
396190472809894835491468635479443384151
175141813281525484302595136380262053441
7683232165116191221458926737756832094
695887267144830076113523368332417531828
7157537470872359360862853535863853962271
8644796852458275345371641658954394156825
112048038170763798747804915792746489397
155978586497285293812101109439754701311
655514779236681674263336257186492749107
888848951808180027411669873549673108134
30156392166426757361313533859590056054
166721517846418994033807368130049801052
9137334686456226782035509280342415719322
561312538818620367431199433150141041186
369749974228848933033745452450466526004
991735193645570500038067564386174341573
18486357922882541771892740242609491341
418959855722401744615223421410639283646
279927572483784758826659129121933228218
54537229459894844291269135392943503830
844245218516383069835162864829017165213
4259934975185991820150018893507072144128
6689553089431963223955827880788244277659
6220738277225179839049232876794175248
51617288796722428381947075773997759542
545649331356252849237155093267086064770
3389123433634884339034712715645540946064
7969354426193395067321429271640063085
953364256452597703742067373335318118183
15792003982866644246672450967330086500
222546167653777718190883855016926951
28486968774344373824016466972559051627
277622341473634341070963739865447629251
7161311925961604572266746004444851955227
61814993303041699364253495426847749988
458248077232881714537922284834132275584
9229804411320546286601470529255069140
2290490523318836196318293389623595646882
998438651232442293907839525190570029287
3552546141344919994380254371728894109134
9746711286422688508626266837749451308782
658170029249961455959496219483747264403
8219112246574368462679579494945665145309
398589851922755424213898149224767471195
471823332211714541892549810720631227603
35427214081844910369680817323423851867
8706798541225610431345161799152650909339
477878107931208178731494551411777976404
5675373010725130410545266707687893132857
8118416456156498511569543014867515904286
84976721452497175827836187129812464501
2269776186965340163769269268422270575821
2453170164507960657327225927743823954782
300799972324183577861849599313469342
765831921252419776019566775484599884633
9568929526963470300886004099463668301436
97421461666451403954539982580843239446
18079162868722254582727716177281787
59926414897857559568549925137115616213
59467193128294524235523118445163396260
5385291564664536860574307272071353647
880036041146555478793117763157613146909
6555925216206003851217659517547377364549
537184033529166853002408266711496488521
40781436361627349347909902591675204230
7284378384456294879153985581126951754
7419463812741195744336027858833942939929
424117982607517786407855376272617800437
2349177318858870403257425040991174735155
9392767044298196295552399127664213203777
3851475092953279278889366334940112525665
1477487270355141637075189496217935262954
226190563355635985630363667668420973987
35302895875855284963847345067597072144
497479561688313999492279998710143860489
291437575875648749592017847438045129526
691727617857112648822697036757923931223
655099393519967630761827145183161988790
887895575481881994974787450852114716363
1829724837269006927270554994728565773737
6540307925289026873079022642818213559236
92777357132760691833768473625827037079
2050207489836150491359054986923569674003
6624253238455458262585320119257631182
627685732559826956655694041000021758133
216912897299716592772146975621950705
4616543940643301431334488870848945078972
5401687257479897490046816796645380203589
64188932235747680719714997828226782869
922975675980703264164899192726407152012
3675292999379251795210252550458046858376
5783634736272374395184045211103054292777
78207888686258727450342157260848371647
6061248115436891793693067551082845448
6394650683394333337593116943214691853333
6537488972853922449482075481000070729541
6256360528567838659837478525438395873
5615472595116086691988988830490748862452
73178744125519165583307487854221113131
552664620464212915459815546513314876064
3600689536729571201992741693925619537489
767229439543883209512665313419227703283
720961163673809566689442345651827127610
1071989589325802447531022914277662283341
6997455710936037475919883164901937019251
1448355476275822499521633504714934292092
235617421553227138050421237818435401912
839788336342526227049232463271258283813
5115632324913978288652792026674525963
4710797790742581364747404469968543161745
158159414150622402628986785767264906
55659188336843952951789585598470346931
835288823563258509344269087769523253295
7906821333193406209178219432035265860
6549849164239676538372155822034553065
19462725969360211054448492743847904906
98564075865252774801413808541316069211
4114739257444314238092579855244443697
710440463642274963142712484099655615724
90357548771754809904661891234284579307
82429802509498948627108112358869912958
859954837391623854816966733758954271
2627570294982571197039869708816144719836
113586411799931237495712136856086373535
68037177709558148595771356946236911089
429870916127679776939828006451889078913
490844201791648772471728347631658958111
8243804480061548970575440163438673197
866884815064772920531419106113272853479
2123546589022495821704880126092156430
204639191269681555843021730775888456005
931319967115529924573675877153119996828
8006284014511866140126773647291930463532
364021262424950428383723218908371053525
533652623202366939119991208135408124
1580133757661904904672213765112856916027
94693905667490931771716786663710414481
8600618385858936368286309304978280953014
9912259326637634797354862527101869869045
8097183887981809052189292834219523340
825749857119880157191864665471928767037
127975996907938872864637156460023441790
5435144787727257295622077999991520729565
545965688795458841349807167550614071287
800074099951788091181434849637723169480
303977980187573005901458309518194382
4999150746312266674149693155548831414130
9082776031171955387618615513743119074759
7355468210418824064028954092186625128
159773412683651944203942686798285599394
8135246256646784932393198171903714208997
337081625569404262777525352829653694359
4712530383639550352490163749226870793305
280695977032894334239296483224181815914
6006770718264759670377256508202044491424
59633612976581984812882578723148580292
8444735898605361516135562169545672386038
235084413157758218989981610527725695125
945262935330529181038969598566855791310
6750840491513733775229858210157054393505
622856263424866476443866959678451791554
189094316416655963513196509685817555089
9874461129097838319471642117350124769105
1102309623391602694347572514715045769326
8720215380029338201420824199182632932628
31327363509826404920894112117373399002
503910688479799936501844789240213619295
4874450197419421670855934269852877792382
737645184121315602546201492753244741238
6235410363648120386802456711295164986
49075954854191981949878607408770822777
401669273905754752578692335990336864617
1884612614581271373533687986512634616598
715890386554969018491743657502659253880
1259704774543855459895071244455494697
728279017594742985349688338455637623482
85821665682814899005815998210742925636
8750556851751814985650733550337685783647
36951682699289291029834548404458244967
56393041390729479414133752500566232249
1847992939675819382245732001218150137759
864499185620930164467030219524398117561
8411996247113237893760641656176791582880
56927971148860470553108293980129166760
6349226034662012435380963360658723409502
8188874463823209853442632437718884268365
9371582337131847805123178513969958024245
3931580059047212329385591346366726551388
1062668939503075707659645194771181107347
782961965587199462898808493522885336102
9898216786727936979546168466962195109299
61379948248271858835080635922577999921
5413757494485756956542165707338963711642
6787751580416294553262585061436763046932
875081389181513428037604307290340652957
4332249313394484288498751815397128685
30073718244732082191206342221295946862
282737649541540133891482031225598192690
779870371558127040089660758186979719755
6084794762553464613997035947319078884780
4866151577524692683305781239470998553
6229189772174470641521041785190549461243
3906814341187376514321434259560533032017
7251874553813438335280447497364218641311
7866982812343742589765979953584994986170
4625136128052246326477854327487216963084
87062022394879029712092656621109057517
608975246735448781058284686048657066654
43919145783890722955448532319138722777
596956601363832314956188232530014311430
3781916778521913456634936022275897839532
475875309534910996427593618608598086694
2220308542488673394321049084780517066083
1586571858225904467569633241956235722733
718883934024419594829568097815741943059
6678842692337112784083903517210716483982
270895694019201946542204977172365027569
77794541356632520729236708527836918160
782619269926668752793765825130751935638
373084265761407386543206376759746817980
66711528767978491356097749827666077023
137629376673594937638779150193138555
26232746277962674654684067374049766439
854673992548576921964689685446051943454
720379285844118267084685500402738681953
6408917659558919525878935113461291394641
5234558636585866185152595846320861317000
784692613507339120887278497210657562668
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]