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
839972784955236839704457379316694289776
8903379889028996175023659283795159403516
831930961421825662494979693388134566354
61769589467381062982659103943522034857
195818019058307417312756888141555109035
7584368584219795041258798393697923147
751892881501515616783144891886754139074
21049766332863289267036714812129038654
515592482053172378034241592113526974854
721490245867937134664215767157817531749
88605288956788953508474821077650888476
88143524832520481441670431569718610000
6616612740931167589469264706467099446620
6430424589833382808729256277520224884605
861755965578751391006026946236268787030
32049487158360060753933745952077554048
66393726064027650123132990260247386707
444116862769626773396869788635933924315
2846338761551309579123663321895874284266
558043633738825523387462503141384044204
909298114591885349133213299197217824547
6367542471458865675040809582647853191715
162125578965805487177159692528651356827
1432424899366955627036016937590365622823
12206108635879162262463294128712917168
840953865719189361286892285587898304
3283525878911680469486596278893141835576
7012995344116791711277942681207117475048
43739647355278964225717602042235037608
77292545261527606631015865407562358433
362727940651204928660319960937874547176
88866886264144895842951441528727323455
884370326361084885966747324646899265001
3967968969246727843185663664987955674294
8689367157844791686604804617753907078
7497822741358080975529537955377822953016
6337264815367036367912768732407682543687
2477999851474391139437004369895719048
8581623190933561549210674572913179946
4469170759488015729584239991811672088033
9273820455815859823484521771864339017712
9928297236638737395917571632432148936
912936853394999610855781352899251689917
3513403527806499478284835912108447650
20448737400049513470405739057293306356
8503760636656209485616184201652580615218
8603627013621775571671231697983827354544
740864589466991311163576718210069043721
8653333978155525860791729685422641238370
264685634046224044561209219175459713347
563532979321678593443039801748476998397
298541973399566944712293300812223056816
144043928489989860716463794136029741455
363481781906697252221346363722273381702
612210489414525373625389511779692707702
104775823568367131873974620660287674993
35225042288022778723989763127062731555
56627151593645586401155452514955862314
880114957322781312616970624114545095680
111532392255196610186888712124832858174
2558825222707595001906859984268035683
819327061216333044384096761556411255809
4215427407760964545327170530871176661
661173703847242448686728729391866743
99447605876778661631339054518466085640
693090844063239157975279633750770893899
4208413847253011531097254653337421993218
74626562337316928457044177983339641400
305285125922311151319987177644854627462
325848615312140454337264699938653183764
504144775064224590123982595128785181351
7935888912996085323281183604906960171749
5494221834632585276999156460131125406794
9909846661833681491321394504407826934063
654431579525951646287183950395931327878
889870788042689127223830117320229999
4051896032679251280137421211347099834903
5157109731952596981454442837518037541312
297780857417683161609091271287815023736
5517820131143814133913336640967673352256
9470309752618019935791922091641793045353
21595035446817186701149271390820515408
870080164848979890412287024255323745400
2904528899332198522415933195759672682500
782961781127686646239019667254360708109
573450279991862507754572258623487928202
725967092805342994299594409620576834871
25204820694980980997039546596374501593
125513775418829184303052549114602851499
904671005936881782179907664752783857
9698813576963451162336814037689694581692
8851499711565645958568449420086061758
243178327382544279233837197135248316799
6679853686473772449783915513554329842504
6067317165213624318618695967849717213278
4920361989526731279488418849649276203307
18979111723667989160783883223684605263
2817491427632853769886628850462682823
174453718321397241519618482179355335
645015477913704551159899409420335815139
499170194204505262821059960546331167332
526113715798525245491582792980590605202
410266756508349960215276734497514257108
16969622373738618049702970493069807901
32206604124269967098179124640256419163
80303807500982972361744237414692796447
4117622268601347806052237723051332598
6879796709165820142525196620115724281
648761223844344055507803260297054234656
6341384784657744596937026775952658388921
6288569030799760181130093535135578546449
1411304318514054724327133297679840310
319211933696746396597287375395504923229
25356742766330089224734584779483918323
728947862051781786223941507672294403342
6332536452543593356467442544178126903961
7889653884236179727374194904541134213773
3654881768227949720752209783328336261775
2579697512389552524282599882430182259114
65432385281212206535417421436725538897
491906158275983140898795489330561939
347325429654201432753962249506047612142
41084437190738149909898381927155477463
851974707067811299949029805143051774323
3098738790829938315861046910553418093
60567407157723002946405721222533858712
9655441323118398668886864781511596766432
161581234177519676331967289990667638191
5932140024176945879364784993205918269041
7328139942306615936476031863286587727119
1719282996273793673691150996278616620
9880201643212729823968785334845545442528
540175081638366361367282503838290352527
37805927477879576334705286653272302391
553534369095270894169443999418157694726
16413568595352519576734391900386027358
728710131837346575490929099592147815985
6746333081424673715497805616505114665
1089538429684428109151997960736988045870
97891617334212681072681959111536622741
770977832399680990622241295622845177
22592858351413692151972634843216671507
436272889458782833571446465686497869400
654359926120945060055520877952538493483
468835338199785839913512370442087509868
2996684652252755286210447246604193355063
45858783377174452719591944429269391856
93739399212359357949579459676379822695
41738923902343896995094358632216604454
41503267452192818077107188599990066927
9366303353323263495570534478795283282644
16374153810732775596648844098082486936
1577271819025884147630913555349489863210
870460601672751811514430332992353256654
620266322404563297598221874487573067162
562260451327934524831975708778216280511
3863419972224019935376872803828833565203
1215280634398193100468428086217416034961
52938662847954505383742565215348111209
1616736342016862763253888648389756579670
9563611876934346952269908030448569552191
186784947298093199724606790613257716222
75066651731563081706982145793157546552
404458879800242513997862664532324952201
3589595324342657706985701448822727423154
864173632392735983455478430625843944154
48251081724272513219801902388837822411
223437379020415567108260282780213049760
25727441837288398004223224896264598590
236383497659979826834968950722389186813
4933331734592367803982587127334230472650
362776435852687513569848681046829926387
3442366753835449213445479581251352276328
7697780991377392384973904014184910889235
6358713494742763419332795565557859436699
55824287234710951071616740544748628488
8475568821861948542179317940515978287553
4929742264167754462725603420982765461894
917295704538562386808637555757458709454
3836839134875075349128287747176718457947
5865217854892955531569178633535010596800
59551630289077207319566686937714834100
730184708103825184324602129657245505048
24265428770686325628811572989987987902
9062238267566352598094477738331415771837
338696020056234162973393954717130516865
6309754989088123464175837348483512151724
2503874525888226675920377891597573475504
568968614779949223821442793978079311676
9548517814405705114550285430256630374222
519067818460910756259734437375991543199
4613676684563222507379607382150410519180
2325455380532794994631014499523961981706
7035805939614677797749215524164771055941
461349851424973133694102531163921825955
563090922587695454317517823190953092110
80397066537728156542534589781650891375
857170282387423244717508434270479818530
81894753847215017714410281588051891064
2823238686914757105234298689519431645
747593659488486692771228938677963519979
26223174456159677117886519531699195073
634439583205329512858459231209823999983
126455083676295389592729177288759798786
4500830615101341183795303584102494259639
2358790585677059641418293105610080113631
735568666488442626836003834572869467504
198554694775111111020417140660693502138
553098236265676854272646433981386106969
981795709268898675007045467548651635463
62029741890385437165733143612331125640
102328356312601510657500447390511379896
1017618445406289228919318966360123783249
34281796771239753921421828150368437406
2461745672148891824189448821251812556678
485060954238103785479810295029595403788
94427253295184151860957336175245557304
300556667967970069314985273190561306773
1975436796275033520097744166682928193416
7619477242956761374088564459101494504496
2993896371839519322455992323229381229720
63463857258630323843220223026330543919
57739542733918265181748406452947874311
390471328262343256388413574983795824808
41432015421844941809863404494733934471
774235201446406137633071444844962096018
925315169885619436633611689230443407518
61097913262383324168009882783584059310
89932414759804168731997735087493086
95855051162664334063849605626857776883
709083203540106584201373107490424192570
3176404091086620355410417287785484894480
4338977630007959738077066786239184285140
7991334452103832720624909106394180121021
2951762522941918489177439884137218333884
201113986044385378014598029308244841783
21439438997149467044460526654645569679
7005586745571927322943507631736488015167
139516844813226439933694243223699729
3322271571617925436943101575432029703222
57978933515127054876791375932895873877
1324989692922522303899051031753243214037
1011423655177990836718094304259987201950
4288437491004914541928226610306211341714
9126385374918327251422761901294190608176
874373861422149126666633252333077854632
18869675408665696475215177952612889577
3154151761011934696373812822884189293130
1850359395138368178332749909940663411453
3576967444282833534856279155512228225837
94901590595472752692390700476125209764
834083646654190726029681566928968714160
50794256683632845116331113853923545027
8564825665734602766358807939581110938763
14262106488560654249762183476409362090
6585223759125634395152147523558681594384
9664296448399487638119302109814487833105
8259774284634179262224808670757457639054
85085571837058499659336064785671979495
54693827623344236392449904593058729497
5449776693995199232731439021266392932966
7095977523218965860718756177313970743994
47634239360552858641004016400483249122
388438273729205400073888269872151216266
584900326912879334893491914334774885
38502579304845627137756472528015191343
257698678526497209975801858320834516807
851976023978562777258604893678677052524
559678019883756684427749737586614933255
9463947595107268715951568406528574221385
23878761795948737395735302638185706247
76649124183155290099698037709548622080
5041851795252576164639141187995432964
93203685767157546123480886145798406779
440160384617846079139731682839223471715
2897584528236784645486701305963498381856
3120274280758783899679192188105291089817
3881749941841577895586295964929819057329
6564439045441988145675153258206665843158
7951627734534541396834161733887471483440
9574997461589122956058584736987200937
6040270246185366421850581121521028784048
3408151664662277581111399752350243267992
638545657268827816312733896266907791091
353757678384613690531414329597493602808
9785634143479353245881583133140792763487
4591504595074161677038906553240698971634
723174487760390472708668406384171956491
649166657143997829689416218332194643561
6245638144743121339581649862603342674548
6050867616621975555683483626140382225299
625948634996493393742704790275645581481
497451796285977389651089913453684963114
8988873081199004718572875270485968818632
7286667471318408569821242214855767270
11287375043059678883273812865620728979
8585382063863028740743513682900460353945
371413904881354257075863296579579641987
7034398578083677468366847691730138528049
277994085679854192368878193452617864572
7055774325186745248271644036873129816
839990241642122701258637294586478423120
45924906321372034735301882816454531948
6030371511057845800200039668335783012
827565183907364275931102869607018134969
6970429624965724219123156813984220168878
600880595839133710209995616246137762287
384843264474515213691338491647737834
626682364682708825487457472454523986
795384422874577381865452455323517326449
275411568388394010749933793379073268532
238431805512146742222354719774522731742
445793556376645679255571346494203415
534965517573426982977774896280344524022
68209685729316625561617017570562873501
4314988496046529435667687261349983903796
9944864315328716444885895091436517193503
570398532673560119188334002258971286087
8079498887605036877830699724308785119048
684139567727684677763924609515598104825
12602318141824176935337195339906114341
5693196121631377561484044935900930212767
254783318551759561486191824923549701
4346296281742651495820772033934211507358
4743739820933285776318284398547263567894
3881402299623407731548693006644558268856
1679937837214430905647398940849512479379
9194442584353381975614065073215724899829
5869592943491644546659874871371376218234
50420244977641097995667829210023563501
2689235090217379184530291821398940273651
366487671514943556661839860026625395
3279954626366718271983753189126824518439
917423818898447444956449587479865938
692170151267801863284504553914952042106
856424533256891523867119405651105553
1654807479534467651622653104473533354731
42186389935023468999380281809030128033
986050184869249421320697407574682428858
110354627568931676884888639945022166881
2552828188597040452636956748344484485530
86638760389746496625684957357535772837
8438263561748009871455948975608017142138
327881620716104927724264822792059243635
693561191109633354752047475149139709826
475267261573942812949923931705255842673
659063768511708457343209988110246102723
1409564861719787416799254403664065911651
175582055040840158890768336227786266987
812071382962325736472577812550029433386
759864720573980354095515478305395797846
4477649554415915968364989233347621176572
92212936000795118436728842440741197087
3932102131479666778438297440957876441287
583529451935832554715312614189843415362
6042267598797794935188823159669792652
970745529672664811989697559849409404384
8351947343305891800940682172999393534743
393655725500733791044639484873756012983
3821752551893935757479149027817967062420
407624570962607173435616754784558491626
143323007003837660967507954393806271655
589634258799782783964093479197013513220
6574963469175058872074990370056131771
422783496629661444897781309642732996257
6856534440469765859951144955546179866680
50131893845896736901688263611574024
2280775640868226428042826521347640116863
800347028595207571821358870220173602940
993424221891126696122155675781842411948
9603223952892882592313642634758553126
4902977679692089646118998658432981668226
7043122144393942573157005067477859364912
5966542030321775985569284078642613171117
8693171574095068895984207876701834621994
842767957096576329321021157402729525900
8377488544604728918685676151934863424149
9744384965581156368134677108234192724000
6859443350822874195561533296818193349
49841877837899908025788683375467813262
6116904832819586272584321906979711495201
869144773088676073906791749919237926860
1930686021143391567956547839977541446875
6915852150548399748557568923852371328137
4856205679141984456755512098524936509
71463759571664049181137439787222352223
846082091357644426951594168932825202852
5222141880838757987391256818383593174034
181944956278332218198527518342319583762
9159549954192066426937296388969296427987
73944593621187079654415309256862738551
3852279495535934679195824839876476928925
68823415114291239075911570962048395125
29809239317448673171697818657798886
4373166444581157301270636732386552354
418368294196853776657408714595078691826
61129365131639513981531078977347808
776422278629720528892246791011339764865
9032348667692755229445358681623238035069
1729635384552575326041964235108430109016
81122121668069911568701753635196577852
465434786342278879322226498579422068144
74063157033809313146279239801343502122
493535941121460745040139197604841978334
45414651116750300937245022179751191218
970133185544357411864843340146855813763
1362888444037473339452425965950491962758
74021851629278321558379891765792615313
52672805365365570302837939836592391255
6019700982031378856719881127657784544823
27634619462722838237903302210154870593
29204550550691488243169476269064791331
9547161658776167388739362516375982352144
7245456845053194476473403316150368687543
54968255653157644811820385677262398358
237076271774145832801667111042731881233
43285381505487229854007250949435745301
433522434877369371359003279528277529218
540383964629205485615891243694251771269
1455181760266264273661822937205937217452
57645611233154424894725382112741338342
560385968049647750325757205725563729451
6359842150299300625675134562803196648342
1413995671322495781977863670636520564347
66784299252429719195961571387517349488
802277169371991553460396989151441963
12868976689707343656971172664422842150
26142147756174495174821653483283425646
7130237163293914983739149257138147206187
2915140275256183167772871650449220965922
612989566520484265666395936516147871691
205424431005497919228053134740507344
85068265442755915781936698913716737566
7747151767149012510878468096143670152291
49563601996509351337486722633418272510
13088872809402612684764905711128125694
62638452713580733866425694687097741475
590144299886552872189818510695391988
9301289162828990121030496618104214411497
3600792354381410922589083940238777508404
323934491786603809111083728594384164089
757455204334297943472367233387483625
57509919761024989205467054616571551629
5248387534274097757061783533128919928590
545681588643417955728833118801750314710
4567338643269616211393322734303091176761
687830619552234852632310993035317627193
915455464271273523939259763710435453914
438551953075491195461988292283070506072
3298372777008621176537045004885673153076
425156005658713926685941708589037719946
69498383435613190655802819485396137563
2730306060945097901827351731513139867767
294215281653910205347827562734072559353
6221368058956526457389475527498772152
37218986680967481372712314279679876141
343961639973417952732495205815816188377
463933314861161858879535559498379002080
939652909622308348001068328749020909400
836761468367690642981292856120689107221
9628460526589577209536824307562094645094
292710938342095384017802968409225058074
632210009287211620971864997878274557759
9636735201108146154367570989227925535
403235117185138716746763832617269874366
438562189929409836623635534993069651015
9683587690323872223238922618971250898098
930768317208286179025109677284934922272
2236593957373363253067740690078887254
46067085139262608482228494867235507003
216676547359440858775916295749339328236
4026149593515488464941698231338349350
2994926155562388165713585319403467129864
93811238408925880622996450885030469424
596734545220737355228117935366527337488
42865840953281414309604441747831303211
4973621374782276394429551715584020782786
2889922483182834184076465124841741479871
7995834705878431155682346344736509586
862334232060704945227626587540360925099
8353864295831706891598372732904917202913
273997414253152442632098766654338767397
77821939610915545593256750290103740445
1466260761793781992052298540548617243190
378470837597508571539306648519416793447
971558704934543199418902171955851726
7205176761442324793735153769199771885525
538468146896728822287213937979195332146
5750538978535716480814455531572051422954
431889684680561805493484552256696885849
91541612801580692910697378655164162620
73516947268975857716770990780417897045
824887179865782380612380660970668714565
719458639576452940356942664737052706911
604364463249600971778983774883744242385
9700189042371598149198669340671838474450
9202697931335063544385101770494176345290
9636495274547986309361753272970513674100
437634966929360752118622376177920492117
594976012061742231263397043818444152442
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]