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
7256509858841848451662531612397157716281
23765673647999361167214438164925374124
1294742312901381873381021290440571761675
4164731644124688522487876355796769028
66776435570248941950421252729563471407
4407545192926171515691428083584543468846
239274335697176554668834843917037512016
95651373638392282975228718978114418793
6605664619813531406154264188686675999390
873344643014370640669559733478669617823
905134388934395122450297884426476628
6046632025193818622678903209387050298582
2922602242418574184194497887989920675628
866379975088683570253719719926751084063
5910364551058772892572178714693945569746
23465781174436144063828795122992736104
1779230887315577863810176681886947631920
8415857010408250546114221470250188072461
32672643136363324441082901245198674
3542597217861289735625458324536240269907
8560528185534983478035262200240172016997
604217436881728657967305504502199636902
64708291366640892792781848158633641042
9647405017354198487819338438587927318599
2340619046494826188658148362697065885467
9056414354202386858612395380816659281116
886096884433407632559305368279015218270
6627983884199440166267938158158161815550
42815857298697945357223471291821771555
750620467948955519859756196415673484122
3672423098146345247550819614617027195
889188112907428353529763015606571772092
9361638826599154551518700973790949051
9984778750597231899087329881862214007041
6819678128019224187986758568917183823215
145318001420875111948530327765763337
498149351094066873544245130231327284088
9185205996948407362647722104762465467132
9663492629178218832557298538175135014857
7927132192377635892069456756554797767737
9132963590203696561628767711972299327886
2437857419329240970693418048855770801679
37971824719559482444247333496272048914
2761478358800384174364999186169597742
7416677479818333969081803856151959259552
62107279986824532748582408980048654209
94671648348192840227736991867898798106
3480218222217810907530335527481976679953
1056764541433178578772115231199068354280
12878512738729848397731077114757405551
867060689494027158482893476784395194691
5434501098681951716582724319103358518
146997970046468054917182708922732029
2258568237846700315086055722393028366632
148456814381600744807535404745108194842
7876962215971888744618576394575657779514
6687275439355605136685468515827650137857
5973195083957807210680092736549373718821
744942341196800433667891749224410016243
9116704483194627134467990340907526312
955155761518265664354775777383386769774
9545224786258722699246673778605677151782
3386648798322678133143541146615459875514
8302288984856838340414165395563105075
64636707857279531005372530747831679220
257692861732242128944883674622668093118
284428473378156996194181899319762483927
779732415259174796380577852958848458663
63283521571896421372917896643131113431
6425938240714354140377503465311342009
2234808452413605242411169420587657812700
26134603868715338492269932860697521995
9020624778895886137130843600707344485764
547271938916242959422018197755089896584
40916904753278616409452526577315877273
5623383381934481606758198880301584989853
225753965587639724469451520254770986464
78824669047824164323065695792166213632
544976223581378322512907831129256044520
666965194232101742343731634581216551907
240075226490233283212669970694981725695
47961702605260056983069958853036653673
2476603059974089823615152422341954381830
1657612878355849879911215580289027274397
69335943123347757049036615246699741180
4289768393191946255094396513245737148370
190039821671228264504473236190926024542
29679574206675261956644116771565601369
33681095406252616449518723893626704476
7031339871804285570298664456440049762536
9038946223558089458046834272928654082916
4390446141859922184721087732752173167684
5844498613864044013903387938834486162
3801975130295155337833687828308717375837
676784592946412908465463554938956596748
6525482937095168876552965511437231739994
5076407388238157984074878151900649085243
703064747916347810166056621186329314694
2873560379547662917569355225501975372555
333227436030657276051111192552746487923
5063940798881343249519363658943836682416
1104215310657755693743418139386415545
5005277851048632325792725898990759765126
76282301381786489557180748853981702328
306897963120544994893626382754766536987
978844228420985811618146610837133118836
203669322249236911221389476789732959887
1540912575363085976411215877803182167
94864342600400730796391278334086201817
82963506995821234078931134372219621282
4238377383719331509370985641448484431170
70421209539943407036836756145461795727
87872524900480246998636361347835133633
17491096917425453772157709961316844705
396161913604922963273828942994052437611
25974035883482765135840191396379179580
788961885259881415025417516268911035122
5808888295362140714322094936440791291182
2347237296869176458153695900146929351375
5048734963925534162949062733592739205318
47236171163594857863413438834076676654
7811720647494426562623095107390552944892
174562521388328562054281072108753523080
5544996753077744687125748630353126271
6381839987811383934779142625359944785929
7723210269816004750603115111181113207
1380935832281498448645479614022022161
13115378134338966554861168971390189854
92228861339804742431697509283014607232
32851247412979264944716034768374645510
235722466418521391031465294679765714159
17697214475595254943404295534457808060
180729942317335907132431653186796106028
9610382585316476999964902974387828609213
3373465535326081688455583432174575153
124157168612093705083041641239052989718
8923468025781496329578813305640594692662
881947984731312268207586936185245228498
226354214832165265237974100457885197462
1435181527133528658288004876689623392998
502050953530128866488735418487638531484
1209151260302923857047168293453426874231
577986662970111473453521611279342479880
4169345635774182506427077685591219082181
5286923161157522294489728068981191871554
470454555473413672736534465187551687204
54577027028939316871952493466399520676
68636012250953375257184442480267751165
3342855023841828491669507088495332169340
6231191578714589896278747401477651291779
2647386012758714138081909538789222855658
518128689712586036447285609253498240161
4307020421869276787948198058295841350
146855939243755812931968933104268097597
7669351446704442110677621825542940468814
590418537251891346988949969298298766044
9783621421736018571863587751539974485082
439376174585975715355843393977566402826
668650085671327423113232536529068765363
644222121259641778124143844017421111355
228549183561652281287171945286872933161
541369249921122717786243916107515250
6816562560203924951568018578302182177996
165654849598777728067173469548762575028
793079108820249689653540423442643332612
7029740751005954249156095833224966787860
555483049675851465115196364830266990812
6342564014397301566171998216153071937113
56556252917685598927497123789420249633
4872526542303681537895996925656974611550
2156804491236860297921899206120664001
808835358006912718534505108819495076700
617760688329941588934817134288362595391
2287909389572526593732307190771660097850
88912033399349643424424847489419090465
295934834812184476479035469975797885900
977917468703815629223319496995866161844
41863227186824475927552207545957317313
966652199724541535579734796657850293762
6002515878763647311493621399698944965
609999934080279907221358957062485539
703179585632738254081232184667314411763
47467538333338058803827050191369277770
141931417264270433221498971360785657228
5589812623515966913857807167760481746674
674751099845691925681988482474326501761
799792379855106500657212961882716876387
450353414858970560469099522741111435158
1361301047362215271129245562549320428940
502739457895977189651177277659717796887
87033353385342215851354393182387564582
315612073064156475941514835357362202457
1702707528191517539996542274165147222641
3455447583116434588910991069326442352247
14597083728057541696506286753516004732
681812477862140648915341498337305976944
3888953729185208918218875155049475962
82816106428192518625852308573029113
8379343122814437440163595526735463046832
581230816315433402386863603233255433152
652599727658140197663099931772154951434
264233381811878957693887681549868216208
4480906337459147554856518234407272303158
85319944616300674441743718559757815389
45445509103330399282420672712704296841
639715442104791489608673327325483673732
30596587502545915077337739776460889367
263122004276741993332208327539722562666
20136666329383262489497109132932945773
78268360307789102868021271031129341002
617713239368221994977271115306344051825
25604799618234096392207503235877168923
160459437943416774527475944102619797698
292870966906599047724245950743066573195
753661846319225303555629469985726539223
201334558462329321393989449642471668894
256530602108158069713045764602421485425
78774614521607146327554697836092016221
380266161416733416805352818142458805830
5489938983055045570134173606170063517962
4018897973013309319532426237429757894181
86075043791514945948375573947486523113
51103389879966296876617747398995312533
1506374143308524419194872920412083939
2926394099841871838276237238119445310000
131729710163274477549193868955368008552
164594861638738167037917211811436991
71823675723679252572653850965047573999
287969245939523685144661110351621471058
37847113633530753261935378719357598335
6608403438622960637567325876944470209447
92925467327032577582942716993602299980
447672618115614118549716275619937592093
271834197265992449789876514843774077133
168165182516373989325539534134462569514
9402814333148166474023287321269777754177
49784767411955119574931999916589788758
725319580331048521010779095371948406315
458528525237527214432051334249753887347
4521437820457407589918538819516993164967
3011975599158538578257722459621430017264
5685606880385165218379016960836246966642
390971753581711687174947942747496694622
4379625227737076566480753389807233648979
1823575076397384895964039260982099779942
5250375658165492111723117858166420029260
2435531330406508604864168693620841124609
630032158715516262091407228154308294277
8993123268934012255491949530455364162558
5289809811389779771843678617525998557691
553294521785202235498917152588577186262
382269255059334037579344282705445646960
8628863648338939908783956332433327923
408594489331529267303738207582197892
876271026554145652921660065525405653
7853315882104144171654599606920664001717
245799914648731161092024268729886755439
1999719911907257747444493654771282616
3437354569636907620980773551981815408088
3826767511128551563477888597318648684
3666616942391848396744201383295552846579
1398507763966431059939339416486402729
1862987259837643112023374827909847368414
4608748419602708844447297818545658484368
91310885905207490211487696248113255587
489333058991231780852762335566131618026
3681839233631019505379924002818834689984
44904220635780582859552272665393629644
29429146431859146572556283081534864152
705520889100804711992094515604212325426
2449414385192796639785546674338671879484
506379527734264643026254796488067113130
540435424698850241367207669366353618735
5580615797887404772728588873398470915630
1152662185993522777559131235716731279467
355187038768587574552680942718117383456
2841622269220739932576620973912118815
4631220122107022471165422161971112701038
864286186036066337612077578219444506035
35627820859228095933146490282543721436
97115480388229911392644917288627449009
591251556765556770348544235863878677537
66946776194188611749553366968939805161
780090151353276756978322983337942388844
77887066274211968432334737059623157273
8008716010911495559331518229374613529301
1683125564134783681749529977412749502388
4314213076852557310359585975894035317937
570688327369390170567758137854455928695
63626712626363584242785717128311742838
28075369462722340997395849860892728771
484432169447686321252747200244196309106
744078315948051308311993372056877731
907798473806480971577159673810615029565
3528656431672094793964585223262144777303
842368216182962451462207973918538625110
371248945154397329922108506270243805961
83246685818244453492568330627664165440
643034927389132501146995418793093595457
847664466307769233789734228563668556651
479646622156254460821452172766956679523
58149759524599748787319532714515351386
782753183835591739497412450357237814212
898143292414195996147602234557358862824
7961977090312887929668434266999449362
912855985030583712523444395143216071094
5536170056465995529626652662374650464520
317464927887515670489902955192729952876
3758125855396704980514212337119419167837
685729389389158123074281128138679494552
17139012968963595802701382781526948759
34445025581964364917351433102658087510
6506149275963652804771246838275014402795
678134536846956478621197544769087033844
6597605761043020124320846835795622984670
2335617948896598168759014950561966722909
57986441163946667598366488347738299462
1223421552433924452129081773409983152148
53048927770037054484940480354274916253
910064349498224623894037162358062524848
8256750752798652589417750849049645716
746498444683261787885114268601766482898
772468201846141632698648777476789873672
8004111020133190334763195895118649438729
2543971270904583379235515440832624484101
81072065050497818578699438248267506287
6822307156822858073519396426873602110
735973395272583169980676252672795249164
288083298841760542381441990250771204377
49632294606128608541228928018922991689
103528936071117190889678426266657283285
7566412257303121705296037268142387619615
6040779736057520491544997374275093924965
3445618127989930641273385223667334304162
7591611483764033232312967503110791643926
3466873819765065812033947902877744729314
21606451488962669299234150446689841370
402272485859324882560593191160113368223
3673178283512835746038169330387361654
4718547575213568853571389553822158339920
6228106987761851791728788226958331838
52333924350991288718416891681384755367
6039498239542333755841033664251776077649
4825141319677906934510789508800744883336
78229549962256188667940930116613602222
254638666616355171731687418311501607487
8369387837086741177812813900756373553765
3633226931708278635462127278511877721
558476299595587782567048666132060704281
2417463996024443100372237594977015512599
8319265443215246577750848615181736186862
8547741592019395379535971339899911202670
9727500550534422162219817940104190878083
2339750278298567438497242616280190114691
6084720946555787146036866151815726832770
713811619208975032775320133597051014261
38539618689867396259125327594738761956
65117780504371115507894525820377475491
471118019197899725127206937217548205158
697691109144789552201153272669792166067
675736615127895883131533571950174135841
2222529010481346223981055856809360753171
1791471036315617684168618268960298275210
2122225871085781232154688223334376215749
525522222021850481792931571639249456128
2703491065234682196143579670857270634739
116672092474577489799601971738398769312
9588431144588008168234282932161869963
3509504011804724709844722021210269655588
990230615225859191199217292021732498747
5335306818835972261816575809275012132154
3254851128155966399936018171674244186158
10362015327357014845469130723578464848
163794256173190648594044833497791223
3031652734322163640552178367492638638883
2919520110127442132791254602176355287697
8742530764505232164890879919304885853267
980526528473632464017798843263852468643
2491998062462826243833639548149288869960
77673666141859669037496757601815348007
5283611363537477258385403710319336748814
494016379473444312715140450842164086266
1076256229149601738956973349953947037997
4315147989063207511875197448762283572588
26787820542550396137231178386152738151
686066915252846163811827265891534669543
1556841378798293785245461037162492124224
62353013688962850619905148224776947931
622133506838516616494030686260115566885
2666238888731424787932736358035855630
727632345278832762804833227382686639977
7551871925792549291782912698940767054898
683223079000693492220368181714079648833
7388860174205652437635254122442740971123
2367597617971667855347335316219998786828
190962125413439914998433303243010061558
379566449954306514134484127151426841419
8283820145123507136940755978655484144801
15309086529571539921145798955055927285
3392522924767921157763102623942846097249
935450653301425766934704681948548426811
1496133519594205396287515600627021896083
5169451477989429569532902434528026345904
291341256986624383745749924419342242685
147288449495651371298944377261617212815
6094123962416073896917808616878529949361
7681812072687628473719612386654049626525
779112848389853028332402935853948447504
45051489356330973612721325853429937408
44259584791637589788223397074124335697
5766177623254026644606913957373737994
592327846004757573423394661311135838576
6202485253324562221456509755664223815011
862826202424827262556954863179271307858
4488326750565895353076834748878049072107
10107796867756285241786986131969115559
1569820184935841447411744000955931914452
559089394339345599435122101662731224128
2980664264444833158327929987529067468
2740367861006840685984765846884140802710
65523466539258605450296956385626025757
63117328762395136631899250593552419336
334052009902272758157586856641753527184
440351977021941766073274225718076712319
518224670423361447047052692473845862794
4306398747665656243578032369184164884267
5012601958693856233987891971553020787362
886798816905549631176455547476019047337
2418699856221425667835108227649213287
864629853291948439031625857799794344485
559639744935875623037728407527516181633
5104115932798272382754731127330540609700
5850440999401414986346901960334295168404
165454950312092812319317354341297154500
7940728141756326566920773023749486614536
4641138051012801811185624985574777807839
2610579556711862289263567540930423473
24907074930946651388156756378562212
78226870777174024997351529083253465562
5411444770076307656366320752713187796
5250558395321671926655845509483288484897
60954558144210456332641714145669766512
69101495486522581986426460261842618839
7550240115009899822347813487252781072852
9227113626363010136813148845685245148850
8244258797413607509998610077072535489
1931361414772303760062809709689371939525
62447079746349735661375205572214296981
4145715498607473228653379339900957067553
719929227492031834181814059991071377
6716883632894999969781229749364265269896
740485422033901117536883890814887053861
997743737577317869754374850864851122643
3472479601642890329643551678648054944
36432522111138503273702886635487685130
83767351916325428148168291731837322878
124638579681211927551466732348962255473
685595213812463362397751361543684336485
1747213162522967294211383838135152866289
7526289311066596285689862980939074435146
761554794883371120329716304449956516531
203725996221965234881762411406760039882
5895524171776166687895583691715513262953
5292511966796051107944976537720099892058
5640127451427361918496639107829319249987
694659316566503972856402360853981866827
336317775471788358965261725838414098290
781238306500428159521818749075611400529
661037646598723782730626284542548573770
6194131910433831755839919581783195180
5061459279265433398866016144216371069335
79679969519536181599300724995136019579
131680911344473522279357510684367461701
99016749609464279029797920532862665625
5575579232725515373262957220798447015990
2209750020467055946814751259181204713
6947447467227671911963561556665929652864
5121403648872880111093957335141391075044
7200311295344650979843805425186211559894
96876282287578821364668657939223466345
1977732435051172992606161342153848493
975219633348623787227562194434269674162
60894663745562039948164121724954236943
3241637217616383332229432955957940724298
8525913556234913750785449236593996599135
2974516712317612559740303779964952717167
924536809951929367971593900503863963
1027662135470253710766214851881221712
3497496220811295772625999926969539723
28297744449192763682681055426947414970
8462757414736922685849632352117392209198
22175955579455433168735793491382945555
4514767417957618423177431209130732964092
8329196951824624828881261181207609691
683669128961496883124252453539425449
790218026975515590131714395200246326822
8952241731957684998042476974503465741268
8174851543314155660151763368717441545045
8260590876863103426091441370444063901336
2252760921652012652318465586674136883731
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]