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
53683417933776853402325352580264257893
78325646814583183316569735673582469037
828562410322315136010094951176756852015
258458835001214143478416873794851025528
8175423051776517931182982931355597531440
4964717595955892865212958432714984196782
4793205832972728751348207785837856717
313614755456469459559788743921045611337
9016943126646428376013568021429431853065
44801905242485955348179573678827546
6104180497598078444692949494870588913269
4206789196710211718164036883159723914
15813869363023672119597840267412693198
4292475512329321568683983542759574349455
6788618938821871891090472318570734094633
18017567666181503960317682965697568278
138755276086366284293894522282427758355
113683722809836214167615576654762353536
1513887089624745791485581721315743202449
1853930129264031929997466588462083614691
8554280975077381255562431123977786703592
43764912404638663967085656608960405200
439546595214559139710233428449157878214
27363511430437187856127458969189203110
386430659698262299563156746203626584052
18794899480480187626179729686496194650
5035347637224780104179226327128758741
7262181327182433827695326621601520946000
7012783123844710244388146236184865541199
4536474861015893709692422043797584843989
343164692115884136655909353335144841324
376416903275768947257979180355531153706
6542749891057952112472627305137543167483
3453195148392533713298808056574917069251
1918458995249131639386324445291749217883
133867151288891156321552735436865146530
4944908218275893382919032621554186331502
2444979998871831308240911469191826141270
57054048454945791077765663187250848345
4133470489317031713884031447863423305084
978311605055841574354254113876787279958
292043939244176979407975248615231179907
638879651208828425257394379915171044356
56184562257213838357759026966939502637
543438988085454614366967931874538279971
87120917228236346949764803789022301982
477838877451552998782528573160561839088
4790945725928734501552798176755772634572
7439757930318155337347776754243071797648
4374144338895231669570514570614656158066
43227739714665605260579362216138658602
3020804819612748529955687045324362919665
967161578095765554736055182291815034359
489336709259924195421017185240696725738
5797554254834238785288592146785355701
9691123512655604719448276299353559312
93926495768629115633787118873035095956
9918271532777613848168785368807517387299
368343173287598532097377004544447947418
94008185205663594249762677989618265280
380798529571412197677429956181552771662
11844357836193270326191790787747658009
7546454725734704952074204604572122791159
201219476687340123408680668349053258747
5548197993995041194177215052343225334356
47784417577573220348008424858589721160
9348892267955840402233154293879859111
1176483315588425616614887672622852594191
8476219693059481580797906226395494576681
2355637913012622944936482276647650586966
4669536089188797341989545994838836005271
6664554547808299291482264494450745553438
946873849577783267883315830667065068301
5474607560879732868859073498356897352511
1128583740865905883415385149981893532
529650843437674388233843577017866761
167756312674429785844681732827492945998
465681137071548212698972361891024106517
313589234996442521913343253693332572355
42694589153055290764276957330693675768
85893390240145192679105743990463496153
8351212893117553712614416429330714378205
5021686149123063892056206119961006688
3605775968517913421128422319521613318
6004561642116030448057094507182946662316
8873429580197937246093462917689173652500
6829162626778325860268343266826290516330
1586602890799527444577274087331440344344
5857402791986309626953081724537050401976
4341595299573260619865253941943553499985
464480055857783070454650876475663917317
74598685648977245932431949451460106439
3571825255004338681537972224877632536754
4443315757967952469895722621896622911999
3411515117848330421256119397211712436177
761089649935672518892113988255579588250
5451630553291998466876582718521178822320
9397492921359624505023519166194487937652
554875741149351767481131503697116641361
17296503524943215182509615155461076404
409353725369457833745493577659113753960
78948117282610111436768807473776904407
3524583078732378758613784694237453634427
6521214246463700594618105367407948675469
28375020385757902783869790343441830
4023162425569170459765014219624880563303
6612659179918397503829494685799823466968
84819409336115609061164734704502303
457944126014333536706599504066876905744
95035377668265881309227142555038758795
5797735552609196569817525275444192174447
1328345382425897272995911932331355272451
852929795861821035618200782547788643273
40092227431420052578427892256717746533
4712489957033407873140525523769270279261
92048453232235378421245505806618659163
8100742797184705814684093325988630392117
829270333297415407744091600329263952031
42315801407794778375060601487962999157
6117469298947625460338937496402129538888
687565238479498940666888865769429643614
3583787790042067588625544482856485613815
86769042508055561997372747349922176447
9092648944803988521231469717428660217482
79441574247943246026286142465481327908
1973130911831104098196079776323467696
15698642957266968191509838970496464880
37422605068102346486409191985063493560
4041997694988853450496071477683748334
191697910551949357320599224329723757805
230279821042214588282234742751112027693
67884938845749680957444525431284088595
9539199529609759369584621014817558937433
280363442722650788068751874101285125962
956989439069938875410331827917010534784
361964790431072222585703815882698019698
15929416030593077549452505413874408471
75668793496736746908476896429549149350
1261396586719261671376859345623058439225
99664239377014902602547818249768222936
465285440536648990054777846437334825134
128362831685428148721118320074985823
771528041912225849601971179861779086217
2781938922195308854913406455609333958202
54664195758161843464114830887465526396
87492597909460172042597646525731194005
72395083194139779282630617913490656905
1225404950241266577279448639408670331947
226588602826838331884972927590890261187
331290996955828333963583448910951883587
8157656534243480258847832298860783111357
278424354273124639297998757752094643902
5274101219505346678899637362563921915742
4915790724115342434641443640305338293541
721794749402495375813271525539109767212
2282666145798133250370638094322128038962
71786579677130714707460136762278197468
6587725249806758995280487804156036126571
692953944440786689495881560943527934588
104517574025645334674528098910543135623
7026177413567602555047689945401927805776
969588381968036526546188143673699419
853482734001582237103252540201821915486
9680147690967732170878406809661419894741
78746167484486586434452374665887719772
148878429270633361719578975535041168949
7252526221125951659575364496390247583354
84379997590249279713965322974958293110
5419594082866955950455861521259337045846
916531684259967921669095211882747318142
7643537622105543582937996910268813113797
399833997228132181664082268315935232682
484414424619625964523270350379528190534
51746066567417514092003682799543805866
4340820821022646355360321254512037175401
6463585665928590273889128913403815213041
193121169778554487274522216914886103802
2053288067836450833460822552532028572527
566299555195876413590088934403883945922
738644545267908501932045782667156347734
92843934395166204777696354953810762711
89056110736345813320996056222761107214
918824154531992950572581841929178357196
2358933929955282524919687585111474936641
7300785665213318292411776322367087268637
413382988849128763743032386100240237461
471690739391189214134786369789065449099
6775942223351338504509438254527339486
3999681834524319390377181761403721083343
5092566013495800989271698674833525117649
2669741350132182629676254980458685123870
7610627834645841372225332390562676418095
489555111597934418850176638395121626372
481161075086337595685396870190996915544
5589957672363352305772664665203066261511
832522207004252073082813806764713671446
7489853332489224953059739754665253129152
369191968313347041495463379186710342024
2388446968959854719421179581373224766
33017976644228262086449833603039091817
69274291549649126921142046574993028445
8529923135124575873255848489201150686385
3141174035132576850653548972614169033578
6639143389956261767067853397435615196341
48381364236471723931236564122697158482
61522493988699599501943493591629508223
9861241679573824759129324097165033214
533736359962005291357858190664434336975
771146535141759791959821594755225896890
556431649843642373223791284939656585100
6242260623499331662643902179633087925613
6184497162606775698172147088112025269047
4106344591575147791542770957681285862
958179901407817703113691656343418109
932310554727860182871781960382552624667
643924023916280560887902383200526851510
699477070689559505354559858535024396235
7748864261012206335161817822964688639018
471764739817738420456945536738162678977
68684395535961817727739691382448625978
42403461323787259033312831316655456020
7826638115428354741282131397820594702845
4777793063135005282116741488360387239925
497968817028198728431829492939182779050
6506422336170332753443049110914284243
140554133448230432262616876462121449926
902639155256493363932739387880296384220
9478203336586021836131557936369624797089
214433748786613140116123548429055641373
56958042239152575824107474881996446999
24886830625497359337867337209938529284
98135227378221354315522347641171358076
3104627683178477615087962445979764112930
7898668914983384776557088140903280904176
7852810190727770394966333986117873956949
22079414740218938374156849039423348087
406804161569805758255464274944142361522
576011712847051872560640748157893027
98892528807678959848709512692052585259
5147271098123627539359177255926226048036
5741601422323297257226530518601713513
1138361477063895947731085088410212335470
239896158813448769188184681339329187670
1796885251167989127734343207992146045279
58756411451162497194931502365745704643
6045165461773557472457273167144973531
64522052707705269466202353234074311094
671679826532497858109439407223988594955
1174948139339701479692369614724185513839
5376836714558354373870128314721578887879
8295403766303973823521407031906439272267
19464344992341684638911624230050907817
3917716495287403277093413957808838267551
1237631565181484187202078237812314299
1670589560472046440038615147783995066345
5039771069319866846384879188140523418569
803371667980155793665003358110245167034
10545537262448825190784378381543271197
767735827150582834906522822622027748229
466256324765938891643273275667535418691
837224914371347636738447791454628825405
36247739475016983882924351348947431644
7518687843708669802758472504263916643087
381933925069331294446186976603522968217
7732677578117755950037663406663735206992
663956508776249499854884193819585499910
154092815119941125486017418993928907029
787255438503408515943579550676917704272
860431686132846844005407990955344420
4888452335593813281136925893334312627316
746577268813428689818322182875145601954
893121962162760621247210932791650375756
3788736340715237925043661818862146802441
1696998365458923976139982746390387719546
190822789562241210793463539644046767699
11948823152432753456057141177173715920
4311238586957886796165659619293913501183
115614076726800113352719922676979742254
7353667914436520721783050874195495911
250920947236681145856406520889429205738
897642922946341084402513436619742126302
453774482193660257455204047847228559498
39946243881852372169288447192331275674
667442839138765037885593138985434195464
2708905574368598408919718166188451868
1063251811585710336831258440337699216625
184127211562228665164859273133685162891
4746533288340920050826867817874317427
1890636039869113834036972601259856588813
3577767855821508459665815249771234245749
6240593683423562300016657121246643221341
9923280813745867765554957082943396021
486229779405681218426126129420243260659
956586184767847078567490446351438081774
496341425055568187294289210605234145688
581450260764520125220116371327456934408
783815611652912218984373638932645666367
3536532842282677146023822292347750433162
599995561219799799852324714352952158573
739890567298595506819517647916563775113
352926021652261564429697538242364716438
1111293978553333326235934804193775048943
627032183796326268322277488693475212047
9567738972004636328052967498702858638990
905812494978698914456397547685532087191
470929332554664282927355641664428685707
731791495083000856675019559427840821574
919594795345445418953883125665174307083
384182790388707657379856238685313843787
4549826366078054406565926384688423586972
6497423769985332178434648066484884215
5071300074271899703898393050682548229783
3759458729417566256852372672118882226
64645597245621272265194939443121084967
3781954880433031201869568584656952869372
570246701138582336059814511418380328849
9182917795565970888944989948601766114904
5351726230904261765023474736494718279
913448795113884989452010450648028863020
67771970147968103694913522299695144664
2701310437593101341739665284237789525594
844182075757599243232736554774669056484
86190766262639838485281709221072679987
4176137838275613193299621312896692057433
72779607568591971160256083521997198826
994373684039917170147116489159952349197
337469279707933123323938165852837342018
9264687915854878225317978745298283064
3527613494018428916754262395811937176155
5259694870561752513784185039266396748648
8293542737461642568147727824260268486118
9205988544868726123366497080961477788671
6510806961097109875949737550487293768831
6762799867698248128625064752792386854670
2495315198087870330720229076877255156418
884268834688849960668585254278793982056
27654083426682296404550242371552055477
3652804455671434830246089885169182265
725368839650910901842377454910146758
56034061065629325737273147141537107108
92336422487395572067289470243559606865
4666109528578046188084183862677047164926
594685174366329497276718622572199655619
1580297117739854216426537337731682996419
1432384812582044381793204008243787901345
529351673836479489701408532939997641
752233087210518668125096889816041304291
755851938729117869995958814094348667233
758729091226638634624912602614568463458
17565051504684959725553881547472271285
5635517294757057979244706899461910786
7126140811188451468747337130598125835413
584934383198655776920672383110091014529
35868935458544281567656892285107783870
7242950949077971591190601064306508964
911625205313242370236381037986397796380
710421553398559368282694863761113471018
158532727464456166463020305893273295342
667742694332389859997728266986386891281
345119524872728516756126188375881837873
4515833241781792570324599415382651262052
144066843473310468627950224540201207939
318520089412441522795544765021777311594
736948488086734080237419810211716768813
209568775709372617203569296595143807343
692248272898163284914397340343894788445
527723221374276280677218206283619728514
90780258264621295823806156538434493118
7811953594285629606345941296727559692471
245876936757325478509129628793259588683
63934822443060468625639127544876031597
3131737759975686211627324058421231644866
421576319483687814546792792975674792785
104568522888467085498110835325896452456
26372586318698375338573756052719811493
265935175355686394976191792997517739389
9972200264709859751277102596223535016487
755438663675639934918245648271297234043
7555475276744575694099003408536388804089
5397889170268544222930049667425120573524
9752340252514212123710949910556743962041
7357288554697809781014782069367855406508
1463818716045889612123473042923826991507
17834091653255239821539121733221042894
869198733964614669251480660267214706525
6588941335816941664421216330397028481160
303631007195757194641728433561143376389
5497702454152828764978317707791253397
39607618722819973498119646931328557973
442561535489888364110415908773560841331
8907673944958851173016016655853068151952
195346037792508572625276539746365671012
286744786305857753891199549868052559143
635869349062760463179293517634001403284
31901419856896955921448008209653083384
53369267566656737177980870875046318993
864383924451911580234779642741874446573
2946613319399771787574554755222689155199
659576818221583379439467361351029282252
8903145451378404312238154553055421745
9001972214526100594271476210575316422782
41231658243941392899298910439323315076
606418955540182309914933187791069256170
1684943315666027101782223677309733916
5288781462111846721097668193103947019272
12507689949760447307605996253168358306
653160377702793817691476206222357791692
8182316721251146945975626437299075458789
2551441828672817617598982992484827714712
733915564034371149452480632043603289541
610719748653464665608183202823662476861
64405049470448384222477791420051318931
850355088462682523117217178027567507248
2854930235687156783484025448956184898303
642363483088631152552292729560821742362
35578778749913755777711913534215302262
6029254374674147898551389223927999306845
2740188936316433546210848418664092246597
623871256092184126172859673443967359857
3859493230735705688776241018694882271142
26476018693649552771649173814581166563
1057334518337177976646801171369765713001
2406165799811637763066918697331645635529
4799289641988731705074425814562916072757
25984123442573962444589113914256436875
565313756820543380347421897673509311535
78977397481789731463456956636884862227
6095919732444799188781716399209924258389
283988295741263341762384181563752643272
62489724212626248947828713593643009190
5199972898185398351220514398714196298
983354941945458224663797179538891863
261275415601252514288639162668384548492
9525270734674410770110083946735616337
957721354996651893522579489162983708075
49886134139643428786974121774436357
974676033809612545872038943380622796616
20296482756494812414939439426811034246
3434275625429698785559569622910853753664
65970669290131611199502274126651329346
531569821977728776796528170613946418604
44919918112895836824812609138009501347
633797322375376271659358407268813011
84608250367048435307302172679531643954
571645139518915336334714877871876606690
6158627829637716107391596749349549792775
2252710355469886549451724323805315713374
2343520127991787842552432307779985789
743650121264349035420759190649691699918
421181587341359080528717644081076297152
611312375233731033359444693624023562102
903451506174670912538761491874613313454
752036981416804433931411430435377731340
196051421011741169029564818989565894633
723917875071106054245363528674743945479
7802880350017981259547303336390585753812
4582879258009610157654375389246955553945
1814894421866771301219101157210926225779
8404358214843084591110836710874923616576
265481993468799416533831389260725944500
6511362252867872914386611146580295397045
572668668812869571763993695279730908483
433728448277461754327620379028475129796
96274561158705549812301221508294647182
4679274799012603398874425137580145644010
897431308022855262725173681615765584041
6157747479532546412809147247075715044
9085852235082618509255809070365489905841
6786850162601540531846940998561224478
4706613460428701207737506351605167457324
203944477744646496935445653647052374
18638197903183415514385529655075446587
2541454224739318362076101461888330995326
194529852962653869210903440576975388638
9483187033299099797340318300243370986655
3448777629214055292798393371536921844701
985984932271561157335867916310194188057
949997542176244587979116334557884769113
8936958375323604745180312616310978615375
971732742829301561881362438562364758920
7868454887023496246596868784234738472261
826635038603197965920017710411034391321
992337411098796412012126271160260423619
714389247537379593584554575026175193276
34568312002805177110822610886594458090
80305825662293594138730326081630192836
3962756661759360316634669161865631715642
3089317314529725505076484633831290908576
690784828336487132363668901780932877862
38896836495988885485925152626765526756
82925165711578236177283182888119772579
8641731614656044770179091572945452327870
41338301416215606030888735934475548013
8304534811121015673441847710112434996248
520280636602681033505055741271068351706
871681889067978136898647099742397835387
462983247100205662101702187733781389949
5361662281021291676195854449950688706596
44683787733692483276884249331690849450
6161439399857016885575461998861938488724
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]