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
7563391178924675328911755297843588616813
5833313337516938839540034563490451421737
6024658490582403767852414815367455393364
9889445779165493173569992101154366364
4481550868036555826589859133733334706864
286987896944383272637632645537349372320
575579316297491320419712349120936718471
646748811718630438911273246016287801106
873134621125165669860245904735737713
7595895243615217515179625695799396291352
5705540177432021165966239264438026958190
76669073837353556802170438737526545874
3379655658827194643515295205968545248612
21838604281743927051213791565494981846
6374280261445614609887654428161053887616
2301486765958543755052006342134387614031
5336423988731349528965731830830885772101
3240422777017128418466306493535560138650
8561791588524165101582157370117055372676
9987148659317484592355658921354446853595
95809871741734937858525303262053126976
548115042709232582410648862160415887259
5629101546434827124293496133377951920
2903479136075670690087386499186329603953
2280203743268300264451234531830748626
281528746899481148826947573333647982395
204098798473453168457949870470964533016
510303243396158558530975728884761133863
4811552726432069262376181256310769212868
6692911779964206854274674605019257670
25957347274398541323447864790467324
34512287148187554039397917094411148900
168367908615442696732127268192492714523
8512897365278150109281941490254645689082
1052468487881937964860051908174737684639
6054861125959657125389362586569011475855
882633417542599969179306406342877898628
628629492825436149898212811945311106189
7760536270785556252722401779228525317
9525951943826573309753748998371748117016
983647309785224069657791979358549516949
428096346709478921497673085350576305489
641371483087204447721611777195329283044
7059383698519997370088633278800494646385
47863136240726970818521403163234976286
6212230321549984418023802742939860112769
6579225770662382665153708577248513521287
846892499629335667450887763311675629334
775090689182500825932304518709592161722
967929397834355139065944633347096730457
5774580785825454963532421561225246681151
2246686498119849708447046303315924057102
67627626261785895107753744263425438141
3399874915858947712994271208905049754472
80841388944042864865432289923294587684
5698351387314145337389349885541541896691
1688994556877021834869094236469939555959
775994983342297547105262818481067517965
5087953863045630604280957199311631896580
9064565229813129317646183611737390882484
52456631900079392934612413241255547995
716785314806356748507904584873382816004
6061141429103517694379996064938880123291
957718789569309485503314447366511454757
4329578611758190844571288989460727849886
3006672149713730218533464543890818132756
631463444211680643157126777067434301859
68021301515140377525328441135856852127
430154983422461265375726593928018155232
32805771892721372117082975238931736693
4095939172323476842369542312063413643
3808659940437548535359449650833540107021
749432189723127945465993381590660725368
853831682173274753589685225766932499407
771472681425789953472611346645246749229
3473357467274577197422341875715781771
1063777454366380684737013517133811639566
3082802771821801690059982460190537583205
562702129582837335858712144165285064368
9132940758945881459044362650859564918113
522448261313158343614433144525192927128
470338435866331570969282659988989953199
454115424801933258544585155550048555638
752994101573942897668841908690125376796
375835237949177677693482544146496298887
313581287541759737022045626579972536831
586908764884926317115792723637752206959
929172523673337843851868366224488919907
835870833282747945616564579408732544304
60406192906735025991667993885139105310
872674249179124224292249869324441098329
550364849382392619965903670965471075331
1712393780382487429564619393221338869849
7062110493449940117583601665162712671504
81093414561119252643617513738870786194
23860513387786327923739977776451037523
82189975831567895671695557862787431050
68386607645832246907761335893774488490
8233523743665575911358563141119935394810
1661983681847396274170215766595513545720
209176146652550797972579378583124639707
380141623375654513264721153702144094824
6537607833403739689157544730862833141
7242216143176141777058963920817125142611
562564374853149433937330737502912998867
9799692813873933515049664615503783586893
236062123447754287081831519525923492749
193780725649833342158629586525854459942
741842604958165086802051208581371914243
237933452753289375534873156824717483711
271853696105540124936919471920391559440
8614689522982156981117955384496691241962
2864930551434681533865343147292847933087
8589602724102941949964261194771367668
6460948741581263253421542929437346048296
26681334949497868344661319619581218400
272284476783168282626990887637612075152
34785998466451253089362172629049874490
8908701855865788419637073221450616116966
6257599022235657940521427104158220524939
314263278910599430175538894486768765948
18126989956750616992006045695247976424
240067537410856714526654865535916742770
919398194075563524250196104801040569649
21311108124231856036763718639693327573
3725928628541829432729652236146830665711
195849127398886524855725599424396093838
15135009706560274457829619243519267702
970020158641720134081462882978685479875
3026852615115388169966673323480228146403
707360816716970490547811835356396267311
8462931127899748966994834062888350817461
53439943627960627316347892172652056654
2273972980418493867646046548843635149100
680549535469206666108377516995363015726
870136426862854439472751875185817082698
40448330648139759845837682658310277159
3041854319965962731870924120242438052355
359716619979409751186409244536707432
8499854392563802469463372068283994623456
5801324014321377201598239435450190164824
4715690415894028634267501598390979805305
4333610697234532125251605210176713972
422271259016536185059093475037281208407
968474623992287390634779471596964748072
4257370296504598930599603794474685793697
4371569173546000435589748316111443045956
59318751182677714869106792402330986862
6148342169637456562680497326230592072327
639520765732918768546398368850921834240
17585937637308382974240820166842333838
390968351229768937325259731865227903193
77914152890309543361676421711548003865
5256586922724543280680039046462191182976
1360388262426632457839278598434075841430
8676926440637936282976613066353096568056
4047734984629108323916028719372317741112
4741380043599242276343102328543658306375
816488312761360025277275705738077256386
25542455759393645748823445913460518
17079943311932673007892023209701953864
873135135586316173715654950727242205
5102150239233400545775299790105723446897
546391034641171422879003731659640354596
183082358936471102566712792204486472303
5139487832996273105016854421685572304685
5565311256284247200975353791193787829
95698091425543992021581920761133386264
6039188289166155481979406138351921988366
255060744068229523289716700648631475990
7694517263716017155251394447682015422333
9733138888544502582690322115865885039284
370853727184354492130601545851910266882
6181906747283760657765426025831868093821
4161449158625945650118267410720271352409
71851170876602614325692205560271922472
6437826959621167353430466272392274902188
1807380123509518395762779491225975617928
652662785381557532469727379177580294375
683415963907005764789332892940321676329
51889377546427514108984507700710014261
403142198901917100765268529318126643289
61505733491006943835633401458189443941
96552657268353788091409112971294439782
2850893784327674202885061534138499466045
4992622362614506279898296522610011439965
76332467389853977288417256810326735389
90136046557347887032389359844252536234
319041092425105361581032262124316349280
394731782009330835480072885820874883676
815492598016935226466119342781413857691
7419214898644792565952111934944050755350
6639439526082302364563145901469483408164
405998094201894952826455702915399899181
4085265433499833608610725841781442615216
661322891462070444365535324789714524395
28296066561773755629544045912437441951
680725169113484255231590550974721056428
762712159543236413792205592991764801560
894192282953512353528297967231054949481
40782053117524110161857093590612333634
86411813676086393231326004978430450
553844262217318483312452037957325596557
9772343641286907846846997728194901653
283367995799551360429965301852693825108
56296120855747041359809750047998579930
9479236366938540218762075719323336191186
189394333696962977437815353491890978458
433815330318398457679611113389336256419
592732836882705760117659370698793222
7026417244757581492015333888496319274847
4090153630618660723480168121911263349630
17743086876161981246956036369887412
2527660630854476224313666149461939932086
9550288086886372182817767165421320623378
550090976176505018031157054520168874939
69359357648452624115922891142778312971
6763898764521472881822709663566119392810
1573383776714572616446172149518574761458
8346377566838242890643613294891561074638
9224298525492189391932459961240129155887
8176117813463488228819597744734573771681
724374412988878394252945218789653648057
8870814376366667254985031198295474959222
2424808736409578558320684272756281629527
919195891122308298695799675979406407893
8132977219673890638127296032834712243286
51796903950795339784866874169792955627
164479005145488275295811044550051472412
241553549147539147657416789262893587
749393677152578771445935463674999996545
142410634333128431696329321461988487034
76723700100003914764574269295219462407976
3977591177752130817717016223480974236409
324555577205538877242480673755719798812
141696296524957873848670711774075865427
727036585752343689313213131748624925539
6260110213415031970263055660772710123545
442764753551512560856895954736791156966
4845634641391768189524486892234291858013
935219109969727911337337996389189095263
344843472269677853418814137754855685951
66395333004561929346199886987845864325
8976178073654119950577931818669654921755
40106661997216087708725700533466443123
9672474110787228665235195443113028146107
13882046975618010698013483976378572556
792773946337716237402642657282316554903
8810990424144355222745525017806477427900
58865073346479036882583023747019355497
750360363701230435137638163598166369111
522989785353463694947167117974927344103
533793028896363820026293797136032772011
27243698274342404763305318797429101901
2592734394113110904821472134266026193542
29055758432459453057092282801151573912
7223755397601548881751084840654348752109
1338656050257542896388974018729856061677
760345303920648730651251581266926901186
7236866940931722309998933141541968303263
672965027900197366432117879630535407195
585881584139180699291270431636173379147
3409333588789278715749463147142277537074
6745625740597165348779593295247249767139
316397364498546063879873982627532225315
2142550418297152863461142454274632717845
98042776638776448061931878742186909277
3036562594501965836371588496912437778701
169124674365544399754859854918194704346
2979928945418327522884967101674959273754
9058156198488707903242455909458399068430
7704637607162581654378672292430636047
732933723818867929641448304744142755631
42317843036096055852482646923710
699486075525653630194614548657331552986
30097859245354743992635404687636212825
6341341175664724239754161694237884406669
2584272737894561553531623065185016173398
153384614879781769204931311898065924
176419091043989185262396674056002243
19937200227061517986806113212236525392
2201969567063581178993980995908455523
73636417129274219240572153821036309515
906138682444834492888315815219652722521
59199068843536988069827267789647357032
5396225565254331185778818478215786418469
9310410781785038408077154378541658135
847826437407418629514573390229460491511
1805281352537495968235126852387105605
886921922481571250372951313401873423795
3683924937437596433456348136440635994726
8405521392569883251175153728154922438336
73286166112480332741419036978275612933
373815676389846929695155195768038817877
616366236771249335137193825283282201228
312379532261984455493372928152846168511
850186521235525151977579663050333679522
21911923184480046165791889205238847507
803443614804658625794562202278422688227
7224921124129446604067191648905221473398
6224305249815490852996948721777997873895
881573476749676594088844407785335628735
4702655858247695684658311216400741882545
371926967161901712686116173601191751930
233228907254817530431213810769039795057
339911163099391247233669459685565805691
975829231661613538663055037768418855319
9043192484381083476415368859580882761226
59043374945827179212071374329576466819
9189104887125123892415512739347615126260
618939729902351584231986131544869045593
449712212209984152513577583114479909219
1590647490657575585295838628197747181760
861176358744698238774826824237799302352
142865725610876840132183103366866115035
54722177619330310893651586314866096389
310947251341113669725275217852658121912
23833436774592625778699775920421901640
374264027678731653124076908627643033
649695964578478310558719703754523717357
6741768783677729451973211436473759700
119658393100537348798644789530653112691
615839128173279462519948900265150829786
9323969485498892453870762209685922539487
996984803232679364958285495034501722696
3353419092253497736481165665897365993
917810474994231304216099062382211967
963650144090570170103178562028686285065
5482810851413198427357301966185732111187
52851777881815058699737202392285822987
7433154617674680644852792688588942338411
874484418926675294071422354606679127387
6108795329742694380478768266103172505616
3333598856419096786557263828653891897
213612116278358964713393659904990288886
3069999637096321785973247369018177815
152257619949471342578430306430823338910
1822125523181178531785119415413413456
863028709455984246753995997852969316163
197898932515107379651599823584928194220
2526739460691043754448615071390358287386
8982986412796143861459799213765165171691
466248666322744735238553010977968547583
1269803039882893372031924833521715362464
770830974166639633242554166435817958873
913872534121810798639432799434442368425
551616838808326856008920410522092966754
693875403334575682784809423112269027074
222568582870544961951646141885932666877
44919795747658697618788420334529055316
3295694767296920705659766785403084422018
3456674450661940669364807584642954958200
889564054594462297606442083611224569901
550117855454211046864435363578425934248
4293133069585672882291637025264151086566
176062091605602773066063681359229637319
7804137730736870692133801166302291636122
8667481535631098488612988520805396755812
66202030113010934311717626754639659635
926465185159284618920599778853437304166
40069049570140150485253405849560797569
906454727658726416949460064028526605
1481522878699551961894344302998241681695
7433144623758385856171663579766284826600
1183708652329864754594616311800166308600
1784472240639255205830226458266081217950
844493989620159519969893231481435678126
131759014901645371948795116285567374968
1571217549732661767977941953885985233
71036008573122973299428702220073517786
6748910629594322422796497369680385379453
620615913387793943128470193992703092788
43812141984670444521030189981383593257
2443193454723456139632731552889928074611
504488741796440856215718567349839202351
741985633583232722376199410650486367145
2785859392293109453126389363552674853262
102410886666326747433458369493650884487
9246829838497422886564578635520759861199
580859560997006591420526241327541028581
87676295835458584203691409627328578301
4125891887415577539389307465565518266190
8803402087138267845558454655100680868959
903926354356808293591238908826298602308
6664246198376438936292429007244085072451
80708471293222036779468767232928518324
5254370290681601256692538789661233568996
3847550992267663794241264577350654602208
8460375837116551778683669694814913442
4554381041593247181112512088285313278024
412976411971617143998457912396129431753
1010254955031956253166342940672088758066
393931972261754765052524580632677955342
9348963567019255176286723260785119426135
6162350842141834939373282946178737096985
8615680699742853141264687848213882276137
1701324595122629835822723324752179733995
718534516141534229689942520787434673647
426893246902778342587922455124338239101
516949332849225148176577315536264672621
3535604362534845727737245963947532365
7093629493705869939041953458948264916315
61728272667873159088280658314022405
95412852983545472521264196777120215469
8320497066138891548055238271677178985293
838476938829911471283149093334875221297
53987839495753162222738034977479081504
1946735720679747825313112827350781624137
1069937636171377478844712940501267165786
534641426675340535215542790690999666641
75632918112886245276579939859888125634
2396865568598746650777214663605664039898
5634985583291540620513935238320758433891
7300242361257816934728578849818530726781
1582562117377557957584225909391288686381
319295696384983150636245341557224483467
7286400159143153966514079935441262017754
852310381465498583234209734166818766312
54895002735513751189914411071041892
9549560076507797868224985339575236153298
7735132969766004850290787218466657503
5051709253291153712639616230806115687594
690543348766035275197515331218663798146
4753113823343733522482387436236496903604
13075886085106189896748760793855309455
7396471266286231189310137229459092505272
204033769589453225354461645211494988144
734927573227791090575785105541562095561
7730348945031477907988007796204117462721
869624535311025126191931216460343992798
27795439743743196195262771925129982232
114914746731982690748528535842317413398
124227949191344823177169274577045083551
7236582782606861479879655992105958564
472911493471754711183993092526818879869
4447878959025737911087708160840215552885
1711415554097344221893455950757871504503
1342514266534451432814315654662399838195
9458515456534663807664118264656948638
822870153108192109524601032437075587937
976391574138086483385888648858337482517
859095218066920087436656782223928835204
53797194980844342699717180561422738448
501614574016715672025241233440836655514
735590899431551819138587814602379519296
591869006300115578553346204723785756832
3290167856193361359089415611667239169448
4577945098817581133793134047569930377243
653747643014864121653457226853688313459
582731482213935031552694692249754705
245461822772545126082209893118914598035
6948303480456788904684644436413192928643
739166395859630231212805565357770999904
205664354841367612769765153533938101171
3349716347377585263672423035705493208726
991887154371215968095223539565044704770
1320572674284963589782159200217123657364
6134693564059107743323979196108318383853
2692502160453747315030791962135896561621
480188571374690626072870120467534648033
358466105433301070375410589392687243
209395845177340259791641172843767025611
3492225484739476477684241435975889172
86952230720820411514285594875741054511
2767314987349311403641740105224484981
6909959570748102170356051704260213347742
44164584398986749987846447402043406629
21495024477399872837007335601633687091
94298022637034829404754715858268532074
184918974881592087607044411544652988096
620664778898660934893436020513024607549
6982659895736444591454302109691519843531
6346324597153841562920183178801629013543
129612195688968898389864923495454376042
97856321750062425924468782360779755157
5059681489012230352809410939028289277
1926628024440236949268550649563942995
579458397701555862426603668392041475427
420110077506425975555592703270227206488
2777391342233832686823479514279192242340
48836832197695144569327906944831961460
736993143792512322896918553978052316289
7053291235893072535889719970745462232454
649778990519927276934595136987665699081
5313725890343507493088567466397080754817
94009328336738769279961952858071314205
49593043951348473099896357883746109339
7365118855431902119168115446137156331140
483268983677310181238221274127773266119
537545035994883110829388743359208751
9437809468519394579789072535182744018
740990451930119271062109451623998351918
3687661475955929657847929800418979094
985948725963134036122270958017242606738
815736693727827628577357863764732611994
58853741920635733373242489281965217205
632319105342668994332195061215080742156
9497819752521192588242327646399665618731
8993354285469695147677693305644943943
32671865634092168258285976268960796284
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]