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
35697830370960177524663883859114439236
272573147572122191373739573920658212328
9695785397121963380861774631864242153503
522824033986916459550083776719455391367
865659724449355972787484689652144916802
1071705353294575964168291710135424668438
419087767584497987151948184817272401628
3821482611930056716165198253479536775
2290202870822950887858922758617659717
66956730673748102094651830055067707
1639523463448563059325960891749822451
2173746110269147177715367762503525856142
170037493218358943385680880876777233083
953996645941861102973124775833387761641
8312253425024428585099463893802780956875
480512025239854966165217361177264668998
605400211024780962947574919611190772190
2421274910745408489896803098202176937463
988703137886553535724201344928295181013
2360727967292366828938174377708285861738
6923419118821056039931897136902764957
2873611677423436913915805434168248453756
44956033144119154734705996815023058162
283481416848212382547039500068748687934
2443511318996762654537160671498338862
376092727055404960742761364196669691069
415632881443761647924853570624645297
1017479148555788979361358984368418098353
8698304986379614719437125255395670956959
3373897340866563797974591797540846328
764375319058415336121196405757410669241
3513317575639931957694472661893455604624
7705681628092456117577383276236904549
96127799721990917428771875511968838191
6170536383736618738259158397767213813729
10708481842848974640115514898758658942
65065260012709162567297367131024609459
11653870519999191376790721375206717200
2307917178155107581427486278918341247775
7163558233327488310657765384436373623653
9618259574134713743085914211927857777124
78252502647272332262890365274094096185
192543299820142448419522192374621937153
328025203331639166695555824310349947352
1332995861303608478979333117220779129420
5301867196687846297251405926396148841231
47514734780952926320155531512724577980
1759597967056922134669972747710489642408
28995218309779022395196103635041413037
6223227037987189991873163051852129553556
88577187679216906668092889919714535385
240243596975966340845802104734698992321
973734127968368883457813628173088783843
5460309667611416250716808652189628921749
7421853579168667852592885466878852189
619794237568132564331664105228644386952
74374048297433834355890974894855510
18621484942711680324996839335148693306
10380036183808027953427902293278976867
666636067911129586324020913367654725599
2535836933794094711690473997422939692362
288768198241555458985724466046441382357
5373115787138973591151143254979851344611
569438626346723710151479646254463449586
3407946328789917478581560849984735401
71144527176419415362086024392199122648
9319476565105581383787082137795775813284
935382956235189019945267895922951341352
8851247482952467935248542914459708435
479134142266107237679444230749303744725
416816424009098556113863485722073141860
2126818376268279693483601154153474860
52571138279751041797547709885594653028
27585692253647866982340258303688639645
71663608981719182060862299142360277824
756223778881517544838254056259111965691
41489419544666367618708656789485037165
368272262182827274645965774269337602096
4790692396885389802945851271935031605593
3982367081936362429923372433743543846853
9459955277896969390683143418163368419869
8754991674879270193829279560684144421
54597892962157855936942552017506831649
287873551454775417444888364476452732563
2958639110563147960588838391423797133757
7542340930073235715842089840662044861173
5092870273101735156133922122667496265521
1162391242835085387521793511487347856908
5164442365235084209950412626279663920
997165275744997373459957021971552618998
579031449554239138728385593961316864155
809280011713806828153068299235661987574
889795714523309267339389401877276927946
4686441242786861418749919711189442852950
4088809679461158578941196431381185129
709182844864339898842518964203442682095
36603609182594538126053595640907132456
58707133395038208428510255185374016581
1892521088456648932262806333202348947302
6292531312231640778510968650527216207617
2210875497943724344969945483623355638
373134092228698471688348276600849882935
4856604957171208634760137282946797954919
9549903145387573757489702368565588077172
363192552867596195978391058940665087601
6057197792889969731111433799842757056
448627683261857924941646925217814746853
11151201468912398228481490786999656108
966174362853568286573578247333380004435
4633548985601374102197796938951757323285
19309387769631635540401281521265516910
16243452380189214771336751742380673418
116555157324352693117763915720016128920
510999993531748850839918764683859607349
231682959556824664925105459209657546396
8111995354535234296920634380649198588715
956548026634601201034668738986437109171
2814171651415432840261229078778880498591
30339561774134088623770916564508664968
460124264951794633644669063634999004689
453741021638699496104072224855468779
3788461554897498492785667484118523073929
2084691871474030116057525586389877995517
155755866333392505524014027481174007781
2197169824982327114441361872197611146482
644856733956163273559361572556548871953
4479329212403928391469347245476386447642
390097369168369314692603968255821884160
4755417226439869689151773097780574563345
2432210292287658725035936124866660395724
416486954618745990524348378282591527
663776399273994470764338468279915832700
88277118352691272559585261921101307849
8936707225191988611196387476843054006588
4455399115089851204640839820449020801022
764856377635333128076710361585291395024
74987604264096606558941198562094115891
971477014288949776745988944093794271525
502111462817323237619642190895586706900
901763822135148931621258153721550041177
20370191537107681688879905458546242691
8906746876408561210538518207989979002335
6550550121123825140936788709137553419617
836147271001749797091860489442673963788
2199854684437403673519517261958074311
9027599247208666809258604092347114002448
19233413104710845664959348038298348060
1562631418857292208827447150811819256356
3635907853193979326529102282246499892472
851551556905499179727876386852052915373
567391039370616624044218091027145699
289220893087196362626639510386633723651
48656196285534602481622208688749446198
2339612886061876128583523614645683862042
93081378439191691957216346923187883547
91208894380494119285701471693899377033
620154558906516093913158525914239113039
1618307943816250430890973535795946771824
612674898537577793177643579098956016
96687584174426482766139987739740838712
90604446953650445822893659849203322721
55382059742662885974355597638566931973
240915114535148344283282895291174927503
80654880424260778962072571443371585686
44647390105798722827824692215091045632
340439413362932171896060503841588417687
322647543215851919037903420585958989265
2443539554199941334283312063987625333633
4070975672059108993416606498976631413760
8070671069390809558002056507718871243
5836597582357484594414415335717313288125
847916942037937817835609646336334771995
937317594517193389429784917755508144847
2510133047199673886344848260632742096608
87643495919710861488751922797541584477
933635466584726489213647204233617702913
75381146359331384586087187381723508680
23895030857055233484472831435258604539
1594819811063980874496608932437612242903
884196014666625185810534168400258369298
293429699884798667199558762321429045906
4221231432509496561261801489806362282932
8242774489995779473137503451989772285595
61508867963688250711821374211043918167
145010517841698175384550139563557012898
95471771026301942532655562984277052449
2659490211406636399573772403341086571248
796229755449874192519338381303654433991
59458700283913967281151316691036831316
6003103181319436480144270841924547767
7507509263748395570513630253829938102
505922492824432846401067811738027541
65751301227685474901956947395352417902
29331209425540768069852532216087396399
30715442763237164652709403453899114141
561231199275864614077641636189968215394
6891812318067464864687165628846086325817
9581583666891817556698905253180535347865
781884617628968128459312088160359864726
5939243926206425620198494418435824858668
2482484721913870365014680113310851680
7225630840481620688543087645273553318720
6206997283553006161456167706179220207192
164178129993129815041139917484579025694
46995293712655807044674163761139173648
241291969804975631590754883858916577851
6499586141675811390742585398591953134
373416652865426291473133618726476628462
5081979122014988395469249081298996781572
567507270638235347444073721619344546544
67487286512899909688144793790783293739
6168164880171025475837098724978857143407
3268668992428389726027243273840636813284
89779637235668585664917403778931233788
54527720578447326402050545910535318160
4384113434531158251977594905991265469141
642199387890188560127814233582658712518
614671450749553544799559700432198578
2456615920164637627429444774249974858394
399694948624167210770588146183199554760
1462787324382829482780950798819049840
90179171841632593689697256174997534781
53001963943987395047229187144814247902
527461310995539763219348895387559056340
788772297576990176957865449988525997170
805437756101871586415289986811121952578
55211157196483341103415393438221091382
57392529603452031777722463555732833362
1921803987344961614418376505138459437886
222338039741744265924287401919374597501
770292276832252670098444803443322987793
6427926384591727122054248951268081501261
2075934213326171464889591309258812161424
6784117416458947937291265487507547776205
189237636435685621699981086530965391
6287675556597451811186497865912561648816
665486845747942114691635132523217762846
9375964671085997286859708342938447571555
803933163392853397201348293976270902707
5878303958073367932998854685304773735084
14773358954132707481516959135412828732
594383737371519854617981637453492369494
8133611641831455586679449422447073714343
4089689478919238479268939719703775409755
246519385033996766275625047538237243516
743729783652396539113190472993617358988
5985579038979625210388063452657274479477
363133492865818387958881131924960208384
770954487648965319297628540377525777734
3408401687076934789128608292129829472137
9645992618983875897770672390426124576650
40787115115362889294909474599456499818
893081539209213218724095265568001444366
33548947073365386828213671238715194144
416534014818797716102592590367053444611
634648706892761574809325314922446072163
378176072285335384514449415656863573094
14736821315832584266379168403554509374
511685343759069454512353030823054055
222199018984733187487783970244751824046
965296782754857716031473952313951702583
5562650674642205229416079458121252939100
8276837549617454126313946859288022201131
83475323321622674671575507351280466598
2574927056205236902612852045890727828447
8104303971558451657111383146646295327077
20493660525564222720925855765141599757
115778687339312832562219690658089257432
6791267330818971935990201937395595935681
91452332296415636500661120431762430826
8691452078346946826223846069336876274
916538084669372850392162646277340821780
880083339495253021091262109848673404469
413733284875902331116068531722992148352
5468952268134682500472976027334877038883
894831358297928133547958262623251603547
4383397160487962570741134777550396187315
969230794574514361727351151688898466
98297445997390915605219836231575388778
822117947661117552678769714462633261763
62861438242738825561923247848958296575
80398261567829338043114675250081908775
7692642647208959626224772477512043270
5604149649338962428669433123156605252
6301536235537147912583248533309419021790
453742574176143911449838906218053657034
8802783218627750243564686101333539903303
681485590125297180170626381899859418511
166790683746454395949232320818513339816
5772190481726408396730779285993460723380
949647301188770950207281609750613385623
218591323362872628097017869774419119579
8782618557818668440455032031903933579221
382376756381723247297824476800437833028
40769274697015424719288175052715679406
6923797151312277387558697531978678032288
1710838656279881317777172691568780861993
892678495488132133123781032551058059536
250700629254214897048521614436869569151
4618916714597449761976697538988894115781
8557748596456061645586702218403295909978
45678213962953012553617291985493009351
93206041373073643575588126885180913276
98019594837582655947026285780902859403
13758937611172499995140555613440964686
8757730760021800999364621218858556974722
352673113016220933928164468720530569439
9986482669886653198228927933225878813
9723871106750657203361797332071721106
3321756460189867213658552510982624892742
3334252052193202374415354023691561653622
435128708712686851417127350122329361275
17621966328842337160537418941012984606
326970944898845482060334849394740593111
3655337590153063904512242366347757924658
779161223600623517184387572665823085627
846748163075567257268463074621737926556
802715220044803835961921892357161124204
758216302903662167265923746165523413727
3303191148121850728921868761393486467411
12616726154190094003269848595205375321
756014143897379995897967574122438817111
983883397336583823141144296925651007786
8697176144758395219387226126288423744698
373076778728514139064347056286170202378
498223554903652743680741844328839132994
6198565962030583405105750481362219182
396032251175116352928614086658950398505
8393421815411012333696874408231850161891
960121332260971225182132643356502864
3104546690883110274251284087642847167313
6409804354262395832859186353761266258329
757777223170515153579107560397650374990
326517781327406839081192245881832852502
5368138293738921131193645834736836646562
671822652140312417595087726368710782929
684949177346698876367418313469598612424
3821847174089249721671587656284320267815
1776501127041853579591069359446555605787
271260053503001956229524770573562123772
86698672363736578923309112748455962505
4172666732414369117226593339473650399635
362063281183733829097367527784446948540
110866588959434778126429539515473378847
9963801551052081837820832926488661022278
77611405589239948815007429846332194953
948218497799728313056524226866147707272
5922551197824914509313163157469234933268
829471686397275202764531817743740433866
31989905837864558329425456725867584990
205091876119738128344079821185224987315
8564588785716864261334206697286755285694
4631210257855767928198347320719987961602
659767546393899895366230282319420282062
159592813147939858672339227254067377316
276343481100981972623519669908887999
2791585029816077579379461177595413478671
5465616426392039400765658735633842326987
660576708074423044456503424181350103602
7717489399459214718742832412365896606252
636466864387762353349466830870174639669
867666759122586096329039008207694136108
6353360488973608898997232660320053076565
1869842496423951587275402300692073535667
98588824766085451592347330617448152668
301099241677744236995228675943166838151
7628669091578019834910964217487815315764
8848770079469243165874304898269526313459
5163218974882093738195292968426112296116
1290894492931973258674651782176653125168
2470303873935506938614201409653599023234
790449437483636294088058947232849307407
685590521135859383309347540621257586535
865826043347234758431081124949954371277
520081547085745631352064420762730599579
6314332770194822591118201825714411349733
4722115282957737171618493714402687456267
504298657385376996422710745798114380884
996553518431656393325251294980057369606
6272675977655055703514151944147334969191
9150404226761606453037783471284516013
2792935628154209109517896558329051652650
100384499937638986562323338338447226539
884354014902215343957328144533715912323
53103087289555377318103299449196769557
16915733388907477019069588367090794249
65446166179443170272401376218293582591
6935275934145946975337348910633431206649
4362182672323239997838107413203386317158
17712320513405947005798814288820694549
1765961360309483475030801472317235283893
9376116609029116307269816952788803824
6445542331043095203961704390500051205136
920076295977373843416103985452527598724
679545053503876189947851126956744002482
4917680399849413181022353164157411278
48629901387316324889328214947836453
2922974715806634675186511784625950149089
1765787952478046358242752107799899039565
3609121324475789665448212914902852299329
7954349161253063343397248980086715817
118248225747671498286262554659949541146
786344126460202754276349391255437313195
994746365031600664687323585131072205178
121088139998452376633714687187363646335
582733588094729420592567184981062898747
158845259490460262325414500427134594857
62797411956648702712434250868773374703
7820622597792762634497269579805287361579
1435959894375985475672315882437219382661
46611008665141139502236152463668903241
400662056138407473774297707189925684852
3255741357382317167173808205827897364718
3258863730328644556171632228714270292259
696198858795342412516757407132755621858
381956288738733893412049557796454109240
8962253960172646938254349738951238775247
725878868621398131496201922213546998064
1295515337233292845371789661272645523427
2761873580937735387845624867907255416648
6252324166458774166397178733924366013481
190726869376775594455903436749629458940
7681307392526251667380651254343592609101
8006672158184038475456127330830415049
1628726447237919908231884335823155296540
5005860634211031549175221269812152378718
4573348487199743969965615168137841477476
776449311977505862192663954820750191935
3965873877832898691075348770334836497023
5641122620545793449609123183463682226
5732476049683411790250558561534176217387
7686403036871664713214332584564277858882
6698146278414607776662812834826524986741
9646414834695169216526473902121686806263
4090305963945911206876678282874877102752
598566682921230701850583663117275479290
841123165552898548018793321854072742503
654369122986586971441264939452133562713
4049168822309407557329375833715244861644
7558359387679337476475763589297165398724
90885145360531074421769960823749971706
331386364256775831413642400879673793437
595071358834462516751971411639114076890
2753658254639122198257916493922765296792
7279557449282828501936791216522195383577
434835766449947315249234749366117766184
253697568749541910498240609920914393018
2909183992267692821298922937577688638995
365338767362385280920614932350361702013
7048423646422396899344563158373066159371
660540551702818870696345625712036005698
59397477908270183582148313820447919167
751273534863623390787799980328529056618
2476674578449430520157996636515256449253
721918191676413553909183235283414331736
43197684248162638108321778258979765
61483515848385213893919940262308103349
632512261279130890879845775659343774441
2137471878808266215727298493399615132210
761333677408684661973851847920572208184
11265364729572989788831170738739366150
276680644183706021539113922292129335304
850048028192319717727763913403686496291
976278984403389261694601750355032239109
458361885278776287936674926526448135049
735773984436477598264486142147145699949
22391957136222633464359144813413472299
271394205293958168257794767172143823064
5658709546387472054729999965724098083
8153252888407189279032538559469832725593
93017844451946914841524397742529171702
9298359773690741759880110617227276364
305491298401661817492334889534617213790
574690263265354554332688198185891808429
3582528980857747927960768879648792611549
7047142838974530429631927620621384503594
5519807669535941409092212124894222932034
29860298067007941591748191824215209255
8838585164758656917336194811328571823252
39559768230481541790716226352721825987
12849483361044744295638145174955217459
1757970844586390626647851148136690451864
9098434394836792417681309630563020888839
34949052733554862521374512347977731757
4954237889326447718382251090125665343
692746435442738592712195450208736044582
9489828129728395789346178479786989398942
98047705646591757199684291835862805180
6562771511249273849298613645417846611909
230470059999748860482257214747354364019
7866828895147762987122246011274618951440
661569158522958928922180727355018185373
983081328235579426796595197270536637149
390222182626927577750175118292503067
59231545195050582063693613785489868844
171211249133327342186999870688635326664
9643286721842404181261145826295978202032
547716342939668583994792577689116067253
427214433251231698017650997253252542143
1846560917753521599230381465850324903860
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]