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
890892566771423369698477461919520591439
2036678624707065247224957626982754532516
2242493225068661584940624837443973597772
809579666634854210866687951116995392717
956901194324875791618459040601192957633
381961357562709465829162178628629358220
3304860183785002164566345946524347285219
8170598576540865326298472178444049768
56133391085408916227858861336187982720
572664051054794240251123910807549037064
5862899897158288366567424561649406967
3854604666804379320534509264310187893620
4289812954374772717978773691614878116
5211162361714266116328161349207924611691
9161935019733827114856592338232751808242
55416544912361505473859898928021145185
8414995087853820925739444638736765514530
2643899501754145680516690752747141656
147828892795923307897309375169378984577
1947665494863751642419567789663341142648
78819797905191202628168385323629296306
5107982992323219104678336997311328474272
497767193752396820097973043406982106740
1431205435909612794481078407124533638510
59609292558677199146921141439441575245
60826807881345784459919827512371879337
3068606724171575694864606565576735850
98901737677134398316369507546254134265
150616932605520426599754750850821528669
876217204952806369883374135615927257
234271773472887002195189357678954658
6830517134898150533418807669671856911297
976090208153324363742293651250490635846
468062067636630225950808325722921175021
532846638038630141194285201799979592054
798319689318915049508367796359121328479
9137410736778387246131562817849568643135
43876613427429839361187246567063013217
540828466376591304736716407927870063629
677999581545845061254685387919572609945
9948332946624879531024187009554580095367
176792101863968469915400830567825704228
4623640725369059966015626535976383874654
873033564216783485066098336695594847544
277816683134121114463145019180174631363
541911127723376395314678186596218794187
52534927954926101723679915579926211150
69435694339369634847545439221371401163
5380170363762541898375238762757174997
4903167742534862347289497111769552004018
18775655275210822721496271114280245305
786520185309434284712682452628645488470
7298109259514998045454796451627756524
3729603657871883320025325863345288349546
87718429940143374094161516723976585999
6584476526536209625871306023711035151118
200594668484100891382027244864131337621
5669235827844823944042237891384545554408
544558324873464536357345537887495998927
3561429897734215149290196064417552915575
99921664170675423161646676487941609665
241771199788784730191012120564519331170
8098275166103914593359549352369159487029
233785211546327382944111780949178681981
2489463763374922682522157124112386252791
610386516918337195847834600231183939577
682810347836868239217414155368765421794
928415554354855286634921600209475558816
207888149653354200689702748841415596096
80282118189830004989891293881515352058
60123375515207390652449402919563218140
10617490772873169806366726364139833140
9948607558124685851817223584975547893508
165151012757429659728642926764611374043
22357602119673969653103736653172267194
8722219013606060407488899950608015304668
1857564110101546104094185038480814012354
493681667631305143523255627622495282592
3314518214213742383764083074844214569677
234398726598080327533972809192497352103
161376572781228431834729295317577873109
8630515919537259988272912715923014769
333512198837595613703713654092554434
343564215158251025519310542241484050233
841538235963951429485257449962467498416
278137146522259324930424762770067289668
429529287953278932867018744357482868527
3297578355203817347046518870936897691154
8199794042174134421774509554758648459994
6266265170071245193851291583725566104481
383692165888594832161291447263334444712
63229278588441841363521915411117671225
1978836578235401314458071354515045708930
9484537943563200323844975704688741133423
539667393352730482437565693880747679271
3182342590102831673770088734905544365470
486329767989943240282578080558010615185
67295884756351107694204370460356121574
952924075269649223617076160864135458782
1544775562418431814769482838696763619056
613462147464853730939513729990368426254
1718309332714402615126994104990545269520
95967784213396842407789257809515765975
723271217836772317051019421999683481500
488914163187244365731748719320283069704
21266105310635749003798434316434286972
187385087954530426332759289571925636795
52651033931044016593889915498728848298
1102238769999097130080489528409180467810
5775610250293771112511773392614414221264
498999116049196609098754296406782277505
480040852296250807667955790586243237111
16650785253908164346336109355023238878
1213590047591332369110942182332574761560
3531546283285750368831986630789154374484
5030498662671495118837838052142160445229
33844484741371333183156668886018085069
8069906777614377753080053475620356427124
2608700159069524552685205860697415245223
13712294512821224684227174678810536560
371856485725870374172635971038517616468
602132443238776911949469363441684175462
7149394564322379734934914247235128934883
205556188899272651750437862622885575889
199364996139233979856185533461624965532
5294352552629914670218246896744375169738
85811124088535222186807183148316865266
544254148433286943875637962915611999138
925383543727619109782625511617747503286
65864412269130070499911564755293306748
318815148448024467948301323650954096950
51177216103137953171603027895259626448
9030534499998436915120916131525693809494
64409163594594108811544060935626997843
4741415149246402959880817623233999618698
2042680284518932401449975913967941165719
80232420650656180906708453739623094220
270185301360282810704536395649752589977
228590425337745251578476762729052408
951149634229676463941024278778865141219
2519289081823459392380357895596192102759
243890862756246375065629271194211786974
31431719009591831277407103510367794850
2194955769956921985654034289170875993830
4335830422401101317328464883445050391481
666043784951477252158936736846279093122
445531589415165622162224363688794357043
96655727167549153002259898753491428480
427437334642450206861939068894767812817
285362488136766279005434789376418966194
673755337556441857191578008340332562114
2707178278575738262769562168624382107
247095611836831978595542725452896283668
515667932029675246537633486280256413031
994642417586161883180025904185219225386
276332317425600431023014970740039573856
57810695612704143435431835560317936096
256466651773629535063532615541561926976
8797857322747668168124683879857693331418
6876316843786762607672104827547435632951
3141409692816096852346588437524728003403
3289138216864521768693239465822149282807
34925045628622657326811568756812412053
4385292267109224584053655081796792409553
758850293037299246311009446406728721380
38995579686437608545315938886621686469
2091274576782321189119903902273081795615
3040205825389945977740942154620069303262
3170377638117719254188626319381886396815
8142846229443171398772941806911334385145
334477908150925679886822443638385763774
9285734720821755941793995625725538521337
2705823597137451746448085894461499507352
8191078324999158711445800156488217264
6735796212340449371467442120338052305
24733374578367184259676575374064835269
5738305080847989542640212778419221735235
353586154795747298615924306572665567075
7926479562538166267497267542402055505143
4057528637075646981554828419186755273110
209888453110124319733266448252830545784
691835129416564959297394610186974186749
3160297693886261383284309644103641435260
95759750926341818201351380499512219837
93899506868583346585216748102963786477
270433068212341674031377907201097428782
6325205952953004946793339311454664139478
332598768082810412495312261470394605897
702096278723879266128601899244659869799
12613721812185428605401174753068646212
48904935877582856941501587003118309535
9431221671838426449771021706795563391591
997674817448966095912641405843925740800
580031353530816579975319230310271238107
878096234309329174287700457186765629267
42993667446095048758086378033107589273
886554403301432443554885295760855949884
401325308351641373010158357994989578891
6746574341217146986921242245664211206
663336011638277938542310611901881488850
275540355112248346799481186952066052178
204544587373810528365856273087581085486
541751339595477915045164845564522202153
379914848801853312948801127216040556201
8407011847477586260451255101946878321
52559042112955548121357421536793593190
709094007837553621934895135343453504145
686020784146904396692651592174087651
6897432194321864889082565258370422242174
449129996345209168881596328136292278
7123584243130817984074979946027481501
3299792867722613254996234845139784212
345839954338776025661790917537262277863
7200937720825502273031415240440619379079
66247428251960037715009121242738249178
693978666263421797514373213779794581175
137024703387521414660804973166056789694
6143592914718482255970398963762524905392
479372286647500175489264278737464945941
233823478014457083919645885110041205040
412161623897656595847780325388767108357
625339464224031530275286952279495937708
857881218616759174474172227302317086047
33055085754641287073684971473205298244
888277299682050185593391623213830235716
720375879253689698433441653437535451148
6636516941796641186555809708207135882871
6522644638835990198055844518405171143006
707890979250475844264381556658129284957
1469183483602733819118568851592972684
68805642807896335697842487361829156411
98736436105155195958040552615072305674
132270988395446994707521161269529545887
218412152283339219714187408821561655602
3998826435846000978442147082226185711899
9106249217489443187790405894654130849238
827576362671212222669787250346278997442
4271104060335130560769322261000098986526
945470729317656425158012449931737522507
8715728925638801468411799895078752599
4815197622371657310353781601779064686772
3893394256421697925342355885876155472025
843946168769733512427584513113820074801
29719360257223365627558925213257845055
628739823265359571745934506916362296463
6503238555815763141285786039455628670
5389954957631783537258457714673238755576
7746694839759396879559299971225050677719
339881781105429898814743356226736595973
711195244122735186963115721535077926675
52834666331709067304273158832344847818
9463299948979940088815471918788491972
154338404165418666849655749208776699587
45589182728120104679808694930729486849
369786558216823752353297613169893417726
7580845171208485672388821263533824928092
177460935981252741190566985364893968383
813967876727095423468051875893881357755
321130875719866215073533188035735313588
212244870736640454718538926332282841874
5583618952601698350722899229102967663155
467635844346645990581569832658990186909
956888789160776173099897723700351012795
6074734117338874177355717168457642049409
79635606985535998793659831177535006178
8508986835237791791167063027762515853342
4627438322309296893974898759617763929
1360516046942113408327696293574274249218
3021841615667292731736586472776936723791
247641134679552154258003987582170068350
189944358550954695709836253475423928590
105217714982797594726148117192571366875
313720276824556475955080156090969716632
95915209955148118259966493064326316013
462263592748807768331071129754731858069
157338459767568977136411819075024890780
6331465082993070782250411748867659487594
4286614667306548497619651406215471743371
536255389643296610243145523943334581141
2729790011098026234244179214545415139376
46981653357556869623576893860150925119
818326385938480397221476341297711934775
8271231761286577454943749374759621472311
450628992332661226422322185697321865255
3177422192266432701171684796259638252833
733950243967435856816027474587459731237
37171087899269861616887633623141938522
49912491602738796469744277888073817425
735381697841521140703060474771989242
8898501229291438656624838976844020953894
391225768460967376653477377905199302983
138818695217802060187884214199211748108
129565476070545471826813894856165737038
53709871583480961856844869318714708072
168153738283633792543873066782737407436
9353767188974348327045097339983648122461
2638313555694737926520721384733969988868
6639419470844806208029186647493453976663
528771365143948184403229938512916296093
28661447451964868227845288354291773701
987947832294946031790879435744028464900
6457380556548283200868764577498998336681
256134840595395432720555432885695103650
73697306444813278232851753503572406652
9568108721694823999093268662770534727
883715942881852110989156158162393871593
546043209041680666478858832777113485023
5105284150556681545888565183457053023
68792589258066408457745610218697229641
56428950486118733771901743057752132469
292819492225855827026215803946091844235
20538065468929138092664375415255855704
4560272597021921158638775234026802451
3799835793465993545162671947874586114272
5136334942568267200643178338805068632634
55883211308944303883989427940025445045
6043442092844121280431971494383567786491
5631802985723653117139746092892225376673
2853147655173974843530178544478821754204
4129109961529364133941548074223335447434
97736481422714233425500396276051504164
92431576497159860393160416242070894911
250388949098494322809412454621707408
311634851827360076427409891396566842563
44847485560578392176371598114163573726
32088717012733923115118314973897412091
2265222681491743881373611091101671239622
492839841324161885191125937665414434
9268172789903667371784511352942052504261
243987288674129347285650212806267805
46406359912932676122664537948228789515
317826547959835574276584063383869999715
2345728719497912400057714282526425459471
55055934446185521956181504231927297733
158396675253803520481397480165355447968
99089923675376608009150747187146924348
4202773881037353624012131293916454193276
6987834246892664405979576856377158704092
391148432275735069625445729681601392248
2646133197581099297240186420720262956641
88447185488204249624593628319631616713
5481586858682722167580765243664498819501
265742394541978884425105936220319634832
6961901657546435889696342602417696894016
203326918463833655776742946951522577205
5450351842703556331261308102750076788199
589189413809614724936907388151991169212
8866711350285358294935781577507779953671
6731144842473925697476319502299299808486
9991301745625706711574943407597964107549
8931301237972682380056978114269381396931
616846863834369313519073387678910277100
822256644690391496414511148459684052733
5203223397798538981664106072695130792979
9056832025852355264561886125226178415
2638438804940096535836538687313611398
327671741149387452276472622839523014667
6585501595451811703931907005723249585407
822545228219938695885622946173615995187
8085675145668346579838678695473615848503
259018944278382814251517064134817842413
649422932364759277610066443482450763593
6762708332113246424927684489728360921604
478881718108563654459787862326967497620
58818401447839318505698806440484329619
288737592036559552876845084645330967970
6659592339868484822682172351914115903274
31383248997854666391230731099344974463
4200750120626487720068281370261892115297
598830535526813539795475760473793685215
199636715162759829345065516127309672248
2349953237339864207934075130227317906
449016607165603148768111773129749769
613513996605347488671055476932537445667
692567771448193799916475274073978068445
8865531446302489960313914683632025609667
972136753071611398654931122638913829515
467989751615996399949252980969606365979
3283403411196035563021077798605315514
1862551313256915547765079000491955493301
845110192622119864759000983675535817328
783445761542196965528474220723579184
9696742341843486562733141183229418774853
575922703313324162529526289321640346559
70283697927268865734981445738549824358
4914839768582703479157476616890740703792
6046148547877365467862502936172539546
722277108093281047147871585536246488866
2857881882087579773316167750862913756482
6983799786486400819786942671929264472265
6308977542619285293556874662417455614
4645418174787499287537752712715039873663
798789845909887986712742907787243657872
8652232496943424778229922389489945929
787894163345500372669704405417241304891
62134484734616673248885722243831805275
9096710913433300323594968450719902963
6230194856879964596349825087338831517568
219047972966893608646221575941689899
3835113585382440463220681988263134078515
333367727772664988775171058919465272868
4803408528289783428664321837239775377339
3675233208098213473518020889755953755
8308644353771751467738163186327225255201
5517287420832429119625023246799317738574
706225704614683192145997612305347384406
388162695259749883365989741865496551437
275784007691347654468704772965916467708
582264807597157965524807953762765957270
4356837719797705903364106520925568198
2046571864824367771673767998943858402122
5561646318936243894694747312333517465111
828458442302804279461737593186333179
5882744419349289146981698465768484798465
94331344356153745698140833992992318990
6526985674098163767243729332623192186783
105713498027773062628850866969669454678
823632872894877028477459441361926411103
9345455930569763404476846419893396166908
4737532613085807630890674684888897599753
215745513174926790879872071543572753929
630975677301742727791444081469241119574
83404693358224251875897704156732378314
3782612127205795879642282799384423088099
16484277545769377953191940526077214268
6913597465252811145243046756780364654691
435848774623233854056007744237584566060
946055691215168288516921653682794474991
630556586246198861267267565921991867
1528671432993729729753235695339748067448
8346847126204202400735613566369845502477
1579399115772731105898049268139715454087
18946091019799114399615932534751284302
663536579166421879627178504531684907806
809998809849767184281823151598217851288
30907806734034668867522434712416839442
381450924236157526305398114916070732941
489663461168577727387833340198665960209
69474126324253985359555594737482247703
278699453492500354957987322816749538380
24426473681252390353716203386012085650
83874971906696580137584494243483504684
207388054577108197596228173595614634648
539844984672370576528125268741529941561
286400938293836781495859572471227139361
7503349973818389142320407225451327569251
25127855122945594461273934160319941316
4505383412685892317439687149366711474
190956753842170164861348944261972743597
7645790812829784177848195737959437796677
647372301945228642037396870343677503486
4124591652012984667437951020295936212411
330582445841220115445699226959087844260
200918595340256754638174049581992196473
10303812937679482188715940957415222796
609650317427465088443223355622769531707
59106048652374177192899252087174418311
3427530454551074133818463648950389411302
124764896314547868005752139375261184681
591224934529650577209317532580579111076
346761865187541851587644654883734059038
66481479873339167161389289847623933561
29866856185103912618257548692345282602
681945936801358950536118352461935216640
540954452795272873554475874968570917068
6864434477339833766016112813908291431214
9818683066133451818418072492300276522314
4002563437452699793697265494756325806
519267509984813979566103098905759539101
460283684684594859697699676741428751059
632333715141687602799083992914064155329
482547664967605134928142333657993305
2973283433315745957151094008377482464736
8764651844035953461546063668377911694
98188318243427324103489057428318486
606633638534226378636468631255386891047
3005818537551152186843348101332922211151
81557882717599683028736298318030709584
72713816981276837747054893841237566268
341367997762172911909406774187566439434
943244448725192042277600537262779233610
1037515527471753431399641122547352926
6626749399927475904395148031732119129761
2905953858683205199192797763568173789023
1232912048264452782259832555240879021
168763392717315815433121714808972949337
3628273950541392508189234885507810245019
9921545346888151649011019012796382168735
646633558826271098493783247551358542063
1393196651962976277594758483749293694855
4923341797289910135944008150257778965128
701165979606874874455715029696424078934
5545883463332797277390941271491421647616
3864168073982813424534815375777147309912
1938641689257528117075553838559581119943
231815433687178840981038814614457739482
9344285069899690518911353699418822822686
747013417838136452731138734565745347270
714758723435174744637427997270155502241
5205834168711361358179293330353275007676
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]