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
9718928882571624507478794359829232311369
1643290752412721928791786034256064333852
1290139484336484697093305425735630838406
8488107050127548693717015641270944155756
3342988986626316847862949747751856776
812174232845150549921280275511340709984
14487268648633437911795940817728673955
2541642592746840719934636588182365878406
47556062583560198462034054498855205646
857757715742938280596744726336704624310
9921161959756144448184557042136374990
876884466717990245770496881757729073855
93186119886606461675821176019058790
736022643642381151631775395624417305898
207163733265920453264009144463336555639
402028293273497613771235346238117671543
220969528255882594057173880995365171622
6912388783884978773693901137748618247354
7079298890535572751519364277235050183515
4666362837607779232661716768732539742756
499767736443793595542131401146246235469
24732796525178231482355375468478273714
6679352656282022674513456971110801339
191650516606363015281868172956231923298
13506075964751635467427184664251579662
20656026356526350603460952948421321712
15888636678587220496349136387276582438
781654708638305345835168870244778149
1764304165842051918816746343603044228641
8202233312058025698198703513283520157725
166699007698593623580883039229574028795
843561898877192951298378815461116763724
789714531376147774746242762741737814558
5705175247108687174021182987203420476043
166321279037625459383556217579658319686
787546784207337702969799603931019365427
8716188582161543701237343789187145757660
1751672581531728683370444784533377537475
2516459648689155508444329661743511636690
1110480691405088218339267431475719644752
886563347828929953374692596530146995183
8617906370702148702542986220625269533389
9850228384324092375967089185270192201209
1823445361765151626414209099933090818862
5124719723056937291511806050881521517410
28023369439231156029665171655393256493
145415953217801595096398810824081311468
8493464971163766873272305643760457974979
5936664076095797279969284211345591377384
5765637168878107134882139716839971069818
187512165780836223656088828125725447241
818467668404390651287393560926434824206
650691283307312144014683931386327723268
52174002522477964044665897642546815219
643869263284978098494649809710164653549
465454663137864914944444893031385628
142609242172440750487866979252535243871
619452249944836610225096411586074557453
982338167545091413076465131385755794224
776612819720558426876552720541170589393
529941952881595689141212827493354808275
7408642482998274973674272980521548184765
30554912311512843732001640785752163403
1151334431444212312552378135636493327104
808393548922169477535944765158543656588
136846756094559490068316168498472317394
5986443250674074186369293414823687238429
278574489748562257259266950715956206059
366634797545397716083439535118131247642
5485213693832383172613446441466485054
7114550571016720463956648688421636682602
105290156319798029032077632276564676547
28212532295898199092997513361855216397
249095043510228424572361509144503071208
7538836152912364397588783146880714047744
910656968036504752042718908972522682678
4225451538083624487789837263522139064130
8520652446808364257281204137553294200
85242430982122611601478271127450716750
9897531632775740418351066949219821173663
4224477258226278543712836792449323476100
589780842353290905842047377726654004961
2903237265937911585820104495275076865675
3439909626587581766030085177438451769765
3575818744164784667965979481199566032667
578419977466533249594981866192898019390
166477124914697451825379218890828402701
95955455497931749238587475416812577558
4476955549107281735010223152131635538116
194983843584844555644580128363997695623
796996790691387405834926174133998723585
328561559851739902410879776433033852926
34583106132126992285585496442009428570
7717373435888402358159458369451324616585
715094018701171716515570135276041963569
469932057007239722107868576885692657230
371054282805475241725609437712397781240
4783780642066057655753508743867376464609
870714984939802110262506562225071574617
69665980435291514960395404010278493794
1863380999069170136361944538912535802053
5733373265353611892959757014792471572029
47681023364042797919742794939129274648
12621838115588276473731043711378455829
608806795951443515087732088531568453
97861040935194849065528188938731152123
514622033988574322675978804120981935252
5262116762733083768574188042648645436666
3573930666506047671654695614844577584097
599758123626374646972177143757039598878
196049865018611911624136211340260713439
6012984540667243720070939006574416282652
37143233243851101488944376294500698013
4801689865395744168124631689235887584527
717840258645977580477523483694892933832
403179326997369792682826102276046264695
684656931299930465816157051624088565405
343453060129343999544186694992052298378
46354812773964594014868201679268804280
8521809366431526828947026483735792095540
2350462241883974997824751021860732624738
3615379423663320693176695415104082851097
705691371514400990049951473678263009337
7785246148355917249120189489918662972
4875164621086018199398838039171565535945
64058449017844688350757136457775098171
6355629551027523489450996404847693937093
3094985991353377121040328200125316402050
38359611495603780263245107872431839986
357583951974803758563740715238745410614
15296322899790191907146043859966642276
110539731704603131489928994600257416930
611872956814542523607952998355190516232
7007804814136864620682551009811513237499
9663597273351004426487193839354391196949
8798766337716590986545735825944859299442
9071469367707022591252546724840794226839
541540348716402267765014309168398397019
1821809367498500331768868785359779638458
47667268827925376670131789355939777626
929362521628477383936665079625366521996
28053897723634554241656632096929992571
485916339524696450384621445981427702819
3946426958037678226087665298333034932888
3240509635589115865157101449977037172731
850040513682996451074824627866349534149
3613783639362493666234873646481170768
7800191427091483502068528710765327849401
2393135416752116714693675930831466539922
129444377693552382450223797832966946
53159137871263998307427524967527257360
4659223043805957885352197604692937725862
279397929066967627998094053204678709067
251438319189869860615363928668487076749
5477241318477596508026743327760378249375
629788767736082493694188846792739115
185525159974803278152217578742951126320
18975387509852281632647298792779481919
989244270729925976157944230104354695106
4318907597536191194929839605390749188082
906024492004847013015454718529994769479
757198677625956146438625520989865288309
1443539924471637600047555691412492344986
6956827321599792405203317494697406648
291863115629694221202460112400591284155
8424776091335534696715824869816852315
99347951032592784067983148693660592966
339115942332199361198522574914673589761
7060381519971939616691985657431579713196
65391453881179889042172999495289793299
520538518901185192812843278749996224721
2454598051274118918576761332237386615842
9298388461992269375534092675362410119413
1933980490674853501652489116809572781024
213187479661054770970913574801612624745
42293469985777491635145159129324458899
639992933466380591316152980845424712415
909588114467351648286922993321265131778
861638388857710357074672381276355626970
9676592146864635217615681774857212664006
620387817841302660556326383188056599137
7462235187205348564483607613675981372683
214492219802976863851493557880819741820
835140975840846632293807721276775389425
9122657488599664720449826112889260853205
3976458178104502875531835854527685667382
6400147730476307118417765772622893904117
124631532982914314036704623210327664144
288579072930348136189353235291276050311
63684491876023318545121014087364687122
121174933065607569590332445287215598054
993464963779845297825002543121352504293
9192272385054587495869146266613790577896
1723290582839485845411184984694350334810
8950555223576992744289556787644542168529
126880599238458118093159874386576429529
451721298205571869285791977857676180218
73512627573384446537297157436466961225
82402335764441199680477861441373407070
9050510037288320212573085937442555793015
1611633187822414940953763794485990416997
1640693592939960834729134898325832465893
691476227597337390061555452438257709729
91959718850210947739851414625631073635
147056467688640885930242012950559264521
5439695030364171476884936695650559534035
1502796032867354113083466177925597657
7002641047807481743414658498160256582734
16578167072987529887952733305628937999
1884546755311850158466518643519099429508
3212891099006853795787552955838320466482
526784016461856996258565320511894433
5190641996152367178987352328272047921
62062244951052025567737840328432135926
56687828036587959526329216361167521334
656216621555514913775618579836222295983
46065806100362220522895953499563947993
2231507791027906597679913871188143883933
30458906774481413950833074362011336141
823655294721542234564632827852208152
7782516731696620076763729983353414576
8090324836407104321568417884119567369822
4706943967141199249728975765621125887263
2277651183796189810799449400669864127466
94562945162876372716043697426281058357
353743628799589571816371481968696664715
4849504647884568680970606613629411344993
7035399425893238263693899143594884302771
3253537433114554176132685346678141476227
600568826955281554534213033465996762613
6947428652627643593964372214428482515599
7361534991302438300512542171768518058933
50658451409913789814515197599450595775
828880608495454123443895180799974938061
1933199587026448560376668317197760934557
9366341815849793203283974820745242962715
21146984841655771370804658810892304056
9632743313272544720728423898335093592937
5090501859303921362932982796417617822664
3716218914842762469742118227453952833182
312886790468121196341777577612279421913
1958721864266411907122398465886015752428
639430684806316324580193559364083127
79218077342988681876589724756814528028
3756989670618792392075056456911819285052
9058362423162066771171545355178235736788
1426129927761994223204730159226299055
3985184422028967268281256557890873652193
41077400216866726453526742923215596362
176531651507350855617329214768398906573
3521670979470802597096838383943609742
2978552529934061836015491802640053936994
712725480314073704289905784558856553686
7947166351193709255377082541256775425068
5049370256211596761515810949188083469
608389656177411229133992292974216412217
26934822809863084027785657023837321
4892416664141933850323653220560997962432
4654125454391919919092731187946861054017
588650039632700449143711710463358143592
7587848836496193296373349549403595838249
16039221208275047419130639058268091767
469167787876685742161615730161450707851
2217207655618056220515311725396159637139
22078220516694888802866900470965693723
209920496463142497349572880611027614486
551295936323567027486697505140242879459
2753337352089684855094172945663812356102
4636269413568828682222121030825743893294
1146314632134225870746049382576971429342
866922236229305854984853814934433812391
113777218176257210175915429237070203031
8733739740848439332058553337814073888638
7179760672586283517490219393567570057868
2137942161834673374718833494160738695
637147754314434231082141986729975215240
89782537493820752163978742686622944530
80985842331244079022867090391471670381
9305924872738162334667502908140739862111
8679217888636573995543366550188462404017
2582862158799302987329533140501280371626
6196562729081896351288737296819898504565
5508027338377938453937009246975318433
92164149396647023674254167989746335383
1292110850711119603029885839227312102345
9413561476415073224444513722160480185233
3979445683444403208321229337915526867955
733490713253655989182045421436640081002
3327432671106957162562985302708178746782
68899633576238791421320311119548514161
7552840397070585635404834958493373033
13427820963755852972917881696138251958
4569266635988187204566377143502712502
70447880640389524850646806434361629633
850074668298192093535989144081374579
758154231416190355899745454543571053180
9352231071206684110064404538768487186262
9253772975172693230313099725509565
1690903592258222878333203404586290874212
6432902397448232956836763221332126456776
29916030293877798274413500028444045817
88827885945923753189658614904570588141
291473649153720979501414903425307258850
7408630538286296811244902890922841929
59148269157223066145192996833527200316
35528780587777163036056577815377709604
3104889119813107210839321491194125381890
3898936545635954113896663791769592688784
782658373457416492180469943577673814768
5949803993891880448910041664790476907072
421154793267761427498284299027009875790
902162219710246086163832642112745819186
3940812021455139133144019393887127742117
490683233988590038589675657191235499
362676287540355398531412055889619108101
5821171254584781891137803232915016329308
2282783748435035491851058796236934274866
9684449126511472720174656162332531154698
3908307739922675153193458705102136813568
186674749781152948362085670106332757507
1404325846336426471326991041483315283178
7708236227488981160783548285897399302707
177699045429363934974885504379948674568
1884886066831167721586584319643851530
3949775779992498410212768894412862019
480493389267029872918709238706932719096
9000934511475919679018986393840549501594
960565652783752827272923785489007921357
690796115816121294615266379710750107256
988213712232373993537468386814096976225
50194154015868820717598553440826697963
4062093178180983098135755732586047853
4332242083193985512630157123217021921509
545617793002239486679829361622240092668
6717674416622076760561557126938212062223
6873763685206628639176584143692578902766
53502179703033223016442215516931579639
135621169743699408832583103188864412759
3055221239923577574140079550850757394941
961592077898195472917977312461414236619
5218227857138297746858612953911933075313
7286656533740997859426466817082907349
766065534052698555288963352182241152075
4448174328666623850382372391239583433761
4039416329262988941281519246211462127
380972353323963848122569350817699853556
497479048926915162046610492855453425301
7290782714042625208468002049118031918691
1908913739618871388132514783129263842592
3123441581744121554699626260402139793027
663512359574871851648525182935912939550
487421236383131118052068048506075142993
329535287828550699676457381428143612694
63697794199247470335759138890639839538
29141301794782642693086528228956145476
7078363716906543981728693672143871542078
1346120119522228447398145330529087480
7969352143345021830711007485173899235305
1632106367239014190259885175767382062890
7106582759618062849239828101619923433767
396090746364234359737711571373144808
360884285624433992522379032941953787939
46551807118278274634695493421448307402
2491850723147595108595971926442778695487
27233299811899005647715335152130888164
9013718813727462608486787070923228669540
3719875997975191565231534923387310553730
41143502445189638486484054984243971011
646523588856135190547158956170144203422
58574782799125253331917407896898021
9062226667533591533470907315898224893240
32510858204365573928737370297485489357
5695947670232994194860184284913476872498
934183597501863869663922410789727095899
774525081214790568019796870708780133596
29228717447241337507370328154492083254
958155448188268213061777959649837463729
542226628681283225612952100918746122435
924669816787865836193732808555354182965
27892064222666684342703271603037854
8940374184254969543043907740597799879407
2369136569534643074478195429885884808
628271394243164211163071801903397459
3379834438477328804551684755661639521339
71715076246064816399768719529452974106
39476641388441031918774175172321645632
6059956303170451985515247883036484951
7085262710198609451586381470996883183338
9182438465534394569312820966861194538
2251453231614479344317253375423191949944
286389174615037130783803935920387805832
8116854220054320487261056886910566785222
70627116473485253367367117549582153469
8696611569065207630728968448186256646110
6763439692056487807714499868481036168745
9469320329789498844255975992696913917868
1176642038216152418975813655763230027459
5765490713773717511324725550112281732
7415584960513420417226185406132187247024
188217651278333570397007341706875074771
37413954586274107306253347020192851951
924395048709395268075589654409126173382
277411437693212143288047821382845723717
601776088133261789430044528807620252468
598285539182369152176164484239042876
2392423283240328224398224883697212832
17174438572619047785316310826563156
969286778384965181533678544928971216830
470947837143711341144537817534141015149
304194609048588480109937890773077254672
80263867655166534198934058239369675571
272392239737878853294405476114952675015
2484130688312256417900678371794234712
774581283234968175374005551736464127125
5665438066612951576931308468192179077384
7201211746945372464169353820904813161447
36884035490111031923211534284667422106
783641612079347568342718416233268078815
5830535079226858663563954317290240896719
442262531559028549075477135129849789218
3677175328785777134155309977167775226
7117421742937728380466087514790378124136
688414636080874297137516422745066692044
6555308492471347199039727358427562914426
883176578827736020135245333198093747940
9201369075382759301064227720353676086900
792790227907381116673029108569851998335
31233689404307258054041524956914276235
18652307458013236331994450221733807736
30555559486763186669866914175889245122
7699903918578904109081035075931687433394
2645180961583037795714368403817235207218
26945503888135469597571163712753359779
293341480472485544063153993858467935712
12119586202359541936468121865164854230
2456870258614554587642147792716276731525
9226653842721046388092634524734879744
982688564303768622922051770899688671034
9858716757782455179557389967414944251757
30763016918240724728472455357383893239
594457220262214607680077948408289292788
222677538091720021618656069840713792869
966788363235305639285989139629326537417
7220418699673482296990899437839835695050
549395515609927063995049922028637476118
9618883785241386065825792169637252720
93276265510444710096294837459130924848
7822569511743055432988875641218970316032
78825516308690946315414264495188327789
92267567954735879796658532417220569247
688775451800203563008286765380634995622
52003306379356197255725969207332794340
930231978963570913032289838233148845890
541398414387361933517716685924910195414
451652186409142193298401986223540456245
870054659919830802093771072315622424060
77237537351351415617921389925365186431
6881564654591419239996892482904512814130
77221451158990098702833470641374633371
9531417149869661425153327431746597676998
4361398591296945476320989262944658253685
6979862553382215635551894638094195197
129923603946604378707012688318729465065
183871179509853541471388397848528749
20863732923828097725692482618841872509
597185969009790369167268010918139384
4614503171529954672979567927491536731821
841192502279678587052453823853893622770
38844755276225076015416497123041984570
216149541221870097982532446080246048704
1791570514897436197519897387122589707514
2576246757619702871262697915310737042738
3416970142408646741871385480825896577863
419396737894115384464669604669936685826
38606427764342091933917429760673728492
490303951224194834053446974585973081736
805053353031126529021542091557541314912
4149665621619616901797703453410190947405
7122556324145235443266316615257257671163
760975511897279104407294127138201547
108023301287436315192956931044327266094
206278311281956632272874112952160211161
6925134929458210607729425078329697303240
940125464849254453519595424321808692
6094191757326456817485522259431574353746
9416152416209292710595474429222425148418
2809155771514412548292524758572473761645
5935990610178980449430145361922568938849
913446552528587737688476375659208082956
41735399903290284153587573946647302531
259890273512240440979291642094655541106
59149425959557133254229137485741968617
920061399740963179702416556888231636075
6801888983906337104884604452646047891770
1025903220688414464089542031391173785605
7288446398861488817387221736536396071651
81244401586935499274926665211643389070
575122967272718454949023652351142928142
462629124340377294666444747512599848414
3501150189656172560321733008637556727509
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]