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
9334195608777235326241894761582319322
7912169447684596439437731607903650605692
935666711649116168798529134928465615157
4827199867687493335539622733249558461
761292223867472583659470741831392395435
5157582017118801711555069914603680918260
24179264797418552351488879438272267294
219946428400168999797146817822362175780
6429991331539531747285045340758279928989
227864672000978247034243380767969857630
8639929840588568897746918209612148405443
51674992452037524318319979522903882194
82634929936054313522471330211451591914
40239893792081428183863857920862621596
614235528363684925615486326640426065980
594658598363079861710979383916809436
3094451965119223674564142036200572206420
4216483779554893503946442089305998252135
710949236883962538885517345663247192562
564838558691152831868175608888186045293
45813783393280743912154640876657508889
9535867473478715220056519581093208101
3693555406545471926522735367652445286
7856225760475374269294795086091796023
74445345995964761786644750969860111244
8123726593355337760270733404193436492926
7051493281184897782598589050616427253135
51635345136696408742519354150827580362
1665172335418509130513174013847675373414
7289799333083936395158840791341599647
97522468903507130023543042749145817076
318939532557930118533202227738987578987
278837788183701646476834127288668997852
311737262940179857494395391536978876575
505869341725700044045379279191322787362
973541742977210068757448442514098052640
2296823451017704322797109079685479964671
994310179741871535043114418956160837863
84544928261385802348224909267598756988
642749456532987482487382560649383397036
7295306121354095839592629674392231984101
6909497850467044967422355401231996843680
99114719993643669366353425538747058603
9983361412475325377476848121168802867
6935745827027567909616002064322926465436
8450812996473779579295447727457352583159
3209754775821738461089826031907070362569
523493634283811772125156802691552966778
459617428015749386031468594440443885478
758326578989858903172454629304213083926
3304545125663100417134384553806812888051
2084362066978305555295548778286444305295
7617212255445279429075255935778396199534
88974505265624799161934148377336101794
974746037066508458752710923882623435561
30936577355667878067258525423915256659
128592149701904783837390477147111624104
5346447028178322821529761321151912517038
845124419484585478537607349989796998210
581628734448194732641225862817255235
89742702812704363918747412591629105613
21281420163407456026292824915225682580
201839435183808272222971097137528081193
79402381317621235633423480342967685247
684339955678571899444558173435529059645
901559416655166638680596211203590096570
410436106176470972184012761894537148733
7157652096644100319975173446300599465324
22811284699886990639361894055773748910
8876594689956679573235145404727678163998
6048756160289644776795073972164777345082
13118861974070434431723886316816179661
18734257796215944623216502166764367446
6786176548085178430380493317469333281362
267229214826303341681333310251319992693
559099164199865137169895377470593357101
823397775349424194944495640334338467890
945248729187690532549627887928821332124
782647614042386523293189933596824998791
9182309878603013136248501615237996612321
19380638598206286719011523110323458756
992964778660724370549539090954181979601
73198085870480434446022510164158177608
4205403181071089319990631874698112742137
3230651135788050941158124963819134442140
396164852723638859311052331593584669883
819581751943135059046877393262147166238
15572202972205185219001328962391791705
7427323942638633796028157263740118939036
29068903538796739012500246319437871852
102120972907067422378982141660542641889
3745948928085991574566676798609595127807
116344628067433378134889742624666301518
624023737427230204560241045890984549517
1252289014466447223463664962823493867068
4829592221879482686911262067947826639610
9697806712334013344381594060200245142446
5181674050782576631788573323184267337078
270112942001343646755955584728146045272
445196134643681402114315962511591266319
64755275748771898362827988326098601214
1209255472071931733721331024538437061037
546551015039906828711839465141726044731
8909838614258593530878405076858344139394
1321509863012458565446164005957761928412
947241591790198688175644159564216641838
999031289454146756995809120760229719970
85819406957814898326741965462552522474
7006273273282894240955591876181439484013
22970717871497606779595761395337863646
910352096379779544057916926613994946309
162834263691244374542837896729194304199
28123883722523893868112873369215894570
27007924372573668985152823634658640
9655779388432807524999765683968319218471
99564847157997841806276706697985891093
38268732063979539518910549699845749874
482638912743234656424339776372091931530
9251492283317132709580163615640592478926
90947451585863357532651638731382873421
596260077385789902266284052741978224499
8343124559407906620315359937706297271231
433795916788199939935180697440409108425
3040785099919951298836522567734912041831
258110209318619718305289942037036541421
582386372973492538052492977648525075671
26169137628073263678510264795938012473
8996199639571710630168245570979185623233
7231620345414879319140364066652552009
2594187131702667418549344806443613489104
795978682363721176052224513287943028502
49156466662820540884458115648269398857
2769251629212246447718067023872127318040
657538683078413334371364468707034227897
70081538586492651869555303608975181432
426313970949850659382736352446922401818
531888696618142790092519222717065549130
5738519195096101188481229825862441805916
701811846316269971998480818647963017875
3403946090466037170385742558891689559723
3894912675677965188088077593878920294246
360216174754852304387369125754991192300
9040892352815752721580176622460896996916
97955635139576571049543372511687734
27363557628791606634114210999908790781
8104190966387757478117848456660197115835
7127160326974755265531678841415199592318
40168387592477367729617141468199383913
62874410421961829067857106779662755474
61858099463918118711050941911714994
797417177896404139084381512199639283552
9301214581121169952534685003184581445480
8790928729166597484551263894182737286144
4827865925014756743413946040398896812909
52974295015679786146799921912839991767
2721408661488976779129603017688070701779
197694129263161356010404922830926722140
5432421525145624756694862169942959649045
877012353186931724252887490922718728
1981235177469010663861516491924279857994
16801686818483797316877808855458911827
146998757767881636577538475901931871967
9627564470263353888037578018902032793231
69105496994332441942611956228209339949
1412132762212793108788614718493772865455
764922097847508820471926123983410021311
9565910612281736287021307946703731385
7786660297208663380447948731188967549598
92484427924463606044458105307299529337
3088494965922312682896035232185970188166
7955137883797872216972737587973565423345
17879154923432063475947517740165023458
355310401062462171842586160119116513228
3559335401798285867799806245723738405
17337460132801110164792401124360311656
5612583516896035487457576487626945297643
281074563093466965186630890578374831289
7776776157223781709222773060987157386828
86104211978465156295576167849063164607
7371215826866207664472931281206441779256
37374114305190086659197534153209723416
8104455896905393816282502431448184695144
974382642783167398867279692464781799677
333335791376769174452426091318355839206
737254285978447471902829988466425645542
65003730856854225045313537135388288113
2304670871246031561795993217790785334167
454711103611830676843288465490634701953
28097187528560632786885588980145865937
88665510963796217663047116835791577530
22924951700031155054735333675533428761
9121714033142522199937231877525736503362
834025987340691627719165844442973971268
948394814983397690988686433689448376931
2698350929131509861938913866434294175776
5050240670992358942330264909383813812298
844653104131083146360258884717150363
3318278831204131185071018696797510458935
619529002477729745229851829450477615807
84566636071259244822352650529248307416
5829702373982408384536242825913584101185
247850634562848798427441863508756499
478964138911486491275613536452590284344
706340841520595494727346139447739792622
2511178050933296271813898112494591473043
4648692833724265258976422260112043434034
94992571543183855449835727478255916
39399017766967220518073311570258602072
79666686225181936107196628907629305571
233391935478542254824899868932572428825
981576895223232651126591806421724895072
2427826024040300380015922854774468369
501540689582569831743933967299842836
6643501466729421221713802208328682789171
9086536366814606998274953360532483357491
159622101685611398439490171055217428118
62095115503057450574983448934057462529
4507377352543121556374141800299636781135
29673789526565568684043854996314587570
1083892776294011686132319204424999712190
9997278312985138333970207763193474094984
249361455184111358519821154975861275828
325977296244573990964979086774767841532
8589625517618325101684729392391127501
314445895467543399744929639805490725149
337430907277181310035662355261191942009
4719553658899546211713894402559688349
155773958314113289547993657287912563572
527276787008946849667403468994139239171
6992948921264309940886119322452457655575
30552701320809943703302946510149269806
7538710074226254795139666629204610347619
235328062126154538053756320403189622248
992346158010340071372727715154354354151
80479202449934830665410775438962657334
41641818743951884402792489044565684447
470413117527352735764741700332242391469
25352322440616846994597642733338848149
722444719635401969211259256210236863644
2209221984247490540480017083925781895102
742949162805660880213831390702263966632
546660575898208953579068235149511164107
39132656328642777985811975112772169348
2459649837134978850318006502577221933664
695070281693151346609405910360363863203
239991166530395443343731232197894422965
244849654459129990599086968637035703808
220114254461540149878485424455463505760
154038236642297010151807236357181614174
3456466087146357460989793684552838804501
4925374652090854729285417601038363536
9938999179588731200713948109164598137
24568666117318097344916484625929836876
97012075372896321021073481895332725408
6897947147765501497219059313788312685784
9679540548370952367287953431764779310
630582655915566533991622074814525102030
323472989925599914625059995371628968
5261861173174447794687359153116639335624
7652436796946169666269176130482786633949
426780184449244721285173617623815813
7603867772724801230265744121495692447448
6046636523437564241115911124894602597
433729442355794539534904715934552337486
563247771918275832397638609620324034527
325695179739621473655112235401436441412
405220064845427653698348611116833125510
357432362299366228272509113513138055795
2947786287003179894914751139116452617534
27164684230050647454950614191578951351
8406783388595562217218842057369423904099
3316368627246601918415062097669894659158
99185130237554963674887370938827268981
830665089399909449841989229220569472108
3737368372031133414051958883938252335712
7239783331088273437354983361660196324
830249446291846939051376710231038593819
485258298366776712394705806602444984744
887559136453215269077327757263520226858
610843856651601261869311619485423701061
126672916003970692526253485400668317992
5251380330824856172174849133637999178742
517969461732652353148346044369430298913
534359288938182257797437758933775803032
5559961437898955241376118495849890562045
8033164851687443422087635395381465246439
3288194602844125549066246761895065790
458259381914423993651978170086235513968
3076357280862562945887664317302338722268
699173148325626254931916843459281344665
354174181917635745781956641912129462801
491955696676754382641597345399041874539
29331299873461778793508462388742412949
9657717281516760997296311269289221401176
136683827720142233574534091543530023525
411173321538688916263537895691843428381
150717499233544531313288355540396372143
857254925638969712844152965370851303496
7112889617457787217684908742438958807325
3902704615477321153113817697565064219986
66225429582024677947128937726854166339
9503918627379682132192561619751922101740
10933005432661063783410495393463374511
650848294809167777465282329619339087482
866590461574910438333117623426995535958
386188140192148638926297879444158864026
37774547276241683049377375374070874465
90629334925230377139726596836716161359
38042684868621581186423059465931776550
54522432497569129557764660270227155894
35213323482128565531083350398738075736
902958476228190497138261710172313528939
3209134179041563811643178208986336113394
138083718215744966196413756947887019812
931348440378818721734638201589764605844
6118305442998123529471882486589070321614
7903505267993990480418704273287248429156
775034955620387162757388216465541709041
8775724536118760329688843000445786975505
205560593349136918418961345957983620941
5061805362701343114662913300530594459410
791944497836430365714681367475058327551
5950888075642809734461457569698287381364
780419854124569747128958770426312499567
4348984561844803509565184113513928945
81104408122992687995371637885564908327
2014698818513612975216808763216696690
317549254604764198733721487382797197
201435406433717931282972483267867176607
75806174763744555995098917770240387236
128337015932731621154589232811755516925
1936121715158360910410983324129310326897
1290987718722672444131527497166081712196
5678535046268191786979011712857631627392
77686188693290102948282185110833701545
4645174431229005637651768338103864647465
911950396665558292314109973964639635890
637643675860620421928397142837512313851
5560954589169596132837962506404293243014
56801926193627879971762762070655714912
6752945855102262468545120372391424015
4783501559488629603909299529142402698
907258282314352657757802969561641385377
5023606751832374649875409528545444645151
1823161583248467543756769101696153826
625998395527179239504834439289761899845
832056724606957484921621131755559264669
511843834656997728362626478891657652535
81814279777409137257514303149622036149
148129245342782233726029486389165712184
5074584517949475220967805307430053786226
186136786894396360954926554119764433176
75602023037773481540119021816168114810
769683455717705646339558836240251262080
7926356730318198283059128629381723077
960398667726379692783144016357986067658
312171215333589497719204537668893777128
696424775317990039102259885259817775400
251663925529730681713093252312974506732
64376070651630692384349274555403254164
958472717236179167912142310473714521524
1221792749105514379683337598777121707967
345115675018004900220388703480862225184
2290355197308125620846534059219290281244
649885578645442896433102904310593148169
817423156230793347507309829787988680178
435271162588087653468982941279525502584
5145951187605367367162148486217049216967
37541490316022265387497327700460832550
8426222942586866304341597653437811523769
123859747426273972292619994430170023836
93618224053487751348380885433611225288
99019153524557703287871071769177149768
182164723637097256266084560458016937025
987937791052465675921077398386430742301
77962362263385549316278343179874835180
7894128344482758808616059851326567067959
94701294126586929876704845344883041855
496446478002464956129130307882436264242
318081247795208655808244680059794345454
5994752511427622562527861957912542815415
10269009679361232603544808045338724
1282187150365119531923484214886164339540
507353689844435441955828328293327349678
5275597188725244950521155112888653154415
8845681422947065388538538063685297132210
183197274116697825115363616527844631555
8263686545177643950610031847448397533250
711217492476873070117235982199765802021
1591195083310390712684889796443700577
394275562555755526240501845285885211250
10220328175555851881438856133814676304
7085450165859703521945661470861716768040
70972070431163035057411970632923652578
29465985762444417362672914831432068369
833653278121804718574748239945888392301
40708025799703212816636889120676973965
6834386971053577387274342026374849079379
5927923534939597283845653731219411703
81671204739115624988185202924925459371
30251966213478002379943325894842682747
6378335715279937861938433751346761503
77719176650911033152566161911788349654
425331966019820680778323676695405898257
484362374192707657097545684408458347450
417486298742676043483929785265985116351
5649882359295071857287289934831584606515
539763696860875022096526692410478244
92655491970461437514270399537153675477
2596407982068511297588401400756965949113
73405227789608085647253883602449086
8555723048481373351842583363345216022531
7629581625696158047000620789794932349
428374204428693623386551546450968876798
397355109482133331940656223764490898101
4982700211186591852961307911348649346804
5583874395154373465234916911292735843409
912919467246488407992656854258298812859
8170807696295333171264041703981518947165
2385955146197137650851737963545764575195
511556413786694139838389258535027431713
4161666369494919955027909743871795203621
39001863859309251997864214490577263229
8441844956828098378594756669125962476652
1267730323128137326342755367332634501
39373998610821710402339371151546932402
15342364013440181475072759516933851201
8467445701625819395605124794111085279
44851699559937962446132545853361027340
77769404784041798162624936040483153562
99698967847320582465040975833051054375
3035111197821811582058171700877740637467
6036486838446729478326846858324299476345
3621112036649464618611716903187597483202
7003956263529469361321867945200336861694
17972878660777131888933243029879196587
344133431758898850047645532618240862132
60203046933936112353543395856859692225
4518370125581807203658697383549141295445
2548949366465247281648449268616665254041
42453068048236667233176026524433916012
119254233297570752239695453681896872025
484837886256390575863663366468456873093
5397401353393576685223116002407570242157
6810808971423596846472846842377828443774
1414336281619040231818726372632289313543
373459803226579896224395638102779513010
85519691470538243742532599968586142664
119632986504494289769024446223317623076
1512474851252415941584338139303019834639
4034947694007827809675044487312817125437
2732661954925444950348617063114924656465
286567166149509657306464461196285835778
5291599148423293979385366845172241725778
715899342183766993744445838730577773199
6666621785549590832810145062783360492197
873529377755565125746706464562058671439
9411119923786965247684535520913645956629
10073772162911280305168459765711969327
83034799858495612432644383704782547595
318547333817472989174534125969432071463
8276357227805890921766951914846084311485
1089595627524125865835865562369031193889
9921420863288611329757152884772716254056
660256104589846667406517959674455896470
452469993268968493423657719730268573793
69414779493634098341315290362761197540
65577606982614844285087982336638565745
163990668454432358008472745286614136342
12674543392971172930562483462385442155
8693705655043598203091819482704893723104
193889214255143865382628216634149327602
234030684064142439926484518776592158
69355014872309480485614343796495874078
78574481829618169945737803127980626993
354652587546870277534792642311030488871
259194634137696731642335145546250106992
908162015260129437978776210877294229290
2892469055939341713783496319704867555110
325890841586932702683441707580395021329
97086038291010785962053649973423244732
96459953358329971412924047489069598803
7811024913786278621800766311824342130
513351944299654272495526010101144178905
8547545454143932830159096529658333739532
6012520147717605190882182495442553788556
1769855714911029155741494469702617602808
1041447281085952817694005038799743206214
775330716407704968398363316938861962901
80926188224112012281875263328552807898
4878986268346125988050941295247236236468
5001388060315689690165185001616083607338
510318381757263948688734331393639547844
2782665023584525465190838179168295692057
6285956793288047615988075077455741269227
12543322762628477921241727420020288485
409299564605770442043766441842997982972
54657529463412443016941697448574500861
578558117304748288577912186572164533455
1206452167134907811766478458981351816964
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]