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
974749021291740403947168273817514923462
8302857596084266672115346131664051212786
969887092986815158260357687951852922202
7311392679196112254251959811895681967287
67864098491049710921721265113996241579
7007917989262102711377062436842082601
23978551201770246545884326748811427790
903148946627538146679886925789203783908
6584323338105148903628552114882631208812
1894157422762321331576358595112915765367
95995779888249491212158426540712569627
723211248122847280658368343582466526002
98010475292817381569354933989016824215
155438596896419248094452054284095066902
375948047584135721719860214982499196471
8796377558664277593859805088521954577230
1782198750567788817717521463702051638196
7292628832637020710173228595615479635241
4898856911557312409131251229577552974203
52556416208942983484830606687527234701
1626855747473529766887408652843988791660
1623315668527185876649164318868135503588
804569181244584230172293515739714712000
8342379673048220911269309978654487218041
718725694768170331751751350626728253682
650531376837480968854144508470118471033
8498604821534968203283382205757509678
6877869692288208403278163188750792901161
1623462462565628708849315698902683982361
8820206883741282682022899633131199259663
2692282810942955750655225853943641032483
23555542711119857329811827325612617066
9328305550898251901620376745928344002492
6833425130320268491992440026594870970
4931348413443835540215731517472250916133
1352877718372177528488275513578098499685
3464448012784201711829546844694290775731
889452017193160223338748529251014279256
4368844745020694486999887278945089291
948135494281180969637338577433836767179
6025707234855734935748487587142125457170
9668875249663637687035554865250570025140
124262511589716716567188757817844254335
313244104129965878634507338167303427
5830327057593340798463371587558844516087
144597695464664193382749820582283321477
78232285974352956019702317986419187752
45824223335725926656712539649342220772
9286881740362282805447888760453875378732
5952727667813755983327778602701856444192
2801471918131877801327871641960311170
7310476996022671193585854882856542571374
6649703816229198899063915922737521898316
796862856575616514258772452616714332218
26371732549155137331711494239597111724
8569532546909974891998241799288487247756
559237326957784478552610818095594243
33785619166496836192188750153602036508
52448111962751097195754492788770720966
6134289773085864210762461733512516349061
224489551407631221059375186083626497405
425462498014883839395125317650782481854
2812317920051851596962843428308311047449
739886749966364161321061710253099904108
4805751549447053103448899272116407195
1138156836868771262684263352237856991840
3794198953343242371646812799163479821745
4044618669403113956515392227503776748556
777658354038797181613598100609695193255
603033129178946709928188345682503586
1814202566597688466985651892469825781760
144081262305121258069142248624924618086
998278857451377327593640146248447283104
177446273841309487822424473449623491690
2850934710106593330473914903957281653412
3292812148087856586940358638684213371062
675126594432403227558052553873334531416
9943502565317512655064804360768340254299
911089379199770997771236931831805693
837124436938042627539768141588772115360
84839040510325133440583764054197812599
762347942088114215594400256238948196126
34532782293807551650164454331765111833
618332733616827984021896620593071422940
652287973897467756053366524555625587884
4775241167404844441876004641891350654839
2346179628546326886195925509825386383821
6486189155286718653923039981169157552602
92185033347383049014774737745264979480
1822222532256810650001374115631774789
851868132892767100872002763390535814081
4132468841583177530921411470693857562316
863544394403145622097094558442571725704
388549832336731388226752820553037931356
125496253158420457862939146939825875565
748343458231882619687269483244529456067
5102437132578303202539552806608066887105
3803422656695012118382896451188893396
689374764458224533646275292607168275178
71324350056627305012197326622112605956
734959696312484962875895139759853525358
11948643147725938393178629611373696407
9654565253819867306599932179545308747
5152421715025228245850485216426849883153
5156470395058716482664755418255654534
491856637238659459208818995163044286543
14514185516279059806211339894834259448
521437612275300524190005989397370508078
7342673447347437303834761836320529387593
144633622996273392732055816870797525461
905150126775644228323408609971174349146
248660657921679802943023032960436505921
2271420489052903782979344735685933834857
432199612753197884695378195970419347454
797697376556699412352398332326393412890
2355556823488907722137087589419282918392
5045152523288830552563532812378598616917
413781542020791240565172831673770692989
2005540664007955846324955007416435686306
354573518269000549077365424704084347
662159871935929551696639290267470118959
700517352814492954603105295344395716127
74849277190697993472230526691794198077
96818210593764780145239659313396523350
218149549501840534754434676780848706615
27998614482925032359463911424877338727
643233681606852496375368725909835731825
443099482868033603490344769576547356295
5358815919653891189122216492268241558
1704534496139892912128455729646816879477
23992948176127091245009895081162519240
214345311260159084944778324448758068057
5117736257602590259061472987573357388655
179012391325921184877915278118143131830
817351325404632414694645727649676177204
678976964649530029783374653226864197603
878345438847107982154511276176288481
8355472618914528511997093750522261177525
136356812206324223664985269996725945594
8096747589138703481081202252867820992926
1318237191714286202013506510470798315439
4643627335486033287823449942479989817684
900865061894933487445525668385123895194
820397383198044787582033738678220069535
581114007940488313122159913596413508281
56888315704311446480767759366341879048
156944532960379292598042246080872332177
145671878723799330923576413291297395203
6746444473676948973335849627692662726873
636640267808863395464553513777945174317
195070943585345696368757924318065748342
357446183756475160926152484723514544410
685613496494744482722493837819014304792
299014105993689669641276759719259243462
361748902732976641268387615655383796618
2011716779007007621214629047430149069419
67562571797721653628888791505554937036
9459692484063605344920032721803470082009
33488913497650678212999097412174595891
6940622677659661999921164105358347564924
380673376612721026654569159677552452021
3410815662201918471475558293458268711261
3842638412379923813542738907104239145417
7565917720359900414474023868973356922756
913618091603980961051786402710197921
7798582559349982502917657800746866625
274256296078947071489792538646895807169
285793346394123364455037620628557657355
28892221575658727587096764183291194252
8052270168214325110081155184396897917051
3215859238131344870760323069559331523050
917781937330455292838571795510280607201
9997522919101466210332911764985066996418
609957026510148482648795157729139639583
611763722314211691216102639141426678386
9156968162776868100944664536648108352
85878091682398543220239990390985419422
8520826895653367039313538454069568583
415710519551641391687652282198853815573
8962867428787752111995779451649591384053
46079945605363620648584681694091123140
689018894717235875623818666479080634097
3138164877873624396718257678326934221
730645683006593354983392502278297276157
2583421595002800519331333087743256623629
635818036790256050957658724258518963756
639928544790846425322249695784669914521
614342968605645933036338145601720939999
773924134841544471429352034948485292415
104935417056330935894783792331265565979
2808162862392353711666604407368832521977
3587248720112180772620933722703891698796
125717353656493341871097060183094689983
4047359360578618804360039884111220216132
438613339521310854978268692932890666582
71934824791952104942696472081459593773
4779615856357500299686034259598228699448
2350724278356462702856093513223227836746
487130594778901678830357820941252901641
550221567330323668045316523526904761428
2565734485398160884811281262294385905076
962548139371431154442216215497661216907
81517255847929220004851853296113937415
3183793061359263183971147339611174338976
939889194008262368844743727319185769566
9080221869867329633873025225350240354215
132829312152579449890760190128874655
769159718699137918148871599522511827729
1417103624463773105189881659930421897909
19098219571344119563682672774948313257
178343997686779139878687218409336876319
1467198876594693983084556881537472647640
292158144505267170299518845929599753344
4458854828918803749468695764533939716119
170128999709136647815131596371568182254
150162156396828126775391097884732013496
769976944039265592422199816884026566375
3159887653149748225187099599553336381694
562322006118505157271739411405475872660
689946733589389557952710746446247566456
238852584107502464019596487567221657821
566167066859204774718573684501710447316
5063742333888407464762931377286780105560
8936838134103881400323806472352466498934
7424848592066214443616154021335272919667
20099646921126522478505354224333771648
50886257846543082328205963974353784890
929735165232208533052993050667766376800
642238242228483545175273231149204525268
70686371227275696560892967001218603740
28779074783392435285176325994986194251
400045129908267462765792480560655019217
7967335858262214835775234963271594107613
5053884711514507716768246545642956033500
292213338959221348004444862803672289716
418081281470892415125923387504054903461
911925137988929351331414325293137258659
5240340695736313267829718375256477699647
820363517855906838473126495847563676630
150890273011825149833421974762297009897
77186117283789198259598207852556908755
939396145691638665808987661383391647528
376127283760369363968849440851523092672
2915534180722026451469149086648359047676
6579621430637729606095506187601339628391
8776554385077506814989941739561675934221
35394597364739348226675791713882142495
160426777469474685732473645995854623706
6548216958196487837966834478953002590
807751349239527893907094628774261152618
95107688828610948078005934214685469673
28561510531366854968312800832464235000
2037538091463089804390225353907549902928
60231065441616695596699252645063366116
99785413871621757737838359397347506683
48834987115537350227033193233124159534
459481585260955870530217217424892642326
432302196389327216098504141962612459422
11550461544854133044335157779137793662
170587185282415447355988437756356285653
1020113037665681511051715804420755528928
5932995781273790396837392023555381808928
96498879609483658722604494585063162062
42389696347624841842789617279371291614
7767727598459116456820588709695864969400
9289928618484923362226203640608608846
81451171780543859905460431868950737177
5167255234122552154986831205442386288829
7423598325387761626031421118592999806141
454749255663582495588215952206168331331
17649368474735263172514270863219943485
1496323710796670374072981001831797653232
6776583170909180468054917828987367497586
51141323263379208185043323345571083892
7595571375814917745821098453415621176784
6574322949769707642052684704325833236774
90511381872417758122510848589135211686
2251341226558553212120483315357135036963
9492264628234418869320312742166296969925
925497843461402485371984451160978434212
7247890449248117612249307626595434811138
1890948577396745508491936208108974165106
28453629668548306675156240732247061533
8712731758876983553067755705197144098491
888060558298357241867199222913814518401
9823133890566954739884671499983785939569
240746836917910282611191812942983118774
8329650913408417314319377953956655675065
5441686546852802539267728278576161296305
68611605703512342382006970238044198014
2813282218128127578241045555287378118380
798486828845519512182599948145781759629
303269434793583088458619114274525896
762582584493649953626336829221210853565
945752153461174266425346718039041367361
6469218475929439601969865325938042747429
8786711563597253618276377986155442993158
2569787757582280516751872080311019661183
276621012508751160069876810440516065474
509286186772271771418034255577174236361
62558643250330791468447578671621635909
880219799395805152823611085353262792546
8311764620147920883230274332902766825783
8362164119303388445839356871809971999787
41553172268286033273183660980546812252
976242405470110377173016939452219996497
6210763711969252925593503044787018708
5677583773877322891593818870147824474
8323213636631551454612603221466363657
8464739217513756737349379496728699205337
104933494532526613274889596238777412687
8838980248486769359931429384557499726128
4587670866239390951954382765634380917217
4609907414651661593917943123401737044387
9473747379009319131344392871981330758206
9117301049197734788092897318852753599844
5994769713588068761170377232834198728
7954531787553891133820244791870538386
822779252279366432037412948996972958675
98366783711254812515438985108035239209
56363402855261823046397293618828113720
19837192655498033455407273469941721688
8371925397282514514110833585269375904711
5115346544333653293156487186918888847
4408740069819105558572069973635323129757
33926857301497212103703553428391597972
6076358444791783384678322820521615239867
69924679186658527154005340136427017814
90497951766176514081546045737513583224
2159292980083661564827934852847644492829
2444395199721858931020211935293536297918
507573408524328775029991498283633809214
6729614407331007865743135825446195014
604744888818387782552162857812760178854
817885047733174249630279411144334288010
1127298178238654446165947443331986585556
9500634773347730684773727390397155469962
6571912128914497653785867823819679297271
669242543755371731065617679316411321
1695641681127658222788521794496352631431
548588528639276948276343995979086188835
92759896919820106964855475577539738578
7996664197996993843368368743179972654
6914855285315478379442229555224258652
6176800694389897672787382077781076321129
373568485892871914314596886150732184736
4581829221851024466520843642600277281916
9652982081810408078567868622317818608
10651161924348711491379444248789512089
7448423960815604912635914093733885825327
98193612427237035715247481197363744271
4433223789638586255687897287465341852
7570277765235319454063299965181132536
474659983751360267585796872832942599
2478766784693322594876261279260139077631
24669717004328682453654673366624671540
485238737169577150536143577212512471993
9255649911903153708170663865356790077023
454170423871613416953495817514062033078
3396456134377811913815191977116687681683
361860786107262253849149333696538460869
7633338871402205124353551184980832063746
6027525120371001934235412142807983682954
602113019022988657462545725323948094891
466858362689928273022773331358044814922
163817113340173173142167439481057344240
584465314918851635251154425755516605900
4428066504820468875697022782321056705
708046881729708711568949113633528456372
3214584790509904115414211855171156904243
191388795198790808695433951873397291537
788947502851939564133868106459841089202
6964455921178951863152921224182294958491
4808551785836112427296251372284299937142
7218344518669778237432245097595093762530
474425457445339393293553053232177249360
464999277901695975878109339279329503001
93729317619098721283495055043608977358
2093713766963844727694512826419373113940
4339959052532787272295111675540245763604
7546114194007382425774163392474631192225
3711781387713347340544596326358496215091
1290950458528267488459187323834775805534
976344709233192568730641043897156699521
639025638434052839086315809446575353466
8461742066371343339456005549605610029676
457437912055940525566165615679576844842
69916379943258746394014659991667276079
26216397862192846844221677535217485961
960568267560625257002927937894295083161
2778543948117168937534481360520075129007
84131922950674694731754493298482614997
8171803515527427404042792748887675688395
65045471903696731928426937687556107493
9871187642045554270710804893424063887057
1428397194296377592530241458945782485124
360386553871462816582119914754842165688
393913038451107476258761053640741634775
621244832126397299751896089895249118691
590178656145296773997155436866950933
540186212123584225210869215387332356920
421665743763307389379302983492668771282
697047002653239999029706166899965365968
55703731301751102565275156515374462336
8521223163091743571063154161662132964925
126562370681912529379316804380663965996
8874385066434526676132492615879665565745
5076300943337463974050906695664656421083
5470224731578248811442872820426258504407
788221372838711730057141692687198533648
7220261014879011206525494829307694532621
657713939731297373961487618639162262438
3692257196386492573793766565275571
7438569664049027732960174852707180575181
1406133572098013183027916021725565582334
4261582160917964792085317197546712347832
7372283957001010397788135731874071355
721737025018759910053247655908260972794
433501659817106955757884742511026068
112555386322435236149985465898344858842
5890750333056639251383133215300220125528
904589322030725897178464747942826657954
896746890401372784771758104841575582096
3893423851817882407065613771139846225194
704244129538366223141494991660495549603
598617054229384869869988634166293959801
708550592219857917382371979879459967
6298219342462347656412295339990334375277
40349588719298189363791248923496668524
441515484502815917254484255281629967657
272912293699619849649594105754826755617
299151421898348949101736195467894138343
8608522589321736371194999555558413169210
3192173974136147751035340411489488529
460879755693296490325070455793665052767
7752252638158848741379238068202396021913
188841916314719290199160890614718087431
9489436311424103292718814335356658232186
64093834799396317088973673134623184521
5246752413146432913689693782674440892360
628684455603111477892773957937357076899
552335695900487388634876490148212706659
6877561937022459905191737618173556092940
979463371054149146587039886336768462747
9028811665693167418891365793662853283471
4101614326807012396729444632363046709073
54280284010364424925956892928097487987
97525097664890267141148812714653767069
2834146698673619901784013280998910979130
6599156130272268288339554549111367902
14495161223614246631773939390279982277
846080117510626668857376446114372497739
18522330897922844417308721442228409631
7737760627868284637362109446898761997
698717704332626062541705354139298179532
1622341820158014964076965933852429809642
7514587812039218160365951659910135337353
59125761649664773268308176731981886748
848087902442958157520492635516363934692
64198832061362742863840485359181058484
1915313467466713398496003338769648047736
543175237022250969538895951716987047917
2828231792703112986871865111595288939447
1436393353934861693818364157335213811
8738250317374292613670876232714653577443
7426914983578497360883279693951984408664
8294210811609875631318812884801843986
829790162922779022843944293380638688264
2538519543716242921459691190519993774991
176711699303293830902558703052162392810
161312742139950773965815595358618486263
805765783875790577124142255830069554706
610148792624357271919551298393146486262
525846468553262841692036456052015877670
237530487614861637069088409909130764893
471896207465133682344952398094414335238
6622430026149788643350257558508399734786
32660757273248584238763008461237294253
1732467890716520729770641515384825268597
24268711126479714141511626520791724206
150036847409311316796317046746343505124
3978916357374404753646358163117212856
990866298611960349665517517383645320268
14369181708568611248702313433735912145
68078708390361280823993266850120111205
350718507037339452123249463500654034176
58776732192493678680986418442656460
3310240295888172200166094570927062474053
3794623193439459418698526816118847017731
518786208022584023412216283111434011856
3335784162958951160838039473256539299417
892666637136188616776612072993422933859
4768454737424623714942919363945242816975
1586443038837322517148409274868463488280
961488358128266047256757802435449466502
9661144513024503586685494883821197689141
6070690170642220902166852344598562098091
6503234178951208372918011144490278129
7053762719756747738793102061158862442513
18118333221254616939581169704416423189
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]