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
6742564442624811766075182338657571587355
1963835295428079677741505020949222924996
5593639442644044396944378286431839454797
7868824664305051123725373916371257997857
407328774753982978085042585527867517942
910479079753189076804203129232279241786
4608124674882549321724012851609273182192
557245284494661830185197672288082139907
1352748699765129474571089570398717697294
3580918655429884770438537298234867734049
635310672471896517428831992580267622794
611785315389956157573159626639141107354
8788465737095401809870188084020916776
173871292183590414221165103709290828897
883294483478159553125805003718841028762
6122900285028480665544527704898323302760
7454849664332726626525013189692876236880
3792988674898947115600167117989807737
182196799705181116901176622735802348150
1223145058903119967890891493853679995411
6639529330147975916456028729224355888529
325441134389523873797172733119497658453
7268724818872433959651825677994420
5170980838774404587999746605124987203927
6769992420143000773373306887716717399327
341056012134422153998360610818016849641
248429524018555434814358639367094378696
3977774941316452451714789808489733608286
93496925135731986698008613483462154525
20540976794916373017518941622998932412
977727196699179514068611676512523996168
1733535667671122127966421606795336805269
4820300093831248345825313263097441593
52684463756135114031378993476572873710
811229726968769374347145680790061246419
670798465685723488510404519161574178318
6788178518408246808644418871979921168
1775518456281479482369896568527043301222
115360656507616778299755545393815377507
4713292816442128134196224778680875619
8276499337407689487275556274319205200
1985603184033335643041545418883633068226
3714630771516385302368352763633887231772
21383541408108723765429261844229989394
150987720209738396052195311835192452
6336558552897451131459471915269543572315
1679959140488733108673544146677368213176
832539741045829756158455968345041262332
14661781217146224826161476499933129522
4964596585068295669044584362749088952178
823791535229765886846933298226995994045
680888943472718297055274390826965800
51072914130067672804401707723842069204
9580278189934412623887056532838011915151
149825736008508545604985345832278436217
3619885873738631985173872108923633474942
5774459379898095514250551204251329753
6458477261535347072254442394525751682
406723204288479220651804512878001795620
8715740510514861395148053029775997172083
826193557406950281613535287537331114735
71644418849758245987442426767890437539
321344341635885499343956263470594296373
398315520276706768139849389895686693935
726167891407755767376212879522573775763
430361032677188889977632159170198373381
5479413463307220511127919628164353116503
9596442575322693163928557981712777429248
6522137015391026618417755398984625078115
6453615361388414486789549910108127537481
36633996902846469681783911615461755495
592873831240243012685619986792145292553
10982116905368619723019517143823421817
6537839793652187808071245196678152279203
444310694972574495610426131320061419637
344398170997137908060068893242212567480
3211479584403707143532332233134714093902
19254465013831027317672520564623408626
8373848069183198162374448816666180772329
734558295050227296594222435495846501006
89897554150868263632074559672756942760
474314172383444631741045547671314237196
5808895992362272120727056353650996157643
726848933409122827055235856543687715302
9887964128463362502739997568200827647593
811337668351436160038807662362641982088
3747845459013853748255866741149794659352
6067585433656225155825856846666299673707
439387898937230689593982314761373839715
1468204753812888268046422344248246637231
2793189085505831307835163040626467857023
483575745548864326398714012352419585934
354729606238756077804281575692325135365
18317979711470338238892840139876252417
4562297446709162425555818007773699915315
8071456043147274412617418650129124872018
72032630211097503315575924579038540921
4093271590486286268896564103106631667
522660264353312223056877854622399666424
68243014107380579436853170184524532937
584258433444034504838523173725285181568
839348427681927450584908406614165114258
312065913665122044314621531828831698360
85606118894072358945348526570437843912
9948538550885471471767440312311456497
704532933615900245825936469858781286642
147899632755920625345097037154375554397
61487057335488256170279221156955134339
6552622221817866834968634040202959366485
944669557668228788511839404256628993250
8738309443135643269323407531380541829916
84106511151685123423136652694418187097
82158004198923451298168249585950209718
5126552260605845195347462749682875693175
14599358361728204332530444363431435950
562125658585602668548731246970884261176
171017481359133723683939446272845882916
273698259206325174385767992180472808475
4377148220504849431127955006361084394002
7864975550203919847769232159264492525125
5244639846666451503122811000177425054
1428257841274034826768792651682688913179
4068335275142209223822650262578200
9155420905836636813571698569719061687
38384919964714547646596713759063963886
2539371757767670168695988014261710602205
2169259463371823807595549518911272251616
773046713395338557787554463664690543987
3326482525846958918178665435219787239
5057918961113409208677401755372361903753
964697793036751428231384235815385706059
5089624211637945508330308815134368481241
19447452468508960593493506948023366753
1410694209948498137883706894385845283
5972603447181000436499984968615941909803
3556412115736811219360103611641934044
869683602255149519179657308518896579056
492778461926279820657610671578001910125
7928917885089564199145551218279582287
81696647518559380609047732825289257646
7752728968253298213052633127585297372756
194829346571365913499947180270765127143
79940399544181957383478422843662088
27239414556103678539695527764293491356
292044804324108145243603951360619763577
1604608829914679734994264861958811686358
381317633090647652498814573328143534502
186543399918398895448848500664889315355
394079682883186657787347180783517078311
8938310987904111543923371808792174193887
32461661634645698182439158992560125719
412575533446207799820402449780055742691
7761606623746257665911752289400137613725
4046156824818597798893219787914548582259
7293849750233546235388977005818154488151
4141428571146420809196315041344617633648
5706381153179494314156793670197275399245
149148855558641410090126248691656198602
5562363148203533420096376260970824174813
451929127667888086268764545254126529
3652702231433543177142948920415015331686
9854216040982162449755048150582240641256
1163775850378542999183824729226971920
6599984240219159883481821935465093826836
5472432188482395304964968529914231075331
951412731514314810693797482232433291363
59948765535046303743146744339976321477
29018920112316093644977125848250523632
22898340221412825254323127620340496259
917321241611320316982083284242560833516
1526135754215176315623812833852316462
16089394407740057746850722703367808219
62858233903666395175597283757115981255
967463749509956595936572359925921577609
3944820181674288728975935964703282416203
7447822949605922159943029243903139794916
1581491814581732537019446088901067078235
41375983252589335578917543193055722744
639071891432896514246828804780746804240
7443965567123118563569839781384149991593
98097246784684479568160402745792877571
396744814586216386710488380791422898923
3015279099264860561332919583420712671299
819189151750257318545816474254926707227
907351507159990978089248843397614083846
99436308279526049468478243826275967557
6464950258209706725253208003743232858792
1763617335121679623231631225599222034636
456998572840435449713268309525369132968
21611240596233253041547998458984303585
4527786396691589527473454587568362954946
9648728828415507736220533526132972299
3612152745141823119593543073623296512652
76877736476185915148524652330672619657
629174466607629254253221276548577827013
646358585219029396274545813134883688092
920138236263427674914207754817412709237
509155287091347591246970144896772239665
85776309744732094341004708351382324142
5432183530134188178819150599193632703
6466932849329834247589874478987218316015
84071622887633144018589485144169581693
2804621654582631817183584689535142661253
187628391705745717538166845232597018946
6574187376709896445291008939841495993044
618734191481239460124224596169595529849
19510574158570982952600524972432810434
9392899616502591188399542480836738864145
900854177083775555686731129725499593081
5237467835423354778834851069924108287
4498347667222536559918013198345550149140
41086108408947372077382840724521839028
314490587314554518637472593966249446208
7851362290331769803016686431503698153078
4001542795226157598663453655967839419677
961741407310769157236569287934752334008
273586609347774882183542049650076868871
177789443464758784125402504627081219201
3960293673854634181942199539571049526191
939817740175524168575656626584174795395
779829492613412250749974904932418337438
77212341773599132687581609197358548299
187977176297449329444241327878036541593
8764656079136880173987345523965278409903
7064946861199813928072267584261346046312
796461236835537277261321711185815407819
5889483822101115319942529354131651397521
620215504405725221334569159047982411288
963236177873411504556078278011188462
8804550373241711790878216016313828835362
150947919077226185290942158161477603092
778774712054904075878454447870831467674
850930137447810637765298006379258335
572667095775997399109524110466993785341
44214690500093354649401229394880168306
31504844704397597260182626423728269553
5803697825661726204579894023396476695216
3816609189838770654182374591848247641927
701787779402663310821973930546872793014
78878315470188414867836432361074904845
66824588826255891935373040902827935769
6417603396629256379093795545715815682808
33562928429270241396681022185897891842
752686142402836657775199653358799828049
576750999991371421443988182712817423959
36196435010642630688711228848605389495
4851904348056010953685447398495180566097
39544247282120969838360131213162928178
4071498988266113782322411469975674321264
12717583572911843389627195203768673130
4972385394149433716052468395646512799
40136886743432878232065583452883941432
368238938162770742721664096655211431490
1105324290633390530043030142272366018
149044915658996246013021289726610976561
47611489815468912565274175782544949054
7558878752899035551853036636485729572654
5031308094101950682959597348972444991
42027912973639341979268120190490264256
3348373850004369767773288430837345627764
8856637222044757765830158005834313392195
86569958179572011948765679866899488854
8388477029701790417152982314109413605001
6476356490261646974662976125330464239796
331411839283146925786315110389687571356
565324016493117375188484516173998611522
542456491463646326773822189652734778578
2482962270951259168915958139158757236021
7540901529867596609372412834521470763062
179924245489131181071522675781788762888
571890648817625430993469968983798880824
7799290260599775377457148744368533144813
4813665517179950164375082554997951573
6114810210263517152927244049418779816714
107541253117546765088566897945086368700
64946329617799555926066391321096114018
219214314921543709965376960544845839
197523782298078905237974528388515402946
406346216321437596519457080379197748519
4507751547557556498053803699925948323199
339891655918269340373727973331012422787
767667571427025199911662189678313447365
429153163964242775733604719572795651219
7233409984166851755196974014536592167016
2199116349215170391233468496567825856312
788126008998565493858519559394703798973
4654285813521674440744928458775090984926
37793573453987094629317238426219694338
274785569750632720574805847713323429039
20393945319972939892369471309835123622
43685979531640590577751916316578952049
482570076686615662524459183298120824651
876878488288590368253203423699926131703
663056345901928126285235631776063008687
5024742871271828809393905238490154217437
798349404094548172572166111269579636057
5123180147142852758127597617353188784153
811893079396349974037832604176396032469
8074750089827802293629233778819181116528
9091870654011459936458514217773471085686
633261557108525493812137641361755044533
7855302378815616956276956119594862997740
392636028183375340246837104587447736629
53055388195462684413947105692377780797
577766974843645477450010401539282261
563661983656229755989021296479261112579
8609145473194179514975175607192921296815
586124922379691052652829406862408482765
5355172938156102566179511288721636564544
6218051908161564579981925204228112391
487078141151409578636301398720136223059
58984657800827199055972587722251161945
272064541433580986062732405726144444695
48445327898890442287795837701434036322
726028165757807411041861348626433725994
436098519627424820253710418709726353884
2931950867053464745159818254515133747302
999640265990400558739355251918765366705
96228072530949593164698634667204870819
879258026675285898061739499756648875055
897156836102327258985983452388427352402
7533115379655120152392571408776070278238
7926927239668351239737339702655250242495
8581425505496510905311872926895817902
5937690495324097635687888622922931307207
908385477149771271224764363828889738149
9419158052277355741444057227104332794620
6107362535955238258249988338284923737726
171090348333220353337407200802563914128
3021303050213176594040086718478560569314
4436242980608249785771158339153024948211
98356182767230161018553929058726181616
231784273124480024049140117917178482415
193630547822950987969714003322138269701
902484014954225441763783198225389569898
4388591753748464145867647464308044992098
2452674222392902431583528987812461796420
1724134838598679213698963938858259094289
125258081552317604743464325513720622897
178174746740505723622864412155303504446
67726489149998818689832376410415658097
6241713911631683565423538796979082294469
68901315119595542191647597729843509488
548083683864140537107994226924545731645
5763543424129234511550259470304156027147
753832824583697055583767962642646948
5978562554329785748783949260255145163933
33918878633460365077459138437129231863
4076432399826061255690659453440174464149
57593597171636816070445420860897648189
8093323255794950598540055781914027671642
6535747428496572414324861723351336914274
790528648178916349334229672996037379601
447061195868200058736505108524333112330
9219502454484725124779672943649629779231
767373908932730705347602291737561493018
67599387329411636138527735798723985153
558335929701932794342446238390062257988
801414196742464278296136360483411628492
5849756982863527369291708037624734889351
74523155316827344988264521238548377853
396774156751214605040714195111193007250
4316730587657255401356101888215447535603
450531122415671642470251351748373364832
3645507876736741113774986753595074444
478944071964292570686521646349456062252
502724855296225417443773957620263512302
70607994676846286972507303979830698199
8298441843669365713844266712708381659516
907313865150494219306789374538078327735
493422221341705116358096040444350047995
501214922694946221304366514944067002680
3410525039795536224199674636904729348110
362645331167967982283799140661115824466
322033474657581761496012535550580924431
248863632307859249053481565537332149038
1078786680067613882480947093699535468858
97212843825648234212577199050759617992
733040661592525351424068158583544432636
12892021513637430126445784847612409315
70849492260330472782382765181185352762
830920841128242503618791383766745862048
296748774397862474859548331378159426959
6464906826060516908731921660178315953
923946135946937139683942272932029164438
138699422678091202969835341558577592620
9889811028441282893390969481325437446388
9688549823411557505697085166895189553115
5233434094688218344163054670450523955614
549412387250607931322588297762718055178
2768725373415757431944642269532903246
2498326481835749449353477079784041524787
1968311688861220691115181063173094014432
23478520255142955762912566652134478029
56789562172567820717236829513043048585
41551514539929131426614069897435755810
9527460654307136588672854329738088438021
9343828031058366174719496187696023142824
10022952851395588865911598364415953801
3155399851184986196415687411470055958092
606697743597830280859735263448269446765
530492821744857890196834869748275282432
8097507835624225881173658026111123875385
8481457920216721680218871577770336283709
385175198617150944528825214965986925404
7440891578969872144243797632824987563
703010813127648060506652999629887825874
507801165413185249462193259103903711
562575694494464096629994970449771625475
30135191923122027085226416062537861973
7397267380557980591728644556915261322571
1498206886301180775672511549772724905
938822222099685481527230608262260913
7088698248017549711647159544576320164653
1396607854837012114550012726253421745509
3240746057489297725065017849474478004889
5686427772446700708665169716345999765913
6380747226075919775795857916506143142824
197525353518055229226992112911085366310
957584819892471198256691242988159096068
6858278798361059763923895937421783859609
3290313891341054903029319945156046271773
71782791944947570614261410116652991300
136274911904974760663250196143450967225
3734180737607928935031132836220344723
1589380416745137322598806328207912885450
52382403668612073617136157662215422368
629873177087672311713155130846995829760
77804009773977388940486393750415393435
371849385569622204576188128871215331985
27297733927387504953574419462604708997
2094512486655884206023555269910928186113
102816226790590391105169781229136873527
3147822221649614420571348903557936487125
704734939994702431881133611383863297
3571846748228033779127738039235554825934
310184869149117887196916012334528418546
54029565588376673023651130453099017229
945954318119521762981008900947098453
7075516777198112538893218807254330332191
964467342791146396377239781525473608
860049936192845359975992644184331156368
9307180027023605444476109308491146074403
4565739033258379769101362373793062778
8136316865454937126915976367495662533879
617587779067201132647771478095054211414
3731714739474239441576471375264516309280
4050923620914701786971824698850534028
227780469134353388501282218227268199952
3691575712617632967654349607515075594905
2205101410082287323681304832516350369720
850368881508949648013534410230850295428
6814200011338368819124446022264783351305
979284935024847989072359329340639858969
162658612077909878019543374924024825784
34644187807384527683361565484786514942
3470811319793988961159815498574599041610
72303793492764985699030950835035297121
7828524047279949327140205557630469946482
916747080364339900444709101990448352566
58746754745651348017674848162854150473
5664990546199776203146044226889763497901
53545288958160532129630640268098815929
8283785342694582949041922798114156713177
874484891428157886253258745389646177490
939881724168672506688317619169486599183
90255285726462765428561949382253259399
7107666322316686964473714688829342481137
4183630539807867969782948746773021883876
894750514837512352558218344134966222719
7330652959283841877448462981157024850
45461934304710711877894276888136937964
3980221890118939222320756548831052297
82043820459691524068733762619617306971
957269449896610388557525727611714309140
3062516520635106160538268270451144543
609826994961757477776170236036754329311
623078092319102702516188553314478419267
33741681049380510695264171877947334340
986315043987783161612349022808187763225
65668855201139251745329234362493494734
25094254902546681347688563151167190408
266881893152997110131304472395426051339
886662017306209352302623249118564117819
232640248836253429955914360275211277115
809650531755848493982282572118130769585
6086327466678200165646153179397529403091
416618265191522768891534816979537988691
89784759638938807343058215459184652315
82024685217759499936360122090666147017
7023389139725504088787571898267212289
96893355337143144394474604264931365819
890877145332252015984786790738785591259
657818727746976113018648053570247914195
79043033248421267336193947203045399
3629771939492480545157009632840671273536
8314912859787269853395244930540365144703
93562988776167367866795693265991372115
6713736415442657189324846847211835390
88721123546295823094517755852524611123
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]