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
508752110663151737234545538220939941079
9691223192806411176156693376904342031143
944079491855243459011175600428488338080
7839946267204960235981243878649428869271
862470754693594265456932831286675581502
4484334091239039614862653121596577498
7027973593345468932931657775400526081615
21446496974346838448940952614985002750
6881850283912181929090439180714739239356
5190509821592536486446954751292625769456
208978465821635312476095648795015499495
10571535561230983133363131102130857603
85977836512388228025653262627691046154
5257636097761815573249262388953273188137
37649856505373336988768500941965798031
70728420688426019140991456590061358333
561849523393822983945728929628749342411
685616864931036152241084807746321935862
1129479379969671782496258415725067917908
824295850848346620731051499830017311595
786662204631222536034107063917491415317
4588167881444519216126347181776070032862
580770587618813668922776893151646862588
3152461362899741438018978871419567146020
90373045328225217088953729393711185656
9403841085871001648321787450893698362115
647089074366695240267386575451491909550
783031629965146090684013438279180003035
986930689338142073495342946444476693580
8320552951122068191262893323347756631165
735897907724741273838703975177465452305
4104991337937821278166297754150691944
553460002882316233546525984367554405340
3146949021509537962766028775662627645992
122659403110504997664787604298323171930
5983477444735694293231726926573224861
967695853162647255944886805388112700
2478576417414978151757459581526295387
473755352144812358733305645498860793303
4261828582288622999231119054594663205568
595788617969526342475817706242247681877
7979280364436542713212916997270128991374
954959264782696599329072368591042189683
1130872639794371634553753575406889583417
481620554863626169986427220162073713532
9896692976894949765665680313035805140
421117095797546386066759985166862733676
96236273919899475199071184226529309398
2447457350834668905766022385108489791414
174331680818495116723781168411871268434
17721979340418249323192554849769372826
511779231924918411387195339878418884764
94867397329452263353947289480437641449
782059794556900935327581552929666827119
488093261666429864714198633681390269419
586292685941372349716819302595853182224
943744652454883216772521743229127821206
1460971122839013931465632723959646173114
7077991393799321811336851200869381085
26615056335046740210732923033574835
3102367113569567485043869029885066013457
4057846375019509149478665712326067704907
9056137319438921976069299257648446058017
95238952297402725914952103686214268477
9557761554516504544965669938361791277417
443730382674937588315493716552386479377
2284260734455773910366122921381128352
90234752728173221296139790856987301000
8978283385506817286810101335704649937085
8818845350283272263277966012139616925791
6489562078841664278885062213394691614940
6937170864969873438727397509276870354938
43981656152690267904174870962165559583
6248960780429888284467058993771132673914
120219373434896897176407401955117262987
2195769570088071515418127737803110534784
1380596193116263806781804126759882282630
283185076446528247948661446250574996653
3385301736918608197153892646305734972045
989375389516468860162663818274443934164
6673846162149677176445221608576374326594
9981931849718336201740627437493958037751
2657617119626664260193864125786243967732
4070308424013880808973392231604391915460
4596285431358790741451812402479072564854
1824909213676606150178351936701149468908
2246259718027260878084829466120294655979
310130865098765135137048768566877373934
4478502995702875595330242586515973359197
704173975410823711345313840493239645929
7381118844449300276399598410990457945907
937477171266990241572389413118075196460
3968169582467360894612865239989877678706
7599303186351598317364701619115172391927
42474390620627759231928253250620306274
91477741154367683396947188931697461483
9107906093711079248438635322302461088735
390261333310720771252931571530950253742
5884199628327461280257887603328793579851
50999371903666078567143158663381446864
3480187125218870238496148352742377221039
5225345498328624204122769213773218378533
6089160571912703668048564341352092877447
8005321369299727815509975122791271696
2551562763041125844876102552895135833265
88215145953874691857930624131411055576
3440850452122047195837066544522983371193
328134246077986451836485940274667075976
334856141973687934817316261068032984128
693867759517477192681307427641467711050
9649600234325082251823775186939752667004
9767224410467382893666729938334131762880
8026277339849975909768291462715513278979
78817901244634695209341930448311409662
8640728560693961943652818047295516508926
1753430834285343446310425047985199857807
5852258929078129910023328897699843069559
295145473903899799157373290968076754837
7411499365237411804987167786630358978482
13687205004893330499119509193850142155
5442455900322185895025870711120546206
432810661587365370596806174551869627911
317576633048882422993236704140033742590
650393611542708590304979318173591392284
72078382641876636138316900764224799737
3693580688736729783847539996742662839947
9562393657496694206426096870586143803431
440569429234194056068950561740939423441
5348515999280232486393521420637855650
3118575125923309575945252534682991054
65108643641713056365425584851621526021
5308746220039686712869083186901731278243
5590910859208486515210237021329629933418
9987597158412804121137874568278244056932
4615447070671796582410451480962874172791
796165504776049735020368016932462384054
889312445955773131345428662825605959686
461968879839599211914426329111942489282
6069719727046834771329242649724349518848
2477753958985005812423175434156465267661
827451695842135643567785622368085759314
34666276723599293218856756867155425520
18615331487772047765135259762866599850
81924307101850172231755411175932935147
353269910823129861945918654949364481030
7218243280877463262290849644599110923925
311850431405421093686358539781069585
86595558602376071433310239339319363188
150793155756287588187524971485973733
34009522196953974424432148622835871277
399080075184670826489802293609717824883
2251408036572286766399007616332076325840
972396597546526062985004300039953974029
859884328428950695193717880526725744362
574097265948158304280117552413391298358
216759552221357341131315020610612982390
892531017217331255536621903447550426778
508077717578692755556203771793834823696
8419584254417696476523515602170458294867
78629115243783972166992289757841678374
8976306324509055419956389980482491606828
963431084817814561727085771616355455042
18652839580039354637918357762125619045
4760764843852101540259311257391927624098
669519019304378029635929731072891829403
7668902738531844913372655222536873721
9765468785483205273711672847903471861971
400673618451852194288030730327526377114
2966972931621536265439877492986520213192
2380823222318506263011432818786838887055
43093701954858411249506782160578059721
782079131014121911272161264664232436134
418727601334345782884141728241938888512
9043871749747615861981636790176610767667
6125726512583691648459349969842561032700
55817418484896249959543576725736892388
268719252783230857436675697801851467360
766890206810585160235755668992857259778
87432793122951418068947958909881474159
9213233988037923639728612993966383196546
504648486082900240578777716391034395584
5164191782019671859379871743107915717572
89209617754750731942868990653986922244
58413940722365488637131720276643242584
5565583354816571287771419529521125519262
7954327550338971495220258512638691198895
5686788582599551854964139855549565041819
4485675598308978040177746370966725011
8052273765158773486781442288689128968489
5066350545127109389959074472687343373667
4312761835627017538345829291868626583830
995685904793577251354662895152444885751
6683430447801936849797778250277497451470
106923843184666918301491134835022384236
5107876079143516132168666608434355867667
6209887563233098750952263125977132207
630718216004458519587243200980967932057
95811098950151784369737147224726349505
475159155999450238168482709703640274514
14228185905162793379502824572453553606
5354122567802614269224948147622435139258
5612685873255390416066919293862958062887
375390089820609641253935846929524417222
38383486359760619843220602323222769220
1213833277565473830142218142529324354360
49379577835870635349979560021674889893
4366693340107890228155409667298528372418
2942657695628072956861641142742415277242
26511514417537491837556193236367622883
922820343203256327536076930773918918924
711456451032984916866114485214059486065
828158163654315617291396891826787022642
9893588834969420546279331184512546156323
82753407543209620389609452890919248704
2690458146235275108792933054321312906284
2047242389104206314620848564847916054863
9470681277730365950962609248267228237
6053437865614190780077667130417211474225
27207856238480688601786525580795377722
46817688605101979465437643336251279490
1237693771445350901581831296325163527704
228153344952900965118927410219001417918
6538125024115112873650593346338052983695
73534897202626283323922913060159123276
656427107256936480092477693731930299628
24872339354222085741583784226799789682
226510948294328411375781299588294788
40796372782341279396613262850987652286
6735578611563479692745298759339949502
2718189363494178569351218801583479769058
193999199225792779541851484750563947567
46375698544406557573783992592448454911
21123554178514669664395049721667483309
685234763427598431345411660847540704613
8793335626838293556366053426283624573663
41272090875187765423231531785778853088
4615960821714225319426869089949744829435
173779031065645164340183053586225369929
6771173086371470508725305710458851762546
1621793765065275499311387922612921872392
5364138448331544515311567550761079956396
657126416765259840005407231310548799779
894590905696285793183407756839706812405
848462873614618385854094980029693947424
637078554685004542945477125515188887872
7816611250255152678394866533405752655450
359978176909448489563482181570573974461
154932425242661806228374286250936299465
416136746620748154215064272223983066290
346590073955346279986299330118515698836
8924975950725558711456387592154763417789
7955239593128317589494849535496779149
326509232721500382518307985891584918221
879098445705324047775057806255167050585
621287855884306237368206872186769215401
604829956378400611432284316710096008303
9982797090824397779632085606440660369746
3769941669023723573619985837395921183386
670333672966664262129354740248218014204
1714557053683197845095384115709737986486
31495615666570601644889162785480507835
806728830381937218260137943514261835124
6846420514521039865286327774619251601754
9069505773846859792818783404406239244780
85368189569778733539383490154198998723
4949711984451393623592227935682933535045
3887922255806442428892728654975486341540
94971544412687354472862877735687879488
195855761698148765449233295902138866442
4014881269494695770438208783369155488665
249591169118270842018365552795631056767
79769857528551813123504348081124470633
922188782039025835275538389258162739321
193050048879861264390554312631564332519
770559078199458516994594015138190944583
2006854013473392940739257392444044801971
444014801903919356492173108569375996230
8367714787271973134329940357478953571
2988232956957094996094776244453445667149
7933891284327186269014186621828932749338
973747533013312685061689227449529892118
6017758870915690978492196916927436467
9032590877209139754616384243666375031687
9234251282740241587499563369982963960
7029688951178983752199166546129987793
5641939721119197353538442427753981881717
634602836419584082705978325627602157
8761217183813014777931937390105799495053
7615472605715981199377487313378615750
44746437401411214290236690635414031814
724218899474200744383277898490055577954
88618737960599931967419828768692786822
1306336368429608131526969470927980433252
3968652584238506337392332490452139893133
68025343514364825892556232474911239469
9713272665413735926438007034612671497656
9627569450012394276476117837644933646934
954739346495896982483789661404885430
1255609433616156704255630659203872756
5661662083165766231363283319477279224477
28444110867356503603976303822288622499
2262504899295751694389227086441746182992
2487622618437320827915452057539155143670
74794441645135575802420973267944976457
1612632199717874111117939828494899389095
7806440230303820238793098743945489636343
6426229059107243950723027263902280571097
88924448872181894259265140654025361604
7935381926671951508616848315614553892296
8786523417360058511027437462370974209
4006977630601656835886386408247251743277
6408602494326107702114028856961367924213
9413165472621533125793579191300916414755
2708993483425178611285261959428126792458
1132412669408593925548478576251468946
315890026420372216537694137458517448047
147336786581123659354914674503447617733
7483154719275779849723332081379844698289
8694747336723125669196039084337955072958
392837211909589497919245959597493310389
6888719627973282445776819183749956581897
9789842165876332554219987157731720351001
1818403372739201958516966194402699497436
69598879270281224574541324733093576
4160229216596191778855058939522683047294
852767332527133860059107745354935328455
43728863943297779466657814369040957644
7831189060231027403092762611810927452133
54758311414655890697512614334253746470
2352852486327197688618116443388020739744
1975731088777979222510389692338469766169
7328446168042617199629701881792098582197
465443748583921534111648274539296231023
17637879515658476396773896261349447100
8891456293053116387856216659861759522
662552941516833356913615958521860977205
19118553097834124236377615386869537771
72928742061268972236347419490830621580
2099822393321386875155144359197387352875
486075670419498401457711174379570015292
672280846620860025732704980887815625287
40577239865835120986840689767722520734
628626206074143327154664050257447803910
340838642577520510855913554726516929109
53441984745265158186384090869831005862
42095746843565726122887747763366029286
2916874484385086102348527759852127935
9194694788035121938302574380752839907
3138202134925521205899725419230274094031
3421514365641657163351365351510992713284
6400733225214975860418364574133280076483
3695334346751136511789807989242050054096
268972585393376669045751233946038027223
85950113677283054046816760277892048518
2871778061234840931041198072585024286152
6607214986108205438165919171605370286683
451643123602912617446869991964032315137
372210652504163719829825192064285472409
424681047111939458595013667744139301989
64608285629957137578392341254567419546
8997543459849668775140877657808093201287
89816301303877316815384574244457999834
459729527023700935173024147326121361214
671534503076179391963036243845646728144
2479792372731222027814086855811807237
1635227885303408628897336016858292872761
3627254650413060581649575931143217294637
99438953532294485725520465548068757277
583482323960339042845399291888072561669
75585551393637406111143030416218568407
3395812991262211147343893423208375020
254360544876194991573552622665078642082
50541926624387875030930767895091911701
93209507682519591187388338843196654815
16634521446090838743313229772603414960
4904678161483487682842237071763368245962
63099898812200925778975445250503384352
1569645332225909737944738190548253034
4807712661989108332381525411464924541
7936320061079801180617173871609194656573
369444434969595332836234768176336402339
9073140238814396856728750276379949218
822976386393385021550902306162225405161
2004940346358611698325708492890549824
24235253553111654122426794914430565251
353189825272895242013626362814121975070
9492631227157841745143487732496055816729
184258592585138497447872080491754364273
7070684583782601917496318211527937775
727731421358672777192076666996642445152
845429147198249379085535299197745333701
5659652521921316897674135923921077971668
1147685884322545531332115276879162977424
562699613965870334480911402850911711812
6610187553856382700320903667383656682934
681859335665907832846898588256236763967
447214915575621712231167040690086547276
62175907915707113198814351112637201840
4238334793045101675095772186895466839044
36249041478729829433182216752775973998
775515638179044422157934683150149088411
554212223232780140952150387690350849906
357491136086634756874068288443714895107
5886883451873150420443371433752211518308
8925977959661991184758912548441494248609
871040101614435768638063120746452750
5767403730398699656449493735174857535
664772403273337632520212886173881472
81241658942415429749472835524998737554
173290549005970357843654656151356656799
2678622054571651342159342209424260287627
6559625493162496943555578901465365500
1841839433395190769526572065378962533
749216591288607230557138020113697203301
943576379495170118914072022493376363552
5070899087064004419443616674500818721834
2906605111817412750169693335761617298197
45261760896480941019494480596018937839
2541574733986688552342771498349656751516
7625231696565883747779593986846250338716
84864178824655649305989478393368347224
6336632627229571178821016980928072822493
6457570348416945409125893076253779679730
705948593806568095709075633959688314679
738516327875432897262511150594269
668340622124162477043092944443020575036
6264995170248591947267764376608124736016
575572338624141042735871845745464893266
61663762530590827558703839282474969788
735595937768472819166026435418233628432
51232380211255455654706168799845008264
55527216805999016722318111369430489576
821242872206500278975499732787465243018
561017338383283176623707673439774297749
563518338824678929353645613324631884
64348547233250350278436686781865196489
537919143769541015196928505986049887171
238722251762678930694179510506714818
234153112774455051283322267480412267747
502097743457755293417139594475367445649
6644364213391714991230135262969552453759
77147264388853154993362770144132021686
373664614067604597714027590587786551367
3368180025068994279421309778750323947012
168683341052079517390528590742268918682
966315018960481013689544388512241339
768298637500963582691300844571187164931
4911926524754242158377498111763995849
167497512856755467475230939158462498010
1657260741652519767766714284902770698855
39362945503417548994963889142699568641
3084343925147617848112399660986690709953
372474635829645537260293844166455511991
7666128776509170936554942779497626912635
550571737652331151001948439164542362262
5254174784019983301160342569998411963922
807638635990848780577729972789495757705
32147458261455366469712192868021452569
75055717422788365673233145229962892578
249965732764491048146988525911152633079
6354657778184553241986124461343821975132
83781801585290121687891958326871076576
53870585954906523695730495327984791699
2965879854786248770485284445741056309165
28247500661867687296303364725243726062
9197108030142450450447203450797628232712
60019665398238353749505309656898445763
5645121527458084439238747406429855992171
9293733175438171473597877009397684066173
65866195395491778994976281029553182
32583097713985566498549443477418408519
8237147667018903318751908691123114996304
4059613643613639547948971877151215404198
55524678892029254153583156287422676188
178443438853650594972377873247855948048
6297547113089985259139596652433452845932
3393733720716058606992034755186574067884
613955467436831728122537243255160003592
1876222931237331642676791814225025195658
59981458630654064343368563642728312964
209481593782411441604291776111579676404
3953507331828496954187680794683403832
813282948256633790798701476153616825909
6781771791838485808414526525532737638008
7904835610652846765948159365752353415734
821195463527486081017387951880977454105
362735670312815882063571585364863709171
5624116908632404792967018842119214310
53498085151355903961240165494263952562
8219844228293276512193717508868126148135
68944715172139991550359990684064362044
569885141627936381034786371164710247076
823075375083720435643926712358556762803
7120459244483560238248792844280184629634
2203597990091545660563274139800833937218
4060948557744315978165833941902658781428
889944993169721912076108035842858554570
366180966741499736841917815213687144340
51039354449202457179664514501495431321
65682658120972037415624138736608671585
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]