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
69358038870998587952783541561427326439
434239092232170313141577562358830916149
4034195411027558661411963058164437851881
5953522122505877734610357318543458615627
7295562419374155370037551706571573736592
67322853149778795392054381325831281863
63716269560179893961985156879449953872
972077476924250223629785742513657819
372584928205778366342507414472894616947
468989125415381570347413984545338353931
6888121859679941429495123442777866664247
482155647379938886014403449519152547
848623313127178841386015445909341846674
907351368848664741390945924646785492996
93379161641323034378625913504790733101
2095740137545817744257438839247131926745
32399105808890357862638492434371928957
253150387072405306533382389259175432320
887214070477698570123904250997076726558
3389569176187633854210273931751156895131
7354462118997335253885797159899387062848
7496211230614537307774025367461762218572
813615772731809894642661187245743845928
7587805199809793482914941399733831837279
37364649811934348438736376175981538186
1668838335086605233753978471795450444594
421218014382104610916167018786313102202
450347628961186026519841162586283188523
167072293542827561542002259850176064069
9673920510503528849721609275442022147313
8924423491161807270985725126200355524328
8898513444691406696029413688123716852023
9879488575094354271756063326246442753114
3713697323859563276184185187643141088348
735643525130315679514264671733007569336
600544864736485370940818687427217336225
777284586734654078717078475515485778690
23972133041557773669566912236363344746
3511715574257669162336639101781817582315
7535901331866667776837293944242188181434
4403129581618266796699758371597435193743
7853336216977651486915601497266633833037
4815460046619991662783201353463217043689
680799545522925840654738926821475721036
7005564618264446364674494775939262026
7403877670916881495620172573201049863037
9367628010653412921834477950219735373829
576744057320139235377214126296558643232
2342171483406257636267696589655456152158
502257203572963968538303675097893874112
56375148652262753695693203258257803915
59107532939178358970110051344258591152
1487847297164523562097502691474619733
3412268569035654391540359250720328406006
4312249274752117951051848263212346151992
303065922358192578948530840563329779632
435137121146376896133271149698422786326
5357121748484268268284414664228376177749
6497143786488102943745128022520986139057
8440571658591572480836322500705220905954
6266767249152361731249226320308785904831
10139448604021581262645411825223802446
5206758827477228840318601303757453777094
600044324660711719976638290199616469595
92867043774581328109328911911019702211
35797341658148616034883366388577631101
319765615015735536148530923371812588472
1015168732324727953341897463270754403
404610278538923224815399997553745720926
4526521082642939978947063419533476261836
4846837771430893245467370497943381922
729291054347120832872813313925667517025
980961267472513980427432818105985883810
565669097014543267862599408982149218027
3442724339217273348234184870827432076481
441230553949700956575233830456226203004
5725812146565101580233616484835094156321
40245907791119606232223922127499531039
6612864687516457194293566915719922849731
86287957497829089579751433266071864989
587528140746245635483459997924533864801
288034336566909328644288814836127194271
31477712128435012333054728728891054979
60589496863096584881386461949484144933
7612953547844679415437128413700692183331
7560367034711499523477158962843139927715
8384370855728492787150696054768360067936
171953739003456030753117983594019924299
511341873733055963214980660989529953
8609625453907883834549147795975144741970
271759499464522983697103396035195692381
556240904765627599666048528948755905158
8939205855329150632197547516913622578456
5031844596924040492698093383933283902841
5844301795329063607749395430117960722341
658512134308716529677228969884178459790
8298567316711453522659255437857826308952
7781202027149943937775947966707271433335
688613737060134126126215719718156801949
62013635827694081835968819711598876826
47162608453047969385931484664870249348
227079155687974811698452351491285554577
41343437916574997858882954759205994800
490663187501732722358144284185578715264
885113892508275154003334817694153152551
8831367366425660957428947840411159892
8300144239724970237119581724666327919486
48247832550217574772429821512329177356
495561055096255410488111228183141986108
8239921094526658747479548913692082235
58487607312369365216100642969862373219
13813915160314773821446807676481509880
75566494841322579058920648364721479701
1543292651723834104436592534883285019349
71394683700923871513231819012699683869
86784832879211336309443338109639373304
141375114197643692148830579193217429496
355894648227159530929169026126765526098
8135881162171010810575774691451059839238
1853574084368586781662757493992578409201
5103703442343678833953546891487623905529
8689809677786101978130559558285858389124
139691764983764184915818743381875426593
96112038209633872845999670164876066827
6633692615572747361853356717521255193825
3790447919761453820921024031495547002844
1529750717253105986817099048229952494920
426298244362229512555342136400066847545
1338532267415965313139856537525591194
3310896557486108937647325305758316438259
3384524534547076750728639115398577247319
43579036289382539350431093268259222173
7493906786538343645597578827533227747747
76205808628702110673247008569435125726
67968662677577135349516425822518884181
5732198594099358449651715684313236983561
790242792038160112494938099294865135038
49822173585185989211122359592376848300
9816659733995795570115912585403463326753
435557827257209522426459269943187902924
376295283254130530087849477556080496487
9941769935822115123515768190527027374115
8071324637338439362346755889973123307901
52721579186993090259816510980262109784
34581284966594337106670865487425488349
89448943527104097892761249369812311855
30267288469175648509448616017514463766
237778608343876725384599332669741073500
9422546438494389157475692108839521314051
526417535754672227948825145565958865381
86132839814335999069164582993474669140
95412660255648792986334948487881096091
543386555173493361617212815255230687480
5139825826258055813196798537552062211
516536588507002837959617839755868997
7996297991946477608184423833202560774612
9081333980683035377119558013478232638138
5678373888171677696892876487509285857921
1386783309300066866292859285962579107
5792727925797927888238638711663910865061
694095797406890526710968688982185032389
156021860147542124930102858064985164
682498191229333432650751564485836929769
837854973585219979754254220332516393669
43389140881619814251943479416364258624
348135463557374574321135827799519055756
7731231087606787985773197730336922793856
9009489690362157433486260839543026220
9967290759378523817634987885942911833640
26563361100692822718724385768387068973
765238965618800821573618861462506179178
487885226552480443094263144360586618932
81946207806388734463264274950389756966
240383066375811511543754355919419541529
998015024090896182899805118481502128198
21401039881744749062873845871952851898
895435429784586892999133967555697185622
3750543967823455509035866958338168229905
140805585717476289457409076579196395377
6211482476602422648745278192255942235218
6819675455201210299829848299689148344095
191666855396634084976794693626761554267
3609671236171380484794710466386461134
3217743757095079898425049452908896408623
634010466436513585105190843479425613570
32078402195774395166375723546581646
39265083056969861527279137635385153
5978644085131482547483368319372143252430
2579146291788464767223318261445912987358
41415475748260493225412748686986908835
411138257334894786706733340958733435948
34889506211170568863293595525869030538
332316205034741166112653245183695268060
151646528178634520147729610942211331022
6706753599308118984793728391384754799
426125126215767423482024573999678089948
8196623434141359032475490983144637656
7037583120975050742532295375711011025645
125298613016160428866249964206899686681
237103452406212566166155437343299528582
688793342062202866007154310943463394580
39365224166233429136864756363502672800
40165795705274925896602330368625705026
7866333177518349869074056462251284025665
7856802073729534117536818700578748236227
862051765774148835137250182611025071045
3526114822771291974797922473329651943302
1747385771524637787590522140753075201317
7312857534698366575975471491185991266921
14952089966053519705841783123447018275
3526161257692496230746605686896124082620
6781882658183297972824136157462480954240
569653888801886683594295592306534165463
163048409698207667689894355290049767050
44898423683267835714888611289443387410
4418414770732433561161385657628113804727
296793037059441877885244611669111618121
8961965112434741164123758325463492338
2170388760855370730649511258520061894861
694956461984701262767541493318121871848
7635976037809546766699186814912137859088
725959523047147484011810793151015671777
745816406388579188518656671354815525610
62909325878390471713109957983884556856
7520199586438215570721608234703910293431
3807687140186039733430562832134960875651
9810758313309041860975971997258393626078
563451905075104795519464121246328896310
8975499666491649523897771529152725668071
672286965394397559096663240614911172638
8359290214641339852960655821139563609096
623148507739183012558802358929512462583
616190673889708856725393665252891368
7382559789479067940119519885677666669286
8699574578079803556845443571521195539531
7438462446886065580600916049647440592
74193095670585732391359527814627361567
945543496549929859235984282337328334607
4478925961357461446099638689891016168544
392147020004108703022075364154916191373
4608412530988702879827239398473821827481
8070747972912486243090783599615492593766
888487369280536773095870528154371051949
816896178098668106622004317885222355522
571985799736458917267994222451413042756
322487816532468218355236602198878498110
121344397436210527929483409597085521039
3073422780729966659326808760852920629313
34068818631535463266557199200040584555
1501890552413456346072932085338653563
6152452469036652710778438500533047307331
1484284775246942961642183963727184161200
116211857021375196059151390892578343762
2410563015013047391613858380148783867
8192828145528427850575568740354525091879
667771266564983993822962280267031312334
4958741467164846188228506005946880434120
465811774168560613445494716555565013139
8433979684514925693527656821393539041282
810845015219393143801133383256268833472
558442758581246392544993544961790248205
6050754430678528911113218875321580607014
9938975549128947850845247972688916945745
95192911307722294389782893540321559846
776147694686775973349190647181782119452
2251329145024455970049724872669896714618
48198801516182457144354910477757757430
424862338381462296013792977389136084746
68781644522458329584334309721140369038
52423667965181347619031120247884595783
66172463675324121813827419307864531902
967733723329923277496985135437097309472
996485761652467719432517355374516826128
2930504864239614769228462809612879915010
96135795320391125073511393298130726294
262465472908890350887109048224980336471
713174483313454907072701157937439329953
1880960522104799755070245415943628702984
89485041254246467148677641340296336746
605784139661829991276281633754791796498
7080843799139979520290455775659766173251
4820652142446383574921787631587386854802
6577956964749974543187544052140241257842
1948605069773480619829362219658682441537
8509567552338073584710682961725774551
6317258270026675593161973411884084331617
7841970229883455631712056215706095358882
4154444479977855510932816643163311003184
262688076651342747562703652224071489615
94039059906426448363147507540781976605
78472835111401370797662646441792584924
6718705857686019425752350325956101227
9931446734165744154425852306703277624904
70167443146821874389030802278477269633
5452142230716077266190532463545744795543
216435825021165777044228395674764926076
412414547550544253672972689591072126
1596747864915824563116846180338796412132
3173605268877305670911165843660277924651
217148565824309964883038172916846504900
1065702992026926532764788633451683748042
22781940292765795972981179852505889921
9952624580545227419993688408711098344640
957393045013956938128948739925299042599
19925332277987953506246606870069881587
2793773873699082667895102079131266375786
819391222795237990063679650656279651228
7121257535426870643963877557537191414965
201729941425764500545479695906328769412
9141314034178058797356926864442314319
845221897868995620039213212763685605456
7707994360952537924341542193719046930
4014740494115811919470909520250197134260
738518255428386868304809362943858985741
7002320416905375842492867218828971839
1081397221777219708853158749826648878026
5211225946632061773617275341419379352
60588635277262599737942441897312466325
703193134385011297910637072751646719415
598973443449784329297988512275784243
98684177799291351191148474593745558259
985813696683823588971009305416140289596
66715779642846764669469245717547056443
19716053979519921586377997082749445317
4255452151867742208626234215835521913
328350042579717104133606252373791341150
5350327661071957564666998279314494741925
280246179107439813070874959186868516057
11668932130344017235111332480078103815
4759611590338675217967386246269191979806
770812697674958399476859484695432334863
33483582254110849191973960938427393674
2499394264221785330536829711137040777260
35182582511280879527998457945736966782
1220662995527035394035116220636185727012
798219932706695870363341387644981671800
7676122390075163234412524201465530889
928452777877317686078911142278521158048
1664847502063608541528672804705469147
515049253015604622861876718776521697313
2050386734952629820197634965343071997288
4595701810489280203133588114513091886347
457120985801245386036539684082089247361
4729979779326355539062617474824817892
87689562982888727946586259735443752953
30684936867550248051674799835077724088
708531230739060266662788584528457139259
3086314233818090259986965424632329288549
60789829933830812683242650358696921803
934363154228866412162103632505260803135
520640956645593222017399542324017485850
319519930469283666656499389104212182
397117564204782191877932401389147076730
200199078847523762052453141323634025730
916758914494336495137313239170751163469
7466337023478621834650195392965087976434
150381242260326860761401678279947393565
79355392981204521443857897776941708886
531211547878888412406739489403538142873
71439160275379150989043776626564859107
744912879351477858245454257734084164534
7189654612263345216059796143136545264458
164451528625479669984413847666429482808
8379007792708847816827482843133054011
322628536860346859006502583727647643714
2117942494207150629816076034408642675406
8981688236654365835361201653821438062518
8185711306651014691454766268874735736
28644204568240940875935276290765148378
2911583326632273408916314478267669102294
76584481152643376655986839669176603564
4542784954798133909308391472934393021
9496738889603147746073646306350268704918
4965465443889144122936905496924993782
42433412172664168524013664169274336164
624418132278871296071513188145550079698
6396443353426352272089381271524559289056
5612380815653977114028557483831391454038
5757711339001021474559151684399779789842
63631665533594946234444456267448415698
921052663515153521934367918898412088743
6906745337744194766094835228626481116823
35336485428247742746226163725067756652
605518936834949159840777445975742016799
169178041942330076625312409152727757414
8928769562574535372887607371621926852224
858063187972570598999641083792323234525
4065611588384196738478821207268225903666
8019723427697592781369312244386424425536
1067470864290657908899400231872054975
483779198027852640968316962159954083387
5195552469766331345168344479230455475077
79031620585367471794273896315247015384
44051679482875313473386216480012875649
3880575157182788909515501688455625226051
799838553774521119756441278996668054799
143251926368819167555396488112984819337
706312423993541949560365979530866632868
964130114417691481684532470820582312376
4549653166943082764050636179848027448649
815539893574411830027596914671055353443
385485742272118592449942979974206262207
615751346315488133535481232754995927939
5619663434497980139375838247645743974052
103346673167630780828957846111230315014
512369811169482184634057396526540813602
6130988661226346289211165035511078602673
763535156795967752368909567732567734397
469656879418044940647477708628798504112
93333861863883315728522798968192265214
2788231874603323831453714858418265056247
81313794432912412888736649528388162417
570423046266087534983914436672266452681
818594855287758043555448128957242706505
938340325001932341177047253244774109269
3325655378571297784132149930288299412825
2921179827604257173098036654542324126672
5626441219642189394754582286382076107981
64494297566122754370558116307572821563
156346598653888026244853224606692506302
911986391450102712913801333217481364177
669123694961340064103248578399882437669
8385643613189832805175048005835166917886
591254579771640288714396445815987695659
80259253351860982774220539456824481294
212718646167148165066389843571173804800
20595480608560010981483438016863998916
2138876076214843237127528657326886772596
461963268596113489315065165356361888447
2984472054673243197870629336968931416985
72041349326961114010149960547098885423
570787168909085488711218660432456747465
8824250763434780376325191822624335133238
7393621435248387234593407725164366144877
2930308188653748370130313867120962542762
8445194439653663429232011787308939903393
94681991364130421058248989876534127303
1529105546053741243745479520897866863017
5502254827451865330730144599617922119
21485734051740156102825842045261096435
6180792066451617266459657565975734609350
533254289175292420810495911860170123820
36082591584822013582619902414809271563
32904713658962652833712234461011919037
65561615006566618356935924873945612002
52708023846831364396269558652159892722
3221879159572446322124608064776680249155
708950123932862127491649745139758646138
8615471611628061783816494084787045295360
2473599457374987682432255845588140275202
35079771577570153247228080456225525828
61154968746293764514992211430286184564
2799523091082289184018964558542766104793
5606131934590392611797766433685358979
9818520089488658115340621926618570148281
7106219053769553969157706654478459133658
985083714023247642206039732947568932580
3463842562119290769050451147661032543706
2426168047707283165411838973534788899067
9881281535486658334115041703563094016337
7645391751783926316946766485815565101883
823051225357330731114948245822218523195
31374414417297413358238726237261022797
859597208832561439135458131689259405781
704724699765377261187044455712551920
71122259242257677085853263561702894718
2329393315505097261788738218403175868830
358256743131103360654307511777970988979
3085517665893329908356050312413907823
7275761410278415958019873429330873039722
5791261797474473338252596438887126925023
72635207919624811094216707527673247991
415391645565762312799908148308212313144
728325561622406937475769673395588703883
3583228997426942430157049667112430576159
242183081558785618895017022872799194699
477633487258986283566545778230955417422
1842864878666164880535532483649829096180
859566694861857868409115146396742161524
6902578592107893324916147493335628219202
202060641406530630565599796712809823
973646947876321772661192828236199685870
218990972033585056049676159827410732468
4364110799088632883595627222113749731602
15347029794215548782887436243063307665
75793714817460149179031520476048175968
106428525835150128417368209082437229512
302483891417621128328212180523834754042
804795630466869830374291625356695651163
930228848623988742228505456699765615332
55698520325785322816579815423673444842
67358446464787827516868842658423295997
401130635070700750127306157596419568759
323265045154474917955522746898372258
572446621303797848894466625935496207513
9881854094953055743352661184876689878690
526732760393664301755222561198037743575
84581948013479768906247206493914446384
2311239470037381674543818282227673532620
183850639253407239652435358614648504075
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]