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
6319444192229392525824746455414369473
3283185022404410802884192342104525177331
11671024811959247973655317541152553808
3298129238211464614869211170121166913831
8733807555015838112757913768933199972487
2157807622582799516733978745798151319
71085893349796734366558773288817952490
391122339029434657871292927301583485798
21792512838969885647272791335633695824
231625966555739274573882816684638544
8806808823092889567363065077300540208449
233241614013649640083066952843863812508
509796586174904442606912838578608829
8768399918735266323479314619878833275028
6517179552201755755057166952754495627410
8721166033149712809844741544218136351739
881783208608642145727239785357996353843
382190627566712143474105769986999826459
42739598525369321720286404245871441364
2032630846743334184727197230552035094459
64297097387228973660591770814256633916
286794276742142186323773701843076189513
3273796882011326170145106228866226759915
819731736725129795250093936633769766059
6574142198267059856162224120455953341807
9671121438869655942133536093804646622803
4104687062661333384393915725538242347864
6706667884312926980255448305787764705633
5944882145806577621224028468776514767732
55227566574515345361423669128954448368
4878854083062980773043892681858789655064
930734082736997921707558404768909375760
4769102867581346812368639580826456116004
802234497907795455577843784036811457494
609810408960860544801027929978631905406
801720138561382513954788262205586921078
850387226544724979337888353027938810995
313552343294359428014109733995415921545
75281596700298426149446666566030835138
55621267260737364919516617546272986488
7403578345152365433943117136692828604169
36626154387699454203167143288580658427
275483608814355490876070882630080306874
8636540983302044918875537909572916669942
894687355864341621199947100757996471118
96264809238073558088668939355072216943
415868308929749124748491873793197126892
7041648496985789760849152171308257022487
6058926613912394114221044420355074899022
8655941223141534125546732979601756265749
7198924395951534394660818918324874947340
190415195954599313875599772835285627239
479837762350163528033474515123625565034
589882664109800754383625869711622633226
1238592488007083725740819295895159799870
82837494287742977843052498946591835814
547192988778509838185083177486549298201
9292307882728815987284726407338052524149
722269538774954284119790210355182063337
4015643872808527796017828690134755602580
5462663848344021510467605705616079235561
702326786984947723081083655994578275036
36250830533516392321766038158460933368
881727623627292099594555620239723392165
19714316289198985095051695335753217382
1249403571942105850379117546393119311770
2018155530085455026998915572355915562
5656876096829873929919877009433871758639
99450906778773734608064884496454003121
3962562313035227882887569289282016997644
607140775551665601741828273312396778968
7676208160077943786398595986986399071
9788795417314057140668088793606796717813
16923363312434456655025645887241218934
245191924388471714063574651578649992722
574965483504738834699655411884995028841
7160400072151956477073425292281648084442
5738613327467155402558505088299143402736
765982645013218488135829780924381995031
539258609813335856677654932996361776145
112749541534973168703977272718856736
2625381320572551631086338677274111557199
8375280711644197782970938487765397722785
935980346866497308813101892979526728
409035317408538736512388635863842714756
1737759952652591402943315965904595966532
4185594415264470505748271953721844928076
317145859664617497618089355449967945102
26389073422742367108151863786772714267
403761493557314788594373427389733344279
83045761266753374203341312499377236860
4680661631778538396867683417907522449992
81924390384687563502989482422054236940
914335883447599688896176637284319326169
16349417300671573211406448560513105573
63117893929214021873108872558601633643
806224116608669631615044659266358472069
7954777997224431248913876909405347067915
6025656316268533430026746364122091506153
381118887716812788431551027617258835846
632216422739268271977722133150056594426
5512465936324822613069661170452573252591
3535547228131897673325056850108241991738
6405430194206053348484687640708893644970
6866598326953508734757141395776783078996
46136896617691278594897610662922072232
9234285782369852521445649707691092753704
884071061251174262721486782973066204839
679280584851298775759061083323521799241
9767166935444382264071336646197942966588
43309336577931771622691375626990328801
418093761729318851691252109933440309274
5092764097353906500854526068563255919914
992640691678626499158814355486012391108
937094628885455314962402805413220862775
8052719550813877626872562884100269293490
486286883754905582892537664620248417498
523593039364100155486186919704495566910
4199109181015991950268542696407768831574
29747255181579124779325061998763336230
92853903588462197304632172072029680970
2936886833863703210936085648828161608420
2734249180952528100837022882604017523512
6180235504178540676211741775813251982
643771284560372267065952225890295099690
3701316766474062720949496228429681162007
8639104111812153799413861934502679719129
47097772243088598882315243874983898028
60979245812420972871106519679502359834
893357054694114640868635673788793704657
36243145396479483487188107268066866831
643483686714853074796536377921596715224
8228088106844724052517940192078307504
760697619546617242244088394709740424921
1298997514109604227190601834226681661936
98067459258081256414658211900175592238
4020756280609786844767255375650045171104
654657845298863301216138293692920354334
46842173836677315538410894064695819226
87976372005501359464281565917809809
5508514572659272503566884685143317075042
10471408115136312550617743861516057616
1269441838871833887757691271866055788227
4345393625693929241666928936164957687376
9566771629933869383573363707780497426266
690427378974001677638825115913271472880
219576848195350185324582449937275139644
964679242162144188712552507406698249732
5785979573177114225524177082606567869410
649696196347351049854578995327321195177
61630173852184278656659704388291883262
4871125523294015942058039858764735878088
74754788227598817028577710992846993331
17456434790547019765423724259490991674
670948239795895471758568381877457296
6548515799559916258524845133424317638
4190978898552982822911694259507422709898
2361123259713889398531202097777966603786
9455181799011329857262708049498798332402
3163832227715633455316724839420615208054
178329867718983071613453070363358713933
5758777183602474249274905002640941989126
2299144874866291474367217325741549974077
581690859057786883483153311140461614190
401376792506548256826511402112525091226
3168300012654443679952515838563285576136
2514981992884725180884726345913698348629
3544958929602385778298076649639824425950
252518087739363478052873422897794347470
6217204946327019700279448253429115158758
943420825822588017246125992703135499864
205913816721634151244315802906887073829
97449831322818769760406941392296143316
119194124097434924016833990794431692055
9627803667621786414233048136372751590
6879326312358511527580705555266790846394
13856593922487528596204186547793928683
172783283108651671516969334104366733009
279015070081334287114615843948450831225
76412124376215263236631074262454079530
9808504953464801335980849564856259271388
4264839020314549409151647832332573058761
9721182981668238755535049958918244156362
1945288849128895357540576053439582112479
9205871099006627320247141945954187506940
22228807758898174217328719130977644450
119296255313665281998278242890918738855
9398300321866900790545875718367069403498
369855762056654161803228717769447318174
7061902611552861113596081407174098866853
617518415746622061475008317778810414243
39288283864258696143477150307056322547
389419336068752930847514297318995377022
9665393836442003372035201840879349446795
1325968477629530338598747143576529651749
6654919340271958312315336317315224461649
725154482135718493762443792991698316636
4297455055323728314914001948239454183136
50881653441430599337224894627948408162
34813162799772062412198790128671058384
335939147074901894003517670302960365610
445387462451525018355441853617787535550
642614556697692349927837247124570899388
7540283039251347204638104556421925003660
240031378832156835449299329971634413369
5419138519079171641351153112781917053629
6563453274904897823640464953601973848367
715143355173741681186814046697590475202
736429985554325520678637992239014041209
230417312481417178716884893993915981334
185110626908297316651346401705215761705
9146893648934417173114048799826142179509
909635083098499108014156743879985062231
5683969895149077579577732490947942238783
6042628823057400849897029209459843835910
9651696657143061891680392888226066911510
722436223846419441432267106330086569
169705892235952219469075085135514271088
227336519631572485845067436609526706244
491132697042745324495453336013087897730
381847980614763332189649950480590446652
4267431092805503924694047609609757671980
9928738496611527963646793859218815562994
261241329571999478449525051130138643577
3668787648400143380123253566035494978
9465120180153949836171836868544596693386
4612854054112755207978247752752664503582
5047873074058451883075861076921879139191
5984349958917939433272587008223426414823
63234644487335389134567493098063412131
93829644986015464587446846684835152688
21688042598656678953091905459738491109
3031955431837095568017104823581048234603
420146387076922197215389134271277268701
20134038851693294672064560144858433388
7875683118703836447178348386530637594
5793998849272043850556527136435649799217
7141158880574804138655205844581713487310
104416108768482231327985506986752658642
721918376763370217797301203681026282635
13194513120698179683578711190559481064
50665168011582071143352256873238934612
98649201485454536298753292424891507072
35459390356312302727332069842686493432
566878839331962696826717792617222339467
200942620746651533919921615955444989036
4593144386952779272324284308426935846987
3014534746867965606087577932743854957877
728192974945118755429544135858287595656
9319403462530612480165439875169895972
571449239430875708633151250100279137077
1576677087997630722415612863536949217111
280820846468803097364275201233351966694
7962767645591215119723094073896724711745
84662162563430304927353337823713666611
30313529314247846330845101375499246567
378253716679962916946533367674398496717
9841520491997018253668216634885241207515
4409759158868827850246866287943173977671
7438130180354519260511347185427876920
59880019731132622261803583652359078538
6354101383697937139727299932697983834470
7495659521014107604063878987844751686959
7084734025799894138546727127914415425973
472720393205543019186670337552728681746
560635560985968661756212689297224944961
8015970647944673888811168338521241342462
961183764507577742094667728975599175208
44458277042457935666351340276965657929
1031820646286752898995531540113758936849
2297732853272986555465856259954827388106
694991911309331670156381151279492886539
7111595482457062794385768344886183536
8593109599830728896086714837322545320
45671628705746574058294519199543197889
928988514743648631116481560591461736057
2512549667446582938730708355170688806978
681989630827865523976500988911797079
6814832053034137811067314108859258546926
684247543921639653025005519101193449
75755898740391812692549450051570024313
99985314264263625185213267023713037625
4234127945377385101023951217955328695
680048309763307658324882631789013572125
119820214799846081142676268944023
1931688351308955528761642031845464681203
4290965562574010882149964986709391237060
894994307139432042571234959836021435043
9244706151092171598568384702616179699999
6750626937792508697694537032781467343793
6182521934108863253483355809115532819847
938022827778223677032191242840590349691
230521737201169731173829204783921697167
3542475046202570854615841703191293991047
31582086107540894439285247298783289241
568426393682044144563779030913686879320
191973371446149337871677453968236412494
1977407686248667832485995447243210919691
73415373874921160055561883295989959966
5155754615156400193862699013219346419694
511194916228687064617100976217361294203
472365498579205310276212960150539177084
3550677937899714305121178456217415401337
54957817830748794234594535117494438991
3480962337777099574855225041181078822528
6405482623696007879349714976248018148334
9384991165567165658971123725490917719231
187494191203780367764199393906057126104
863432748322326348649074891750657559692
4307450443798103219519667090438150625647
727781769025875319225621360373189753349
47161570366039473846168142159227842307
11562358138074489110668481846763507753
9604693385363791780353953077245066124609
6629176665837751682045399530843667705311
8422613956844138236543351937136223864964
8502467055265528566565398817317090470
3284228716648675119112299852261115012411
5305924827502408981765939691495787209762
104619672688500312194522796885662184676
631028338980364145563567166812730568972
8491529223820570547046504207261622558
484198034789886859184396725633427468497
579936476125913523341560662997894026106
28831913757236768134665188016848467427
6214582840093010499814273991647246946750
8544836786755288285289023787437932751350
70415516718877728627791334880979409674
3760613746081516411181362015709176440
601450111448608538953665771147821565847
5535904344953875483406926378890072858
6073540246302222688655015219671648754634
7565752915739677580050342057218595276782
4863146956474819206877608730604470123044
294923344751625781404431702752319596681
303066884385608176653972158112362694859
94199964884052694183375679814085749218
2620951048009249853185878690662403748
303899271652928171666042393573315564840
876442166758589332874978287417548372428
2617878122820737921787128790236406528
833850941661056636993688493846179417064
394180489197827779034831553733393076473
89875114731609419306181527841055149208
515720895128857713115210984795352755039
16447251729095288810519643342962058412
782596338003315726655861049475886154721
2505828826926013269161593922871993849830
163835166693786252936108412561659178505
5276447751772771520057973727141752962456
5549138923687328166399604782143463548061
6138015474392895842616048488062329360
186753848474520732794312495507242068773
858387657308697819084779797162548469644
2780104649408555468834022556623635638527
411828419261582436560643746206473745483
8419044672383424198084876429268823267
679810307728113479945057882887370126913
3561310621957276704727816943689578266098
8748321532289493777289723519756992387483
93809475579296519035786666894814719
446636534209131542124287657598631656433
228128986765254190663318863150731645604
433363129004803785082985760286493037121
79436212862695929182853453655985296944
869742722225279489452791675585944529335
32511916968332761037829832369336893083
9055306838890252934255659117608475700
7112231449874213978465685635442367753311
1531946326911173670089052305924017139502
983125589968875849948576947066941257
276962101890702434529287132675189865328
7762519104336464716833890739425453872
7429375916689972137013843421489165787044
983968502083508519067717352110110121757
147998344085393768474016095967792385138
1579494830885189660328966233549378089105
329486755279786770718572429920829008523
96244841724625666692575891914592035
3047991397236737650822233712718925923447
34585271507860658019501914762125131029
363543344707661584834772120533825661671
349357853740796012669255698075333791
5079552225649169727392313329749652375269
7096559084244256776663991662679726654757
614946743267953189484873136192258925181
792136211359731374866361389523469134969
7527172355922455918617761532969592554959
174257669707201715194903498937281832616
173967284749660147283558679964414248493
2027682644664541692723843876565034221
6095653817002764488017135691935322165
863441113659541081779094377541276361220
510980049817076642724755342693612823401
32788629401399591840864480760385265717
478237658475459092442184349979846896113
6342575277171128262953191298994294941955
4103343272896367141475257130561321626878
32892999630721256756935400954350854261
21993985499222485019379256165592389912
6031186876584046235236905993417628357002
3782483920821262418191921856985254863206
3211349792655265107698407696042736730
51145564513615272673207655108616137300
957034982496462795859599189913078373809
430774835335411464242077741689758359432
63775153214636322115396766312160343
42217205439137622704081503969417728495
8517045995400313427850495329409077032
3121587564745248740864095001369076446187
317739102080213189242375185813914222463
689011468665812712711437623429764103500
7189784868646986792997224619935569774909
248049644271367458047252600170567991983
2932294396205892602667671206490494463507
431871704791327847641475959677168041829
6712896171219052785416741844117930695661
235533578787873749922717790514598137649
33406942325653177196838416427818587641
573870261332695373188929225645756747664
937767505530591988042740179982083479607
122139844895706160896072981264863202466
1681212661049134404142614932567682284091
778829756433503439938845439373435328619
702473131772267770881488667398493054549
506457731042377290373558749331749916407
118511439377880927592691359200731483191
2039595787961522676566378564426040194783
813032490057859674024661752513387096333
4719592665003398129240217976595220021935
839542554997739788141428868937312632772
855072785878583513521278771659832260618
95185825587773718581035574552994622918
55942149143784055746383456715093137110000
1412946190961566271981848923783423106
7032334856969168486830023670632911404826
851721699954119980442408254921814982569
1367745100004936728190361554375095692402
1403121162618221656043742345433482109323
75843398346694835857826568836737387804
7916624816932497626288819848859759278214
40966100435132748313133101528478018897
75686923336528428016791246290979314843
7271928092481567343434314072514032505900
91501146441160870815712644120271759523
46404780189489539909332132650014545765
73983591580803828876230366051329729188
552701934669793183992322445880274462890
7800580432325641940227922306756311354835
614458754771851383905974775386028041
957497365520687187006499531627712883542
2163277398219085220160125141182281644970
110522255206630260086944636968855764260
51337270175145777008606650232782371443
7861826553252009336114354950586339354
4236816327285628868095364437576386732025
498971551183476747038128809955037041066
3420321499064005909038354239781883789406
919133363700798927607581530415533388788
22857041687774821436639678413328856779
939888491399379236617693382634341743850
3092227215534458861109479783620077051
38901665904671390865101185483621928201
6677476831518480586274853329481963859563
7799256720205728504394386719112239375610
30531964295376661407462238967577422855
789970455745613620684056356928081312812
100638865219260435415089195062663935585
423671086924181609888603450935875864982
3607587878223368436497209542903250904476
734071060743786273427704014576797218606
8471294587086166783430129450927438433995
4488689481038362136095434223724929725750
5455406635052571524869699897387130352
50298382862103424326538447952315564775
9254292998242521319167783032659936307260
365869671681285782443131447371562926780
7015811476153982429557774634397707177
32438002432144172214234606449652027195
87338692469163169324124373283755581412
35071583066104654576469243540483677577
661544308631483452527806335084295909737
60608107351689546003692959059838752551
89334379840958764812243193633202617091
7404802062391558144571101974324739792424
189016932294409668089834686810439967806
70113875286116356955134711468610964459
7627950095151204820870219154752765157967
7427484312481368155348689357675327931
83589839644743447364521066717063984626
6480586856627661995540017920808013143138
65034401561055991575593996188654843079
29361213249935712396283811127839357674
7400522920342912656605631286641620757
817627031112993759367425145373551882696
919851741377386050417304294267369526979
1952430195914362102078796024113089227098
246766424099225772567224828536427241168
4052362432888759518855233575120149283656
644589344589716433105168451414596915003
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]