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
3208616726937167177438739586782542663545
872034882744160810699842448482492398868
22673101936554102872847582070621064311
647166824157142341223504926567057305825
319973292408670698453326939960390311671
40395160897613314385546313117325773949
6502223601098685827761344710747666007
308466266268128810001066729485743856363
9867105018737228495258687767435634523770
1324710131825888941399542768458718213040
5645840250033023341050286690347089652443
88381412104204629847789811996419509363
164329909153442483989539511671926437123
6145605355429867336120986468115595656247
61284587705600190695486543580619335862
1401108754009413973934522805310835551043
334594362173326855499827319144705498024
8494604782637947692737992650887531349154
3482780037104824217199804059568414744021
4877398035719298886271918591349447418706
2687771528545670227130705917311632947961
4143204316485570819293792957148944916400
44458685136967612086925591605947828373
512859412253225814869511798835896094485
535416414434579167516811629283192697352
7576531484091378915249567737514110348185
636275178820909312758799943719599512732
3470486836965190652460009645105177783951
55419778358423170439315297616262312449
116514345017411622084252254571674771
693168578038442054285743680718413588223
55283918794512533213203133667451428174
648826638994906333517538554875852581910
2806528198776990547453654386803363393
5411599265284406491796564327453012338840
87785604507632756671386888118360025422
481852831598334894816449146225423091077
19688596743884962044299878475279195816
30788762158897315557108190121805774171
3567939416045999124462847079350828252938
5228312594125391820994838550689615020
229666078281755793134172856816117618
5862659956821929611216462274284960127522
5033330136973750283413007397679674817930
3903700997806352558998207212417164784581
4743270896152837883457496053897496462621
60869798934640679671927394644455427312
8672698168704679467746326435871642781059
5629802689385803654918125837548180874436
121589076585556016313746604091753082979
74302559788475205185203465328345671605
6554719814584894992754369675626826546
252859665102241316636091000894113833559
1895440758404685802899127430920368501620
8684751045151701783644219676445993738
9829596959702171326016196133598064257908
148383679547484048733879644870114517612
64096491441731335002722238526423273570
3576469833839156427761411963493638500
2546630692951609463848096389163987452
272732352873279377233560102370426202
682397302307714997761230485219967786760
9810502729358382729581164455198738305438
4699626161123352447023168231926223411043
38649145756912865380888610576003028871
704465476322599286359554282147315647795
204831993723165534769667370516937272321
20453222511492842811828542136737934162
173565663327318621725281950907615958516
269168863979169672991235538199288212788
456269193611530729170552907196157638923
2938124690217674510661807340526515957076
916630447626565463414029245238746182905
7163290622634424181112675004140316481357
18569073791352819989416136444288972019
717817242686561376851532696345336438668
43928723948292482829550451840883110888
42057501889321052047472541846078635047
7129437273364624186598189232236193357920
343896225609512595922696355562170681385
1899448825547592990394642562991792481817
537059436936664678465227103686749503246
53483346000467142377079879493046625356
4609478969212485186421325527228257837028
391221849695199542725390927622738328695
2310747543526804130671375027441585725361
221806342396224035735209682395254908
6872939059168328253061853881395739931960
7145855167043335422440138091430033169910
15392959378957159762661322389681455887
4993917180949425618749569788913892654085
71620294455352562853999649557743304
205151422730576846373144521259515737809
4431704927666994810855603660154623342299
432376672326250136454160370993416781490
841556238471885847267291814947851157
6945013953337950798983477089424555
320172850081732685990679580346388507903
13367626399111323948665873897364502510
223625554091280193320089736409478852247
5301303545349496233652346432809673732782
4547603697259616697092704111688227529
2461223535873969305594586726604815163
9410570511638288131242493973564216681360
548979757856160708261303404228892074807
6966663883976848833604777469586912834
4182879292455037760637036980986258076925
1200117654611899423160367230784877583115
73004015532210371793719804458141599338
380659075601523259116820466286555357844
1627901728279402541010146584740129037348
2155181367397196254135067093638982059199
609917887078708913296973225213370256612
995359092639599909710361309339541748234
4615207993546900926815559820201336996755
4294663529059808357679261982688173697600
726862362819476865094159624462620925789
159958867566985193018503956148976155142
834213412024428241034072401759686218022
541340264730919911997833221489343064821
4596831584777111448612343873376275589818
104661954259836897145297945421391524374
474786842642058882361657183539666679531
2353479425377593391855422983579790818027
998652807778987437204993974835743223073
15682086816324678392694729947368031286
8508333165006263409417486706162739974781
8549176392581815840654984407755842746317
3622979993402241111759081008917526648963
3518298124837598570494186295864274558796
390766465186289975991201561898986651595
5857383197689512205097413035571556427495
957922657878496327922762582494666310
7753365830283610284686846438033015247
19121343921273574062103169518422123239
3599227784036424477738287295658217526786
839890296861965463516993402410957979513
438764781560565639976076953371099646562
1814666398315515317689209948537747152887
9280854920369669220074081620150319572001
9458624510828269439113809987963479864551
281427124525086235642975493399149842241
386856089534425519447211421386542277903
645748083966246723820221677408423619479
1428175130959632474678514961116738137510
2987891458487035687610077430946433061095
2855240622463987444467708991452012847567
319123361580215393521860422496945016284
42595225459868963172160573445921619779
5400539699681951661349674350038484785
467054087624378614293088939959779680140
3573651849294350893679765031489758056832
81920931216623872413481191824841369229
769423967544400332547344693078147903103
521145365531419212272831009801698847341
558643207801948173644116172842282483018
7799614038139950727122876176755607317
1799822513916407640831481922940395945133
3439135767771414179660914215870670662290
80026032846697937955319342518859846096
641166658867786761116596553922178197395
233445650825620586889811475962815988947
89896317113427071304161411860159346402
7841586643387698730206052929668622728
2312803248621207645335544727220813121298
997047140823525708757466243241316998571
6474165547255836924419129499508922220
2660450682023058921946526150910695588627
386288923546386225355021785582173979751
5362856548818837395094128490781720357385
2805307337516695546745735506567101992
571931658573726236416021937879018875480
85009333104684534085138598470332056046
39738204574845645990992554755218305406
868782295261083777621315033345112885550
631325116338231699682219333372490502348
4429449030252384615091084571204946608686
7216977658472194969586447511101632453907
7248172956462553494168941254167429729086
388412476908585131293003947672246064516
94152231988977171855639362845253875641
1892745236847192715955013486780066231421
288632611676413408772346554170117356310
79639255336388841008428906977798673838
525627115691941300214231485106292907606
68611331411023782867794081282943057008
962193459235164021198215866113449657861
27541719872112943556402423662843523053
15454036146257969625850551949296015240
36196128238276737398767319268950613537
6673959440537067409817157027803940642714
8233543734009689659763829662847218272108
659515692404042597687048863291797241323
5891729189015008074250821386194382753
6880317476655096487091401431212557751159
7380613556548699844459526134232336486
2116249071641657528569286363868672978061
781874728595527894461337529194636576738
8666721891305589659488436173508499192013
70916934913227405045682148875936861678
6940138171163138937348812359585132965
93023802193895870412135460112264001810
2729715756462879380314693607341536505
159461946173938088854172670672690998
7720131630486997646578887579823281581500
930754167534867488293730738792378792368
885873427638709156752185136351517357766
8621355891017150594276652256792784956
53133401229433337209505220339197863920
5927933482459974737483142195336166037818
80675979622963650687288730854827653432
4922729657727572865712726217014059660
826218078657671488743204277802054795535
155891296425693071951049208851583039751
28163665568490605257446227612268978645
6702521578945214682015367973291251325460
347344321092406782091722936782708283606
5256426189468824873634414581369079493986
412176422685328289932493370694948579361
46637997318791882794107123784923569247
655492995476211537862706569286849902599
76529499993926917785921779453715479832
71546005778753341828556461970794956417
6807506827685781658893476886106979673295
7286741748535863757898793161975935974925
324429983009495345061575598624416487823
66962942804967921784670588320627499714
57572952583157027275368768378634954245
62452913487255940936853180391375913060
84166084646533628852877610106816987150
7858140818825067729810602797107745211585
13711981584895364613914174954635492818
2630648884393736209858552564567059834679
113102428271663790443079084312456909662
6664397382259224580171075804190425456847
7248518840064716605845973744944271784966
863749581297086175445999031152831601401
761646166934271426053521371979725747089
4555175365386397229528481626773277424993
103758015924218880742446776570973399232
5316661738128372168913579562444291582748
820313619422375220213836966807188424718
478828967529684900015378296823376846913
540614495012137198247985395148994126259
3108660456102786306799683483294915902721
93715100244214935635458760744865517977
207493236729429216585164883698815897
998864528121490410786495956894789267855
3420546168015963493757047330897788254408
233652358821964826789981476703255743
997015614829812987563534240922591523539
8483129791328996078971857348636365024
71517140485931804518782653556455662589
42864582478846241921903330600024214809
50780925100292165342230786281950979782
2008496114282430498313789762636585416537
510524643533587134888587750782824004526
8582328899461731991146012574990216949023
509256124214475240855674304784355419730
9838966466259566972502487487331553124
2086577827956125802580061704582814626945
29461628844162876647465991995347918984
4789804347686625547571313522216414054721
3505330914829310173122322143095607856
151377925404899515022276523663228431802
198658032793443713842609068164565095549
4158537747373335965962362366998957548962
94062098711658369882152438353799296248
971567466258452526309501489833460374307
446364869863960228957654408073535697598
430426182984500865972993760321328206688
797496909157142972162778210499884203972
621079696334442131212118305196696187404
20779971206138839034109295475929761126
55697482936713950799129830016551618735
1678137686430891781025415317916254567
37645880884722922648066764223093287002
99556539981645626203672593471825739288
2559559285535186744649307390914421404
8489900685295354826944388221630365085924
6165882183266898549734946583779883864325
446438554945537663955563899624056595769
860663006815464434795795757214062721648
1727300085574545351228127999315864392015
325680444237549766859675065348234572610
5437744393788812824721848965512158542
22291232196586240476284685452092456728
229658881964556653189499514211633977692
6161692379323344595857639410639015967959
96969972843093365643395355052878986953
171362551066407569159595804765680228491
7527341133964106264896284356641978331
854799295702572774466415494407517039503
8068514977091970899324244575895249386603
493591371669629691245679466038223801963
4685458832389498744337825074590012586
211111118592786152507517988896751395162
7419758049212325785358309446553136844404
9602172949529448582055277691295158922796
9335818141092080516797452646444917278318
1709256052554258412734512213124978566591
24205948401318465451328121110527831184
7266659647209570356958914993931678389287
6069245574272111361691354567342991080
65424357724061427484597766382265273533
418567297142692922133489331436398021580
66937524479125012393325920571219804340
10291703798168190212065636654262886027
4966846050442584462333692446763487276566
678799958403468758036051194255857663534
119070923682552329715491491851187042287
65158817198677505716454328122177623643
1089498812468192116246676869278480902469
6549956126349614413348611656807584094944
4776974944499327155552053524562988841662
367435987795135789892592835991190354132
5636951047105171544373183852741494699907
899456467678961741734468779523325068571
945695994860392015705871257808388364461
24835353672125113420492724152869402110
9942845169057747640979979704723562003139
594969846632719135745591541955087784226
9741543791731428228481476413429397513
605422185598829796892818158143039089098
7073684496057220712662755488248067827451
38078499389688882949587942347396025765
94177823716800117940231539716487703565
7605380830186890330858469687913838479203
5271613925934169617933657989822962758
3619941289485347972952248785283731079964
404148854315947269841674685694956451
343241768302176610141416217109814617953
4216349012861710714399545678726437881578
2591750014667077602517130034560269465
958469132423627124767702655319152519725
418348637894657299774528616634487605393
5887894817997090603356487498126560744012
5972323120041055912012237882432298549057
2714214732753920174845081426393879057366
6665975152847460246041913540655891332101
2456689230711658674358904979717940288658
83942442630188221100994787147929053365
4171318142536803589912727800959875897877
2677533206749989813912886104964723330
2249485057723466876526332929691188202
7071772692969576816516444952686871912019
3272457990839859358862593058813411927299
31154454520357762428783429176704181375
3390238157641131054797017929888701219
64108479843388943421672120186286318466
6904800976958083361611313094251487356
7207317096087121352788829105460185528904
762450554168186748924652484481215704726
8965414790902913951686508135481120642555
5927882621742297259037857840828733523499
7458147372088556411812719913430096517995
9722271712512135915412463977795874
619444926466217295752403207357264223275
7393961494354097728548212750221983279
824511321224588929199906880946843165199
859368754779883902241363887424382636720
86648301155645624157424062531171328755
4209585552015596866662983445668413081540
1207874481703111990881266555738532741919
2280150373842860400597169879201772234944
77279494333599033020753031805917057092
6289375929032470634020781741352283043494
264332558146494646475068635618492108376
251948717608445484794539129863183085591
9434546382059751199406949373441823861
4098170327065226933279282836520895328831
2242626116678630218502330617446783761
4247701985266426352291311754462640027349
9561853753475030235879244928768353661749
5002862396504233154489749298788548976422
7209522584744479354237846177245614471897
27379422766579631184981152393460017933
255721642384368708129594855571536388461
5378155313988486644436947001286016454573
33343601464842816173416821877573804918
684798816585582988363165106725246433103
32474232344715129643336691542954506484
1901507253651907344755839308536874286096
56759342810172824949694628732376719088
454245576907435252382613081819256184506
4841998225406645719382807946656103359
2964157294025359038981372839050452135
748485729232621128791211836099104938
1983115631017723756191439924787089293801
138345051696502757408830490562166615719
8304488611605076100944582741499165903906
8591102991963910174881002922134263253023
6029130298361541717230383990881885654709
48657945047231468412480045256519563
7647976210882012570226047276582943367916
5846301241703347350571089379547094005689
5826432443104565118262271288628199199
515231423533980186885993228950794612387
27541077375106775328988643702568053609
977644054170983177670133574354380487971
835012377008990759479211990387220792189
936175387150955843718029425456764977732
782377703506399682122426039912714455965
5694475519365480880962232555425531873618
876482034799409608324933503105678098900
912473459370394448551929573442283608056
777735926385853936162354648232710284982
4271208723418468924864383397193378757799
345864554418242622519744557178445842887
19123667913813869231254856159823966536
349385498526422297947844360941268939429
596435191552160620088033615859794790379
294375583175460828538551610255015508161
167437526760954966449021993815161993059
196753454117790226266966587380491853982
435761199183247687309884991569391932896
845423454478846333236613353364991175718
328904158478594604542033170320938118908
5250163576226319964824824739998317184856
224626786753900730162265475343225787914
4879862543301782919077698019180735345402
363231854534185292742432455494275504703
10031050557699234803500980365067134733
4840531835809747752792164622814485318414
470210099546757309420653087393423333867
53443918154760302403694100285733768502
6799439596052363433494973423671471134389
2555454067752715535364629525261785134054
6155877064855758414752666308491113497186
395232754479395533555479800941191109583
7438870470739235882348933890102238552388
9685948111837703926525789701303755469279
5520374426705685524447539561407654106734
613216162435549429872093036145798974764
307078061411249542955761514795674157497
582518512157819243029911497159407797647
531378548263368551260875960486116098010
677432376933628832153613529893947724319
815176411688697853280535046301040724673
106444095537364151741608733833899133611
5512388091743127324580651417887319197175
7643349094057181825603994756052819390
22995008738224896487754476112152845660
2451633050625137269428131050733940264414
6433932683644223169625767155181082345343
44737373906619897321734210188161126167
3290566116517168891913796530350071255344
7188514343287368384146164105390935171921
380898135636667266024719317790077246666
51889075320069275784436958266228675566
346742695085330856641294682267316076171
4553313775674754710057944352546534656508
715651217962777460987654328253828089008
60585582197341463526251471502517181508
1817925730729033211563927741480526001
805621247974826088456598656690777922699
637760025354483852603121672900134882508
373532812212918810963519301329920743793
901375032433840117377750424783466145855
7531147172008072664743097755640720796080
665395873115473693455703466031521033777
192525453085919463353613234123128747511
2640380177417510922943856393288439046
1921558322575352338405556502362824669
78514909531416670576449625753032489749
654668926364850299795506815504284409026
704847554132622247337023942297849316588
31213999521426482086810443764566391467
4008866941665827899183520714759677401
8590427624336027315316565530974588228675
621055329775968842134891502880092944948
2028247684362917361410295260288380675783
68786839569198634995294357700713682976
247665116053975178162694444542981359255
1794902395325122105147059487937442788301
24520446244484799455687268317130086703
425990085567773479396668219299591436143
7875147359549957717387043255187518589633
54454105504421844080185472759694902847
4694750556397736114361582978676949385557
650715104848176653703050298579506105341
4801338395155250919655653534334975762788
27505000813401673595569448109949912909
6804178714682691377692885676813818299218
5918332684634674318827562611522172234925
511023706242133618723972069742352437551
6719370580952818419774562743659516929
4496469978948790521561963566175481507833
949963277268506325992264361173210219336
85252218705185647291413193180232179858
9912199530386721703987809818516020084054
66209658859318067996619349610088948186
1852733269383151646390009418552411457561
4425635568688922888213939981926823408
81225534869740464081898265864537714076
952265855679758577618853842617583981448
172164556475487749267562640514717146120
902913285283914506757144463182027835277
13036074239169648989812462128552888987
3427111374805949452783595463968313058620
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: QuanticaLabs@gmail.com