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
1658357470268183770617879739548116659601
504239626698281693284705106574464766435
931682753837499527624479145495014331621
9088053517759666402582046984274964868
734636668554072203038874860722841443931
1861836233047583231627453416628091214943
650153036679856616553937114714844096991
417155172337311122544886048882094312625
42732358402562674686333357853533415663
2614903558529504187670354635568034355544
397816502546296454167806667674348681568
5469668449217472152094722812689257855
8818179293145451149857318227141361046850
73674214405603838047939090406796613107
4481652678691515229828513122929010884677
5580897977817941330343794377758174628
718060652266813961173022681909938962728
209851807310638726989017898695864291503
89772494533593580514287401596918439325
44515871973881711687251395430399692700
329878168189152969753763394417591613590
1757119874953722469551451082851074829685
9422473561428642344792506974345734103108
1885858030705284710788244810282259834739
753881318951015842044222203310329262450
902410772158105655173445541157116941658
41578906289232079241147769753679863521
557881153866738555628698379619998787147
303435495072483898859546686309777593247
245578066053993827058415019924063628203
62056426614983086457630047680241697051
2978402843305364264839828958965987357077
89057091245377501366643814370841523012
26322433999157821251581473525729379134
21114970761170461144375332958042368121
6932869048046137863867653143456767766748
35292148310235394937169952878625212476
656851978382771633897375325830579507067
413852681878118819090509713689133716101
321164663157146819383611154609881149127
723534402482981474151245361388829719543
69811579904675281617559818192798954906
62818430200614993532991327771360275261
8724443723382344795874846576255119818300
1358940169088356588857052262110276974
3139721881744244851791899490568469144965
828461066937440824750301030853520242969
942392815351428107336094370637193376908
5911552585059012306130894283554817597905
82035373492584092396227987198344562877
940332421381484213423302915059243661
37168123719961303049877120793077231021
6916523818190694010434196019678516296
8971455074858606262516654165858749555453
808062161370438128610125686275057384651
466952037234143212177073912106362013782
695288347124815037225984471987233858717
656585438360643427788427617264015243020
5436223739663751720587634945892575578564
6145426786983791822935308861135060996654
8362771635922704776178208320261228961999
402863034407219228692229710776589285968
526852078242638611812312341515011039450
17226152758340051096009791389933002469
31495891763504816204921452512790982945
2468394822618171699553197284814827929431
46684786993517750661114249600573658614
715068291522789676889968979244948655910
382350679200117127728855297348051566027
559360387016985982095766737410256303300
8769251141924045255854422558133126539566
229272743017243268268583859572535109
7630879077247554983714747050180052865045
955590861361400851281819948696957706881
1033317076013652808144603172769959194163
98965989101683852255586572713840876997
742951872999191651016644646114490802550
58006797508729854045795397131384415584
3351437721089859625992393795406123122706
209357956441375437527014383499876306116
3519211617636519513257991765301539737
405116999986055512968723855444893613414
299428182992617158182617301136677065623
7397649095357843437047363502989872379160
25787723709843779011712527538234995431
856626226211514675031391259336447271513
38867249102449223344026975915924672031
2251434115131211519351341525928341561414
489105487229154103515371670265042895902
26299550749520961806169218689457667642
531851235973170283787004610211242884407
954399316890744276184338597925473885255
9125899182598927209928666983245732221462
965726427582468069405037950575281983767
57738274097417103486814379522489334753
7962115160247860264184767210214152167748
86465492691816447865386657893387294818
710294253055682443639711107024494079905
754868463724121715634685444931885489140
772176117958437457473154841309846602497
7512301198215257664166607124338692624
271410671776852635792805100392116572825
86463787772399887799812521941486203786
2280219829368204534291139755784068383940
804526363119049971065117089382629353865
8598971750133194535343978198438396775302
637399705483978379781464948472581594354
660642645415945661752768769988573864059
438642979847981830939211941485568446610
182490786834774952381503940912462445768
1839252062888672511896447844204299603098
432752503036879468475671581234838794584
558463015314154874538634205160783394015
1075405639884176909164917841260269903591
123665575605630264482713694446654338506
1561959686595272783125259528756911016946
768458409161781851982736340617623229475
5338218982857076589682948585849724942338
639629145639100037311447698594045229378
9148854365126460546617463196670141766
316090186673151645769292641784133852977
8715956263074849406197516646211696129833
67572141057958344017371176626298569094
3022931127534131212694609036275250483522
3249764676447054820988126182745541003621
2631652619801017946682248168476252129617
807879822571260223129018837683394219
1404846345973743956256267662901650921583
47091440694527461232757732045577909661
786394193603381423242487603937336759828
51068944216380244285562996412385634255
740124751751214394917342714947198269791
902653369503797567685515089932340541597
674221255868214923139493772865549991691
131443276737431848057208590647539271950
332022147707499392822944886476073445
486419855732736372745382005859964308939
208264363189708415946627501536863976563
814891743305398254635111298446974299759
913257680527815814472722375748262321611
2028343142389606770748942033481422897038
361235558635640440201893505592436426601
175842973168104876658488537439370001136
4551910968344064760747424036119922173978
593369918321327690268338993990835042786
349243005350439779653221806886673092361
642397364279802715298725571326297987170
87686729909898289822120736054834745499
702208057756282277656131878922958401878
927455736481209563748697720612547597712
22589236814988560750597555922836118211
71714013578197032402853840315592268154
239153244244351874024771099544674139364
37891351466743481178727021531155817
9570567936789561274207408566756781215
769037056818426288947593750664193338215
109561054631133559788392239689785631266
58976389791649948630621663606967336316
64111034438665279780717225518399546185
9896281919341774413369467836899897451225
6503767988123404613878117143963570737791
96659986263389046182486152939535344930
3371751282475894953771718366248693802
549251703804608368566872204779727611582
8035725247311263591665616661755173749331
93227857503841365269905929881916532507
65868474715073877839205869212855206307
2986210895282857155778641092877679889241
268763891589201170814091746207140627025
5734605333993210957731548319609596811990
836523277534877958946237080359171818069
787715774197346930069458411639873357086
336743017676580486166649209283180631924
936613797244557253777146108970235677945
5319736479559472673427269356389651956240
966790206751228419031368310370965497639
8604167720101178803189035204802286822763
985219451854777891741835824527971771035
54502473763425914625031848758183917785
5252885859972943571284795682790266241777
34243164788404291248608392835379106382
5227608968085282966031956676731732073365
6455882942134031726847033549381887325134
7921295272059439451726843276458980811204
6836542311962918139486024545255734585379
702746668324467397714634452967269995016
69796984253619377159466510619412916654
8286368449886592236450346160826856278241
426431532064907375986676950472816171200
2444729531678061602222941926570372049256
2589547121656782687527993824535068528283
38181473332153413442443432378830123510
22784999032544202549842902815068567
5041186336316309861743241980904934197888
685772307780215626813399864844025122277
2380791559185404579668902176864986032099
3559442314754705502895742646971487887836
559539129596168440321552307721124492502
296170415036271458564931892199273986942
7634579897893343158094971657930467678767
310441809756973687587941546393659288897
416847282463447174450092517573895489196
4280359562254820524131726224448817178751
5647456483381845811147559534008550631
19774379185455998431827985516376183944
79662471819084276109328980165517938
25408891486563797047353280477783268015
9838651176147645574260111833578858564887
468647639017726326643504980924124295065
4087214993082954366866996099281631347582
87581692405100997788228134530134839042
41712813581685702923439177876585809761
36754783651266943205911193092318991864
675916337805306941665585278914222708488
6791226712548171370024093227785721247
601477831664192990310185079377368729
8298534040643549412342141903229478095888
989682606162133317928450401612808349534
802140416364695740904937915496469757077
4775626284638527111416144921918464904982
1982156269248697285224222169354971899
239030673445807379309101107190730102148
54951436928320699312755233875319331747
349947071685033412238163141404753837376
50889749077957050856800587831562639319
928247551858251956644367384399624414242
926474064466966366862065985265547344595
9937648447331661317177047513489161252356
7991976890154582304941856845488469503642
4832592527768516096721221226712498445
80606156846442429971919560815967408974
9000527782304963838517872257274059216448
2277450495034167272667133582687149031838
7485312559779447522426448332810211609110
282521557351856790446883884144392587552
6736254655248253223245363482639118561787
8258595210633841136446813415981950023998
9885357659088064316552712087543257612377
6532603441388651416997981616506873820
3383448186106222826886505758420796257293
50219132206749542738772808385721847340
87359929741179967997034827814981127189
2773865047914881326356254678807689295832
3898449047364324174536767356798055357336
85596992423747129919025683749533926049
882447524214555577948224089313031799403
846628423338912684637783597926126345596
300286586901353279076318515425982672074
1293907421937944593155786912765989323627
518159524284355494411012765736954103949
74394783241687252247479562907020303433
37765730623170463190587864014040289425
5679173029384612774849569440822758433324
677813829398232439869778407965931597731
154413071802353199169796011179194563655
4499263545949089537627144514444947188297
847038369248957079407337649230631858855
8770980371246087653973839352469891762593
493277440435392718084867824533755605854
811152229939447313424806397625811098212
9185168776519536603275398458254012842967
468866373445242951462728911805273526987
3594247401453799343555342026749244081
2500267405807699685352770805984873933
9554690866203162413999708163206696370
13452413815643925315497489781177595429
6863629611863997897293058965985665092190
46801493773181378879926209041378062828
14693472026172796924304209967190033249
415969908366879220190509234808677649693
625214444253841182878794356073883747219
5030307626358052154554619055423557049305
8298223754235128266473833479345493857172
4980349799109654725351271097644483375577
604511429773834976084415134304022373508
58683533049256278873155059727810463386
7831172396553244250238471959347976529289
952361727197948016851301027381974168519
517230033271810091442083985832861941239
54081991309217769967998491652211912941
2182182859441114868731732377380226284924
231556148629860938218012806456926401845
77695761207524128125492133522104436486
73615369938449896801657403312948525130
416592739186924968918281468055126847131
2637655718707246282962623919207343185157
724567115704617422299870791962734841797
757433227670221231231309680533364883030
8809567523744817359776412902299298472522
34313612682519820440887604875314899826
8716548652076863777864809228996643009637
4789531295668578946728874740586338921
881014776890329235890123731124287945262
586163319809970177580627216252844628599
7669132754953015649076083989247383641022
692635321866343915495188460773558651998
560158805110740199245667552725767015339
816752805582244639470095421886489279938
246251279290546050868588284609635521691
64218212953422495503222966683195876706
594465163902569964189234134513389445546
6620249540564191404714868019540188573856
805424750456713935731991518822734173
9786780982222361515241691149543692465
952518618103295022192556445054178588358
18016427852527485595849178490581158918
380651315984636285819838043431549157364
728218640503063122771448493711986626321
7551643344117719154486232305245707891
18061826449243394701992170766648332795
340085164949066179855974637521021330
1837610238999810741428798804816653094415
548296949511608110832037283440727994614
687826989996106254828167586302146952260
361742401585830212742230775563298802460
52081972740757788938205320647086131943
2154990123387334533031462388848248701178
44934829399364672478019297056129598750
601900250504236607230926469631070933108
1662110969182223202768548941146034935
67889877139260723178978345463316621709
6341558578437352818602791225384663417
2898233757271237560432912822667224663799
7733556147188381190496666194911095897048
6216989500026973239889929749828773892
3695782242309190662625065173206376556804
68419313543621331607628099397752517741
1095249922217748916847222267656296105768
34183689625729189081831436547040469638
4420523145409291464075287506157449543973
4439507190016254915897804285342584165220
862224768102890802286188646533538274189
6371985244328670179977405098522157487149
4662171420048226440586227649471041751895
472439486329782412783193143978578808141
2221663052019245630668434489851353022346
2952858458257005994025881961142720581372
691695858517882464821167276293143949618
370024761775817374568491376656777624009
4753519087075768438813229201729038968
6113720150992817951389536791337672902
286554283233313394672856878541553857802
6649953159367162614643377804511781586562
778457754631573347826596440368925201596
986558210757120508292611622406262746482
347915475533826571447200926266287806869
40355262370266851133363852781339687239
1395494759603079839794338335700046501757
770793213677141921769879928889425002
7399637616222417988964356429662164149083
6031838860972427714281828370579822158
61779448609418454467465225849039066015
469451652187636582632198872234327598418
4483640393004155262013054491819925434607
7714985597368432773391153713420631116707
1568470395298736218448568675961556282558
5569144670744352537610483529924826081251
163631515658907575324094836536121518021
67092621352345598672370845570826617815
2325821972177964198363224019193772426039
622797491595427037792596625544372543249
708280141012297950624282145926485462695
619851381817399438829297136391530254128
840856357707317376551027816925950139486
431528619134932929663218696743052481491
1951722928375343612059421448288745462886
453846264347521921499986939835735379025
522321822601965116305758529735221166769
4260980016225520880642754341356757525181
250840474388017426565253977406895295
875593904679654591178169489469422352590
5383824014357375186932359295375110075747
7425997625408721285375524265723884247302
31811042688245333698172572672479423591
19094607685469570684558482726455826247
9979137845936595690820796760176135462992
974326758028764211242582592647427101423
7121512257663984487032163830497737837
6765986140011645559127389977498246738953
589742266503933772835790452940326049
26052789763922775694359580190132177868
6737937541512374556067009830152237883637
202024857181319768079985548121337854685
505253285432848900410973169169281251479
892968062191699896951949786415144512669
9207617673108990136221112816689820931835
9265467769493068442169193910617069521538
1600426233206477780048072735363597548213
32121595987279544036220656879387313074
303733895309480197339711608222977035985
581596113313945871032655493919833295659
96137377671644699515290143392941409641
4978452497934201264047206475407020979564
6421511255094629746160449068423674462571
155711228352721280149347397915572464544
41409556794357974388196540154195071637
4847397611521789677099904038241276578967
6849641853234538316947791667524615277030
74177863243118822606836609138876464223
26491663780027723664697483752253030304
8745865517568258427611807011202836607488
6119829343296636698691254348109406814
764851073200935635697677584457366089205
957553738698179954416166091311656700
4625527724707930325761658681270628181162
7713854996307950233842509263509670384
428510621220543697733081082627388062139
57518181222516137373246537970689695460
9121941406380325049363669852486439230
7231936055505430661476338885675358476
772898208837423419471746519111863437515
5950307860231999362989618552695880414763
66344226980679884406267099248926721664
831975595581882219330726584954996714306
4155107545978524115331341198227118837355
3418880291432189750035725368354819369137
773971997063456397545001644614683829130
871939616895409525764432373741868047074
9253125193421825448485171887392448578064
80913169603759523228147133877917882604
2252537639152219259520276841648428315221
8215910572588193140254451566151334913073
2569209355484207743465140082548371
4996204831344735406758087976403537868132
871526631201461230614857640690829522986
9392237087992987870641698252525434332484
69929412779352151709457884844551598267
455179651431443837776409616876926555306
334813367488907489911046822499803934323
4189713551738118360627743085268268209
503514728627381816812176514995885027171
12101865641720170725352850408555804411
3991822851732504305492633371945223172113
53226059159524333887055199857317888445
9427188588493831221327404438184557511518
7976589496187239574769119806378276872313
184272318663564576742365980861351474111
894421486697041510986669853936870814426
1888711764325840427788236112912920803871
6170495979137648144468134419461553960
6745298597293376100061836561855332091511
7173527591077459873074868903502330776817
572246786835509844538139843692522212
901820656253245960522623798878244495086
971184754451733876688741713611291688378
3543285513721359953914023524682921028961
3888822158521427576774443572232139511150
86332881858310068651455623573550378305
6285550619352649398079525188538495661203
464882855970626442791353913641116248148
6666524240072961554277149212966440136574
90951655705300232632875202513547565998
255016242067433972257347975281313303601
828289271519850158413597660201177291699
215488891102343244332853809662693195786
637431541639336879577242196128481546954
67873144398590136737524520928024365370
367157997126514903714568743662288479406
981233334304302382474208812362844323352
9398787123489343458623142538123939747
2140720638974935547029173076229493658107
683855676226734168276258176732415694765
23631330641385986414763883892411063433
7855554855138836329324415223732326750
45542716332788851125578628543970926207
410439997078409238867076922582783375951
627860054855555598483612634660175463527
4550456739036890203785173622256515489980
3586596422519873181595301184689729272027
9918521321695896457504778669881348668
67461244234671416870898299717363718211
5700481392462978413421677718411879564079
69556760257511346732845775361410894884
348362389666513261128132285163883096
7079409951403530333675403645606332649497
10000829342253407853498695605904810918390
8064178521294270272797522053834551676026
314370354965653560141952514221072937757
107037043350596912917099723734183817204
88506551338230419404487159396981304468
3858262252241922688397160497572732595
8283967730203521286462949573630971887979
5764583115064609842210561436460033128788
67451894111424674024394109441013152063
96464793397075018917736718370271397421
22906749701696541843078813811435763831
3593627881543969265852055429739876197559
9397103714873246259423819976440917978622
2886717577741795337431620657592384812
3820718068544236191833601454140159093404
7480464458211373947796662896156253759561
392334659609939430867431439663394198570
36322952255866189782329213491313157171
7381401320074613356824712150217601080
208862658167929107277782545770012037570
3703601979638209413092542482537520652900
380757951803578749308311109238393103793
951879672131595054143884887316696188
311691995515148650690941053204813805820
907585084677796432235478639727144752894
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]