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
8312911720191639992257533440496171507372
305386319517581545248097247616819859810
9138700759055170756076921694836257527635
317950816810615443345572134488955573233
54544066624088846160992812347809853822
9527426376504951588784967047665638623337
294635298194546934670216114495185366133
8813929132617500502162112917994637897306
31404640620697416420164877935534193189
624457934263959999228453135635299666015
7992582623549382799854253601727277568745
1413752317098741821730805417323144984400
3411293842037810819731503391189926637603
2819743828644890166154829623714825049401
9140648074893063781333075711509939815838
1320808976947883283429563956765193082
9208156655679317455599027198934915005214
2644573849053879997689687007779153555
86921468928084478278741016377476164574
1447882254377885483660356596724720467072
8814944256785215152759264459889658814
8742375733645717890896135052499583023742
309441104919718799345165182240077666452
189049444869659149272551691770430448145
343891045116566233110352930605922993978
721141515078568430348554408164035773522
5235183940585058446497027514983693429501
21739200248607789812997466790076517487
7927732730265332298244896865631870448724
26582965471545780525033764730145267670
5933769223045682155885599005582637309279
9126218063419449172326068624686061603470
703526032033461794577567391470440057067
891691642033262657669299653793029658753
7669987626673530853791682841781655462
966211421120565837490784059818924205911
78108950938459828848130731257216759514
970755032925108540238483963341839937838
454946117114291226205007950420822938713
2486242868867161566328446276157255953
295790433326676657081718625585329604553
9394567650389538839576411604486722883321
5848991322559802339367934376665036542637
950149892229135809238812194862056956574
69328782782855569797452551614205050973
3248497388807099512546623979996469037223
7931300298074178792847693511770248098840
31520611379804467197359273871293738511
638565251122943961794725573713333667733
47717345393776628584547625211704699753
1172165754404115694114832578672079656521
328552909672347547208761949283675449899
3858977444761370233352313751548361169619
970197110084947725551611413476551756973
42494672561213162575924502949778542065
82323522130238763978799989487266594162
755788031360926831209691545549271754050
238974471295087533198469454487081194244
871541122764911757932777260665697544315
3191664136932362478431783774695382233985
423760344278231622857556452945509050
493622809291811891769441621515945018295
7586685979501455485292897290703446319264
243353653759344266919247697864647592116
4043578561267849267142326153974061034114
922498387459489624765752049923019873573
240292125448102232394762159515573216429
7774245081885687188498849277715794382962
20109468310812081672121364682462735169
6619885268587414424736335400415601263
2579723448801348551678523409301148123074
1165855725796363512523556494457331557982
1894497422567987107894686985979743107450
81819866103284957550231770245551209555
6806235692637800676340281210673883029346
7012135677486878131858413352479859323388
6981960537542601644328717643866691687544
300448632248520023533351428929444438
6192865859476731709871893593362522882814
788778791006509506757355308975151501099
73627883357132413976627749605798789682
790906178102819656558497621953863522573
89566036099290566046432321848260452434
7513754737166121809721988092775174396638
880231762310842317019159362651615254
948275992235151533160924454104060955040
879936874948819716738913600798853045674
1485860790314040819461938335554953054472
14442853878004901443539021739420146053
34333094015570272813741851506951366549
361743692886956714443099569774192042730
820235003763275595487584011247762751
43892696822355993417867616666846554419
6882904140563686429432285350759511909729
603797379404638168855798915176349143703
5511786163272869947823601444082747533
9909517335992928481838011429827655067246
945115301511561443941183736698414749723
4637156834779092666887231143644414633
6420367488853862748953558130774940304977
736163607053176757516341584314937933996
9402119866502418650489288619302850993680
703957364125768494935320849929826318766
82172802191124754156812392593318083487
69568115377845885584618265885889733319
25703764459871118467428994973371462169
5706632582897175830274461460486417337219
232040873158654811959939878193087329828
419692922498862890858662618266993452144
3493348849885066523833155311251223260
600918163770192178049588421718966808
850741604193739863855476177183792643967
281188434570499020254081447799053566426
97784766646331144783616062195965402603
3421325591934466746985604715204178619471
39608556234992125841075054744217581077
814087883480355964473712218074958872777
98338775650479673893825574519834571115
9251726230186572933798453096171513643076
7734539113861434566793555462076251879
760416287369073511672505943989835822112
209739842337156857384630835519369503168
3041507976146103351618845066955383893517
306083101263500787896203390564555974536
7864107918699560992754969923711897227034
98159376455214286382387315487696230601
68644039141038463897920721127463232569
48313153208991674817230970583863126168
5080890033408354728996394476182392759708
4533185110447054208447869035942182305356
265519687262415376940085378272139646812
399323341675808595188247928747176617338
38021822936241772459927182565009889323
3432457749675811584973925042775446683182
892778668467902742449573262247254565270
5359543980198643563941908668205865788939
99857630646690241216300412424037408
271806991862919252382489163125237787538
9798682017049744090872982330222992695
733771119584547973718334568621187893499
597285575676886551538099745345432176428
1972441014389470109825064547656763337792
6492511398086387539256327696852652418216
4494688629919095899835113490936016303089
8136598349119597609036545182459323504431
8344323810323935802579759874268252004622
652641124588650768239274248477965992497
452961851089368693466300241847991365201
2466406818411045632465803161259823053706
680178528807641261643463449944113683673
39829418505267629392430935781774784656
66983114537178475730057895815856902322
393816674331472691267657101958518279764
3672486496663670602519377738312002269
5303295210106923745066879811445252245043
2702926229515627271263468081686755806787
844513398040137875498700730950948013927
9358479881666423199837369006523949329
8061307097012725414335093074309419312839
332963791025249644898557596937675131727
3469517168652730865213856274910534464268
1434504781011927249179443568353474643438
720321486472712745201386791866961327
1196182824502832448819825066760593136516
220243435936560828647727876671759384216
5958639500584615975463148608698341787
4273873833881562793066471633597516513145
75208138177560223117631597556734376973
3899354637602304649659822203378016779824
317894876624462548062984443837499782688
9771380759342705368349946944199449585986
31683787882233495742859830860153427085
37734423114664723126592448982575362761
217741158584340367623224303137832206470
19841002889666153475875994688995129394
4028567177394815830285074111853990391609
4717758672857100959819284299469735887567
2347183986449093482676637985840165429
2062677723164953843498161988407741615778
1310606011737189607273689501677494126993
5270158170467365210091983475103782982905
80913867815638981225601947674053331582
533849833363716431299043856171052101800
5024664529969389719242019057928031522031
1088806283135551699161187613462848031662
6688886333211135311565438292120912016084
4104902762139727536418959451994239193641
425594801827861977576501912264036741409
525743748776101360623813087147937651840
509243799559756867688162395705832593562
65158199717739423259321531218882056545
317652856387859939268486161783461112297
9461543137763880897689054946672992946267
2436462244270532248179336949112255752
865842676825469115882162603020713788803
713530585984818216743870123469234636250
644976128334906235286085495994981328728
1096366164963452819910471226729589804208
540827635663649634205024553960791401203
6453708143525150171289766771862327234982
4606974914849087597529886192107680182707
944949995296015570363394378481745462
462172516247714217862484848189413722698
9950388930776439805354918457339434675572
2122285470192464387153393373437852388249
235616462077089341158274202769169449131
181634834876742034649551658382112926269
978948431663753184624465374146832686576
8326301910657653277658720463575907564
612382248213972956628900187720124056749
8700236521329546396615882080447558487
8854921722849554076243587954778948103
2421849060597595599988758248982853197145
2482553421494487800389444166447572777331
9568201136678340609328309019245150322461
56868730833180627266112581428346144472
9170843326503213777398998328540358452721
488897053845556223412958835987986122413
28258290370657031799936908416613174364
1221876735242883740945706250674317138847
4408213132295528117199721719601735532739
299364266901963624991491083535734068060
6496788924826771117131745836605035506935
766122988935787495861186588158794843874
97092554011075669085552249792209079
91182986731146954187777619264086736468
234992444286322983699320817229848754865
526745578697748287453616968941572658914
974352666323564740928098817737610611178
5602436851212748139689576998248466943
9365786423182849954825775675507631306415
2160419773949224273310453036131123819930
836633294108677632539944924417490172714
7104750412592675212240886533459451483102
4357873943673447124988076266815023521893
15763238291014629278815171898467699139
13943102429847089128143025637976048977
8410923039178292615498661969200861343373
42903134231170646523094381622739902410
917050709737548774064387812109140211317
470136707103199797599759391664816246678
647745592125206776738101383572247574026
153877231115976054574010289396743305742
4386229376785344635170161066493914955591
3367412165912947397673580635478447149
528937142118908113826743768494881512352
480713388456909829392417914451393936520
4874168659233786066371791199650841991
3349878535741412335322071732429689025323
191166138411192277975717690458256689341
8862497272554430311594215352639657548181
956058836197461910173829188887849902072
5289611784245763120426581181524536002861
3644197237687758459622563745101113560
9082674298612397131331676519810291013
5699575146296062660661837879656713375112
731073811563546497879972511501876741214
8403995639324648810472721740889326384642
1230501365299641943699276869304373089106
211553699025222615277864507838963942836
9696758865043918630100936482288406634
5474334589867047706991698619897769373
5344619441617675782680288636723456047171
96383622772454925629368848682245627073
524633846591221533304660733265656433406
822587437163451647914043686757489139224
3942778287500692326392443774159452710
16673392907318635053858882448522726609
4764710183724968797963062282715142679896
527465434509113551315809955492845105
94735195934996680681573718652949784645
33223926712872853749639932991631817582
4863643287782507545925505768292660702699
337538729353883282018242524497740363182
3493424123851856518347837978132391247
8846507480565447427210763581869010229202
319322248064876810387092869584546807413
731125429222132961355886925784079464151
7817612278062095273041591952563017916082
599677256269159430691100268113704779235
3504688647883746748760860637403875512
792924705034504814226677188806729969875
5034283576899613413845354546492977807209
7227262052597749591764311450670212417952
29774175612065229293213866608263286328
66765868631989455714050749761646457291
5506528049413165919161012086524374956373
6257977553697477726976053731400743696194
22805232277996165256977964699098674986
3756765583423287619173315645740431331510
6270632472044291436049578857456716239154
367763668975016325264878741858799475566
40656908297216727362746696312028128526
8965934045267537900024246342544541632480
164157897687456644893679260629650577261
769253524397909958981355555400589187047
7783650998669218175332716891706370765436
4101383786415352158554084324630065449875
5354134741111958164263361396258660053319
90894031132194004589907618495391081922
533119581307957485911830987870698200
837178025722832366322662609793729931213
9664704580615834439870006569525179965847
148075501389844280349261652885666733439
2403112181938104875733389414688177377580
945647527081726089284722641417259087907
2071989184248524347898439766671579161
448082626904267227622726544925222217249
7327242732269091582067395676719235
63280666214171095527126641563366956122
726605237232571637141455625616612423
435768128312510231918143481381428861433
713221014709512850155474550950081831980
3010935616832037269214393259189515386686
21523506803754631121777908836062537480
9329759029567163710242394059339545077035
350076716362334542797048151447420709178
567749266800561620929544653871474155897
6954899577702286473240313153781649617
951616308676508120484389347986380836144
1696657790507968455081752148762893427034
22085893997859814957566231999060526939
694721391377419710356003448724848894233
7060320691726654674338645974190642756744
4782468541846210358033326761996768798381
2536460286563673123890867302509166153403
2681159842857875686032591149413965226313
7137229393834471574256737152511764812136
994556682153912125935664215862735207021
7668557072694579189739182877253265054988
7961190771261256106132268010336050244234
6047633736256587828515303419544750472547
3855846417143955122462382839675683441672
1951259276502931697745421889850775514392
250782676103303827433676300265851823526
5328812991079516494922512336982312952079
400253368685579460133004803886465169477
426290122834324917454361264259258911307
6599440374699567937039975549546127525709
261900427107938187076834676676494259
51712637298070709132762962882318304118
66435280976838026279888234016601571192
2301362150772730539377906488194979235925
2229211293529591337460069170382432619575
97493028168383894823911530652504113848
63943524685411358927887618911481247133
1715599423191373959899464374484131179550
6895802892492721568136789301702870537640
856298001080997228032726572941635101956
2029999856429517378271974700892092749094
1469182099095226409689655160506523573008
5532240233258677606070718066461318533162
752918447035101782256792410441966728093
83218811672250383930115792965336795581
4346301796722342764452522875133128891468
920229285206199912376624204253594814158
1353295949118754475710387047627328012155
1509137620565456557029498788359148791175
961673236188467801643061087712260674832
2017157279979368274681654969893926425425
9751230881051931334140877392141115252180
86628382564569988375256231080547041503
7261887449057537370964298429889784862485
92664038681145721898369648919487242689
1318919471064957396424898966316929174
319750829679838227656358462158557619714
774127559468480478797817046241110369643
44683276260649020139104147632299888966
95649415366546593478468750269095393196
288612175674545433738252415514655075735
3080716293967109121695055232344286407918
229333099686111867621434621880829973588
145642514382866636476202227563958008184
68780717543817560928155709732022876503
483037862650569339461716861481692262309
85127447909244013981342869432152973521
623832377313500979861152932110826945735
63383171524463188835840311616540503
4116201988927277386669397965675039541091
247096677364587224739011275874551429
568519546472381749238167242749851174109
7436340680067964521491466355833960209625
61534285888694587629330882194987440415
515240994311734646354859105593465424970
32495716670840598762504974576482156306
509651662188604566247572899783584669454
3208704059707265814019093158954370527544
1726134137858289789760511910780673416076
41548590832810536505570269870391486694
1772918324234045760059795988755143636116
619052409469595745916288377581084373878
1510249141551813828751612179077907925
368998667454971341128523265803982179688
9167216657505691189280139107475480602741
28195451455449295944004185194813993833
76498573458841885474543097733787684966
5971628010542755290018898121229130295380
7572674798778056500047556341771518414668
941941337338334872357220919939618129895
9428502076977696448487793317239403825
999842840241587715896066500369318731944
103845415770303920281969834851783369669
485379859857342679814890342708251516330
771188664296850572062867417553952766435
8215205281539332945625801634146666694629
33966978557730909266134845405492467288
42389633930705391849427963590712848996
3237669131826248453648436026339703013
584241232802964416551000020884353737490
409142934883224716137596995427798104965
3955912681288519265038724391685216411743
957913334266511377753796522439038041432
98468075817116773813374254399188466866
261055293130773774398336759385248844480
617711053969427438418078611338644025410
45114481601960292696004481248695485949
46211854745530746399802864265963172863
739814754675812689572574180716684313980
993750469123958983890327279153943924291
701992830563934363810196596392297837743
911604578129428147295376827375026252477
6466791228896968919135256336118512972289
340124817388549941873453026788063960
869283184054691678557135761464078495750
6354544670972317152965231352824392058608
2242717681760572789589883946150938149
60238553919471254738932625714221697885
3392819223173632211697729282637851823526
859479882699709336843021266410445731134
4578987902714694215167318675586466242
5211239998047266919881137042579942446656
802952425689334686871139249670183689751
224439964244655358692355364125558668487
2145260660517036522430873011614088234189
714593085769926572443604268104147201820
7521565730099671242545153253563074353262
7173492813118288544891627948807675677275
691045171862244262704924738882060292864
7680639156994187392776476018314733831209
52444984561465593615148480272415035921
582184217462491931849341756770667504444
4179127668637915186571943480110938701814
860971776889244512062714338622115455534
9567224143324083233566231903963567704116
632641129391405742631537712466988888181
946368224755936045217464500873297135678
5203952260585794147920794067833112808786
597386135107392462287745388035733078308
2389713712024111951872804700200112316129
523015529076792998151694447678066493312
92977353210159561441729418532222292871
8355855757511562662445344604840176675509
6954848511969200218215744249985565807292
2877805945919458652766368848255721997988
325883629233621174165847994244137194042
9419680243747173251256725898696181349985
458247015267847572402255775845356341966
1333202943988312718971403401334720827683
714643414907449318442778246479619919929
79836466509563342667152653130586761173
6767223637082696612186855102450916215
8228371416569889515031298801884476473742
252496878361595667491753486409558894285
78851771361730278231931706079948633403
423619004338412439685025766616893169981
920369045305648305450692827988116873181
41116347965336640649230111589056449608
793777272149059849847028613358888802239
237447693276407027135836272952799484559
79265092705984287786747559431441760561
986335142843275287054864538838870471459
9804783985788203937156329578759382296420
741972738596532613114625900481596976384
755416516102793976537816853121337092470
5415587546453422374455215034933493104926
175141261121824536017112868819337236734
943454784737169340745012662293766998334
4824652542188678949515685461763654449463
7472493892064753928724319475582597565
48065099686698379443639678546779267695
198411897265970647017437523265463064368
55622649809325786258129650209832342552
90551136866465705677787615707235775983
564538051497839215017311667778956997
339984492253330563180438168733177673412
719393982511325229736892727877953376457
2851222524693878995129526055484952056627
24678876083373498595376557824906888
231891787310525293856345465280631128706
6126382614368602361413542024143188932983
56101339345400473238164058845289088205
74707526629504154619352050718069428364
787834445114206399455671784757097810824
153333786078654517782025969443424612684
430039836025384620464128139942744884
9629309845213952482761108097735623072516
7123937095509397860864374475136135606
411332113230506733931624431126121856310
22449849184746943273958420455344153026
1393462231681386752210304081241617604
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]