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
381438961206877884021482546414196526224
518929466365505675217715892649171273018
89421448420119835060767184743451437154
677381447284790122641751174402963182091
643242417732602152463967595384147069
94327862854879444203559774817487563288
426198099449348237532085374184887374736
7877360324314574504046548010999488269271
21234548799249802231578240547788237604
757952024226900687216009717294358373277
772019033801370816327352320959095702575
852168634653379340755423692553976247269
8165802749325157958318483414696689849671
3940423336723885876820667202742136051990
777753571437449898441863492137830294774
590075695137404679238528215766260145747
6034342601185394649636578197838473414
18431265430554226809187679717498421878
47894931833290836157555842697222333582
97446101778139745433546319660844782160
9451749812295938119148788744628274972767
21595186854759183036752905928328793533
4142668170523667174077145419896430226470
5654811774636072740691344808639583998195
9508517945677271281960147842663985896070
5056373918533123483198095750225113923140
6430886896729211137242095973631572575234
8616668766834276620191767819523650199148
903984936966863977549964223343549901202
458084077598974722455739801956431395503
1585147664924683867379814113601476964507
2591373251389373203934476421741276321556
1536687634699656630222421590932493733
58962657103228526468664742121563592271
336520309519125630279095810127417525216
1766260624285395699548289675480681056918
663278612385888472599591639619827449922
4422604938174714658994458118335010
9410367857393935144420938978575958041408
818462795154187781951572737229077428573
7029886657308121332379857031720827285036
8695770590056613919190553713966543796952
382640046166566976498127571662919012366
87031505208473073420722745294088486357
9718192565754606657671768838637359721575
26606272592185135637634549883591799634
69475866796877391595685731079656884703
19768312362899249435730847639898008600
9736467161113640920492755594246063437119
7037548523921851138796779857955060352839
25171181174964738282472861302251106445
34166342346672126441957856323649156109
78753389707626494972776465219483846485
3443194468058584534192156019269810936
301047711592510640371020155692512764495
689085727724274874824776821337130095554
949881302694336126662343795991999972
19492611600851831409734575190527303709
6304261240581989183620452279181128018690
5202433829022783227472103874923528346157
98515715380409394336105554490722673655
1233844974438683298864559922736755360
78572027490583164946467852570453411812
499143492426808532832431640663448049953
109119579299323280865271961968854092215
766712230354190628314184437522497903756
3592692917695019338413740637999124697
237145204756393402253423593323338859283
9912375555656787207989391234201211383070
1386977443245107842613745951883154009340
202364855660709270592512285749378232666
7587209465068881456966722523134495224216
131594549593428792068930878891877357563
942707530776656256524671778547177888614
1295787524564627267184675305464149751204
678635385954556491964787441853216651667
963148689948735944333695910399683313363
88687644698131613842784509630281426616
2495339168637389392886921417750132407880
780968273330558271661671628593929575192
933723775137277547951968824162326364718
223829231877211282985799936764659018572
681559018072356780098284958786979095527
32761579250492032324559777057747649501
901992626193662147038031478524817245380
5967250891866770917789678032606250404403
4228786549786542986857892191517484518218
3949600815378995461220011424276326724977
4623959410197100647052412319666962991713
583853795065223757388717616723387644448
1337267557477591383774079783578143454702
9906912789536481988271251644118314222608
9972288088607981745670065280489019979161
86052033441778159923424595315272643064
7143511477083840997973189843437486816860
818967531515852651611081163761536816963
5641498624413964843080231002292070758984
953466033355345041113552264987743322604
71941230541051198506376395318387591289
96281246044893530330258859996340163692
2510584423725357365260934000366136346898
325676869258965624373609294529886872950
7445247712335046836150369650428243832728
6845357569803086635768763972406763476572
324286597548643883475616751146510396847
24810186312818627852679122884796124718
3493434745853541336966813094318145986399
3454666530887662943490666355599211559846
9781374649955211260180703385347392623968
26801965452495589492997616227659004763
687532559628303345856343489704183296673
5252925868955153715343524961216619638585
913635405226103026335071722199913739978
17256305762025612934307736523820243216
232283390428396236146932178994086864111
61485801139019311836539378446161521025
1325700742828098712415560282451934345
49979839575691464022809675323723371518
9303564179797613337066138485813576269303
2848547996146991322365730666875113936
947110914197441652055376215138938582817
62937839934320197156624753405689801488
224716906149228854664916018440070145732
9370123293486827969912548705337038758366
725928192811414893024154361981148374515
433240627279188917281084975515495601399
7162426937004718547365086292314871634651
850344345362925655889844497652257587740
42852327421649444927704322951232817985
1083420394475368818556998017773518183432
567416791795367997941429912306851255581
8453867321052922344743547321697563981141
512235023487893247833277323014228507
2417997588751252698048445984322779779913
7336147874508020996475343603549768699606
651888278714985934840795978715840056543
7832304944602865405465168810210881954471
1417328495438137660457307743662920184236
8961867843819865392976051087271532141423
143438274085849167023946186834292823760
280074157590449886695783181056021383061
3689646696653233120587247380479279141175
5770810518349882418367932441910078028855
3684963988453168443144547860029198304
444431193954199484365939679976489815604
334225841784389497882323961803495183294
2187544152628149259165830538288889822
418654965179623661047583885265027011700
9992691680055363294666617732480559661203
41961371870466796914872214080096126398
6601288941684301354748148617164568726
919035099538529173223997706366737131135
264043362536278253316386254159853725477
1339185383784672888927882804292183626425
3553306936222938835194319164646944669
95196814608745426858654100519940958461
381230928044652654237566761546830814655
401328534673324472247419891595112224504
52939608261951715601807653102379638749
71291690193581586826273007850678034102
202515638767127309623611038148953619392
2990561363689080398528042219411997559300
9950973996596955304634469334454536567177
5044436265179349899812736427952022667189
817385440543331974162409821884720315288
9388943129867614174076757347798563115140
1045544540223026987824392837283681641
1246502382216812502721459989855916157821
252594170912287969852789268674757136478
27255230824736635482074420663788963338
978199043376925394541707479964318659766
6783439010861602883536772200862972839288
253710588292250190381835266619789502857
2099189436263092165953821279296145891
8927970421012999664092368467374026196102
6745948546889820562116953683260184415131
676550194808539510520012611153415595320
267071563069495972609081208433195471641
3083444048075254767346674174100770736855
33366514209789849303803598440315303841
41134275279672633124743774975663235
359740487837919058028951995165859455415
812978917610287331582461355797843003
285256159784726364403923313609476442843
214772957547115884813846742122241426722
9673141934688605876021921273798418763679
8661110071635896733718069648743992681756
8663851913906635509489461945112117675558
56066323409327186987722078702631606730
7343738768071199911483668608554532683773
784964652791528883911534427026627821530
490875783039709633644773037292868561
4367846321177674484852815505375017043014
16373800943427352870281053375979802933
454441726386188573852651163840468829907
93403747376932573931483573642607684992
95751753166438786001850557536086913740
653026905326686113942104604864432583554
7900700937543124693182135161721260483931
106542256255327342199561245479836453997
315849683063955173141562484656423969183
8135898962838405602254383053714094874117
9794177845948560583841352569853256636680
665919906809624719397691531246008689750
8255832547803015134552998093305274111208
2254199055505307697889991270253629547075
76587374278226154463102705871312299723
3013719578445653921848923120703741835102
35996521001982135715826123457739043968
6634942946876025552533532885674769985016
260856956543230911134217403890859964912
935941247875843692042977214765652633331
4573021830922782126321148003743498928
5471840888719055629164146588414753723877
453226239318188357457874321334883201553
82124454957726096716384692489325346792
4519361832589103789146171896704094519044
8163367533928728676835181806581792159726
291355831029493261332337546595933241835
769453991729682277245639492715744807064
5319940960818252155537904627619319912129
4379414469031914356597171879868617699316
87913729285321753283109771518090796991
48006461185827490665563901078698693346
98282563594423181414939314465080753765
8887672186599730549038795146616510679159
5628262651486178713339895184765958975654
5054674713698469928128834104107953646225
60735556419477976979423572384846442279
54582591271124138763136343371951663604
757793422695086703511319717427694527893
516086711541832378831714483881692148139
5339288083555945721917412383982388201
777780948732968975544855928667121761859
96110255812345715986373729768933821456
80787213594478975187357331452553469131
274998267688593820553884571647778027835
5800436059585795458816669763833993464431
8262515149323342388318755700284278951770
457511508008387345120767999464166125157
972165059694486700986029802172399126027
92908316123476271933185657431160772519
511589675496430690811944524532789543638
7177847470795496585522613634696550316258
244939269755794982007146893425262329619
7128626321533336118244927250789167871524
495470993297496115551228152905989448123
461040801274171944584176943877514224349
8623485511404493959435116400148092416762
5314479289072632443062733583835173781545
6317143072202184529283341544433455648312
90112423246727256064387712820071172576
97497838235652671231622048187684421145
817244938657840878337052201615288169873
5291233994289766748178709984723175859325
2192264932886292425964437281260209964
296823142322496856542679809818896955287
856528093049239518301521239506420007748
97326309272916828165990975818325912974
650399747042084662937438898941067415877
224958378905383864538908799911532312056
70859998444530156534149826488900839408
9649729245276019964779399399102441972266
599263404689692852025935879556112949270
2049515796522292538076576419166959326938
806478556354961714993604827395073913916
6960478120466206931920502848761552709166
68479026981279459018254180136549487507
2384889724466452315194017542242226018180
2153574891528212589241457345315040741762
661519244389727836202784614590335627
165508719152430423626577105208813074340
88753930304540622069319219753733183579
545330385526628655253123914795299232993
7237680031316936955695558901470690091419
8425474079756518338669251010231868562
808186351684562977329851702657529019982
4748403078596995523436258429858065403931
50606473731130072128350947392196511133
87458513464889552657833736526036786375
945971748249104920985925106540588034028
1499786931981668140154924312892587258435
96833434856631185258843164294533342552
8617503457441174404213352163875410889901
1628555396791587417348012994433359166
446489769016880347799725579161957784803
312920849729929455077044622409589748021
3711968348401643405855048969665549897388
7289448727325030678099322162498082871197
9690320499768940990830339208284811665487
2101613490563351513244771916259044674
180370112753462702280208451656880525616
9160849861981065716173529182339218923388
573945986888311037815604904366251105026
77974847347789685390201172745459888
485130354584401980165894214967370687035
5946775155456785757324031548932334918560
6507563133677069151041145256202161851656
290699054748564848732631774578611821907
13959343290530519409740663945894616312
1573748144692442525379425667776742315198
5065948960954032629788337918267339897548
42672512541665245453115444967243379482
338483459829985963014532687847664001393
139081867482249194902526559224778232515
831439889017112632973152314715816608663
6994440860075880618654885789696871206951
512382697106215531219839372534168165675
366060797780267356248485320484224258536
6602981868019857671219311474755514764525
1881945813766425874339298237493301951
588971411202246696347986189487812349590
4636850899438748669560678993878939343315
9561103851934290902042748211868742769713
1880192027821111807867055414922031566487
361921428463579163326512095808113558675
470524557435044079827738083715884893
95301108538967848320894095454547716
3404247573791816663469061278542642397070
4009784485647326836843416611559147029
7108595284625878867513299261698146893391
97997953184295854766315222288222741111
488771669734755917888168830293715895
3296915724585442455079451685198868261082
34036045534123408410456047432641478855
4873558169384103482219963195949093112983
2763237453348708794017701545611862281427
7947857578427347245342405552615855506117
12766605971963424386374653694898695791
36175444912554388277304372629636081986
52638041928083955398179739592279633043
1032390323323688155155355216252439538462
84905715348684466433740977756347272288
2493873276476837348382982504871454388660
6686519486692888483675038018460973779396
132198926251248350638405520699619930575
5726993473671433317858317002465249889464
935330185606864842643116374473968334847
356485352091872847989088097493233443225
130883417507967995288935893973865815210
413830121442980085348547739172076549751
27803894566813383303555875519584793445
20839353517373645818201369654285551520
893941663751699291232117985815697055799
95496738586959570288692962868943457457
8205124698613746180390836717672808156
16889714493932118552457580795985057211
76027733105929894668841888164040409735
22545854744941411975156495957773638
8903345465177384753739874470393393395203
124779156000310464108770589840357294903
8641758358974350076426700118082428546
74125646991523442546003053925879643087
7796780699475611647063053381355028141150
332784951191860227254059748727553845922
231750285898620286358395168972266978741
411733773108349240266827487591511306998
820018083602567138463859743434342284741
657561757147737930176667187514575782638
647163375300224714810246583223977832852
8855680798951284382428253170587647597422
5463527854212627655724436340851558499792
1262296980099120109359351653617861381801
51246295671256938571233728647847054393
187046834213343878172387899958435041692
39665519377644436008488682150698694452
1678926490658109699957713190104639187
9425452924422179831630847662217598304491
6546782589969750550521905352126633113
83471084418914649274962659982187609850
803579248104130557155407635844741398749
7295762028364120693848035033722430998198
370537162089977132991686133372197197369
5786216535876271271582492419140279513213
23870512360719442998349731775731274362
582397264932939847694441513352898371562
17950733880627452342023231680919277154
244040522120101195957837106350461468765
21488588307200161394524061350921467297
7141862475536333388699243454644956666934
8288815975296295645634162936754359758444
5830223818256669810989579939689257597460
692592505324674656927452603375276854269
4707253985139476785994997257839511007
988968284904447556129078158239650578540
860160711029869727094454994929035476848
91853604921454589849873564621665336943
83526679533556325577700373647777812847
9837486069112257479795215886613914484463
2294208291699250945957546656224162576296
490684073871391418581415345293507397754
8994859834698955682598965240674026366243
834186097388334782444283759945395782461
3669871466566406924452682715496012031160
4106309259172114557423903791788248975328
62688480583476243606900996066289502955
8157724655634280909126345160332365668545
8811646035589859570961974429281148151588
8216908652183094919930894155947136086387
774539165166473837380873283898822382155
710025112275942338661944339174884088057
3579872395415262513352226597180091630
63681589326199949445779255926634326943
516010568836546038768297401698350936816
3665872921476938823899706503965541661445
59224489738522655582534905108950032080
3528744889836607402785436644104146849275
909538497513020904287214571342627563261
641889575931423493030186231553195914236
97354157700964350375032124372018768708
89747288091838489833024375913044067875
1269180313087281288841409720161883327224
7272134017828605336417804672323610158395
9032970459558847636423660807318259736
46371394481835297127279769938752075504
75876017258410438068652492585814967287
640365929578326227855253826286959973834
2251433187796565109820267397722217277253
231467238283320977637921615597822591924
904913403340310344013277629920334382689
629169982474520484262355697786954739762
10901031294318285165646942254085279164
481714302394647654923861628116746547354
455967651876591791694297452713131446158
291417324564686910653305042256145701900
4503130498707788511562209877651443067212
1567306797165003626673436885261572087
6685161347961047198975307891838168567253
1868700395196821648332168154486675473
5754121325503037987165055392158127165349
4155884455058560248574587876424999902724
253126177356564752438165900492869304900
724545088606342550679817956836382956221
937238176809775167592705175762577555508
12181068289411693213037903168399185
294069826941598851975656128179441069885
2491877397836134892530636782284939631592
5334296253537886608259742118368424982640
387765587738255298075742843727739572897
40635847667358699596363392615793167861
348187325929181154465261372559955498299
75451547044162815079652110572053647754
4569339429371094344064105374415667533145
879650229368346310684335045721394769286
7224482713029619212810047484507757271609
9451191771202896450230241264774948171764
3378214232682294337850814400182065694594
203731177658519641761439461196936892460
329057927823497284469601264528941884733
948951389703590341082158729428497535754
82418247161638678510133779292462685
230380597045577834965837612389496265936
7971589623832718899262121508361994734
736228201907169714848077952242211768268
4288383450397853732631343505218435483154
74388546710286343685141587934575234572
1743580682353053885110673563669872524
3280623740296870563362049736239444054951
2549598621096345569380894546803066576378
5452329288864141052257282128205643310
829022193327234171038209958894110429715
697628416100123421602520337975965332440
3977354715262116935984411742745531996369
81476159842197358678901165322416949339
700756066609495186408148110986407707451
3022368991074042454927157027510870501032
58428703849025398912624246086760621596
1025758018783226728949887560115518592813
1136680938515192894465074265938882106329
7216995038949181130733979739189515118964
8165334772995783792946721249272017153228
437291508587451122720333373845899262089
9895189534586031951781608082641120808558
942752412330476497333175671512474096745
95102709896941497465997012598867401873
371180995962695429684975713283997711737
5632662638061863996158465192734472451702
3217755221966711491832287644579976186105
2943254616188328334576909268808614652654
471237696741885185286241210095324349909
9848321973464814030318860952322902129
6391114168426150765442727334775290974980
34256919412061219339867567687166968456
1567841918738283423282676143789955758115
12881096614028434834135815284999398068
9605319479295513826111612193791854629529
7102430388527663762350167082860498035250
918218434363462276786577279894454713456
752879104813451265684619904280421591007
770271638020304811356371618276679265184
1896789212108881489290683561426282065
884983099652730375984126318756139531729
233271416176887279954597580466553744627
6555173720471578592261221856413163866277
855125698297607899256774936902690447270
444593974315965242581633297844270369596
760824792300261866074879950765592716639
858313462940253544907383387911598304551
404520647681185261716811628885262312143
665169760452259515927677479509245413207
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]