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
5513518477661136345727835289786142726798
1073308971778535736420218281943322115720
8117501682738248508926085083264093068673
45695450481768118492506639617193226778
99380466341100635049656226128615862970
5043590483281671728847904417637460194505
186369977266472451423696253160517094735
35302075288505140737254934260653779258
292733928251992177122672400444788513127
894485657261506892537243075376524727774
1250129051591939819169688461784673716353
46129329720887168480793822542432484061
8830391015093465573290908202128286281940
467636296471658536690889791571815591982
2426756133827425460849007944228462279886
24217548770515707883805625566529909880
1989160140126387212995545222864939878976
148494726807973575922299360778154715927
8487898335809632142056488656441989383891
990187616061315568998646814776719821
9715414512543163865299698277151479234
586183829829733463246798623631543848182
8233309942282378703718758071467183960
2574327898922508679740059412968096401711
39480131748484563614718317860477125409
86011594551860704368301081458175999055
8928719624969448710774787004218041595120
9194130235168679697021357645526134754088
777544064230166293319365403440325715886
494026817099699043869456412226741608452
75633832766558774151812164573055532640
48014451648765133449519590317076451012
142966788932013846656389546490814784114
65377770106954671006315550294956688116
7501430337108003680266491916111413435004
6264181125457132560910374051424220524240
2794680970778666454744378138507182763855
56288976462210606679208228231728251867
325848052869111619955914615523617087431
42873223885358260058086218198258199252
4380307327132984468658053579918494705405
3620735499359880966317941169780766643740
457183415597489780097372439142392789774
7892692322963344669139489759574378474
516367514402672241028099365758593661259
496132351057584785718403572986274196797
9032747752947607025154403018963749880
682865637033405400870412549669233527150
4037300874029629281247936007396590376851
521665727094334257609169042249502219
7561245267104173766055396748935659404452
13552761416694384892246660097113038127
6398200749516295295327138009188052553126
2002601510146305437818019094166232098625
5954651055448113907853888889683927540
808337875339076208568979222264763066844
465078992239867184545389453652964934868
615717953935215209196789785934179156065
63976280347651020143258442726126045850
2232545771675750435755488024767869549367
958461991860592324315975199373229076606
7876821071985184563135431452469253834417
5455500968588079463225512562677613618
80838388878981475842485331640868462559
918338946468341292635046259282795107
568826138948433861568388873733951648954
84011151628559879962046199465012495888
960641137467155735314420584449334469492
615886352161503330436148625903852192805
5267961367583135141992847450796679468305
36186581283148243405534557860222374815
63889179345755810911757661575823828510
2058709956559907286053486498915581234327
96596234512456019165753765588788357398
69009895956486466666105655756906934558
163358758558127484335857093425347352646
5357947411396439750997317326310231565
5569869510379020402094805371522544006396
399264199204688045055225434819713782432
863969765859922556786199282626278204913
487097926368170509070573391155286384695
29091684876110779935177283002205442948
9061501667368066887875982077116871018163
943563305418993521368757434475363797
36817927505716774177896125398113964248
823558097378421295239788136656313894520
6215954841243577420364337101271084363208
57865721376852129799815476348238734551
497327676816669441160333250248787685211
6666122641568661193271837241157581664014
655541156152907766636255214292797638280
9824598171433385269950594554306751037777
4980674128243292283985934077987733199140
3341128379762148023432609247398549089
2915392685181130226117107043427422398290
2112884664508523509015162254229633715765
8383660387152205966068358928148393536965
89607886928249215961851964589584959646
89969687731779705864846964241788589637
501790844855310159165368861726254866148
3379242355373325125035227521298721021
5356840771098693748786279601770766668
8776925387229490974794029875908476498410
135723421456552991618938751765077696410
43708678102492802803187343883831868278
280122255381494340238618537998313360543
3730436935941970634228911356877913531219
7200894195728181959112025742447048514014
8674178222436823916827569837538338885
8106291737894973193054502027509921326956
658910013701991730324182412839413001579
530641671877142721574140496824953068312
9874521112481821925535661621861232264302
37924343728779298134068506873975377670
3198872272215247510563514355394361864541
696877407266184522891127418234930995214
581956253479327432057634944639415086430
409425722267123528184344882435069825084
87228772106059671244690379236686321019
879491047963472633589301224843576524111
8559197721784373337046828713311364501555
6797799389384882663273051765362713756608
8546524415674961331256722333172627563835
903924752859604256862066657469511538366
2052209275243432354944061209479732449742
840297622581345541437523032943174343690
24825380746227331756973488525120612336
6209974247655883644613243378426342041817
8636330030458658862257606362515946318660
8796348121603839148881996267102437044482
527934011211214938422472513983941724
59845450674429049391437719698075070769
3177933348094515725738518675191214042782
1663831517782580566917027405988495976788
2565138077243066322081207655331318495416
690243534882280478247669568529279802003
476295305780632868085474555534362070439
71113149289932843663382934460411254434
885836382571519779198141813094139102869
176863141045617668559749247989630034554
514333285497125526847605243867461396826
3458540251051159188253608282497274701646
929428619572987107571773833910536331529
4312206373501728666975009899442874375980
53289626785278481436457111962492353498
9823196637874084525039575015297860653424
931992258635261749178517663096012416735
308282182746202078038733743511428434483
3503286919089662512525092463393295772519
985863006643958522751681930855834372559
4710428483157820334614658402691972025513
7308441622708710726248841699678971913554
1762121057946475889052578638978170525345
6256879037581553701084431922302716381379
546518801875169223482206784440081733026
8907756559415342494698432943659131141768
3763563559959031630277578115970535408210
41392028280522550788456600382242418998
7633106226317613466619784960707967776302
290615132276389369847843602761407727754
2560263771458710409258448359356249014816
5525415095053739081616528236850861307
498428259546193975235946820917867282533
593028835632503456191152674867680274260
81789749142250546856171773381516005131
50623401902792367563459387666227022527
557260861446876572363613278547358652071
6079213646063431917534719103489863752946
372295841060438035055017925537598043215
961814259536297199453020533155529744581
6917560985763472734226314094434314127445
7228771114678050174038162111237158308098
6719828493664632996042682195100008116262
8658661163112601747769598826746312242
6716741795256158541666117414675050769411
108215621779512566746888091807662715546
3560163494988283741397699385089616166
249960623556872852827556899987029654246
2348799370304133266994721291316155165982
7409426983356647850769933555917628441031
753419839471237370267084851427397764852
2789636259997501931952447482306843626
7569582519555968448373175809190932912688
4393379394292838609144723416889566064755
3951141654497038318275875669412478609310
717518569197237127798072959991516798628
109375089067287616893504715666368033969
855443954496227657454864352569813292768
604893410266801759131192374719089461260
149450729909252976047035387559995607516
83569152849405038002147881878318433403
6115588690545856876699608343606454738289
838217909533455930624257255678143269479
93588959343536943189799241149244450
61494084853636271179882387445632057536
9958989619938898687678271208775979775378
696283825217689292766651238455888684674
760828267281797478218990342426411415377
8390478721741768746557254281166961502226
6291982449668656602348637681629623295805
465244357625101180317174109482276431569
223931319334335230135512904612113591134
99789251939156084677130715230991999
709694769050903652986667780989488671133
7094715670872671054344412268174320446
4562352943549755573453495911403039994874
8946535622791638818966127134710044346299
9860367180345961075400143236923738099
7285347398835234483289743334729827558804
2764348641851007910274684976578183521772
78353894292221623552690993320409313080
230990155474294425360136757430916693326
6040203158397759627269409156412925366889
481047972152628541786121506602143798649
34899479254942919497514999938364568839
3779703210392013177228147885973328953559
890440638166854887648022982097942092426
85639483639463574129218984543823895774
2240612410973687850179152207859942357039
34958696713904154128964599298841307314
287665322614830240214804536195766425217
156426379056595756586728004621743494240
72975345285718042706885320622649967937
787034514121082117830654237871915978614
1411114426891535607631308950554634448248
4196423644177518353108900630853658099
199640157865276846018957289215840573873
416753101473412366611534950276646554144
871914442282322716916181791420641732806
8491243314182946997153748863762367877130
9689922528501582270432648022880075733824
9777660585577766459535361524960448548024
269834767218614652919284460189818802464
50707498193969209204854216807294118835
384133196579862785142593695459044125368
35450803217311962171759790762265008094
4595421645944215418411596533795025990
778719907785746817926590886577988403282
1235195233387874861678428839371345262139
9582209554782904796172195140650791053496
36361566632712389678480428856321974604
62906894978819761658832798538098423994
528276686338221958717897446747472499224
622761538077260143898461908621040526764
284245763503180024848060848178058463784
4961777852351070929783205070267597458
65107517950321384510612404982191314683
43848726541616185406026488973285576443
654471786854690534579522986775521221914
953775867494146643534215378936798916887
5553333257893126903582135446292256702824
35996895372155136493537135475199837980
88504761425568024375901505219618749779
1905270875822302861133905972248112718499
28754704931287439446959479115751966915
95991101635217487396448387803112935415
147743454239694319098783090914589212001
3657779132999909984661672678339784783846
43465542925228831851728791744623933410
61686412572101612504896053992598844402
619076465996317702577608265115965871079
370674191569108967256131519372667143296
1400978613034897864414327607908115274821
2673108935282957676381135411269910213324
93174349696422887492231729184859946283
144968596654321285747206558467295155376
887869394635147292801515526445717161035
403437695645620738639414860711736879245
83352743626240037748280308194651670852
539772607529871460394425251456545205115
296743749772530545565006211791670378801
683683025654216922165721025303931208799
77005436512357389029399192352849148403
7371807744928914741031745695779277705694
614188606628518471661846677532777645755
601897537658675023711668878872586336490
999644109094184934788444341562917616821
3223611429679303588032201370317713347197
48357075251843697257516109657486798682
6626936967343669187485042698826761095491
714988249813887385117361714624567976624
5986555944198374675386165458535078569458
50946970746414655544049327641184787547
125374613794131789916347950462895952841
2788311010956235867795653453955317049501
88338599208667322137922525059147901788
4551608165923568527230566398583037772902
9076470539938656952054037686227326135156
167069034746470800737036864417839895265
3970422724167977162655553309216562429450
358695164311183229312816474328972956612
540810755502714656832613634881415618330
5626555557063977737054615000385193922204
735068493106849926128919136132942635645
11539886843413498620991703227197816743
1692452847364873127968171619681777308348
69812917608116573268188129838235674500
579859048660746959691360181654913223749
7344301933561277853914419928611175912545
363766733957298812866759978244071645064
3261858515997513368364173324166222867
6977608431166271958430331227172833574965
8529157191546418459853723163757330507611
8234891594673439266687313490273546118456
54331183151916618069145863213293291034
903074163836221710959513749577223315001
1706694283437791886031105896586020255851
395367351845394253168142644675197920
2367734225893429264717709094608660559807
6575514389443606877864043710182229167435
4614713499522816193913463466814698463943
1873358092413394140154069789383640163328
9091119372506521397341091456766717159954
489580801681268754291746686184242265926
5594954060751411818519586471666311624342
2419217317631803788468691793166369349893
852126807363208044072483070377379865686
9383384556238609575968514169333599981
4730640310482584964925959845724420394133
918927022557904383901373705056169647
280877556722290761093402771673327501003
677756138085284140947765058547354380
723686439943890097219458299243466638186
4567286256386501958796527786640314702
490726421336934338942047355302567143486
488670086315550538090026380369463398408
35303454407645179292698232608349973312
6557741470148325417976763118636736306
9093307380484900412270576464694411837311
3314308335717140512639220843729184051
32976811288466417197959766007416253428
90277835157898104645167433983166106736
7830251756922959419963243015890934233890
7056914732749893149151777080303233498582
1385468512678846639993531927369145976279
85424217178620668426004232121135548835
4124860214773103562047871234161674616432
3193668093445096248856359610283766618928
848750668539615169465588358771152379678
4416759726476710160671233758974999437568
804567126297031365509227886992253449
134035094238540899030527590456058233036
1941941881267684222015458980016437877
52756779689246733149567372897223335483
707140636476503782035561462956804697561
60490774713628378995754259636824257440
786867727655942304352187869488125329158
68474559277262393309498550274650435069
81476884532056815063032333690322981185
639349328755351969153396820439494817142
3781965659255927288479723817519473265888
65094976003918780507301951114812291617
9846280548913779677664301088195827219138
6098174482323535862432923415705155657309
7998845740552376281859976218106748671
5858180666107878178626897437922927116079
1121856119929975772763103048245021421043
81744352550835864843580714790432728363
178029030557110468091094826056616234
953740859513868845841529179326478512955
527151774246218023912212317988611646938
572625063579109694096179004401976636993
5116323233336759570114031899709256761899
628924088858728240786843081278853112968
45006752920875148475975896753159567151
746182713803067480542394568787622742476
444402490956583454876052925586491566263
833421405220881984294261274271930299255
6302782626929752865146955664864794957163
9267139630171490912376287710587422763809
52565329999206145726213918540595775714
4569811781844242801371088473569348327645
3762637558494900981892551643881978388177
1916881393552158900045123481826822421777
461948223996265496282332169969942877237
43865524806278393624200615725714852275
922654174327585712761745845351754049293
190922096350968833224770373711368374387
222978127184380453285542784734532303141
99176868144962647896850345221598792495
567060403315914088392017798941702918631
5136723912957593664090512537347140469805
257263681566120327151594686683227002319
2436376898248339396741457527736687616
1439224052627696825054929585297460201026
2574265154622881922029754531459451315510
51857167836226965091716086747860854384
4798563430777534551784135376558368899441
388678162253957218891231482126723693538
638701323892022209186778234844355165106
47258409903411071142287821580731255281
8296568167752365106553268560732984746
460657725028350611527893249422771287621
956223595574389098862725664936351223332
4600260751423424193179470696375785231
664973787665819129421602104678625525980
146620299397330531779127902158468777010
4513206533181159110622255402917122953194
203392818987465596128501809793185478511
72428022149709996432799088660055373563
6139107060468222459339855672761295854072
909915965015526485808706425679426813208
12397039443674860865406099968482892553
676014756466401131374117329933439177525
9020143930464379914530782134917963102
6702498908495408047378813335464415109
1325365401251782628654586205659487856
438139361000070775589335808927333234734
78385689775113822885385826722678385885
1707318028703471145156763576842937509815
898079243454991669943189435123870454779
2271608933049349410935684476899415688862
45481972477147181974022585602817691903
2272548795065236371034807202794444007617
6430563242771349706853916601471992998482
89409480639610953369582475417553771432
581218553826797195046813455671416011556
414452991888940157422992256547069298844
72829162764746499234691452029181216020
9573725623847220442049744687217465445702
8214771939093014790290113334829384189249
344467561956952534828988303390572217371
9091360421577097126807515475448742292
285055777461991179426430881115013530778
49380874278038622942726472518896002750
2517786037719434895654482928106198426573
1083136678039547741395496121879030347701
81744784194967547352343395755516143327
53036581605193549445831093570213289066
203483167924869564842566314785969608437
3657348011278925344361194050572575022589
190627812439814450241297636880141491215
82535071585897482458626020931833121607
8391425321378335162070649794978460871977
432437707829669314659236217578658862482
5045453253853078404031501221969586041882
5329186373535050209226884560151255692020
5475970055759849845953606749185919885047
6361354947337036096978571654017639996
1812908227667145956583762205525855277983
983778147225490237415895041641345169989
5335219823108735825791354448171584565116
2413972971685222502311557976993049087015
7157820856491838164048967779734864438146
9570393177599274452646012192226530616978
998439025427694661490989050875255431331
34581894743501121191985036541873406662
3756985484931865210982714062505298599456
989850236562957493549175692510049118707
49457399626353334000776098764011258818
372024135763714348461882209481858972596
5043528958922691797326484801607248032708
6765158441790453841886106233021756797
6796416524556282311823597776833855627563
612113456238772641752434450170462115553
849327099337476658329573484980968271266
415198782727685198352216648872482807076
325233416391216284191839217126727387801
6655990286518560917325011997261059575945
7184562847742793630311022500178463157855
76636322150622540325678960399382884910
698937143945478533577401016432580463330
154015323948451444245136790245664718231
7093685039013084936591891930272058988298
4246109553268156790343373010495367145973
2040181224849657774957702319375862052628
197248146042138026174437646653993556666
75049765872949265073952904521761872616
6787532161321046755514004406230360107869
637926922881823259729152826857493572557
526036524097640211640018237388238319835
89173875976345025405172541932721893099
48076205581135459696395629438759280518
2013993157073737363453312903964265874579
1754797609291520886015825596171184259
3337181175309995419481076642337378167069
7853293590108755431184668740793452023300
9525701720757804647063388012612891046556
19403279335180747768104486783227036020
4597763275859517258142337624385193281
53984357173871701873399674816045984410
957673983589662610335706030918295196153
9025979952911990585737569969223957876280
4604661163283532472033314566554919668
287579515460739257461710445589796348726
729697472696326737646779717514178145712
85694379366684824303263801315998367133
23926429955466759102991215948852878200
9681736612219559676717329848833744831151
67463305391161234467237459885547474398
649734623962677734675161490811173181786
49094455574816697471534857168721449243
8195321527204575764111329082374531676680
4301677412703411279645222464451155775802
955353472241766513091439621186043899088
3041408793756514197086937117752593519378
444660352468668439609452123098312239372
9153478391376495594060305676616934348687
898798899434363781976459834719073084181
25847976896152432443388652834420813548
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]