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
4372383199989942919572936529056752263
409171813406956298596189221382287464876
97508622537699079822756880555999522258
4320520257232312649558944044915184085577
929339673803144392928589303345184906118
326082285626192049214753530664672309058
64245794661545321281499252136458766043
288271860137664814935351325924690128517
9802442171696962215764196835254194356
8106825042251270455769891914768745916
4057704765218098794023395731851622661692
816533341872954891812689038620356401071
4455101898619017199073966153170421942286
569711704523905939603263500581197731894
59248268478168128496352531373710411953
26367083262530673295343773647126096571
3848845663514318325716664162493779946541
279386152625256610459021775733872983826
8476176563357723258427251327723614505618
846944604665839956913258248261312087532
82524307715976836613615923622917628726
419616966515011507014937168930307
10206749951559129178302155952372725279
3419609863442673847451473613158510907963
6743445957684062453442521014514367158386
4453602646118990463513337236467548521362
30349203386545606329341297389896073698
8174686027683238743251475238142020761668
7411701840902120299555189493578498103458
1187904584999132949151336372306977914273
846099312619590104413589937649266527204
2034395789094068213949993224921160132140
4200637344566410821296302557081529936
30323881024207894860825494620926924900
802485918695148120098642324832481588944
735289792269103543486411771717334238397
8013544758234141400583838572303620751545
685325395536604358349747200640232943316
7595695060962712691580506527102946316896
308979592502110452931834987951729731461
1213653267501454454427382633657159168
467965354103429489298853261199752017538
772832583286962411265985046100791669339
2313556687079676901993952586940682782780
876625964025358756592204973712443129894
3100693118442987905721279273879704672
257148113291917862852248108835863331210
9756973879829586942125669689882434295628
3172326931303120397757614882240114789799
78728650367550786592715361172341597598
33779642342423882760319922064836090395
223485336201285423450579849447285740
640161225151191373999213035948485840
4869346762021382376716401529977631021410
731564297306868867731974940610671844510
4912566355673942306363022860839543352122
7913572563400463407764650618759012240
8214741853848575245833804307429295125145
728034291502880741955182368764483083367
754847370238397831168149506717062427840
3534896683062157868459723639885726843281
211882238224265691803872560792295304609
554577752034083352570892230511588037265
809079120769808204636755511142659104271
7396147894175435662835978032412694082869
81727656849192993252861826173278486
15447192274688281396501478018416338
6381610529699168681502055931214588559
6057604734249548518259193375795307797
6140596135506798185659547377731105062
111733321200274549725365287391094871314
849610226999332265629379209579281302575
3359326675991653485931639066515193078787
322889952424462537047281608568005867786
283094685512516046998243692032544064476
88532919770355775384036696477766107573
8490855489212015700832462968213233944569
4713108047707099193116254693191495992652
63298163285936476016567231560217211046
28817460242284576052590458414898555552
3126241384546876102016396728601525486679
590366523868610114409848864944375935938
50783099349187436309394669502379101757
66188148234537393576103345349908749881
684890662109946794379580976574488115576
1602758473599734792892358788479268995194
9697655347526776952580093692413465358
257929238919211313640847408554922793952
44608990901077122220760115245346337670
774010638205874789319747435170880593693
4271261457119977763101981625213324946
168129897134970165562201304652278912344
732163698606748128337083636118994543560
5451845418589673127574608564883973008444
939444009047234275961074569118747281181
8766208927199064859899007761427749121
44654919236576318111460691481637490801
238787564140492171551724442946874634887
118546102799754243792778718184271677899
57275086580357229398013865430024494556
7226753024378434802147632295723391425
2614890439679634742341006321703695507563
6791376121455453963252094625914874852459
5305309231561065465424229721410466414617
773110633435997171619556375255265028967
66312308182925372027358332926530808485
23818790449017845921243356789285164391
862246211036323261056847042533641383403
8941597428057144324374329383134652233116
45207344833057549075845537710627137182
513672897686299154796622984236378265273
753352642729678803851836708228045548530
18395434352663237966080269352579795365
5882677642052733980532619761357918058696
8722509447656044548575745282614315799465
3754462112069987734303896791157415809
2833189858658647942181846153200833296222
87677311266465430220537980740681924091
1715658092721435939380706843458374592425
623939024318145373792731518018876448684
5272805993649779638194057885840468049117
18918547571646533267904820292483222097
85714633744715665218782548805132391002
8970330050701386221476715949896898288588
4225870325595563259645106587813857458551
9277731229936002819354417653890874176
15569009977945802850905429234323949333
397682844485114138007982651076595276647
1916336986041728164935710897536517289
2824514976968789995669084794837835392070
306082436366134745254624489942915792881
4771840110411732528761031943336940835931
533610091614393636988872722574921595131
267969335003451248426291593887847056955
7210368834348089634251958124717563928208
116830126640592533347800175516872308154
21875767659723997824862389332691972526
8640514576424403218366162797136452402435
146880070982000941193473516698628715415
167163137376944048868956182154820436
8874449815305138767957773735802681483719
6588223158558919117341987977312450836397
8421174557155452137258473537477384479
5747293269836716855047227229136198828804
7933487635975991362959716166243257428649
884573557558665919453813860483381013653
8020666921023769601083847176368251219123
7226708448258012225198757673186824591578
533799965931607694875620224049147149692
5492240086614854727584165260720518846059
8831351641381728466205667652862978575
7466133438529773149425459470527825351452
398915556091437988297871897351592124729
798498683012024641716780672159198366
26817679561367871043543758230133768172
6327477485315135212541656537237645222
912541605802511349856948789990238185684
13132111350474581307948279610648809651
975554505944288027403173629716187064602
454044108830645392304591284579804693523
6186597281359906329337293181954576753248
34168296624190251818628236982882383209
216238231674128951108223240814757642168
4300899153936039260053275086259360076913
5581230061473561188863575032817925364532
90846259229754995166577717618330946256
64839153355817384414992924732865843129
1567871931032090898635842089250890473678
31035630358927524179330628139233403775
5256567940176967348231079242544164961893
6034882523717416811656731638466146928028
8444781390461962929394739813561543903857
6899723539677864129169398748879436235590
4847741556822322333513217289874939224760
841216276931274705779773389297718963292
3341182785743653813834349407614915412332
750479015624214166198900339158218736101
989521768731500375552127951165288119734
45012270877740654671372660833507562516
605416474879568010274004762127574391238
3625904844652078403533611425485964249786
478138685620563594817786838441985891305
95575208876331616241189853729119066182
8508312152938108210787342456994533986392
276389143904600699825727377133939433603
644527605913971343988607404336484836475
21598658801372828107784449043835915290
1425521090965961973440382702973031291968
795829149846986631516693293358697271825
971729666198238996096647011794619876593
850594692999416913782837234641056685251
891796245511460818154876340589794862580
562223576453264060950905447855179579663
45495196275261848586104843819365988046
821402237829330424139534294255955284236
68865835563546102861133179551666434415
320219138526953220372819550183459469
1202474834032339783528782126417868324242
174845335975479532015737269871293392823
5082976741918592952176174467326478935384
60321977234782138875508717124748149480
5732139147732818109584366192674021671329
6337620726503816279758031604847893134092
1950481586718199704268727168128185014803
27239715400984667203399557247016127525
9001708966843268663864741626386229723870
6998501443215654349978973843628957384110
4046432548536158962749153031273562559115
362659932913395510421105360535417673806
39175238233971256094989584695142839970
8023375869282404268934218566380923353636
5495145072282359179092205643167144197629
669561325479777491484389402991203503
327596157180807243492723850953268429368
84216943395139188573209831651447927349
7492863433856591708992186662632482607
882466723501563552988702127505929526660
8041160579998717827271034758447733721
245410993645999149495592459429363329374
362372841755337288774947741515423826350
5900827359809268439829978525202978924311
686839297839976662742249738778956167189
5571934466753312642269127561319188263751
7163134983091320407869565650672755568869
9795370089777668298830205233904485311
400153007125921730659472781481042108453
272713618156425422196294616667988245966
774337264235056694622135064267335555151
918467166387569818431086249488658089757
206615573619152769243180182150339259287
77627988907510435943163790172440478114
9264769824441185335296405335448132497113
381737909479298314974046207487977028498
734618242989223807417219133780072383968
41778932340170916612744899196914896384
1207672652517687278698757407728694751873
1372889216888496443784572534312232525286
9044823784188461131325294976772561432418
566377337998906472114551166689870879115
26956207647715006462690387164232431740
848340238103470331736833934857939001514
191681305096982825607688570659381091350
770395035988105441723394395198620382972
355929933036820255241926029980210801056
1862485972031926309936861971946835084640
61111385300231019144682917782029355246
2492479488965904760520858334885047622971
684030421499196151757449143264293914285
43743824349419124496653859146583389698
8554318401183754365419038990794536597
855011108988224964948664648701084882185
3389721814994015563408950984136001503
1359508656298526765877909367013063505
4676325752262502731366522418912564416248
649479845306499496849598548135816108235
600575772336881334205649760200971825624
8437822984201380716621163989607531156589
634035458618918285811806583134715368545
180590574342149877168666651155010893
954688523850735466066509702357365266765
6223859180089531026375008850258747073
48721880555995027289774575336669726501
6442922962658451918422391380333258756566
854439544767451488365959722262654228099
497031378844493685616473110632765468144
4856791134534673739867144989942891957973
357580614188143057741077944886199205975
55993149034833971031742871876491918406
7582163950113158166775352122715341561
2621130520707895617254168331232684965622
286119826226461410350812302947431133
512996764396677834657696160648691888358
6901256338566555368978128350134596298120
5971246744689503168066179434970649699317
81517166946971178839323739972020718350
128147279547056895781455008804954772316
282486703127831333122644667020045971717
7388382515284743581766715919210178867227
7290972673806853983850643075278235525943
7135308053098426559796257322743219707101
39612254343441577596968511237393242317
3438937840341703461071162022795743125533
87617912604427913507856736739703662
359567565696706609957441758101027176658
382622109281796229371597851110740545883
8175270968413772336414654685392828979431
1668487347364123266668783404474154066997
3832328671121713347351686539140990619040
6276961896465253100490068485373186556
1678210339636247079153257369428322369
45665900942379579275605491556085633266
79358061973613571879782610198435543564
9560342722486740317846675488746285016200
8433998131864617899171990233140832884
3797259978813878735316514676893980676
328936632660394230359310423330593838960
889442572442495245449822845502559754487
2067104946383753214386812850906428014820
6579827993954468092942272374525499416
21512330250278484344653398133660398215
29729649525081766914120797433443440621
85257588990884660025596352045528894844
176267682848742523859489862063221171
3656809757167338795168943282581484298104
6863296923475343873293161986748012591163
56709548769515454003878871487636046144
839032801319971098574031906351986732536
600461609265656233267393319887055274526
551682996467865935299055535810995067848
543571896820658826642419409858077350514
7845422151329559927951656273168960836473
718656053341304470629126632221168722504
175445691473297812671348240519283437101
8346134598426411621764234159712273714192
952565859177479743346774044588643584835
4832839873639282370257884242214135796851
4044848236458195354038342605432638321
1293313856175404663625142715956057654663
6833551185538198252252748910199059803401
231430774536203677670776682207213206696
2227702266567854123396492964610490372791
7760402086535098113289727214337840941181
1940576195254524165633832710791681462756
79913292193443323977032424324226492207
96321837054421799865393548357310688319
4550863516806329424834708356125396295
9800721771885822795062748284793382838911
4876834898837117844943439043243893875684
7553246949178259662359818441768445388853
1973186925159589154678675767656585601108
306962237753127837446061031587088149805
955190508124613252463618688967311146560
663887555444853328341580477527558419212
3593402996374218537255416288498835406186
76018285157578132267945944687893975972
80936243735909601685776553517888549528
7086186022185278370964718210224887012984
77467258694998062537108169821161256913
7549825841212623633558017679304650666296
11029558221709339891396959950679773832
534664992874364900543336507329512882211
3367680841406955809284141829128758832370
4445605449076323927150315780946090696316
590191992594560646221884160538871567447
5687135149356539796813448162524754041548
6155777767246687131377492609856371182912
729948566478987213615097245709689272308
7236355487409340169277742781594036493398
487645273807320221095637445784817773835
73387841927385251079237513414211786476
66743677505288168227794044810675564426
127870715944106400291971767541016563
8211571661897982949833315049794445918777
573050265739124027480916816885833903554
7477279728952152883997719191773754547769
2813932978836695766457865112175263391430
9790422628201554760248725591635322284970
9980875945593774991671092515755941025681
2673354473313559706073971228386469856419
4750945742051550567528009923727879127624
7290824348726695865617143683489739959697
20455944468552026451360382659396982756
5980647644342974605985987239466187223
230629841736811799365735498241245288405
230830319485528161122598586888198463500
28272765215101792837624615480042415814
2677437953569637132551054634369181831283
69342047427949428224108543215124293281
7208413651782919965328357530884741663907
2654541659303474615174773210470529307663
14092851886106777328599185023691585474
10602113116871882275085647656265287075
8721834660149211932528635671164289564799
4990297598036597472855790166166206626
194781857814273849141636942946311511653
4599198837536890797438718134386217956099
1589825701363791916058358556322157172
981123156725745024912396674397825086633
9119289385487094850272199799558136561316
305131735224409390744826220835619067062
3374750891727600222454574453517053405277
77392874855928441261496562724835025961
44472175362794442394722329452239667565
8908300751779958468196093861995069446492
23146627531344764112591394357911289442
8738435159838620390462377073190282758173
424094719578536844182867642715062679846
655583946917875278703094619977187685528
767112588869526601735638967027242125
9534604813654087578289776042314732418
2005659766735979834926961291526995077702
4394254689995945489971661682855517658960
69390322208846198229083997261392501795
585237473195696544734413469267704798
108560968133371643877769675245943087815
708515569353394468595476739395356595123
46629181993147257893633040271574869928
484962446892283840934308189826784629275
9315696574352482233639747044582415914184
78505969871871978154649592282834361678
5446308314955695461282717518848184272991
161829652776711626720342014528893001
8807700670752119253513727929287336078100
8181264359431341847921394804810123708917
45103498978980896869114636820934548
515839269651496822136373275260480641498
943372783731235945490315370292059727660
432468706027860834462752154970873259817
24092712410664390060783917884642307326
47385736894722988059183869380826096886
7922343393382245299958221834191312109
772536039307887134390761747957870841659
543470080158677950347715049725852029750
1910204362034924663886398287951660308551
6783698376871916540215911224190693486921
7495135035013059526544244008693712235850
25536749304184481122278958646836485294
475331698627834684954789404417359904448
7503974546251337469429945597592661703
3629384856042225161835185102779027223517
348140627668720268360758211121477866027
355943219908025771562146697271444269438
2373994292683462766427749383213092207779
8629343260141078416987648696191264676724
771287256104532226056820863075562816048
9324515275645893672090591884552001094
67349775779989754982445980688312568812
52772189273384227311492951437162822113
251581571243944317856741112374345423573
962596646094605160014421513648885723396
80092197423529663296797396915378639009
5455613669505808434734091430582985605587
5356475463938811443810281547992720896184
30388308950389293147506515423619857
9271581881751583708994093451113362545972
5369536969411956860549446964533863229751
7054129115124568530775075463355233143369
424364667591901575678967975611945231004
234707699325180675225196349997774432479
90516623581441346385889331413647397724
2583908496351165519229299177174798798234
6155119713657724647736188556110120446578
541739165935586284601701435820511116514
161432927125663635276607368398725469214
732030238975796942038880411026671628679
1250833174205279319271246357754140565994
103922195646411262323163325968711421774
1423772092317402548090646539115156901410
387638650422010745063387769470794906291
618999329838674384639479842159488474098
17638805983417096287429028693887665118
3667505197435902998654756062484527891297
2200511816606342789417457973775820166009
558660416563807230845603866575746556795
567457044805357269221478182881571202666
87072115691544844386229709992855403042
5317766452182613350795175790596751781136
7024895675977352481959478897515490468788
918642744508417931342412513338980756600
54225989677321832869548080823219797779
730524456733745850798066229555196955598
62903271821264551853224947122846191974
1209381916671126168241311832202544755485
4017180266924084307615874679948251582572
309488385943453253111007826305959105541
695713530713330138248814626544522635534
67005176152179088772807394588976755671
399665791098692754612035480306765524276
8908886051708405836060304788772448021942
6203867876919618378888881296978638269216
770035806822437010883738602606445969677
61725387214618347655667320678038363157
801548037232400573265765476360168533391
9913149564194839778861774213676473207810
71498698573135802567556331414034131374
434696101088752695486742253113021124370
714230536554134168299088399376874654498
6270951340173711788050509021717019087692
556621158117084721948628966845434837853
7842197062194354529787825389355367843
3364467739236487153551188483855067156086
876686268490249404329856579353673138
25382629634700323692335128763029604725
885368743804449049698474823112923044778
528711755510183855529063203964420353514
50883513919289484616627767144822313496
8231952443471431358716778816222126122316
800694061896345157127056306858517858800
2852115992939631617724815138491021223477
19124317594859669994516193672204007908
32657533430752191908453280392081581504
4739105944847122839939748484627253457571
740447833769178220440229779149832664587
102914363264815116691915718126980882205
5846496854647489679463059379158417065766
3055339350420746995444690278065295962
7206804915516543280977254813874858352363
98946141377228835746048897883450731629
66636986193656274169575818169984516470
988740105190630976205823087436659127167
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]