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
5118106660989476248250743197510129173257
516395281426676154072594471907740678341
3674609414646131225119178914596621627953
5751749780196891473342133862982681569578
210449276866580559173548256898080423366
347093144551925229627205703777150215778
457065729265488600230164253472417145295
2098172683416068789637442939993279992255
8352809030046724926064711437575590625848
1236117481132305307113978200442751579688
979256163193803618328433910560759229017
8503601829989683224895793945544288575884
2489757131534029177938224578302082222376
81327501456943948961417291828931347747
786198867373943281933120750339452411318
153913607520708824529278295682331418329
1206626215923561323950881226797794096030
5640128889862300648312124400819457005276
240546677448518478941109158418584023020
5501496634529679622856414761452690285110
5568154561983978355476818186487759884
7720124386355056490717493810118730663512
1809635427787015809332254372584477686677
3651371467383508271051982972460231422950
2735497333097564683834914633125315845697
346271672880187433623636744582251179515
32325180421918629061145452007388220
34264519326589326012045416838016081754
103558560652497537180235661299481725353
6948177778282644816432217058562319748909
815595578928791787423344175564853732666
6984216173366504193882412672998949628415
6860950424973746653855173783966146941528
7180233917563842458022225912148947737503
5611928513686539425434500053802446903
21917758788766854779558804230484421292
3634181426409631876152406748236814094730
4162965575762753498325407452336683194302
57171335359794984486131418321637754216
980254372384546867196068608720017373688
51712754379498872072286161196257969431
526819429411124310714373834988435977303
1079133868514365108333399840155743385491
732069675114119276378360379203239977526
795572742393233448749981658969288671270
81319166952917088673984954698472527191
17868644742596687594097755282927552505
89202583418511258773567367564101585249
411759341971510077353746702172553639144
172454255234427675022104577862993988815
74431100437371416231034156950466584214
2447106244159001187592177664525435395178
861068121917654100977998820315461542915
498563462183340725549673899535256939
89781568820522254267315350747281087452
5309147727428222414333164001428570142774
1550452395461788465679577743684969031240
131877841944303294454766743345810449835
7502551959427294315112142962414845995210
951144746425414252071505210347935638898
3762276552571097512021037413931198604051
528216951609525370326601550267723347375
5338814375159204661553122316897110083936
5881553350377713397299714943582237484343
750275091531645618683495461302762162970
4154223422412078461389147776235590511
61831403875487834269633337714936053973
963150847522201434821053183931018062187
773942894814200780532925712421558786
98892319647560021115561256543886673545
31667665328544643317230569326639732162
16835556944119870143302815493945080844
160284083145462291421579262843062833725
9997977870668939902224861021322708022
148819599251859115257949011720511414802
6091510143315505953280757362261290687203
75148798920884649420907088653277976472
130478409759714111186632346863739858000
789677561305289289269044122201541607669
768727741701656674948909139871841702764
266288317984706861561131847825298627899
5697523031047248510886682277068464288
91184997768915267224107672894752171184
664927895959112365638116679554740854449
8377791342276213666107622699952651938
916820359634946652793358742214525415535
7159682040464081555595016170297556573229
4083203767488810739858256485474849043
102847571239750572857162619537557397784
9459251047201294736184107434300240146028
225536458049777589341418379572283839511
2425627329753145860748891855779344273580
826673243121134441531509521974380886716
127925458615115599168407789161163153714
86595305368553178964691897299885655469
903486842674727251673275918936251142684
17085021401376757814098959282069819524
6582380853583544210262465949167416102308
9436511757716840288661271642237967151321
369629208273143739324041908129882557926
839842354426207477292015832562048354850
70809435162748360279805565689245908739
810016273848179919039845438163144973775
175867185707536999562133811384323378455
105027723653123661294124648837441675307
900871218589219662133594974030697039190
79254576651284239399033609424962478871
916258644906626861782260163462069645681
2980838145549990143282527501243936803385
64417693195084426369610553719595352984
7839337690061082916724851300846159064977
721410752802779282430693192848985977112
94721824594772039806639676366650813968
3852883564209960996051946564375786773865
287694753936660741461981965645176779686
229566388445667486439686251818043509108
5589733663211635270371123325411799762
798288896102874390138666608614614348664
2114579822751449666244046127650894603629
6666109828685727177281327483713118028797
6556918363897202553680917127516978461817
66966331306894379869810870546704703414
4069750132629673789249665504167022968
174680977840625234529892128773088157543
6342452183655696106871487872971964953538
953881381837795046360709300174129797833
297848086796125245911982014508057283220
7934181198321791624240953362664033451865
73379424456526267342217739542663946952
421670202857604820863065494479667317628
108223964646920446746525530717885731805
3543857945940442697952983472599228248
8910932548522411574372181632949416974
493160549672745602060416801181897577150
9427269193956400517121790834059822217
715983312175616435233502903796365599447
998311522189083811424751871109022039939
394532105119870786720114806943431384781
87127164933987744185114170570093364913
406571913217292316977384990824459837316
7567883138124705323480289727562483939230
7964691560318219723356896330569449815208
789779056134493027625106637639871191647
249448063882764411979128913653571543574
46856957335999324480314107900374055041
883363076138104657881573470620988925217
1728820247665233882982936312337069826850
503092195374206143269016383376916458259
43728592871664162582228201791836538352
6612452391254266768628588681857521598468
6882231975861360328682245756962534463801
3048858778077042418067053696303538976023
575431995910209219059067422195312307764
4277634716992016151612549079981176127422
67054746036704516748838916120169085258
3533228226409848912326926376610344076229
86963377242751566517349062311787831388
253922785891062905038645543585626251279
163385587597733210269825371776834133246
428348866136526678839438714827474894067
705439579753855916509202212469228423228
661276701772243919317416252509440656883
180034928043916124441673819750535401575
752236667390435080149450589696559183292
1961236432732429504546697745618453771692
2690860028212211114025393056900139925935
4481708066787753554573745794958828159335
320046241449595615392063779439771515729
1315269213122846398033861719656067532938
224747153756178558987653600613195239862
532798041029299790278398526549480973286
696938163668369367837493687105577957626
1244846090455664457417152526213627871512
176473131150821113604834852499371984482
7025793098508325995052544342797789413405
447623862471115704346643888540796744673
1109694776981633986817032850510656954910
4557172936533722406942352197282941293496
785694986796204649854049076649948239377
570932661215770778396374879837019534124
9909123998736320522114697889439353242925
68439389125354934530278102875748564326
825917746658272303054141674293336461352
1977139848823776634529997224333363333955
59208921225024427022867214513351685726
6762214322714688579086341755519269206751
2949780847677056786853712961245325168313
41248798621720607786348259331666811018
743369623468984921688421983945257556
966537754871808266579662143144170215551
1723794952024552333455066352281714835364
5598721515575087976166655988621551699630
278946556442730996148188873656284365511
513259355724471859905814192520877913203
9380389352451417867934129767185191222896
23160885834853924234795698635656728891
6035518984856135195284513390728746234
7776475237381669268546483648485527613487
200821038098424558612134888012486816468
440722594219440593756717434213014899774
547563641476515575845843008494594166443
1411155292903808717695949404922088469511
35408900870013195832876400276153941675
250238414927492275865190701295776605558
439720073425683946879789533765196856354
5306201588922597798972245217140460631896
79446663262973912865286338790588668512
47866717212814980383164957788680515651
3531976135423765538245287855804225027916
715454747894421873353673885444624069740
164857323434579860637253160357801036170
94062765277971127501838218708117214411
2049362696481901507175487508736322067875
383256575197780554318618960553869463563
109682546669238467934259558783187209599
1039910587828595587194403623771154115794
9319443262523745606089871753396740633644
1568325517454205405542897394685991962387
208453651144067289698139465401657456853
6541613577048571763239866362940465166940
9320703919445138386541587475328095399580
3570240125917708651810543373042711280
3743929941498093802697903256663062844648
814952293042413789844872098985562536728
181961075448159959445181288134851028343
893160865978594521825108028950299913340
11232298480385216982899379993951447985
372986011482305821348268355117853002524
2201799774672214986463622236554392937496
6113296291319979225157014842966759684935
8336408824771637883795757390538883952214
162521378120963699566745103860725898138
18458563371811797803486533598896809957
372299494028937650571258897553725089628
4377736595991584966089903332659355894275
8601871569683334517791753263280214684961
403118575526310763447004589320093676069
55074274285981329085031296953854848491
5962762216439701659656752079957936633741
6632495157692398250520395718269292114055
9655802286343149934697125994175222553
2815652610885601448856438504224735685929
607177707382675858362986774762429663111
320857072707162845325731661367878601238
738392042248751288271711137368553745515
452563359897250258354721564831769957
2764948489829036612316153217541645698585
6959382195313209608149575016940126932532
47261709801298661390349737129588198021
6934151763818539746913721163892048329875
9136825235716738418552955365891599388397
30994725156692031075072794471059544281
8727275755631010114053668038721894138094
7028630669457275120785447968459891259090
9609425577155654861449053819444088158035
7780541339196228193647269180268315278311
9633236277194661947183691835869399665951
382044602538961975607273872775651941
594329974692214714714075532723112527101
488971313394620547759817963055749049909
5788399940657456322437741326498248254905
452023414226880218491177523734618134470
251722349322588416686989323825224311955
87156840104381621331990907765015097608
4428236839594254684372415470296873971282
5354281154498988321030445369061346209
9054335376468122912726199492957491454595
727568058749900578931181598381883352341
491190662443243387307813154032884252828
820058552576272286426733166357893766371
5496865537087342362047224570766724289768
7456799031642290935542396908396561497262
3228594457066912801582476520935194626680
78878766059786345274114923119786995088
148942418291657923544861237247446821697
114568749057492505096714962295399653485
3736545160443246937674712439367936077536
8684746586639808755328687798614839109133
774317257708671644578440740447261578737
80017241629831083068014346315653448425
7382315295395573512113246538880425031983
755834778051410777128675768452331331784
5996670040167776678762729095481223349
8435896253109014195632514213594396689899
9198591742105416974128814502129198078899
251716772963734740173691358327997738950
640320486229869333614356950129158704533
9899528446398357253265502551111715445453
335013439562110251754905684697375953104
7111646787962051799273799672424807352
144654776108651053306657176339148614
7501259585009113474539801151577349858245
890998928775917440551198397616883165560
57393975944294189921447854983972602528
135529983794339633959010867836731359238
746835582329781676913869316145484415407
859181375383173580638076604478811635678
37433702199610152219997272735481845573
214037949749737341552688016573679476314
45416715426847519956784289798028087527
6464864781831990636519028992238794012880
1767648296741326330586173518587559381
4500422315027954943343817647315031356784
3851742479341861975094569489943878601651
8851478871191484855492903549769133118569
53323841268562251918682642617060888681
468224687031260846298989539320560357752
444737051121880930946726671436986607033
80868653692671674071936724319526442068
764572323263521605724989593176552165680
50011162875691653635728678536342854291
9797964140741787420850702235915293685199
1960678899652679894716923801510845368525
71408733636229878714159136586910467596
8120645487112601813926399191744462056333
785418264965135992228203336343976362581
984067276749392362072077986400858369094
253155248966746934630233124729548422413
55084437593728423899153012668004407204
6570569071231032367135198919631318913232
2478505385057044915545819560885392013524
9628728340658647157583933418826617078404
8380594436379890347293963105755940951358
13571477295318075210158265346984179063
244286046014571990442592828288019873335
656572192344274846894903549726301971303
162884903896612585078923466347011385364
9828238729970671546099031521851257590
5988434251743409816840437640827666984472
6105548150408585675227475732617972054801
52444526295199929405378944912098246666
3866454456948618734360621400958347139481
4623716520844166629756556994423689191231
152238844185265867845605449392050127918
1805319855178445919513907201874220436311
45178629320478893868298181238711434558
63893446463884707398146145230737927963
64121169666719515238165576460692191809
4777405283721568765769488373717324119
57811484955538609287468142832237909573
57771000033975792968656677684171531031778
89261707727253307357922935707194138267
12037380173436890640377211362678233765
65552968840085232752421594558185284500
14684365854618511081970132544015286900
4415927580875636750622839467640858404
980953176084191082054235489819397512912
555494807085043510124559530995198642555
91326966253881181983098686995436114250
82466898194512352308824829595262049436
1919906812261480444672779002419059697500
9597149519274094654673885766653420098794
4304262739612740538814987007817574409536
458160437913226738646688272185869685
521654324853557615815936850356899893
8148718454649780540264711306737611418301
140382473091667176196738817618873961521
7099312997583858733625559214522827025318
6884388335881709595073104408254766069514
1668619733804370847566572818752725152123
23324595300141658539870411899652827349
927318945853159589013569965718143338523
48621982648897756710289577490823923172
443997147593450421077235793265453913735
5038258968240412819683761267261374792
7783769926704907910783438879591234273291
9506147213201388719341108173265867189849
9992391158646429129247634314242573854977
3178419777999724433692251645689299841
9777502753607747185691478856185460836138
245550454593976978235482954698351847269
139164808039799357605515341398125757817
48951419219753724371671842982174836134
147860436096978884746985708528312137861
95367280401836331849975358049358189062
42688925281213249343268917044828922183
9695542177835461875738407209348556029576
682180699920543653187215641410433398751
3556381693478068827255412270845581579514
9917420797596860280453853968816778484476
8112263762692250386097054079702141077635
3304660572768849959832496059616343343465
4446629871183625167433023329127214009358
6584952267825095291056528875268893787771
737819854647802344367899761338178422776
6056204561428971710710231299665530736
1356186249537183497952397493558038989332
20283043952141111641144483215081964520
248089005885397834771661903663454072861
2171626496221136901393754163681187443
6184174294767112804629679795948172604526
45129925219223527902770114663003947421
4697371336461836675298813639950593094970
27313594052510986034039271714478342840
3318254126296735224046566657671162929727
9767522514508856979807926877828413327
9921887343379070368154684507803057237756
92425970399186209859363549275811589589
248422659375100708676073275342718155403
454890817709925530462836628393738406600
38664680248813684381980571054775093717
47857146737984259382118316887840025242
2234238976697316202962697594451962257694
26988132840878502091227942710242363424
878080804815769327882547964757648226662
6739435583922851898356429477477329417719
4470968802297384922346309696793882783
3243186664744133515234291181463792229036
97677618945771929775568399826213069318
9967610195936941969063988180381261352063
94057968843711196974073118743484773240
787586040406716693936444183898536215996
3430924349779109764933903231714421299677
3041963815571765275676559427435581721334
6319256312953677512232343848492974047289
177080766476474534004209981181067544769
3552732464334119948453131112273467023884
141916588937075207288253622409049144865
12640323880774383974455463775547409635
170661377606229237263369426187970026864
5657327019656749115288785386327410118780
28503195715790778377143561934390268421
197852153581786453992315355739012755844
8707899453717457361036894006121880695859
57555457045598497869345638815831981301
9215257915985945808022104267586671935342
214370601727980313414398895692080945789
47475303454881207124490210657506742025
24549896160621696312533367353561953303
439312362009489863844293860240889144879
743020239412901857945065561815499761242
753969518116864135988304692546413525861
360341328487984832722501699460589616031
7593805258599401808724761953588972602825
300913839077475219302715492885279043672
433443816374995440262251391719646672303
1309302456544753716276055518445985117541
397825774833798663979882661595596105727
562110767239184135337043880211341481366
653528171711717133088652255314290968716
766128879171381122451954731043754722653
50272054984564932394383886456761408554
5895899777699219980329982182719216394980
7467471218055773717751476393067305073
140179444386873264018664711954567118287
70487596657150905285415263209714119603
912349542225349328338445149774655588263
696912289424563724280919456598847571403
59544238580946104677872055020416719215
7088747658255928832779861476735752556851
984757422928705385262947429924272764842
1207553499766681509137616151948038274741
1721358784407548749849994375286110026305
98423356511237758727439312723369761995
17596598601173576827381425538758668972
36107665290810253179569317432530774196
2069153549743247518567837623398737695199
729795041262637632421253217113377496589
2116423285283641427395261013434855563797
109348978531399287289015747973670882371
8782671653313352103638151630834612271
57049259860474482796454630077322921646
901968504162007608922451780237785709575
7923488963081742950490898540517484018541
7077322135498815228247084973520502356
5639880540317487740275041833375724385768
1486438268959430380647048185404290087746
135390211009762517370226102154160949736
4105785654385384405932041302465144996861
315256661396187158610449368453685549204
24763841450204623507908489218045643382
7324131833309055791542271268821471101661
2560605258257665291853134658115726928378
308658821713474143759983676083641438229
1588182699373588982417234707111958163374
210483548187188481984633661783002613623
657479023920783696713749494866966058406
773075009423409339387271161523211445574
5425544353388016464929902641347493001
274848082720992725973337808596566264788
3796838809467494385281133401631696254
990793002258957427647477009905314473930
1364220754893528684843664308658428807845
905971306766874475478611872615067714565
4391379419294636761992524191610723398284
60352723882262841454672836359074841125
5159786218123778457752666212670271968407
2765740799269564143087761933524895112273
3377156175938966297043556619308416979071
88295964154167484091545128037007124746
681399199415537515971257547203958533
160085387711918708647329047151380807978
629730916786989688427709079878613319498
16826490811574163739343250422354916285
462925482788515691638726373787857939205
123690578947037788041476346216115302298
309167575492996468679135533071778322355
33674467846519121717152370947527089277
56158157166557205415224165989688817940
20773767271760825043666965944818645618
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]