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
8129466383316615881386309265883463653787
806132245838792483282417172325128277032
48069373285710439531285890969045149442
6258818870208904833399429973753694171
1652415461100352111497954775965865158
96523427244390732854943329650452597812
987147973545674256695255054605159929626
7455270639534695113169633513225924229597
49464614470713113924244638253440507939
698113048231774597792069845340948899261
25629609718128733726711119366544626341
514554184808617862772689123813893479
779085965407236401353478116433259062360
8189505051622660420934096021304343238273
30788740613634958692319762139291173790
718344562029362032106337283197581483408
125637031791733885858370163375427250
850538368517903289691311057363122192107
651027876515464640536406631200969176669
5590624157062168664886857852777291403186
7261897750629605194890442215661045057850
6739862542646615999622959440452921442
4287238236224413368547459284226044453677
4506126586339588980181204716697520597982
91585799610969083238114133698493621265
8195829727773483596582752711979161505439
989527543082682309344622880412979959
8587727410332998575578604093362283265799
78258875471349438394999599150151585926
292290504822536154731298328500459293455
7382596413701636411257881857724449883440
605838522826266104810318116598429816440
5883245551584245812551034713437797403694
9585607121458025449628768650348027379895
111328979669831521879217357799671467221
59007584829283648473609336230871118829
9360944141564310226570792238391935563546
484579658380228499602111143915965369867
7019548239099487617557379314741435414120
1517626415874693596824261254753190553610
3390849273147214410043444928977713353546
8303668827659774921492677502429733836058
694520947811685508622059371437328977505
1556954386808245104771494085222936055895
502876215353732787539631206483532789057
16238241990980917564252375863905208676
369973917150990077658021317395845982598
9441440485946214125117676961437009119
80547935347586655476467606995891012410
7472613875545756253824156053148140641247
6639318896482945608087827859628396855218
7695655881755036781830314688689817851595
2705145951734734867796008992905474302409
5087308657734149840885752383264576287429
78164261196455594878429352455490266267
778179813561223514848476335627240833938
9188832858335095933750935074837266262531
96043777911792929895214418360787351633
594432651378920387460352631727436962368
837586625295546624982816197217997744522
459427631645937676593665071296818947875
17139858112802483285406790636637208400
504843001886148273404373568593821964457
1047651129234761359715278590845594661549
768398393832324923287797763607157528570
929450053416811694162434891157344966
332060385360820780536155621846898357308
5480776627507696408980838356260140337344
607392234816604295985558647602622046860
599453692852250459173794533646250271583
356285479575578977345306635956614632387
466119421895873206925706680788025103482
971984225369477525596609913805427332637
511771513144159639784931170367843686773
9861683022668325768170664035849318977548
608382007327700962623307990997359316705
7368976921922577427541952143608579251290
4902848131233892938944264997183838407878
856198181354597778379228891828072123
574523262640383066499866712706618015326
97492757093868475296976038688859049895
3551723158047542620377110334483842671
289157059995258183415681731945899227989
885566193051414753412543720783193348687
707718965001497637617267127720761288421
496692069977566130504743209649994038264
704862566171199439211948644067541166074
5379344263477182022878149422169187029
824275319814250641361277416475530294220
594280795733100379385746407475329309260
269026372934659414757956842230578103433
9707561827687531490096881339470637933365
90191251201181428107865117818299395293
4302823227492575972433049161866780585308
3733897938035419144529593300718937144963
933194317671345172235415763412554165
2871648021414024343833998140573777186521
9818462129691070372525533188315571473426
6843689238651704986636136361971039188
846275433548785599836025943999441847833
6985890223772155969390675123847314488504
126643614948696059426919011623788126743
559949738096750292578403197691082706994
972165924893015653920523516874548401133
3846990253833533216606658628551865392
39449425585569730077602943496239140304
4233285633112431833466489930490229709693
107558574182897468451874028659587532778
4507382549916935440768628994998686506444
661326236408713255862007102977468860744
3249579364211619464328431733918347872846
6725268338592482835575244669221483042596
1000545148159192442340234692788038749795
630740398853787587663679985790266666621
8806881099982529179232388537849672329
8395676982676498861452147133742708293
57651505372861372148595253677935962601
6521227053513016573414845740295638365341
277929646994181649239422267531185791420
10687089830542517724553655932585314015
489213257264632789528997469175449856304
9885346887209209100645351946596565881133
3803814453081966618088595504785449358724
177228028022624243771178898629472413752
509797965541869301851695385271984155742
631950224785441572868422691502878868650
3180514577444179150713401306912080043578
994850996703305472491406099445183634808
490884562735358802349581601994398251981
54947917808553774140313576734082363884
17166958864635032371883134939270782181
4682709211781761985819575625222557448
9811493686826145957868886236348942356183
969787039063445922854625194506183333568
7074546494041391463778635450841579117299
3564362228206804966350755095942331407019
2830767226441656499518664659352333629410
4921807857073007102773342270748740563146
93422722362180638987612451932384742797
7232755999373588737370845215581173705123
4839926796569601455942856942481011405015
149036442471509240196726348497827656731
732910142007252566998684124632560012425
1706846276829835901541489289158010478601
307293093164950965099273892408282041371
7505114679735165563797296911860344478001
227673454167439137413915513640020307472
1008854075758056583197399738468945776
1918647536564362239079592763493110081906
8392554959329060300088762078932772606289
3445923189101297183732171436799612685615
51948848772215153812759836650783119457
80681918975497830140089579530618340
1984716984665442874766939539862956394
8619918293153523717032434742540941384425
6992625916838445434373522340907755591574
13883238345113366561883176869669913261
5532717537838358703299907832233826488584
4579500130483540303736357547668998558318
5636021167242051251545184168344710187
198828027996018611231771320649243993848
3565996772903718790227893684894658451775
35187559582339341565073654925957596263
12087675342995157411489584625151401109
899378763716535054859632533295925854894
2132979480291491996618738939631366645756
52985498576936894308103982974261689240
6327332619688425171086945426521755169552
9314322215238165396790220183587085393
3998266257086350725855993219096726677
1747624236843832451277022613539868479256
7860114562035318688811218352580086491909
9864102119736278635024145969995084945770
2980598310231112131868103424792771376605
395266152266807565664139970999713949027
2060607253032372479233335563397626449570
9384500226008882636847864017355885629345
462981142579923961189353782112026647375
760382012997721938644429934360274696217
5104113849289797487526367052389963935998
55504534916631574413984673747874958118
400688308610206227019570509441889363259
1352108059902651120680985733612255733318
2184226217728717539752703020160299663700
474964758758596613965193963644154568643
95156877589330255389291128288399421901
776083813984533698787316166469235
3444228758663578759056342108624124826178
655840672157654829672779301818474959925
1284241695627942330491055121492113592651
32848305992612796377358938871316498799
80716523622283714041879835716267513090
9281598693765766875338432955704356477
8422534320954947715973891411635080771634
698951486174018598204253166319669327
54988495893380270227459275753065019239
7146583127555744328478695182293689179500
8827915649049076157652775985187178696963
806810958871906846402275828123042117636
5175930746856163767968558020535661658386
3816478777304040984266623042160353314251
67154024591328158855442834147750381515
390110018947556295460451807844220258451
54673770896141408012232152372795574664
5337652644989667571348608108347142297343
630816228844132085328258916293525856466
5311363134053652764670706097755394238934
22813133386952333462804281485355208700
278173694974352490848256514360536668
213480421868866122826660798143928069601
897710374886907737156224282128656392753
970853534763611958299284778576560982245
698917796192103113331543039936730736327
1922882594390388760366460407394112709
6282267232964905143250677571250249025936
360917691185597660547618688483114246328
916999058555752691235663809619563592778
58854119380678127442007712093437673839
49412181913819233162889543833394362724
1384651040346145496389186179460255252887
913833796947906481698999212356406672721
751961811670844528038629252879684637586
5396754894728777803848309772873653443529
3447244237466348151770726429868175468172
902231952139184168185679963699368491203
4163442096524703620239443770205558289103
94173408369225886662434601835155382665
38957996917955645087657176594892964401
805797119951681312185967769151962463221
552348382142905163984836941343161045746
7434599838196388849943818564700417215698
9632851779859154796952203943313152951463
2413264497608805385273938357959859398308
9016918595112632366495799091995635357902
26844167777799084178449621525110755672
648152216455902885929368980735039902162
805537766149918513404058620319113495457
660145424173591071615803530338242121
5517553705702846221255293277082947692
970022659945534380673363931789830465653
1317380661653608544915937721477413098376
917536927663673079387895392759802773618
353941037488318658216707867658876223326
19024199136871363687312772445661312336
767095971012274834685092913143414436136
2048557375545030444085875134514244768004
947485557555116273964392548718397589491
78686910140485204084240589919861390340
464949556819416410461642697505881703402
758579583745595997423289158246616236010
2145180881742079506641920431648677
6595862442655799688091117995606422066431
237432249474890487169045455157247395681
9393397548549497569647641096366854159108
436636439011447913636664342619377681691
79413608749179796697005844477877592108
34224561362926849198139552352486293
234249611010747019946221952959487412284
6470558731864890432455768603774953852602
4833798835404946875672782436367462855812
7942797586417243272329414550199123731607
6921412121571168604997943798557679961429
61541412739089361647696776613124428679
796626817934847226599843668176002234132
8523160534805067190722461607557069317424
4654330416505931379633555049281715523213
925662969337128168051606674385516605796
958939169276101677512539406517895038695
6872137351763060113869363253995255119
3666416351052805255341477378240892
5117641999428660290986381647719152234
707785490955255431495118764871933162
4411210932769086466447155784373810477495
6844410831585032838569525884717467892692
41786355964718424884853370231858379059
6331980529077711995112536555281986815143
45707211767357477660964398979921394765
15336348318838011768649751023082368062
591696711136570044175133135522018346022
3615810766375655270623807837290119397178
316949213235409651367280821766342164367
29464297604211363342491904375783834103
4065769483931992377710726569721872454
50442627847096289953878838660240081991
834627833119570491432827325482587161221
7020533623744805996151936035758914863306
877472291447228162298375455236478115437
55261563063365444914752510932877587864
49507732382379464071915188809166054493
751635929944200558682871594316839167603
21011438533761352035596605231454055822
7980713662823246154950729649542765649136
1357855015957078430766719014833662188832
48967647824556447993978491520927519892
10873531983318888758675116948926366654
1213264516631016799101093938963008611
53573107227841198787803035935578766120
2828458885169935226312715853585311248223
1594533875653518982848021026505417794520
3374175896276157436470307566625329366308
4273423190324615011817556763633696038
8768794980326423513084523385458028311402
4891539026499550375112434012694446133192
385177787958127070669453874232361449121
409272819928620229413979790247842878085
199688428894916686023193283050417433622
849733661739797931848578538413649848132
345137776047694412797602486666735565071
71550008370505658978737467984658761165
8232693594555864993984249807795769383799
31864306777623561023501302112869916547
2301780143551358748593824958793262131821
986178668614797195018896281241117351523
363166029202328888167121800306696919130
6839324098007540921286086259739613302796
703895613218600640145711517112094356535
5645796438225206935826712135470172922030
1843189806860209307376563330273866782
246146633449214769170896262272640468075
758571103503626758391008539276122722889
9329798062129209036958211768315247327
802382223750687746938676360170709229205
802525356224812907995484683822372719687
122982045231193053564375724904210538601
713720787958535516136552536912696955010
21447411908248485342988729447435616719
23947309208228687072574226693119608984
512992059181426465535540387033121162308
9731245742972824816143749144703880816114
101869242217876672592075422910914347629
67017919535549951461874373154499913269
9279265313726187314894762200968178092
751124021301017398324401251863791693
9016702793725606438755385333839789157321
2535479145624197987143707411706177481517
444082707923840767973155726522072743322
218151354476428848237745743958037746430
6881561811693839227872812666678493654
453952929614813772146684555394530798877
84692372825894412893926995999875138
687979472823530640557386797121810022733
25024489007157469173917285773689204394
947965383356239689174652764243750079345
4065596455263298013129338610026637892
55284925924080752102494830932885298539
9369986318745329723694989650219499327431
7525239140205593925231375994142096412465
5481681313236712757067765215527259003072
619449926767478817761306399889169289157
9736378948926326572654592326184099680
9645643976465858507796284733648947128562
7353669891592305368153768755576447135559
2231200157767859715887178359902556479448
443548064483738040305711555486834709226
156522435936278187123830933644101737332
7598347325923190911697458024841278685711
3387646133422131512976476978991982683
614113741365639990459135651259524695360
384572911676373516686335413780187925087
368559782976957355957956959893378953156
8268746768863298295596221621492254741817
6259360820158605688527545351285916444474
257639865149173037077617260636250024376
9396763765451071059743768517400286698
421490609754164742089423547985187787454
751218499946660417129624901803079646268
143660652565402206460155277708545092096
5470829341185693900134954661321939731039
99162671661325668588979779752730459275
5821803414383519663272629292819866772790
989262226117462830243745566766951112420
924614556571130681011174464387320985761
286377986070783673059226618804452787670
352917106164293190265622291257697505062
9909578796395162185532568993923099783567
840480506317689569927275614777199863543
7096597842946386867042988916818857965809
372819554755454865265626884651554852521
50005086762840536555689113362332275487
2191951424998883395854716280301137304749
3987735291799313784320837370233341336965
364527054522153554132637310930194841953
64938332265651167195077585261732060128
694781947333722782341038852484983956
3394480025144679010435367803727889513
1833975527662183877906114346338726737
6606554590105742918851117123834965776862
620442241900389096297028557598125006135
773699476096041946566548864394267125259
7055932102989665473072160518150885335
373621778315490565636628599383012926142
303930059153950799155536011097553217
473995174329557398833638342747497734096
3626664286022994166831762054337584021253
701640414051190026656730246832683405601
19359644949228363087398083827195171665
2778426718853972716724176614209224792232
521179505632713145477309035445885871985
3985235730444377620525014712674045785192
208856332084695730592647390350590343428
954522421186599923288011840824629694279
70801991048351205118554755281696361
12183820428680532841021318865052247599
84337444939212336251223289913092491725
6062222490503279972020804714690973292075
96892434557215549466671646021459443032
45652311754349035669244586014797492404
4022868079013879205861162443476374459598
344227116128331553731043611365631894955
5008723320376473777352844234680434986
7809185741785105430971698522195337806832
9667149629673867945141479078461044549
209226177478120945225373685558211729910
584885124481691314087792056754121441039
9310152789562458126218564039839462959432
9931623711075540170731665538121669422342
24742499661803473742302586490365086809
1956346557738692198396438669862986452081
46588751100562721921880248096942375517
10358148980378778321784285437322779937
41661623806888954949219325538432079376
223748349455996705974593514130985776687
3080815524369308364791978808479588437786
300918376270188961027453102298022131467
4121460982387267212995208221211664212114
4035489325689266741794893895523875817165
366279427176326544062181238628111096781
904382292964617652217475165308242863623
454993199099239748099105145946789344826
624758564249753854898862160213065192180
542761147867993245779605619929885226292
4932332420183645857342663900846810861154
529244745964266895942916416548570877564
360877495941887622555302759163750669078
30616859620058294126842551593322461248
166032306780439625214902235764243660460
9723418446841106117325377619516292241494
3801514834948717144953316770203828881032
7131524791884910958834304922528065497623
3893367694155643982824418143906544891346
567295674319217819353673461166333326753
523319626574829318434499467829137374468
349617248501898595817049677814694677996
219739692079897560164588984923684549443
939719587494868518180768538174750139900
29511703910495808941568497105615417597
834938671440535767358757267139593501213
823532309353457982964864817454366797940
62776305817380862169974800184728443629
8001758272905267587827211039773653774
603425372895539178016698912722635396882
2365855634130294931240541099062042241
357038427239761632582372215742675975178
1074970754789327622237301917492015435149
87511655666933215661853862324412975452
2134203010007102330242263474220232588792
54290658673942797541962523495372439933
9935959662784359900733159270992869881374
60134612707955712914614539335430893813
899043807524894819389302716429916639957
379518717479141226842987699042869365647
17336515418397287211224054809289397644
145318158654352633527792667794270349203
8202591032342953187037305334722646701530
656248771535953821616227167937618804985
488314944240265927231262563318595929316
7300259139641182880390401713693899036383
857174777491935353351131735526261204659
5635408422858483626165544390672911397753
97537923357549950047835082972769628225
153021231915547690686807712990464147832
8383753369256049132658908167227976774624
5516841966079667700360618902431994293951
4232904840475206454178898342110135733875
7349985669685825684168221070186957559299
158984964758681265732087607230245611241
40568770599739871566579815367755128081
21378713940727336841192647469335778152
965725125387679338899596265991086047632
6425455976429070657084219253260851059845
811836236734282167553599572593136626737
7523764267137478229257784648057296443
92649539748785214475307057427928019780
33868590518505612526556941312687694574
4692692419459877754467942642506586481
41854122937427694327968163621976483634
2239262675739703297647742768723961725257
9633616046923484268053372752554166647219
615992678199207245684154934445982732614
4046317070632326908640302389941327489067
717878166468899494284980410961999299963
974353454530708069202477672702446403797
195935811593342283561533132726648083804
13639266849256432219628377623750257838
69372470977253115406140799467100486669
768295375952308559757912378168273449042
435129855427479877547318433806614873403
23312115053024258424444085558920239
7734974720793995708852287300418136586786
53987025809447684763572897240037432957
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]