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
4340474189323884173879155363111065968577
32109492831719089394952660972489778145
9524120311629911563437158624892034824166
4687449486427492427496512415526326274752
4802131519755874617479647557788918880
8084655039676516885463679340476379876799
974930055230681805377955337226070117277
83469341098390692358867598655662007205
4169948428687481618464604316859662502236
380344612258242829621885242464621543293
4872898632137841409879104256703891864957
184197268091406419495029079937041722874
1566543835846931755267346387337215985864
277922529435957688063814279307562677345
2054623609587739274189162700943252341
5610851483746353592579611399564314384887
6824925079841484704586475227272722942865
9228932847469715461236308081242428389048
305894216137799322255837532705816514459
162805659689567746634597069492772411854
966076748953195649989556165591451966458
621389119355742232517396072769053049376
12282522477510783963083204919797938464
2145389138846747896356611265452999465417
3932358079353443572629158856586669432
7093281916159209579666627741790082585778
35981987132167681367043614438584436974
5830633555491646287919772350625391118758
564273672493696193749980982644496044471
449794964375118634263154961394525703830
1197630282776206344186529076912517308836
864716847220770264070156525109917653528
127190193001618765252748112295564225409
106963684925272479622585431184346017399
62976658143622739855249644473196044516
2777820412652443734125189275248616909412
400723224650232155813454658486198543238
4132813715848947236688327733218317002658
290838525482323987541901315248184534402
7187855813857957272028404857205746591777
812083902277265896019138531860473008624
1039991864914008812821443731641856786467
322034766826608208245586555952769067947
810581331916189998899114964638722003477
3562231377348370800764497306512840111306
815681228762714171723677366321626071862
495757236736122434407506183276972217601
5364890769296522249945306985357466573402
8294957669907936538873246198269051738363
8884814719453464932198244088159295619752
17859224610384054559460975720161642923
247590884385559256003144422927566051765
380586345577394244849818504108989169975
326511787478555536989181188976391477014
3973341863688681721563386187895261418855
8084634868222442801166414727186968311192
578749902212733269317389021650336733538
950177152763351412569210358310163688006
877139909013273946593244410843938475796
7776113099911083753260165141342840116785
911478242826855963518604340486997073467
9676498292463569404753017932189321146034
9759585226232044662991854235811818495564
192293723029081783430175634768753876970
396313274622911342459336757176031316942
169716895895974414472978346852680889357
41611632658268218438262350332925327018
82828493235211877575712561589021208209
7982707553789010141735682589344588345034
3844189031637236241550117754984510113656
743858364834676521474583371472418156806
25623717579451726087133163934843068172
77912944395200610813809600256456377806
311920972997836917304716694371494148773
96298170568869486682984850675368396434
664886867407513440054097189246162953890
5723603842515017933234591758667496445952
953184071702646556770063764507217246409
7262474379676299927985821450866530165607
95579589774529712841989565225572113445
3531720725078052442218402375732812658
1418418180076039437156037385766822509823
937064055743724652841899371300050009804
5048637571191630518073641455314112367
9804880534766847376841837664815524947811
987037588278475134649738059164378473288
93899568906565624389805276522676944118
2395897563501198394138089621988522464778
591479792756675767151978971760712822137
2135984566826968791670248193269493896830
2466946497032993680584296490176383548291
13042158589367114050688339651679647640
91882523488216551641197336736903989519
671676195339467341257264503325853218321
74196172509478354008852616108137927
960381655066910586771891400423888693865
782038185151927490396418662753398738814
959120367194438798719520819618255055814
286937278252818045665784216818715884295
8599515543603277129710068259299411316556
357298037919805459434032850992357269013
2153380331797398792734651814585781917021
179246266869658630414117774464321457107
4456484057539650100757125407120970965557
973960107865279938339479763302090404876
6898506227902065360274488878706217581889
63442173129285549452965560581076991370
9948228757407880455369941133820019888720
134272924810413830383724172857513596984
1540965496667329220094981583354445873454
20674145527241983477338493853885691195
5945925512225752801455314922156787978300
853654911890344699942071691745197492739
6749451566295976757278569758814465
580792074282985134497143089900068962232
1127986819369547430291845337596839231451
5266553137739446719773304065552362198745
925731793943535460416454341986666086311
1875910443259568145547218303425571389059
9122259517823023168963307691973328243
40358784835658851194747311924833016914
715326940483347673198681109791515351
903638564140880687901568750452939063638
665429149370929376965089710369961846200
369084004418694185108836805713873635681
78337294176816169372280274179441859
246984021023947504718364639764619851611
8420281660951037762457189439744935969817
8707697581319864761183806948275267948
4955181719434188760810223496283556351213
35617157893628748450599989765641585273
45836939221543034031028631155593456630
326248803866463166742498606036247516965
902094659042479148133084945618240921060
362069395084561119151160711926114534147
3130630184164631621878528693322259507
38157424470964669146986571926271006222
174840859906492835284332502660754894186
4580276453424032356056739723831156009852
9790909640614674197958697117386911477109
5955416779547032282955975368387669939509
5729678721581746167471756126136173402351
1576450914473488145143968860753522739717
97725635601747228890684573079669602417
611521208621382899583794766920098015415
144112708390710351883112443518469106666
21774599433271252916852973175158622469
272988053828497807945141120826191181198
4651229217167726233283839338929540038329
539952211170123932458214822197524655325
5048567196746957410164553253836222735696
3850853553949067927392811298458364958414
237182159270216971149673329424713408886
4434146936804117109745595142887843272877
74521706945196554444209759833983757588
419970403705832040352091546797013019552
6474980133912398221395375882947380491132
898962499219864970887663323171425693939
12765842583534306138320384214268276515
9849815538437960286590001079881659133883
7509880029583834222479678947990295276440
2053246211435850139712263041268848249614
5192372065938098396144794710909026334088
204724252571557796449355764879090907112
299932296009193818169789205980190213664
406829369326805500320591786355115413273
8530813996118876132028537469822116968856
68601688572301463028374296200624861121
395384461250373692294768533029423842753
434838730997319485094697865837091517
3783779824696582623068841491611419881384
885753826958763330056053128421120015986
1297344088856596465170108299771348085863
140217437615547975372021632314834235454
630922652033812167754668842123326086230
3233417298878331734579408231245324363233
272673915956366037596588006708998084619
50191815175813525019460724739137170295
798940117042646171116221586404442465823
682903612879451650531601585706061707197
857990473779893729255368120733712712140
8895551618503839216682747323538769598279
5212581095382962249039337473525699038770
9127742125541727954803948679321228398
732117631286521967533486838925432804828
8425700037721470633276049667206138359979
1063721954194902363674725516947339487970
81505734251740272637983911330740307905
812120337079749333988981125164579376629
502434632840461297662569710507076899598
697820639869188429531769532357856676217
89137948347548107524712176035252878401
8745494989214727345489873112274967584642
141490729386955593128581453054343009
115512733717163048868842093277084046744
3705626343574965883980395028298110578769
4948409762233947646712183713013906704
615457107103377457226434410118699488316
34695815033990461959033106147396338933
894464015291108172873358737839741201315
848832269028203759662725259165018191547
2078776217852311793520322287749243741891
49344380812743414492432688664962095687
997355445564447656235845485169382281119
660331296700854484842250130619607517612
1367916253241194170333712188606692829
878234567564495048174788612717032847940
2719558935608754559889051279362756603224
43478268775849184062051986530939246477
5169187846988805169966418067380474466858
64250256876632884846441096627190835071
744171395864504339466428603173313193299
55909127859942559068482232534103848756
1456517593155267775119756334871250988876
77114458169268502692576236441469514426
1870263342255171562123564528893694585798
5739285194169913589256848837836518089661
1349850396980702779930867546640658411
3289744486983426979132916429422288047
986597304761931672358577873044407149
9612777729771679409397656047702095359700
80592980653756199070877724948730564719
9402938435993484659656706311530450828693
9212616325206396831570659209120477346
42007261247543387843564939344684514800
5411547010392532995936392709132820542227
2324885924669647478069863396841961993685
866517102795848125783645335596184797765
5895103676642995447077212552914912233672
22881234505740740136399378453288578326
5385618098074832463475319254405073244527
70588526773421896720813560813077276520
4835902816976261484978406088315337171572
9623974911645316608125609854783118534909
1694857933240574713163927643311175992
1034231526826659737556508042545081400
101314718976231920277593450492988987972
497999385379355967611862536174161087085
637137642694402832683559453907745261832
649382167018543440137224311937287049921
8799227518576371762172056536651852474293
741420058646389448330736622721248485759
7266315383301667566578548742460962775
301449182374932640959475771199633756157
96368607882454729427532541277042977335
4994197530024808163148563774835147324712
9815211689386843817628979498559883297331
9524921931056874483160945362226718139586
3852852841142847411935067771475615974797
82444438980834769723698913455114259783
70855617270677794727301517736638452705
943856579272751434989755030306364145898
585241852403982377744876789652086429936
502928286999913774751449197761557336208
1762810262716105431168277778785156875362
2235360545046248722614759822732267946251
794994383478969286247989701941330887676
5596461578962378489363746417222138322700
1493545736479189114863407822190946186224
3006124440389976613283704186632953716555
333574987871954660869718822365641638188
165981902174672357924378657365444681529
367389868750771266432855374463699749452
75122558346632766688940046955718131414
591897654504297899821526448283510454229
593252697013137559885862187473795206929
2828845132454945646972843986724257623148
3118464744274151842448973543889890725048
162821868290242355972963651519620998516
222569183422732846651601245637885626046
1850863551652662125558664702446826958648
9849116296859402960111858822829567779253
516819969429691439686054865725531047061
50439626507382925238865275784693392560
802766953242186525361596275776024374361
903884614294103443497720761404853143464
74495817211220251955779313539822329443
562253282146308097985783876273084054627
3198875479449454147272053563238822691667
5329854481486558855136865149771056296052
6642705883523282478051259209458212687830
4045158567321584679897626568911141042998
260407395629608315323608889568476814902
5387533343521508649729361769347388675706
276083668503733113437246552334542574038
542034692810210921265056080104378248858
872288523887662366868211184104740262954
903439521460269875488641584418266965858
93254588719562362496815411975366313489
14657170389242529135776224616274281382
7009637823062639110790605092763527688465
134735488694593553234568854725133715743
1836898548908066532033553730258038741599
8263505428363599712450857896540969328070
5542136772799388484367553274763328665159
97867155781858347022108912776206713760
552899503798459391688433925609625126710
9171685092262536948053288008998195333402
38275717877999034148638327238928709744
547757596372207588538994682175823462237
65871906346552850138749991338715048241
281167886367227245473108653645125828
63586413467147337436102565497894227896
6829400476383028415885644803964823226772
999391723064302296782932190952444958026
635585282497107853423477999458216008833
637113134879625282770947691954336321694
858669705996794843908458973899437804
3901362729557901879964384562574342535244
642549421461623873698976621728421987165
508076301211898334337519064566526294485
69456955736615038166523821423912555903
5073138191281833233959869817123711876820
3118339447099525564864889072793888108743
899585469219038602282676580645661298948
1924317919758754537098495460256999663
11383124292917458925592372084598506147
305556099604337589747980645992028709398
9874421036588216765252927407406276089492
6759884880418867336246165207888433025642
2833584494268653933014092664470582815973
747879339765113876893364475543216996792
720846035020732429007612907142910935491
191420746915818966460447331774252922268
711282764318387878527317114653393719297
28345346877111507016279778105261249886
7036292082055326452872089599535042135413
9948647314622196426854616094192954845031
8851994994661099677133071003787129757487
6995726525519259440151609779904834664628
282072127444703462518662866499559664821
268376563240605183521043636174496733099
1144998979107968310172505112389377745884
997042239468228169295677695273629616230
5217535738378129306713540030992269109
797825174150743392835457290308182286600
596546334295795605838549415922149808378
942361534385164358741516070855710783452
569071658876604815783524602758690437961
1241917439828485495651559351995473327899
5086939918859792717224189912492564052424
389594156064971585724227750837590992719
114363075638414748131184784950881274123
6856248121060856898700519451836023813
2544505976605278762041794606996307290
728456513936663754145311109368748294426
86099519960944171683795315745296112258
9171802429958457699396583265767849277939
612349521842729239786805152225186845704
1502376070201569972296454197742590559221
164462947831257133761227502197089902156
84774957963154941851909528237893361271
8764806190295567791392504382246873846488
7208761534055085575017781504158325767339
465795543564021309870731612493567978404
93639988308149610630059964491778941
9240606987327872810388264677717231865053
1618805574391101755067284054802437109514
431719052254475773584682778302784001644
4372997359294335852168014898475265568196
670455126696930321457852674060778373707
11345436868420136831223226977167832883
5469616182541513152894038488129880463838
105984414307780654838810246460988695825
5997534194887528779931127328385643716
3706939299327378116366587351746634543665
837088055593787050639599615734187736597
29269902216976282176731337820694718345
554943078076464215161090299528247672521
3697960973194904907399645722747081962389
81764840497945292831716268198911323697
2872253419049225276859531049883429574
2558883529746676993286807724296257078315
59208673971685842267580486540249434845
7984396687303812592028214326430663313105
6789317684341450945731914633649565202511
744447756320899935340995308713989865489
460435752876422946677991708532348807528
5695224726735906351199991460640166767733
463242681200303474154717407278474696024
7247392115109089393963844537447951844803
335668179782561380280372401905364801774
931389048076202594317247034315884354356
199695959051869130162298815498990103582
89238354075380235007494673145853471263
587519539719847512840623901995579487681
6534644043608593232244271186155726261043
72107787170519478368491604848115965715
1579599031872035721239597896629327424009
888287797196774647786542553257812539085
98545655863746622482754071066414248889
2245377786925996422738613607058949515
3689938818712404433623963791266494137800
86225243226473781441334990714946103438
651819402298133617559206077271458728139
5466890095914484677955952079163551268315
312593962904525112711676205530138252
309564468804632839188736142849313103417
2557338074387661764845633069217541189726
263533524668205895086378242630216299131
3316870911025247353226385724753065975944
534265187603162259752241866100584164020
2466160810391664387476246236864710362358
3818447966704738522383136715977152749712
3271618221708463590595386563298595955519
62534998503141672669416617965563554834
47804623171316313136286296976822758930
4412290583594611108812902000285074152645
339121340482812455680849707455058286753
23694271863870021887409625821858728289
8345863975720025202670962739452927140
3712655350141444647646667893817038226308
3356535510151691564125626043726465737068
156196172566575705645562602285080106142
196255959439291811099901396827686042231
43116265222528017694845841627727565663
5943761962676513680742747062999015301559
8577207465673429299475446547971996328904
5733889928449282121571073507776983827992
9667586079123271655438353969272884499731
8342687923103251165678356340828852789632
46392054608844264433388434672496124721
541141542971137986239161205973033979145
32228189311709280279238936122481051842
9918336762204486857564568180321024922737
1276454281033914731348324297834537734092
13768978239555563152195716958840384393
621567984448503442632767198850443557873
33826416247466829868667241281683779546
508686298854101851746069431725055484
3945227523053120266333258460248654879221
137358896906926923832744784598872433615
72279603862053588178396485910325114884
2290153284167336748533301431234578449364
6815501873886219526484238983252491564784
77188163876579844980494021628936613666
8101652835907972247066776460979113429565
430173606205764241186769404665611803753
840618639886659857510324034619035003368
647281958253958734772503737887115022991
42009187602065595739873119144479786779
65406550795718782285414759536667729570
9922909474584944636879932573997733392513
503624918484615443478927970350166867296
55536078387260390464414634656217279618
7039684489239347803076424314472649232211
824534093147306240945785199092281665431
8061429528309720260887767899335913938911
4011572738047900266629865325539845046193
67496681879315114205311651399948483501
45385061730882490383165304087144802281
9289492673316827415984975171407013627323
52634442530520134177707568357860017965
267408519865809461451922587994747159547
942761576855333321639791473132798179450
541640629319130347986826371263458658000
484141976242495887996909745057748568407
7122682782077189810457317185383155336844
668571673861453147080615415826210769153
6337720642945449902019734650873490335445
753277457764774766447785381821262076196
200072855418336019584969847247236026320
416534072273844988379463892243895293605
295772783604366694018931385293108561923
681576121689545783908992545717338086102
265164855271977913878372731360013332483
84902628620777886236404333524814736934
9317768348958039408255232303161777057449
3380740530448798297884737762979513324337
5645917446343701870149915796899091625734
754724378655799145565417028155483712649
90227267666733068101152089145832812986
8614439981509586201221897857390617045943
350160498476797226963886444873267746552
2789125181375661157817658470929594894410
8079912176585274293728438777648869755921
2935835917837096342453414543604997883557
5984792038177264338854752492917321606422
3820990449553748324154876966397854852503
2520879078744549359894141450527913592638
9245306862719880146099223037452587936721
744186056735631537631494421825615244229
40429860458196426983957819731078027183
5497273547941748191263589492811915216180
4840496532096112576576488727824681333
3885964612912257986571682356465886883010
84794384439967793260363641887558218847
3218746827022947529455475524599775771168
60099556683915364504181987728093763332
2899416267577461932178563092188046558
966050543846379577928253952200925436916
3701180750721817732830181852142833585851
74895370847159822722656202153965956
758881978504933452935981540751979076254
308974283959686161892677426553368306149
265052056715541268054696481130623582009
625584041966325848664096126378071894364
70333100712885863705824518727236754482
2182925756879443147938412346980499545
430516415773696453037418348610624775933
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]