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
222393738586012437750913441175458436
4266609493994855328986895257686639832044
36616769554785069365964931821961493785
82411398715329632018290561814342537456
31541447517149176214612288896322972572
6123767129552839904654594334740023653483
521270285622521964359239733069543492
489678695897515184766987853755884289814
9847845358717535482428778076706356912624
8487175133853723421485687941275567611948
4873292537171193683267875976425552782141
517019215191850579273343233633567103
951340876447385734455315971660670706667
1811276596847837549373528691666458276
380158168803736322537171695367147628757
9825898688308865711746219130425595648410
6560501348286476429546239816517827885101
2760328557574516320396934374921910137978
3483946418478053633612499901906065781180
3745291204948474164317118907972159616
6217152182117764849069279868911686556430
8880490887828820974694808496992562992890
73193702325207786608255814139697554301
695290795212708767628823354447161885780
3932348751507468182819541784161438786
619953035507579545139923327724043097152
962817702727473683434099181138242605724
4356920186911329410110992404559952538420
5922821279602974690654319275834774255603
5002994052836459656116404654569620557075
7007462736703748344551772248869878627
83364542742114151107619268452903344280
564975406491940332759838949337513884328
12655639291148254503197681894796576658
5971365229983451474021766492679461287357
734240085538586921376889107518188293207
269581746642472136929199425572456002275
4498254727984884458580076626540737692946
4629678180301906663664836176478721085188
7506765318708561300458872569538645124922
933037678319368778385876243867423652388
215578272365876740309600136568995521531
367014628117127432431750879720823216789
3876964963320015474707838325033021739
47632103323522655076804726672950643184
278944437290457764099209474721212592762
3694170199874159490452187757323580997475
700649369875571333977884854507381227984
666465451828948036270462224897393145891
6062445890867689384039081620480184332936
387483494115538194267915902560993432946
3460488457999387358567854668869444931
4318715974552754821271868903718360904376
8888177093256454593808949216565323592
778497796602780781133969407515372847251
230366411073744742523711330792597393510
3578523785336479288099912642211953408993
3454764218707430953662737182355245846588
8030536061191676455964461570724280681864
5528231679367602676748145777229228325482
551513442993390998326962789693188036010
2674285374164963367517367477049876021
608390346319312127713881530248666397239
314758915206823731773688326565034915560
2822440219751399997570302302736968009390
2996786979067763913054674761971775402335
927312249596422255269283988952963685647
1645692272449042694030032767757522402998
7822438941720268155487518575062741663
68102114361707799026039266825597539000
9346206039682294628790436128249169964732
6269658177309056374077523881935078944931
288765054121807852564437640582587005771
38455893700126963978853674701745657695
3382462646201213899354746388343093573392
1300741977738769586574006720704638655955
566389292502292034855759730765088979366
38781354886269378632525316818491323841
6930645776837186689886867428947476927945
5696899152737392985549268724448515589145
3414363886451825593398895570940271718
84372147830845347220672299727371698273
91021462156145238345357417754318905225
3193515878946598214038696497575399951772
638522722653329098743977790378150381328
145980742743679357066539208256484839142
31945800115563921610105974783222324861
756753844516180268085985745445198188835
8632925857665487434637831220741638646001
334087278911818122915765601666519669095
810486129586085550126882440104620092315
4207810364209603523915141243658189176233
6674444279987688151635417552157219645310
7927145433371582444773783741689678331286
337883421769636655772879197965868525623
2140796362357614990342228418256412490
351142686376320353832963586473442454072
75101436378989244608686407665958347266
9652757712917977734672872458333962612251
3720700348336674551341464257962521492488
51681257884575966555235122933211329947
997643126773154515708131637392215974350
47344509312379527639132873176228715049
7819335430111943100040308603750599277045
25382972653896213330527888795123717880
91643031855035463772010252780684335807
974478164302402504878844455679763164266
131942455354510962494199258198892159987
300492938578817832896941976150992703614
4268409334617247342957978002223912576455
58418406284988839914639438741882456175
1440861272126412397249203116711176542997
68967411095424478077511366924671715996
2609403260728129381830823019643912311064
187674686658310144643216699530067999592
129621909084368789674239537653942052462
502428677653680682917578487760280754070
3158269581367497374556763433290975708014
431318857385396760185235442696657901817
6024861592443810195435962067587697085289
5414602910592989309228434502627340565169
710826784529791616275327892580414548829
175811869182419450712137476084356586963
7121476366218062829694149440544527609442
96861611635228044091172273675421914091
701126426407950589614344195519768217865
6685147226232116729729903571757414733971
34427443560301817489557320825496406351
568253468419813398821625782920461312392
553693621752880018458640412068859020972
7384936331636756303839451601759024498422
622521868923755740367982239084082644
715576318007284793068928228126152534756
446854782540878184176867129248942735746
2408548462096974671076295768120594324041
2154873125635432942636593685597973043
4007811239698786509763328138246710113700
329993008235729332473776369954532795433
7095370280794414329436465095035569282
10184458631874853092149794575496971250
38899103804950498368521054679781202672
889774604405264498263479142038623553
940915262807640092391177360788049835727
388276761965873685638699785338819792680
878363911521327531724021350913230301064
3024356560223827218098789856515443419020
4758520168223358561155544520573795454
5362469336378026999715371664618440601017
5401830985328311615711194546825635509662
5596844922379867205721541935655813683036
10106898979594740506965702771915499326
3303995478842051696473785818592566318134
6224178470332510765393379269114015097759
988046532021304297062619928198597091402
573331813128726460014577720331597995246
2172234165896652309264543756880279112632
5411296572621098702367797730393951348911
8705915458882538693426719259879693819374
6268542273374174889985511515399092937
3315364744451116325194005017443154574736
185782581415235702350126709334168805968
99298218984584266804238485663388308877
54626066231640742362315579897398963218
2854865077901308620686093156649162835988
3007564048041320567891648179525754148945
46156679601434410967331440748768768936
8629248230233678368951603772325283641520
33778175693934220381909506133633344041
270226246548163964710769863988925164417
58979194127474205652657998202097236011
5687389937637080861075550734428923407
296012953974941539397034997214294956542
807125689843454433312444976572671357664
3144140157614875119370311162372394329254
410759749931745500796141680139438395207
832074729944375145684324980926844968889
6130522144594613983150223034377945608727
6143880865271676848961145374755355553826
488833658087561509542223307675196712011
2295952815484303390651496797508170193271
53461271499536863256181343842652271959
43522208356152387696304353024574809339
37170105850235150556770087507576236
3041632493658154649549323318253782063688
507611978831972071907946112181645504458
3155982158691525265038471233272735989209
23611332014945189348837312178760959084
62380116985983876831651007460835542178
942321251525074262947817541323399981745
52367901704864699249438334132646218571
596899504395557389530253637974893783756
822414717471614711274805549799125383765
53008410294067313781446131746483614847
3978553721956352697369367534560315688411
88062471773272015746468435378237371170
6401733850122307428354191767499849767
2999411599942354510437804248966572729265
329411652736509294822619832050765656336
537898269577174277658116384248241163326
611632972476273995243195210381498230
8576177611487919312454453143937831665625
314226797722732743526141942599769935911
4726843548776706964658496876967325123177
3715558943014028849379623929591260243554
4523722759444102399821592991112019228073
811981238585519689401932332141517558701
3641112088435144377795454434942552105424
23037478504527811398632634710579596641
3512113938618676671083179682476625116656
788760086881960693906635284312768920348
312273457562321244374293795107285374013
21189582125441675426643087044585240
65082758865537378543476410431189377265
8566890869343866249068537503667424137491
97769966065186425653548882988563146166
48329674879679583992678716947510729372
1204812387216905624447688411088656906
1431352682682270701843924118378708637
6052405926519124237228246969993102972
49081626882774254588810292223325629295
9116848444993742116742869497143056514
70307064911007393651348060997942132296
363937350591676822518963345296630569407
5159246212426250952280449203518569002494
1300996429784074538434269352947213696196
413640113523194270493824350278371812352
22167607366957845950265419581186597685
591192454267472317430897442110713407387
9055261045153994136399748350653333668869
433692454326938934371608164363773868335
370989976462679884124227115341713965839
54679328922166851938835721727361026362
4432890378988701872482339404159582656778
284316555393382934112432256990297073580
417757469882678419022001857548093183239
830491583783346532656868849872824295773
107272704617139913544429322497062585438
4635625645968832365646869052925269539
841915574796614471821755976419193944993
5955892744281475969758641567488785036212
8735969251933980625892771826919029646145
818939426023559766438936880989446489451
2047129377708308310901632905828509950
46308316884629391328233680056025045441
4114848642784197550337915875265958738171
369643761354858985326586405860086796163
2139213498838332615822237858584116298957
72201946869831393093531229257663644180
3609451827826520723362439193606933044498
5914475592403685641862673535876461585
223627917165861478122802737545520048106
17165344908290543028018262792122346502
23959513501675258471977203444487373618
311981314207484479848563959827052444959
553820163409658652717976645635264613908
5728299498287112674957827593234659312
2187724722537527747911951172104124897950
4954896371889748162741686626351330426068
8311323311479467919150524430602926508812
8202319399104081692176505637136617882867
77355154524109328032045794268433538058
7046338495344643765645966709929630418734
9926444252333978324791856857655542526054
504928735708522775074375512382793532337
5511170053744529568980395283293233989185
9175941505398417787462145599539184697
3070692871566789350798584010883835349842
9730270495537399210368175554748834613716
752367935667193911445309229469042573866
793221978232169256852581347093716284009
635154835365315994441387888185726793
626067774205376683609255782777187932006
3885568317396194442494209791644976321
587016367669833906113421677749021484064
6147138967717891655427191968873263638
8490749442801809239474862693168831423789
649348727085860291387875982776693744
8331988703514446825203180191962940856
2574384133046099519334794462504663856876
39936813970118531885205913268518261518
596649559104697523796146635785851868493
119796578623503386318507887449396612856
3898555496638437929165331132112281907
5257273557286554150431782038815626347487
927231196936235839709261881132786102278
867491596507318981550898132917896325162
5383895832814592745990236765344333364817
310428961873891439914259398231984055310
6939119644994258683897714208381667482190
226927912611449635056463236536017793932
781376992274787479544637559175999839664
1571911469865408922819473682357075067
669376505794169276974906614642999675450
8632417326763321873384210885886355778
628598585443279812777102357346540604109
7072101294078746555380296166471460391101
2096423084223506381194112855261215958885
879695945555666425512259446650746008850
749585944907397969572397799471054199075
7377306564384781244264642198627797244835
375179339594417839966129634031443122414
4227800287673277253528066629595475034785
481465271546607713156946461846188247961
1664674621764701636793744794860399623527
2746505720357523298020368082929357771541
2067917733681933341454218298366056535626
847515382317221039836392218916475701903
1064774149447224468355352896462640864073
622944697294780189378434501884433886611
2593281044076766974177347626616067751578
269518682363566590042287563033513874970
3667481399361370714696548114955756538239
5655948279567367768580889480617232897715
238649615706573078425705600587422621724
2760990955024976701286299222627313915409
331798159597831265067975297446088701376
6117462434722929376915559855939839191968
80192608576057969154866348458733442820
582484911005596924272223399182582568156
2984420946958835321224099289478322997290
33418795896068465962117245262392028105
4885824246549720683889842075198169423
49754180537264836885215041871670344374
1745779591625286408735266614638872129043
6599677442083894610626807525318431681211
6213166812499366924280346375461925431014
7799155784791317182976994021289690625602
283934529854849427739551427047307392
6839761475745117211055897607112648765372
799220164082785385299228857084786212
148659455438264916017614637936877243474
7380895399254014322929045930857773721649
1198376240019957750884981162883994111657
93858792208785572871534900568736736201
604293783984499313339901052241547753385
801425448774566246197375136822262237922
9293959321809985562626616419803846235314
8715228947079529161738208586188758339
5781558715884262187584837746644555466827
19746399836525291490825495204543228931
208731809132874612492244466925745672997
4999647516754028540838728633664944301194
680482978521565273038249971849768965700
36527853609762504418228941455043174185
6305488683711817724991391283669371997206
108926353811495061537191905389548382913
153075457108573931197486571159626109997
8848534166858082672571925362501995351741
2490285841959888509553326021616052736184
764053545160762789274089289433554079922
2828604056951973681610498690317910725435
33414703614314172061105047157456653613
1590320613483677757308515749518345077
249760932652550597428258712303569844967
126976909660353782642292693293198542241
112478353842666233144737065553877199501
1512192064943333657094403922492390704848
1560684675073479816474313240887994063738
8157929746456897722984706248154697147087
28642887303364153338483857454246171539
592898293188924949073251946163347137012
2083415998987687812048443128749649966840
907549576857616886795446980619077756207
422098566545300895326094854564037056950
999167195745494395859611800227982447908
370393541775412555675423923958479949739
16566422860431369126836330338043915797
8557425255791757185684013109812840424060
5902891061446572611915418361153469839979
6885753545757299842272292079394754103709
331636666022448414909695503971341913391
38229082674217042662691164624832642221
202194026378154961799474082873763606277
132994862646263850293381883245512673238
384143782243599386895608442132199921454
338916679770286813123086178151357334211
27359742784110000240229827069217340247346
473367274225936930674282713754372033029
30704374171580320043151104502274023379
405994808705465350859651945860056466415
8048519855029125550063065196487233005009
114199003851846364213031795317680914940
69957090782732114168441755660176408210
5921209737524075585879159746619532909
526705047561921916247013633102489956682
9712205564048640936526863279623518046971
7372888022184543694040428004156225614030
8645114689788929136564146868227582986255
50777193008978336183051466214339685367
2103914914685622827012068323687625291983
4837854735711124371964778466351833301
236072868404795974756345438594027927960
73431552492989251263306130622944918560
82789024138064824556182058290438878126
96078514550148482736783198989055935679
5101354961731110677377008133775241914898
6795690085081691252191023032400853945531
1024912774228952538941548549807443948284
8262566345435934247659568888940835468872
60614491277457448692457864312678032612
59443806403135920718881780832492463380
8056976149720451237469861853907294905
134246685945450923410918843806542194
8383246839347718350728635487307978448930
6320869036714011431327507164538444346051
7633355485955058282237584658402811734
5794398473752825818549493422840819836027
637734893087213214233334224454591939862
2068810627056988326611196996694430757062
35303816812684912491458151169638841281
8758373939065537629632135261103016505276
313793847506356113318318369026857826113
8444170147713320492846141593416223814489
7302674612970339234247887652688499761
648215997998526833270957558365189640
9352717740380211655111319314297607888
621171366224830478032802134921329371237
655174743078451363966547659720613632076
978215639371233418524596976392429926561
43344491374598981055207788824569945150
1266351123628997616937131119941368179331
4051569592749033118647038836267795741636
38031870337292694584436062681268332557
69565111494491555893317777785343066422
70474873359377911085189567782887112993
7921454379519731888772431111581785266593
69542897935430810699234081118038664009
8746158179845971231376691196265743646826
98179829845275776396673497139325104565
9826491866819547576171114487604426707851
10944086927257429253988626631426918212
2246315552676286847279032270839898162742
5854357856984822675268875067594926823684
6052680646041300591080268116863120397847
7205489214215520254054598197162892324241
929183539139420612868997894974860903221
2959852983212125938416803558329959516322
192320861413805923093462350383878136714
8155614947209405251353829995511638045300
6609510494069704259537412430611535434041
1742158979972652824460372580106448067926
27688293152973622522443127562445236991
15356939971280482062447655238578183217
4638568563565443406197626579736846633631
988550898559210467429907320344137339727
8414779590517052959648137304385717328850
1237371866154040258185633480250791547842
248920662151055736845216752397793882560
94457389974399063231880333169353262609
95948292205420776350698454087129372321
7738643453291043214077359305684593087287
7034498091566041630717487273606026244077
46809019417533007232394958371536171159
21779268932750778135635893433911257247
332633848593870736446275517327153719149
8972811639971350595586256384349346153
2660740521338703730797841291240457369099
79982333791575243769221551882799614916
844887334849497919148338939834399738179
688432719312207944891331241863143652923
9519364170653408639445792610346430735
466382197110641870654177718470027425007
34290903299585936358763408479456758794
618641326156592118612001103750315654041
5044933327742138477541696837861541275
8049609073427886470954276040638211501433
402991844956412853035922068386240451189
812497938777820026339855871572308108976
585187114679264850107472832534742753193
5764949142215601404782986877696914975975
34921438105918306287868029872157563276
41168774725858484841818374899431648417
382439565855157961275356112223072928306
5264588255328822500879229695799387968870
9068648966013196286418196012386651494573
4380457767377608131637707058713842511198
976793544833499244396964973511158364103
5938867647664356008718953960080724887
29573320777757415085865777169932298768
133691995799725682488573251406571923137
720679091991491648607593397277423091925
284826391488544461861467388519197917575
3832269417063035671133594280494419474418
907973492410827626359006137948393058704
71454876999263232510785716363646661864
711971647820763079099694962679081643881
78072041644486911833400212384474614106
1902788857839136008218348265111324118
9126195922599064903029944494171141435234
78007699655978302371155514468164976434
50804352522181110004418108436686283881
237275284311627142160185373489240112653
946653792935870867981337298343986009968
86695494608844225614642630555416138515
79598338828884255210518796673618845587
424188408970198194868211731351121504525
591396633272633333737434464818767692345
56362091189235832045173972240534135242
983460369663474837864391519211864080
82286274344199584971301236477734271943
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]