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
3397443643815994104244995561618650815053
3547922688107880641163664471754399353
927818302097335458825651873584142698293
9523487913456964536885417500821074769946
9687419532521752781282098847817803851
7367441997596442512873278722546214855297
800944311402426739941912427740291031117
221678046319758361927063887438193852819
163106759587172998316152934115417618268
196536382597367586558288666938475517105
78587891533094724659736854310947227151
9674984072503777953378573140988955051448
3740220916879271945676503863681491011773
397111628573347691685633843433028522444
5970570829326815599998616200115252525844
355636046710507466243326697962318553048
877320442277913824848254790074745791429
368295695715087471486743690148214305695
120253283247337955921288745852117682584
117159597644216966156036464687873405157
84168240471563544887901619538928698533
770905263057645493220858689248653772205
36649536281351685955605274338240755088
333389352151803173414887151958946395131
308773903913335615972682482273474427026
854363013876964881039124114058818928550
89447544747800853920265826495288947297
911899611906637925630715499187085357
8094921354516640673393832724691024176365
1451356258507612167487541562935412307801
2473657290414581653737320188827594984
638675068912255321533857167951032337364
9348182996635346275011041641809357106657
2545921957483517113944552885610055348348
130938701513390858456875992952254314519
85761453315064040651774512749136499707
2686273883203153160249168518489069083637
9233795748222321539654202901215387671126
4204899713757536915959406664624584306
2264124926049406397674545842634727862958
9580830169157689111367267348620311468007
694096458906230317461976533689799479546
998801958022370971280441732702368922350
7784981089141639336797754096595272754184
18652266440191769118343983882171874151
511917541225434975216836490505531976788
382271718639130560471951839289719396241
5795954772166827673628795094323238539484
349967396194826694674586593489749301503
314673844270730445588376911436078951611
577388215519744378189036372276398649999
402124071343945418648772939698849792284
883697795102526967663416789138765462552
4230685821986257359029273207772232817694
3144221549912333447496183951748736017628
9500250225695379904728876312461024414030
904723869848853996464566894879648278555
199534594151826147673074647413811922745
192096109141536686519497681207015465912
165653783146647025156047024273817701801
1603748649388391525228191642992625064052
2676221496437373280232208087892558825595
911172714062029695699977988639687176503
570184245122395477872142893698590999847
235159834942654444173586191778996046
81827455850281528634364299250918109833
7544462917938175532491628790904820461192
6261868853765683279418573917424074665615
95041077113307905773277015709117519157
88497906760014095048332255774845669135
2051263693845368825233508069138810923618
6940832226893590166236439493734710877702
4912365625642219345728319079629612057366
8457440017904333149067848250610078908933
967459245452660773361951414117253679165
585959276738848582009057009100077085314
6052316511503113874638862144818353408138
9846984019123415433671320921742217605
502240495542862363678817262542094669170
826365244142172644619187357543580667185
2182124253195504508748744339274065842540
3295682322283745731292712222491679195211
6330194764232721569479316701388914629597
6267101513451587197825357716501440275112
5055287800233120924013977230885156721
803356977762555963397362545768107434647
288743547629832469944876812236022568580
3653026338229674510867951512156474866
6974264835766420293382757134612316366843
612490747474586689081975363436362033337
8817702925052131927516892188673977508
72281337794839432821156736852379533511
4706681687991508210515332224981410452233
15760465646509853926376412218723899627
94124324553776994458147344190365763585
817949956413747761832104708960949048128
6147985037021370501885371203317057053157
7404494549162326229440859214793344884483
9417483996982608282251771288230760085303
34294752899523291694988031902716082943
31729137154912875899523969117618004275
794934415472727420495436871466814032893
63749714847476528876858465856191328579
5210609247794173930627005894836459275431
458467602204827138013493497382356949231
991476690769417515047949617794578869686
681989646367648828893499608294457542
2000127049891327608853401575168637293421
226569095484628740332804610899389294887
923588483600547481962424310295911479379
9646895469525165686268428978686037568335
7529252370807877371113192551641389519668
1695911554671772319630961797837841374464
7997860356535844520447429200825594471844
13708667191164678665127055828863225310
486392241801114712782344746692348154868
113578064427600750648775669435627047602
49154517629492789253421723574687523367
787876754758814441287001887479626127830
2760746562305839360523213834339725778149
7080685828445483712536794509108890801804
129780987116173262049189098266816055659
633986059752227965488966671539914228909
586796098633155316282054445683954789054
63023835733999505302472277535776102544
3582950193846560910595566355837313837059
516262832890190077164726625282584848688
642341275467868814462913078441733229374
93995254122625294881673260392424240984
573820263563311227355669502921059844653
847662498484295233153117177883612602167
517665395350908911951944584227223324031
53871566530597785476268441609327653617
457223334148609516820023227653974144812
877656318598693052018526538814996607604
966267591135811579956967238566757436833
86412531507655547221597228211053688941
999735592078659569747347881772248767716
92561043341563123151767754194752659029
6107314647092388508300497114736209274
2082394787226210962455876933101411623007
5038909913790705180904777192944104308
8844794538128119264434058546285480101684
504950733406790641752622684238051012483
4577380661026531451219606578613816202851
2006863664124328870240822929132357455
5930143619221367316215631525756486426303
7372111955152999353655922251254878634892
473581759164354091505236362616008019355
3248734950469243119554726947618640937023
7022434175168546140737374390892413152061
783099835295854673925803192854389169
886219137219685666378227512433247177779
38173733251456654205763562290048907415
434839644178733451397578197183296419968
26478821245429422810818144968059509785
1740812812797232185918035750121417394222
635696857066900124550243991429674235658
1943556134574205690851384369586758966149
86286989933377779391954668131329019524
5139382955856068746320975601256963752612
2854654323016174452459196036219069501007
85605064308023856733491136614742255061
1766773819808027269882634816562378627827
8094314525150589142237208458072217860
7533572578992344289773385560694459253995
90632386375173724841925740962062598921
356722547102210921245429778689424229816
822972521562403410472266688225959477361
414486795858738282676433119912528473970
4903222830008310713682336526746398011692
107512553269464468330003894552292011781
70307014937720362086368571911739883710
7809507349354500338040608529259483563754
37931531726291484363987855198757743927
908729963456646503659551568540221152954
527117495791519470777104740474080945371
8002892085957126790711387895855420412598
631519534882388118282775664792159092366
83019547325515239558733591646792618806
7391323340871342293621601718778391449
8178379118268956833296513689325985899649
5408373091435244850132175784903715127086
208172818534649288146061533511067998641
90948800730464803556862636712588371104
56087655387312033045227534153166218634
53943452206483528671499830137689038747
10302186921389148548134893666872584290
540029519469159031584236688128871367028
946312316048019120542252139630517574089
5686398675686536576790737342336720562918
669310839901547156315017786213321549064
6199478892021745276066859649547029154787
6133630295122920956098003340832850165337
548129358001358665245648593859816001537
737655339682972966537303355783459093287
9678899056749316939217858910792773022318
8942235546373254589168597805925395595085
482859435838904111142505853895734491678
54782122124572113293255832975116971426
1517217235489545484836093513197052013
824468730134402285172346973982561374
2110865630714165926944027846369825512298
2865019802346233669805315243435435712
8123144739365057133441763771679278755979
19192253013512660298284924879272604049
79686226296145206083791957719896328431
366765452223903430099766986841975308015
463226827160186964857547245751416086044
527502341742744324593450334089541535
2803857328822957250643975294379779646662
656325146926870661363716532851284528148
35303774346091383182385054567235281259
6649307415563045301545969147580456439571
53588854246971724004752360676974136274
62125123142146876697669046255216609105
688263481257470513713335632363559926826
616874395752966964526874012310223408845
3470540586998509162055337208779033797902
206344427047636625623482855396483277969
9037791150940109128574898613113377792
4064347867583566225839721976949623343378
5438732893791333453541066953254279811310
2677939462179712281728164912347146473727
526990475078175492451066257554852304295
930752264683683113320629191958979358149
3910384793067815231586261533545544699690
353513969669568118849000741373739855254
3208605445987697387485028489353260994255
54912286604467924047356724542724084346
5399568746088634906792148616164340353533
349733639042897782199055250661591307115
9257756052653916774286544535953975513121
985151733548867513920224074109452928634
5237842144572919527283852795065118388
3145345586656294894798278673229866986
2918467719066757793499274680246173301766
737989448435140991065145597765790658957
7181409488261494955249985727867560
77258592137166007146234105784417866530
5529957444192997179866788701461583864838
5375112195621790541028157220134418088803
83032830717817308646314585107723904210
33946078250209826288566354708935018245
54344807257775360362053727561280678099
940491959081297532501703212674047022895
976998324962244771479901586118372901600
182298333256485966749996546445579906899
2183157110718854978518056659657076056653
9542989150615922910362446914598539795
36211142772259249016307669783492272349
43791719469491760694413340486989108322
84155524677713338393715618594642223761
4593101712142833731631833364640532717485
8876745556952444523923998964516372388922
6152895149329359271232944687272943609547
1542527613311943401541988694991063709698
958957138846803505326157511701623736762
2763146478081011686028484341733824499077
193375535833075185562257569397531457072
385138681346981300859975482231718595038
3637486715215504812054404947812816127771
5727228055844677862817138290522191453710
66325975166516276126555195316974863
4428842614492225245920339600295738343157
862936965339789239195729762967142987845
33736585909617625492226216449576844582
810290615659960034411827663762540234701
887140213324846517965196066974362704317
2045145979088990924777615868494898430
835070514492268724466741523112674974866
5243861238556086256128973177845247937725
2011155627384274626327228459739936526048
604780737183024249294979166187336888
6759933986835348333433288957474928595
639257311929338532443290759595831473861
6045911301718117730285374148941418944
56104555427498592961413342337372415950
75886279110646578837380246118044124507
262096035219845519688014270093951205
638482615894287585648422450613396811889
54979280557657332963422322657197103024
7507377636012589503492017883601943478389
32252291277390153199977379462572122953
238618249930520157852749446151302553627
40636632988108059488867168659939604040
2524673724919172215835536343348693708913
645018098728221397885202784814968112484
2585554556919741775510238064739938994
53182649305227147466706920948011698711
725658597972150569293218619974876822
54264569674255007855326634496468981683
1201513735508237945093108343513781141084
8482901161327148569889868663505637563196
8407470248859397530051291130535235254952
958126821600526368984705504360332079174
5249993495146504157337351407475652944706
19926578316271407154041524324044709373
754228702326210022340762677479124471
47722035278640268892515947149045417182
30148634777550103441815731747992705565
688968405324759565734659796447372509533
75266575856387177455306753489736863348
81837253222637465108510110971569698568
206075129983061841073899159901023295752
970694723278533656348053986358374051
978791304395578073532991274925381268361
9292386572982172617084959513626880501296
7585255451967412954310928206886576180
227799785993234446691719298938842732
8982829268184073102677179124264722174621
5174204385786018781161902858612233311583
396330633016920543516072960717358696042
68024651014770691711739887986536296627
9453552640888445579257478423251666927099
495607759007395875481396431158814329745
52939128197943365449976662633590534810
8102028399420430261957339632427032314
705532342980857323554672971444606937535
8965357574281114625540251543097670725
32204957201364192698062108892466403843
108927318450285832079146194281372601103
585407041814308783085267467457915714288
493481516211454136582465406430432254034
65282961726659503805217801237590599118
4938267220576305974261093427333866276132
7424185918799118477812900763218321990
415944783416800714066175687279176043414
8536805264275521558953323073447554707780
9871221990093920191716011594794278415082
14373493864127108199400640672246563083
312633957806187961816700627954055116262
61085142186144752621256794366084049735
987360944694741940463970262337738293727
9695577953697742435672635441517555685431
5050313984433039368280311340745891226186
367158014899913576066638877367784793978
571391955661613711713506203699897073946
742488691568148429235835265483053464579
74102606204909699081253640866768293708
689852418279708524089567665206934319500
436921718194925293689475081562012526795
4870149927001244989029923444828772279
262917252002416404133602552395641458704
703113155797594815214233737439826889193
15538214682448049805479480594362182466
4636602712323356713462265533318996461906
148048093829817798317777181357641777988
62998193136436467255870305065481692775
18096203930995969156150878559840833298
8096858862437848530573514783404656258989
7744507998014275836699771403521011789128
124048998679725191047267026313260645512
8356839881834104543423525130624974509529
430420388222406397079113541949138372719
75386500833785077502777199915034731978
73948206345916934784438138580864503043
77004113153519842751844197862257929683
7916611883043106442450006766921011207832
5390857326685614418041307488905464783239
426648025448196263021142712374575855581
718429932425466312784717433982813153376
287432514489238979573638108880757107357
7394832787032672315185176147413235942
8657419791613039612315259732891047490
5538242882069519482214853547551547065553
9483653137908973336854895905421859295827
6482280458411053650574075032475131856
5305678040148654609818495036478731774039
3449508940603426324269253938174618472137
8307905671956295601228893383354436077
3692765376174371908469713909589164183848
77204459462672961531847931692230574322
260788731502354546768232510966588065
9475192329428836121074983716940917583907
1595198070161022814036215741139096716206
5021403574456086279961035303907338061682
71098168760881789826346309057292067721
483802596253936164084902719226672264148
80647846635905192323603473288790435164
347971269456952449297559250977637503781
2721119890575999191610626668588380761075
258939113346120239248193559558995146404
5776598230228515227234027833733060393798
6052619955991221192987751574537883751953
733395992569548574194826532116928393511
3362892472126184220034812994847045074915
8077907719979494599289215107930721721980
7425804251392373240271631909334943697976
7028534870152172792977398517213797225594
789426742826545547008454535276139611158
708221459466650468112729593480898018905
1167622891456880680843515479812519327496
7599129617967662296618547082479874863553
6930321341908557545676268062985376379095
6181335567864168197933532363620139723393
163117289239238182775186944135245128723
5174109314069778503614289452125394638239
746058154453820122329163189528962704285
2138128072776211266962343656270158303401
6495568217274186861680658151633763249237
50679626610267694596607281327458593580
546769942968693711684872532756421064905
858812183530850986471315345189286062170
15854863266554386152357153513420259034
8099434035509738529940354181117819366717
145895081008629684132966513351942489423
993350708051838917449210491957952452950
7847369863756389222844898372298742031735
87035404775299018405535154349179724551
1818571172012158763688211093277260831
911951699814495918508904422277273163613
844027268829304480330224776244858855778
78263725301154583993289862519639533715
398429427355592750915381489737095397756
552877781862842894531953575552655729847
6388890263076959852801087818773647871
27793676614437838011245362152590183796
17066186904984993351692895419534413013
495890169385316591758359396491820334681
3046747458207230359110303712929959796100
1995412518779823756720568679514087369659
92319761683241828895871732676389658860
49521794239946028433292300198768476526
2236962135606498984856547880445064729031
973759799606114647175397170380947017787
966373549651626660183483170257958591278
70679617636709174397234245647371708815
2095482138036341775153533958471010428597
250343755983324283159414357724797955881
72981124090544713677432373460366348602
720872802585813410933384678120792823021
1626775277198964913881541785871590377272
45665222686606726545651747885275508611
553265516301831536293056457834896972200
382376688189360262404927652141945038091
6613603755102107451773904100116729776270
2525342075837435535078385536572387796157
924861203658075790378329588620027081940
317824202228790171067456821523418138368
2035940860904849413967663083296387067187
759883927759903543899977419688473697054
241252161948797845882548706502268179472
8632718846616422575959003585323127576569
5001824374321397144794554621921878879496
2346165143292088653912883256722429484169
5236160297168275376853507826309858239643
882592313769940375654112872502637864909
345973073459037218662073616949280981570
9599271369832111689960837244103325072
2650946941460817152318580977519422113
352799299895111965485412199103957077991
1822984557916282799666584057337142487900
562375127695740342035761756654286254319
4691899377324066474190329359485504129
21082629374685756066539988577315205701
2711879483348223100002790200498415602226
165689534483973545036354106543765627679
338283923755210640847263431747345217832
2567774892697531581985581819955012294
1944302742008299577980256699690236219246
8836597421276129415586612148813350626290
782989602843174551177138490869087503361
9876391174211500343720886169352743697610
47415037828957935318579004996991036955
543592752975994798119528758665693167127
590646992663567798413143386698487114718
630658995554412146564963460489596814495
9115205186141620998775368716495667055398
7338114578078605705945623766482654976410
15778430666185942355238205356719366154
1008480967211745248716074472807424831561
7871626367316777462599867703143237413718
9692116148335129955516993336774088476303
51395126130952478523742463465635139684
31029824200577241864695294146550682889
1761587181965461335538303629826001194
736715221641366158672216938872532059007
735459935818957851086068859580290674329
670136449352729155181993428091545921576
8598609061919752806765655783441073636
14442101816872114641443910946730352408
998232353455155318186214140947369608574
362784092607750697688138613476054225437
7181503084037508546574897164250190722100
1385318168134917662980657545756429647455
402402489105540326434916391398262067
4491162851246588501769739901361092672
902636445448628663745267147543456267956
6008448190725245496395942913850475026334
6355906012318892712891097162397482741650
8020579944683134626431751353950526676771
219370266360732244822263004868957631508
697965183614061012306326226024751707
5681769869966469544313171552012296290
983262617146928938481813571757047945514
63885497140164865658811561510423185953
7231446247074564743796846057556089117991
8344451564138844273138308590571160324123
1669713630363326685371318512506345718598
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]