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
83909761670990616641417830039453904597
2387770529957126866358834989737623125356
6094806923749031266817933401760112749107
5515996691316664445873330549799665670
78556899882152934958756289324272932048
383851674565349112061437449889461055409
243567159434839147139015712379198186442
9021270783961402458417772894790557143383
486358932077243432776249719393344864223
498127244486985688027407362445193684851
807448546443989347713627693862116563491
2968904786121888181857318779443589026490
683194074833967728687622654266453713594
5845707171483989971356971283224982148
865016163549764621791925949689181814894
813048411839653382604671968964733868924
5426468692643995684365875733383842669365
88128423425118981034638624765093995506
725405268302506622398886010854916739102
3523267080627168273314174800213894223662
5220384576744995487901161774443277173
8058504193838267815465577557274766482973
4140661611116726308498985857198595049
814125360919158739369601302492742646175
920983451082641847048642832418651631758
94774727124688933774019384894653498223
151793091796684650832312727745345418567
417964236872327136312358977391011718324
21185224123806945487555358188475303180
2199102498922290907221281825516122559
1428481563891298888320938810699988935
23641831165994911874483662367394377227
97975598381651484897847161846179207707
289747133346481832694310460843574623953
870536969534358854817343960317255906414
80261772239393509017816416722030888330
2371376487711432680956427733955345268158
7435546770249310448731425397190268525
886720456312501608290963968813022479574
599732550773304592894573411690794935905
280783035924301072293902551371642932092
8243523676963521971847107130974970891078
66224389697479356272363896497352466194
89156665573372598235872507510423778219
7129951514955625202893501086366411
1209923777675704275060025743879070288203
5029148230613961513951832832952609250
7488897224315715212633202109892391881
4500772687863919638325572117431559176946
307047791826987353062152968597976683869
6945321027928496980740285641843624331301
71659838909560275668592233030875437351
1691599942983708682532752151713574318887
851551859272971770372505137402020964476
5928840979548445798861682648680225628
7443138518048433289255208358507763466890
12419011518840226994938016606622133030
7690543329684681240428468260388884756160
46590064447309913545981661560794743320
7962819433744447743532524380396164017491
429849658795967089156892582710901424598
537661995586431375523287280684085246895
7938137323521259539212234035424068137107
783312169859088493741129946961506540
65204842399190915159859559159338866351
452964996566681161489170128364415986872
7148899217092783379442121255718839501
3746774832916674452382476656731927549173
995574925152169251112866267174991489758
6726635470974075340037188803607314141170
361624448583764283427988657891653048382
95575472789610902139702242449553319383
293493832200529556120671772306837022984
5627977061769979122922185960216327901378
4905738583396142170891174175242982079788
5740128944582167580263949796253562516259
11661276479017562418818882646758456442
7493519131785110200280181955168088067673
9183986942683622415284823271035939653
889233369930298737946396194111716785086
650950986490130088846894723279472409998
9337603283145723294769935825816665267075
9851620493965861451328298064154552915
961595686979868475171829260261820057193
385279526437360342539106418664056594904
1061252062621139559240288636513784058938
24863092243573642118225689114877439630
6940554895982375358888611654379990998135
14972557793953124795278331984161132052
4432247623296556541548553381149043664851
4993275270309334510744987070243643558987
32996802556604139996759571136337496418
945172754186350969136227449266483113438
39795155927984287719947626785764105270
8715506093726773501943019474567929119119
7541296288519367721221240897096107700
180222964286743160525589372575493714525
7992779812361515550645041173153821592997
140517789849840764389579069562786073703
6513106791475764705547467767911518131
6492854543809258239510844013910885247
80629319300421571785995280480954824360
46476393611130539648463846550873813623
461413811494986543258658527832590646025
562373113643738485957764908886482666209
51302066285879427672125485656282077856
706775732663180251587105478466779588
9094991011371391350334309436165644642956
6748914037766843776938653313310743986260
9263237052703009329768283870557814444555
4380892853171290141433492338333915953635
201617402846984771477280867269703777
9198385025434969706331976701251934405106
6410694079421393103781728213970718918550
481072828591559356799445490929963328109
2518671713334416730292857583519786368572
714120213889396472863325799219524934551
7996853224855395112372533054882691774627
8339226935735709885153603667546657173063
39305959544446918288867036474148023222
35611568344730948551141451196649310269
6416348676669713697545511045786159819
9662438584541249532526947445588045327
6698310966582546072779971225513576277
3893643565244871441190954855215218231355
517138649503763982481577524146763607624
4026342289417097596052523387513225746204
86543655305756596057105606532864531650
3122583922477319878237316006684244175059
3286304888283670439950345059575009438
9515648559556819987239563331504842134096
3879935735723290339995998794663643533856
505756783408948310674890359646079655296
3683260397595188664442188383122571611718
8355861370846016580843811577468325395102
834449419193876549139945960488645909525
418056673903946986615831611680263725936
58252582316882469455825753551035094312
425927742771789158813731291625533903350
2688639837741946494618681671578851062857
4378263321294628780811906363374714188733
43165278246750227877889416378141865931
8457645080304936674477422440979446367877
878285401245677624163483894737066395666
2889108990339268657982491217321069067022
461876818264953140673602765541146144382
76392523576289386621519107018189779900
342749797025652016299094708327646347519
7572573570887164744761664337472094984761
4904191430824254177849183242221837818
901437984678645013407394921430467298981
280047729739182659824978702249772618556
969114368144879185639465624393578491461
120828439975679615199374831748719349328
814386921251961478016387904768534192975
9809795151062390594816285974916934298967
8038421855766311660067708976648636292337
5205212343099165186247686494219845808756
7700926906443893292634792304463426831
82527625330158634014995983221579064871
222443513755247157751278618833368823635
7504672896652373582925652408519172175
748270319337231832613388483851419619125
886887717055137154781658820233193088
9450566581411267772019015338209168392855
5381325263178711632259377368766733305
2605735768038043478864855852932413465111
4702214268434182700367914747475236594377
94893172301074642453419415351437207
80904921541949788395112818051545278605
4517378321687054869566671403232812921964
8223480573737848824891667157504490687596
6384799969608073716360978905334212078416
4574261299644405557339409888355366011501
467949936021520888336156107728222038790
8858778360222545579883506013942499361136
595119557005550293056760560452341619913
5435667576833429386555289781858863794
6142128636819688131819212763391602232
367689626767801443576427379610579315942
20398715541324606397278766455361641033
21376888582565643935921465559967756691
3636748273873645171126643994465555102825
796447165783500836539120154593697133490
4720811933934123196293822465975228281
716830277193812717876420547491592820546
435679043288913133387625437654254782136
45985036873688139267063651903728152910
720697015397756247851482440327539331065
7025156360938198527844823584753334577036
51246354239793857122331928919873116226
7082866798742033642084168907983234414168
6701995873209606989646453582649484348897
5525869952346081755692617814941277429784
5254309393281143828543185235149551555401
6943804272623993954158815371559582307800
114784581873277248127381400941179303541
7928905987687758635298055790496073325001
114543151151445202434537583799578507829
95086082813488164398447413815985685447
82322502102880161456964167236184698893
40141167995844094813975652148339723028
449274422085357097344896227018616197146
960479064672168943660161132391467202
9826506583135693631486718351730620766670
6486669581915530934369033488658720078878
188199899143641380194123463022002908703
1595862947219265374038791569784145442954
61763215152559403789369210149991934865
4243407675018040856143152769509918766983
759672642606808772718505797868327257429
247861830684464862344317633189343457962
672067937325795811944356697096365034
1462217453439276133256136117587326270
4730771326887129444114527671283831578577
5176480448829154927492213502641986295632
32707263931258502135538133069348959534
4016194230168018752530947431381925905
39323814172375664523730394859375548555
91483658156963533681808304010318556996
8348695874449236928617086720504263815589
280959478246674841766874113489869654914
8598687433278682733337002565342337243680
33839729848357889821296939794116522596
574655031781388398510578145503113703548
3409586380222614288266007986102789125322
826071225087213179113627038241090517296
159528785766821944499752244992416980961
1077372999123714990181804072781628842414
10926174885184303314896940485771593505
693679575838548289066787281272317339277
94965663322775759288435512732742112849
641116119430787334055212859206525841634
521510835131744710047498873934217303496
281887173377107523664371917656239051299
997775977180788853422249213597246252157
67871641716294482947226877741090356109
3126109520418475817745624895135853665862
740936078587584776536912365841466669337
26473505345409893499436426742479267683
9663185350250136675599645167145272568
962560122453757880126852570810395138397
1999257928588701631673954517751513805573
7097568343248717749986549703270850822274
5906974010126205837483746394867863953790
96617907416141229313819604436140621518
3746303145464372507718569246156238246014
3931389264298356429083322183701067945374
88939158022823766189997116355233352032
321887977988775614836099174418636259650
946167415159589860495332627113878492791
79988431583344435844928646240945137570
804295763007886210591294354089747113167
2370528859757127180380565912468334691450
595684197100567496472325245109457116429
883056622911907779583537424609988645932
50134825912788791577018512165528619351
65199622766300597716034754930129338157
1572230862913215615995626620593915581786
484368638133594831197345223898923599411
8516857834379720591225677823549669473826
4776684497675502350553488411215457944832
484838191178192524122471625269347356561
5914746787371765984912557663248986778896
7867753415339765540434242789581379347
736981645510451820014685464440686545634
23693407835163512489888444975642759676
19599891314650253896961905823542676781
1114994017464634900636466410166480779707
3707551826712613317986203547624189087827
4587734758386553195315338369110053315646
1770607490723819578168393602304232239718
5016376144632521421866392427884397807779
9119845398052300364558742930678456853568
291519181872112184367425690582111824795
435313397152725110482600582890278152741
9737326962889797593252409130420350176715
8939688970502291462810496872685399021753
9930543553013194206931511065108735699926
927646265709105065051362046973343479722
7493992083584413509854898360247120138635
4262339550835888660958651302387096195010
1667324186298421106042216655907371222597
2330835912914871222851914614141428655131
5307649192884753134813153074193518245423
6831617927413817792380381505754963217
93375896282565059267224158837967822324
7468439068024859917397868195907178051349
1756262157575873363085451438847162491
763135139537893645103435544569127248916
3111581726843632115855293674364851251
74528994678962884986109100584792083892
939266783934136480188558414493342995626
6064367577936330577170879921878251518203
469058233000488822673230259611703249629
9339878779746975461222838567767636719016
493309047296540186420117290624291651161
7913481971275191265687613008748979999368
65735135861646372262595814945419857937
592294334821991370167613347873759752204
7934395142575418425025636073171714634262
353325865846833289441787508510950159823
421663345662436642137734355486127317918
22781416639944281936150626715849173930
586421264808790384624010733490135379807
542272659114942929061417838281673745601
6291814829208326623516575824222025736081
338988333621361232742109141860319982281
670947255888802782674498591454613363
5088528847963213238013433619744738641054
52039578885369632572914402874968638257
782566444738425413337727096957786012829
16257716399276224249293680009196978625
44688834155870941629343797615873958362
243053457236710258972655891218771572594
6542410068979121527044143572574028677208
4337774293815834676489356562137225267508
9967770972263535868355214595944847053304
943790071475591173421432138563367082196
470238149061978799088747905293407586974
995089462963883134243506758746038186785
1866294260246364961210916656124614798366
900926256348871224603911109362641487142
149545314481034966639322033110026504284
5118866216143597342454813389788347578395
154736845502818684878229594221617591433
6125686323126208960740349865553869673620
6057502053624396202876436778249433624446
7052305434983163565944626080857385116376
20706548802959711054854615301591322937
12594163104338617571007793834547116939
957944778796836631089228897357854186585
9358653161903050391744953072241077354068
723489159296297417494554515189181317405
3084756716166493192014443564777637232610
3811084924483060244138538165246444308
2332147034389153517696279694809091504718
1461862165874202854514866333750740736039
7547234037784828740364035559193811614461
855311302027335829320696410675872499961
4396350127454658219747664063972351396400
618969466221594749881259433786113856057
3758797529423365111244718715150194148077
4457945393539801594392411913445057902
412922566894923581641786191274525103787
9694442712247926427528104083441514292822
692493283901327820884256894318228905565
98643029405168229766289903730074535553
8934577871838343962126617294468988456305
2913335251093298975199899746361808944
5184354473974857165129936525318775087173
42456344779029911247057705679607473604
8314623532559582115864197542313550260
4040665178353521421735025346885430449858
319748932052226927665596370825760591265
8824187685973067530422476620998494528844
490162044774843244424799970779812908021
173258022189488470377346381118548054568
905255461372309197215382986364831919755
6935448114568233137788398601443017143
7508235782608272919435559771871842018996
73202111645547889788707427106991138244
549017387549766266709395123753213216543
2714106076956242487879522512188896135218
767220611516518098116277926748482211250
471953143506907452705643332447227781467
915617059024797524294412437614364146499
2810373780442255706036715764569144578255
548745602649923443736911745431595358443
7103703834234800165744819103388413002635
193226942715565114062444877299111536926
583674790382402693674878375166421942226
1465491015066105489569294021241939334669
1282676362784618614469389752506982499567
289176102373587543178070930449126535526
1064155760426979842655429851652849941938
300740834871683390549561359898611193036
473444863935473635651745456139795168704
67677906600182613756674730450465734604
72277534409319091477213091252592567674
4450510922007667835570436815833264204719
8227835472169510320068376755795597397481
7430621748392861728624504930638415363762
95576723777484259512827810237812796380
1419233689018372774934009590381425535923
2252740369518337760583883793211570135202
5770856842876220163770982215182236414376
602132527543218858158846932286443620944
1819163842488023490170688441124915195251
4734436189994205592083701009388269941717
2696879436859904405940432183929645841021
231298765742518272606692961379848829736
8886121481547738210720007611938018829157
6321139560996719133975338361312844817991
9357494113041965648176032331110705554
791877783859357338809523665653984375603
23296308286626718098983176197399715233
5935752716185001594057473616708349277422
961556085483181520993137769839585277419
3466523634801835765875638923651390478709
8128709863956861603923925161663256365938
4104217246777936984376144082789298028362
4238395186836095734229443624322355062150
377940545918422380587847916941434885557
2702490730070103260242441743022533140
4033736879692118002000554272550065198
7244302533526907358943808936480268004514
3596711864719073606654898878680653387865
52584927392369757540335886724767458
54785403567695481458428058322165355387
4845222489316218891347457391349328929
68542070148472936811789345248056155193
3390566630466165160695753627688022852594
16287222496870279594267774837286103734
7817220459817557103241219166734694235
607063224696647632914152672652940102700
6567131591586767908986485660651345653
18768137347257899054573737127012868243
2876613723442676583550514531242438414889
2344720424904065485755564148780848989
7952705432355516559285025404541127457470
557021765012344247432091136466673032517
8377993697228679410116142826878892124346
714099993612837453703818222154548747223
12127210591482829384079981545023736778
5036589240202150250323939824698213459837
9974281215972333833731625356905354134866
4810419690412982325064433475825443499840
7992712398273830728322609170861821201081
4309963062308851576856484184980879839858
44078246543928923517078181684822523915
1332747563129878628943363966599035202878
11258240650171087573953841955316723577
391359772655339839717717238261189809239
802715534266718965554477867638186643912
41067802609862816156673794230576263899
6301299963809365817845316107251967445304
27629147994985438735292327884257601775
482764543544620159650077083408657298041
872311493415478116120917448910155288384
332327308248173303964995448873847996423
1266856563889003718727318411312666367260
8854120692452035697647757432980018207250
6034442571116995571431812520861738126489
5941982795723167430609684111636395226
76546332769224081995026938193899552678
27177478274525388250073285271144457018
934548117002871235112229082265487618449
2161512878332300807392567343381725165775
5053546861644754694717995877834016934
2053324244134013172077262343514640462972
6204623115314083333311502716643226652376
564020835113207347437512210266255803686
766795777633706304250537262650491975261
84046098542622749371790279231067682239
5353755434862256790179993216523132181893
9158662471848164459365104769755966303
83617772914376468513393122381866780117
6042757141568241965626572388411023762
1183563173978009908320619294508217558935
976228742389307521624601205292258282853
846726751628507669697231312572013217495
954602511976982174666414632114546781211
7142476776533364616338166128129518445153
43636614814013918417997905936837323144
496911848003274277184214436790650337017
487310161380487099286317195721839811810
8738776976801785147682180530096284816
91211490370268075208426767676735698774
14492596737421827299241587561838612082
164121510688389766747623066485672491534
40284794245158183992969174254262146565
944662114289144429328917721307187048466
6707454671379634512190807850129186779993
9631534239233922838467441132646383204264
1647335661819846281440382126534430323291
3057359131157520253599712144713573054583
710191392108937095499782681278993369678
873578059041438893730987028777188479967
937886243481879879759892170825247151303
4033462991565144708253265393227723834167
331114739201107550728192636765770752305
67698298853250146224661737710575062377
834349280775056153298721374694926354419
69628082730188469767386384988102616982
32095079924364698831693762708891794493
840368717722428693939622503791115264144
401717437984693866233421791187458599676
9863576291585966420885087404236310494156
96813354338694793273797162012540604076
608864521562425998569451060418845611471
43945736380168679994883940683683724611
81836552800636740826063838374261778443
9190860423204824991017456566427933988296
6885762433432371245317213890512334761
44478772862582388298991802480792334528
96656131695938838224314856178494989184
575697985318170492828035019830194389105
38692244550460742561850889028413171428
47222257099712966795637150803383135530
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]