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
6098222365753539710845347758254050312308
61485343834523602017719198339937932431
86280743786307135256425494506256883667
3938730616172392529819871257235894414382
6100264485458924334753086110743770066579
161218985901711360208979811900614431713
5180970453821110869710891989859955000
19407838280948971415041325426051373506
758470646446407344032004228121789513926
388586654829995331825251334796960996
76355013422923964615596396981312817556
152238986378694414861073221725105673
4430496244558048590457912504262889679857
785442765195394177576450466312686968683
929227217995408585202623119980516868091
1569311966342841889142410395632871883
547355917855955289953661720296899142880
2314837537863608624080418144629430724343
4750904519391757177198357147966431614805
6321810593495054590128125896111551386070
60223036951889112495822342373945226432
45379329159737925877489407437196439
8370154199454765289420489047614598352201
1995896379039105918696275793387885679939
127952627849372365217609631604665936628
29721773991336103042131415356671156792
44884102653520873452516326258035886728
9652287660386945186635496388772334474667
84851787525486375512227494188061701639
7352170818099463413682950294125058777
879813356228298383457505587834542866049
485726594130688481140538975709167282507
314152711819637382578289250373425932783
4471758382672308639130718545186248505
300627711273604643796737991687229194171
37757184114179604072383476261594834225
146570596446241099117203945363403689996
40382518770384359178951166168932866808
72281815226442833528269546315951814410
9703945733993649337511191531773071959179
2348405981461568500388729725791590656915
865897790611882623123443889381827924477
524037487379137582667788326121770705995
7627211538746162495238264202158738738301
1822267928837202175077609274358019066638
640616126841511143808304914574014491357
1137241650528565411623246208431626854317
23998206274929426588378563953795991055
7570599778664096342841781243863965077809
200783518629396351263100797094987026747
3052580561434547130096052518139883728976
3411626824872066640862061446736852922721
114373122019508956979323419948008045468
84498850196365166799983378270291882175
13615688632139474694534014992256978
9821678319367377170590438417664367047528
7408619148078073341355552748623336146132
3313376164397024256468968593928328364146
3455951710568301287297031259309450398752
2306172099051743774519198927340728318425
63343100276550861120631088926711221733
35565639968161179033201376259816388534
821937483606276832936616304255778086998
324444346815352080914871500290574595572
9629567518506098119044385233946214796291
2368848694732750222832393604630225721410
714849574491259791755853283120825096064
1195931267946974775906481182316809595
884178063784346554443048303143018764601
34116355470129475725343935873612018103
2546875089154954962461329133981651197846
459868983997135471207719417136068737551
634843992417214469854826649328404907
4533684587948402191458684812907722314700
61934163171283103867843376225862172253
44219812739910852497928422928113056872
79615432329044994833191839377969581430
1477717899588840172165882595554825221191
904196872814576322363474460281748037428
795963391693462350929751812995371363983
6720843612671266129487697852406264202732
91517114466428224976362118125261669177
91542697504333606276752765114442808439
69068511882119180046669513428439828960
7157280083213840236354117461457892238521
331099444239625711637539485520540106312
38917317481030111110159653700675992616
248634147313337648179497744199918709699
969543026751260140161160807937165087183
78410679727330362041480261676878536088
5917247844831753829316465286748281644630
2895157562085249546227572384489799103340
6273834026965120128978097103441560617268
8343713116193667735526788168893964419392
329090136469550752923848468871389099658
126512887116468823886639461627936125416
5951607715025746574178428438714136423882
949692633522337738583378917423391747577
3427495710947648880769515867706285488646
8371562052796623127958454683183338784724
3111591914097189536357180416283508169
99185697174712335819343944012872958253
77305890436209721334585039517330056050
196868109316282241061771349431147937892
3592172542452528513298785704329887465150
370430891866465636362450403529316949128
4393779571597594560983501398165564646762
8687398326176421745469805029622446867708
891686665398536137959330341921491122270
6527998125686156267573569157733365622
3386064965753742996281710639315231238
826298976262431787438462698618932944702
8763567122355756926545725431397847581354
156928713676339519419927877017445713267
469216181916482213913484999110630916640
309386995829831850695024749144985753106
9042558661846600127470998345238628543517
8567922814756794990725156898400391433612
595677910482380772747447118141697309043
5891129721847160277179515539376193521375
3605784146611735977011428111895395836671
7078451937571705442525082426311854401532
7773179250682371907710652033961514379070
1364209175405687778886457311157876103467
724336333514638731708527856118206134900
35126156901925150993254780998073495651
364770581032641635431545363970775439534
926590244076102388417886432496084028598
8279081527862746175062580736113267415
201492346114971188142388437638836444235
7507500212569029343281401049154537322701
224466073553568146869945510453207226168
664372749919439044871551199254378729725
45462823044197311023634879447225448948
5237331542823573399220915231186131802526
79894703546924209216679180223466926269
47905790517599889467307819691251687436
4357777435518260661434684723480156214341
6251772746743744491839528503433857533886
685468285726475559656717374670077465035
74292763885233623735551635832916919
674077435015628277696836161733372152294
794427766130915852385677360963797016523
300688386883422279522616296662313130
9025883664864756703505390819075177875
2932504839026948624323407706244018687850
5463543050666570260580418852133385627
662410057777805995008837825993816532909
360821397914134166756255960360993419716
9411631067896845823498963161390084573002
4102734269814233248328443129382069627310
9688281880695684550242202538827127388621
3065026111461739289562695160299387037
8209493172184591721722894894539630595569
796310219815915131409758982284601784
7806112966957010381020047609504723091563
2738338539658965863388205860821953688980
627765068101174391036102373623232084434
256558322271351843216253874290449865924
169893142678720540594021404665413669402
95463574515363267052588074667124082515
78452013814956436448883499897554940
497741917184853719141488455102182184121
624159294428901159223623635199164626
265661354291576805063395951930710466493
1842947472800873012715017209664761636
200305375763896786378819457872022936849
103440823412705778021032056204414807273
308925216476545873297594445852792003198
2246631474177399572326501314132728775645
8595344656047436788596455313238010373427
91473639640245132704449532457084786150
1984178921234445935220002978498316003965
962735669907230449624733751271426557068
174722645532635986437857871953214049674
281736576155486854537289248433058688381
407942767395732892550294473840858597168
986154694518731927222713233250754734
270033851338420615061105726448660054241
8106131628152679214370997627426376606504
2358267168921445999262998994191812545755
2369808954848653472816984594893940364382
588812267495301751619424220421267356675
548553352170920937159727498711658977341
1026357660679872886299310976225909121
97232574537080829299651751771195899209
651852981416952799725574312838895796892
5207790466232953378613829142451527626041
58002869249648862281905293291609052547
920672673295386054193335448526924696240
711624227264306320088388867376758897409
476352058552719526089236681853454076147
6473655696378801684784215697779195651504
812520801326572422123716355140177118914
6086261853191342748413221218502265461790
15284313232224814005625662069277064267
3088225477332024533485798416747401229
82374850338146989180202133902069581616
585619816967041693142757611197130358876
38558404658208994424575782793353287885
30426095283241847783142809319992415756
96476299601152885482227492187420592617
990526431302114586843215461876889387792
672779595408551553298769886608890769275
965026756187442477017644154402075831141
53014392844482054919837760159749068995
8511463038052901769681962198813217162113
926935830994029471452929153719228612
267746245282025687826629267377757671433
9430968189132021838471086668433067641323
8375474267301055211986027459256677648574
7805136768035971438956467260766840587659
37842457388241473356063955795912242759
288696924870804133965905536615747065107
109820575682981763402235253524785029375
2637328748321912113131729139720748835379
20771565962421360113101822042989171598
921708965967796510865636232756656658657
3838437620779194344181025591478420424585
4193442613751920691747843309757456543917
1078642457053033973549902820528476885766
423933816661616879930107362137512849519
2690889563013283825746551282698153722008
3932711089709013407114094106778797897
4402483434144870718418101422926720049838
893734349265305283987278768617686398732
152444228618582062754187752048054796949
876284499415660588251573881889747055649
9307671021863896802606541522666664801
8504740233759305113284406960810211721466
5734434588444706738331787556229542514765
471677551443273776883717590277731071354
750795150643119492634418880835568715352
8242994365896491541391045761297643242454
2714560175163291287354502364375435189080
7528564928975210365275314683836242168293
4910942337135938576869199048238012199805
1885487940014022450432587320852438254181
401442293753965269512483592325614016863
2373390481677004233569658141906225959606
6394966343063510589444629456713879955879
1800913832387165423476645623148791464007
249915068762842317755281234297420901219
73303381766064591375014199363261046920
98875618398817999525814282032305505899
8791915056379366518871838334956850252202
6886145115619621997240698784417772794088
270099063882809539847441814580571016823
16227926510857415405449977492011286651
341362769210922953888558787828250838071
33232364665250536566434448617681492183
907397286586860325873335306745811147056
32725094742830210199066885533581066476
51065393637486017877107934638464414312
41479802411392858439035848248385277886
17451128617134563222238488553572655308
4633122325357876894777443730664718706362
9377650381887856219118257578925623024220
7021911092249983342233765732490898848117
5982979119289113264130639734335096473
970833304445264932544329532484514723417
38498727689830601995444790806146842033
3583641874289129714869569239996830873
375882784746389090211561760525538588969
909657872321202795425749424215564999754
945441435535207259977429656374959691
566754019417926088382570208849428734249
335512946991400120524579946333960877691
7293298424922589128485754334961379982466
4657355346738650325654473981382258251528
1441391897292136443322105807536515994065
780767257113918113683751497932956011820
9121535645504737116761226310683830463454
296420552499504587323699645925454835489
158899039392937025949724170268637763868
2050242055597573675625989806511695478875
919181921270956835399877808380885261770
7479216462141033330558099422865056956832
7559348331158557962174453482617546089148
360593959696805869653931760947885284742
508956686744500358911661459657293634762
115318537833236675099742277784773066688
5518831410393465870463875144965181225385
5385795579194328598810722297128357882751
9555587628351352140290583681761669151591
19471643273779062644818519732196133295
23593965522738333367493361233822882755
40566491533602584309318285135672854859
7191435064375897394750468690284528282046
4099359573569556999448815317216853725342
2917287014387382954815911758807776507298
8439978389386233414782819480597511773
9607576707228840256977466117471726068
22729856476072530915159713902855352914
3550226945885668949899343557303971903643
4090806147382224501730171975545864201510
6379760014571314968457129080658090194052
4550527259062019239249596165522365968990
101795991504041649670385497764691634440
6840646650788322926087195874114169073
30961217465124478379756777606229664957
14123078951581346399167595599875472173
104768177093692930368493950141249093966
6922502642272636083832356949861754630
363286507922372134607633878356194503
218188045364183354714248300747698085162
54916347715230689022992848317788503826
9891220036917984101235489294882682668338
8413863338737381688875134498994563429
486277576245628281125699735628634386966
4842501222342970153742317418600639533148
5525633841406353392624185307120270348028
985231716539932456555895651360108311942
704975277958551715637493646995105620664
5024898423589293360048365734434578393575
5342410381538951724586087677941312315839
304753073173846122207299983953569634
103732473984934589982018056736759147374
716663871632708554416644133270489416535
361883843579651910490667778780292683742
437012623388257620591896164540666149974
4064759750017145894478339461731027227623
4284761741361663541850781028679624937793
44684771154926568037095977721879232190
15666108529442779661515257445316864329
8232767111308441949894916351762357474631
6856361394917178527047928067957635451130
7941823966815217730946527519650374167969
1167627326298774145440783244603383642990
11825161644455274244469483263560255276
71744260132720141530116060015063456779
8148645919439935719438035036445990629293
200771236468297123793070717742094118994
387048148460872941892457099919619728
27982539959443304461763611198069657735
462484696570533257091771608997962291284
53904503560888857776499371542387452558
782839544355968396057398677132487691639
11436665786171903329847208276271095037
324217546657557482515021047965892093792
338784153354413061499899149874587698919
8675178266275236893306294598952698733
54331566160018924590169240544317005125
9014880734558660929783053899588938078
50866113673648930338465823219472856954
5514405673116868897438888275473223367680
985137291999182516802633125379808418778
12016404992387729312936823750742849568
2099511299667310960210859627422138174451
981122655079570299094009098745199255896
7905336392651491341362076658996493526599
77585035817021448888955329104998195944
44378932975582511025935123614549928025
8394982845901254607938514776961468872321
8984315571230474912415433299593309612
61257401821599842732888917174942196534
232990893429907198792679154377168082590
434840578678754841672305589153923885115
783511562096685461183492605936251398755
5954570757330169519579766257063504086
7528524999636645869411764654138574017353
95272194392650921497234433825579401631
31855335747413001948468235247153356383
3262813799068237132515259326559295419
6305350888246991660756533803565894465604
3226211544019449662233983586265864837
8957203335369123186415843870438862723
236919448816709738246712600137067475166
75162287722639081477525123779897207676
1846998458296016445389935742449624646032
57656835580891689814303109488914445312
2491952275121184304966240171078441503
3143389261735650242015029057217088771551
87998301676353823692935270495684417116
7982152124323429962794794557925463349043
2535195047106097597518201259947959818106
564346335213743426641409393979794383944
600478662482280656174999877368253326409
37722803019630315611944763387744844610
8522428622539056123423978244904698889852
8757570997734808152441096446082781969
2395641557221842339213564937244560126612
5223126757612353041619432647869987329
63783386909685564856600390513440913318
831246313873226336732454983869909598094
9503604542097315661533647857254365658827
961279129008780893081041311994337075604
5528902372102542855543009811689228237664
4085424515704528375080931380316858371015
2617375966329201790767205013984652209703
9289372930781658274241518083684883299978
2084417149807488918451683536497157072995
2854555086781488813789955844492256452337
5288319548852446850284774794725822008747
26745993923184284907733910176187244708
549975232831730149209777157842792150563
652275075363258148141587931679367505610
70561061247033432513877738832232021535
164646173607902388961534846152753547271
4957702366644177661598068009403725653819
3593306468803789655696669962815043067718
591533874676672387439162658211319091164
3851515742491345399220697690944983326782
4177981310439242141994127473111936493
428362335926981625669107324256680622622
453851338029199816343125256518319046378
7824743449594084551892386483322549021300
8085590393127412993145442841745060292708
646715546430958287342706013741750611181
5826913093057013226446806639477357601643
9195863738405595535057866586154023837368
9161271126278562726391227428522046113
969053826821816132629633640630199919980
529234678827813798145431445296901608207
106151556276134444563867207878859487823
8840985151989716298040359124529940089758
8550314164734319341973488275624161552310
384052774128951864589352332716457310213
974312036032163161997646148417383172608
511382605563229428691035703863977984054
485818685689300717233727774654445704645
7310636554281797376194213932477892764675
794592494451415365835603015237262262325
9424144887363178027488691769702128356
502886504122741926263635663414221979825
999744343370761791194711413684913303595
7416108936745912472487724240915946687396
778254195130239138261492580322618174256
17575216105233735133637005163820404712
780368323083919120623483769932145258693
5190844941527787298663131356420815521009
3530558821994333140118597435317492477019
6356589498651289189478367004760130125591
2161391328697904824698954316438314994
85177285778998398519555245165726431740
441084828501309097949547981676276051611
80008285236747558567648243308913241960
9333732151678684883205963187066325827
5196747672126032923068575183388916038936
2304476155014732292896731202756329069897
983087722447344286785934539856992827156
96855431824095084856628738342657021236
98871128574162557013552792601761769267
26035824790288956619561432271917163557
3543392461146909265642649586230733036276
3451310866703903604652106045421150958281
841121424144764852354885875382310436624
4852937157096350136653662627609547454836
653098002505271234386971666902572482105
6408512280119582498581017501663789616346
8803331313904159362444147632927764337683
2215728636624174532310918458865747902576
7618902925348672708871266457746536531766
185382121488115931511053828632904021541
57227457293132138231439890667920332419
4903528730754351190780169643975327106376
48829460938975531931184212344148235744
9848233930991815990747771807995266712997
6242264398331338160566419963509243993950
117849749056309112557192872703786705435
296464015832739876862186229090743846805
932895664236750980755316219120076385462
255424302507666738932586210713698105678
66794362679172599196328289287953551775
94711199424543334386728418111650978565
4683297794745170731178462903625539754453
681996049067178432411488533984818123163
8982174429995560426629052604810186745867
45991216342449534646610869581228976615
9445904165634197564282055629771659124585
7935673735157103551652874868868863225325
12976472519270454639700866499972101395
642188053120700444463737664813962953896
6006174438397426239771746560577344761330
618876387141568229988323563543487825041
7423211341789011872372254278160514981651
380980203122581398492658170728532469395
258022546185306164913298915616883225322
42968740973446911147645570779744148231
1906702332781725830750557039790762529090
69325782859834265404192480777529066142
510281562634159229532924236278283948283
5132454099266241854555453714705954983091
65295153494650650053999400941473635771
1707752838858093278415317271954888271344
9199267216912777313152303280906411457214
19462944394826713234970427189397137850
112895409769945331231391929171227465279
54238874383193437406725683525860029512
187423342872010835985409023639320622343
311160865282226960586466323573813608910
447672226988510335411193433813513695155
254843172605452017337480948867647326879
62902509324864714656151801958076671444
240340827313588514660367136714389941154
770148868873884621091752750152220919715
2184704493381553272059521739759073882760
6762693342669626987881464737211491962662
601182795244895769548827232575418189
46831422103949764281507957669091496740
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]