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
8912639696749900619840967021632465677692
475050957442368535611424505623670814860
5033341194255956814011326526788738211
191351293646609420528033936937896671810
13493797536091157273426174350167378871
53341187403509656590273827882971682681
97706872372359061111357479385935386
1292463786531248275694736634369267223698
55439321364578456552514586444507028254
4288167622314706295947474809805826337048
163921354476697426092062202374647495580
68146167658728023445402592207039058522
36585326947454529431749035748995155464
6055289856749203918512673939295279386632
4665310737014447771079174880748866409033
331537543541932844687432950245251295149
6536914132134764310410026299242588854099
904085877145969555210975265716174243582
96755150243613074118669190957909989729
4300299563531428867431557701760271729482
3674366539274209305011507923405833679677
7156599567236583294476346043946048783
998923943542576949019287653874790423994
68117858249589963625502278403388815391
482021701100973187652482266999438774880
703573962105775144094725227671847373038
8669756092702506487352271701316083425739
27366891202212743037642366237071995379
79763222268783222059455762662433135341
15801506431017892359817496971641781395
391533576536289370691686779904646558456
97348129453622213158165738751080784203
6385704234823532388856856444921623351846
2267942049342652945097958873224145444541
7595698419723004298033954152740191195321
4693910487053655487052995589233827688
5805772333868762875315373595331748919831
22894202955488331104213046349152037647
21984051765383258839813209194229685932
4558256717136850269610943259899065567
7184252410041469120073035371859230379690
4649780153210874422169539842631015351
42827732157891922274083921630339572016
618530928962191785501474304774333562496
9784981613006107555586166733152757976216
7310115738029850698838656296943352577
9294622968495040875089368392795119691048
21111671795843545487584217431052697688
51830792822842033157591882692908146
487210867225785874136612884234865827189
288650916336202362974974750223171795734
7020516546714782919449359302872867998
562333987697600545714444663888626254892
24323713168365727737086104103576881094
187150283553599161494247451248269876464
9709688838044331987255142927142391953146
5446484164759531493659841915514612528148
601789002519414185996476688319746412890
5687729325877106329538766515596711225146
3970477786321880319248033570761721027826
364459349732532544744685420478868451841
985511051111706618082162316672098354929
56046680577731855756570186732347831869
6001887858702938312295208811711057187
2727171165373001135686569052930090356736
6443104762471716234013991755327052827919
2345672531725339517479088508203588163079
397865166952602126210539554799727342832
93593399764022367275873625699504091609
4264713529244556761686499241280712062160
85659874453777378863569928251542245303
2736316702210759515650203079472695
4498805564375891533493624869881958718419
729584714252102998569211893666443301358
43459201633177373425288417213232920443
755924157283571566935285094902151022470
9632675997031732701327417639715825707996
1766942964627568608654467162558387728703
6174582340562776838260908629049565983
8821213474232771676260156536705665462535
355053357963547363292003331337411338756
3629338978396492141773566464488356311974
7415256337259310659488637756480152613064
22677554406986399335542300692920587271
97345801978566728084449312661125106931
606841738769511014467830369499291479694
507279029125904824022111869740111672215
503999967945589491814871718943365843896
5257534930788873939685344285656148539209
2379246784045875913938479052454658532432
239210632525961673012391784168840657664
16967808559774420719805442281572856566
63366952124823234271700327828744231746
607360309394262816911229838818994187
20669795998915367831026215876450298893
7475528638396071112818051295910669772
4430358028103347382534997555411385301825
9634511685111569992376436844846435656
3955132816796161503143395091823721607654
60428242457912115309361405916417229547
3030576381904586841273069559639610335133
886047689205584658134733764480746386664
866525907585605523765332803922541568687
95695916207412976849707979638466958041
59628383173139796307363490670964688870
77853626929175240242615217933277458525
7017126998251884473224165156308761156105
7256122598555953738640314973701640001793
9909490158373233462421439404729132139937
3726002618051785332897051941291287263
8367735125098663529340254243332158253825
9244958879164242348778794639797733591
76732066970705866158009734846182346551
978604224299480340898309068907532852971
334437952727948468895716704639296996
779507460717248427429018488439473833685
5421933336778045618175117814530642381612
500975948838924757438366992766217013891
203353762259039965958476412604493186082
79336092379120876270339818374572676712
9556660687106936416674469191570299969254
4495613375982015649456264115714399161098
864974938152103272064692738502548502187
6777692246772319349332727499872645447693
5836666168058482792538291514876759709933
347563611683492336578292935579161919823
2723992165818260830415522473220780318426
1717469574436833154126146217465723832402
646161871554732379891689706539657672690
72019824827863326098377416372250613315
58646663100665951844605838810964062603
642729439432248236721865467560342268675
67464223425620428877910468229413138205
4531200165075526542086984305247327417521
989197715629294232033075415318724306309
193793512068147210074758811790929234744
274825854399239265101535142246892482868
239797157973368991943206380118550687116
25446044991700564515274686034627212981
627026268420305659195301330667958979683
700073215231843515594066811386316212823
379838972311720788483783403570496249193
7132272748804400840211621156373055626
5061820140699444363479255650230867495
838226880038499877883644823328969506084
45056503168129574679133967824987319465
516821918938290792049112124222366043038
199653943433586048050182627530228996750
8074845833974296232413591706634558692312
79887453495971068773969643224526547969
64708007653942810177223255791941798990
3483535623679709623231779399481946297202
82326896184430361867501882855869637819
4489277359203102306636735615619644382223
1737985818569864599457875740950197141728
785931813596015871512845103719461355530
7822281252397603268126739342814698681
181235387501654542669999283933793738860
215879173882777514514192402489979853565
96867872038511583736180632579754637898
290857231085161318949051203355436294981
81405118706618549078626954892881237923
50919223774698336566055303938939806354
84626673480730411232454161812357723
564159092951234878119014332497496451419
2307956417545937275580071169876711118512
3038601089041468339150706570962391894298
2425302645173797457197607115817982958240
42766265459946598838280405790843789309
504128965177242875370138074280672642610
546895972432400863442701436768443266129
549941193741749651777899543953811316220
523751368690520350126235100996066521457
9224976041715103722236753958315878134321
4135579038603183956655071891103788982278
2248469062477918586800688975730615457
883523445581702550068737201248582459806
99261190197981247475835365892891936173
1008327667692683381629281479230041054456
5276314262978804294022915799423830715
66344393676335977314370638578359373167
6121948878388811626490926111677780971633
1412796416578500677682593284701829505859
193739026031190072003080132232031117952
43385288355236311597155511191684141689
67920667638119519727256778364741414190
474260782833952896314835309215866281440
6341613681427920371339005019103160312988
80473751938124264931539544530557508777
55585625824119398883581571029619359352
3626921617615219114166328683194835952805
3790480465083594894687701333760652111989
89177168688437723889258372517093691230
5896721714376058266048551167572685304983
37036342911811899370466555655542094818
598713966006110593541591001786318575415
530525605808972198549147438232169305011
26369074858352107278832150713266912989
823565599286603880158357876118677233988
236931541439520758217858084202711095741
441491897174107519938401204328271709376
734264870096502893064433085004928276
92949420870376716040865379526041709904
860933969596802543031066129262029148648
3115541132656090882989521061591580419854
207289810426850783179168500688155239902
4203807347746172677434566600660149748602
89976025736617848535761272549144069769
90808568427646377028649723175773097165
9027704529012949899491249879201794641399
604658159652914771856192465260958064376
254768951598272762573533893341279221689
9163822316562719234923718778543334762962
4240901432846189178036876887513426419970
9854983615844077436326584404314448056577
7872122298387491315561439775413665678543
4341181580845998275768678760896398156168
1074397019595957299346754950135584574167
7140293542903230897570996236794724005898
93355905766248546364662801697634037033
312258126294999252408709186781807767
259999879449197631031619667022904434770
1582489364466755511246815462331288194597
494743847184852274139858848895064247134
2520801281833348925718517250524582652730
3526912527274620859740699744920892904422
52634702291396508628571160219749675175
660461271913791838512081367289255512963
989154007149682837792666429450294072816
950957465547936846784909026369395109366
60674093284663534264879146770398002560
750329727561751185764716947243928819992
404886042489399978981436471772615715647
140073692270513234993283358860214006193
5319872050328962985570944134229698801676
5594274241486811875344113495528215397730
377714440555970676025623772559557342444
14766827014124777257273415280026642283
528714291651605374326711100334442008490
153485138447893013473448220742531084
3775425310946543070316137477022159440
661694389650521236367014346892411164985
10441805295187667893923261112171948547
67935627760350916106774791812242766032
2239640996135938108646591925903742058
46286073540715421968430659749563571276
3436949112709015429927146042832374087738
27758870390064662386612748968748071274
618416732462614170078333677695217247889
304826043770253294964655415810744222405
567651548361371530008488619243034656491
4992347055787983643676187436106138664964
560744421234398550187419527217670824927
5817347747685659863760401876179865803781
2847517118397653290519477392497839724309
3549111130724004394596339117868439506737
2574569445851317303281599135498527794
191796149192715411483612341549405801570
6485558935357646195078987309924768454520
53437974547359011976896282961578729967
999264398988309290445244126559616145966
4349967618752081530189315454790481428076
6384469244746308930975873756427832085735
659655423038461653998660566227487096301
5465242463935620315868345769307980333016
885098185405163315538364795238459123601
8392713798822018192214321584487614535007
263432604189466547407911150065794155237
597445279046189662647968280319568173722
180175078418071495539754095397721531003
9342666935496002861703588870321175183
54408991982999722943616392327185241624
139619716407190128028626720648399064390
8076837187901617840169165378964073876837
929148462794364301498684885644525719633
1846145219607068590898483773972785291859
546123357162273064361675921532361622
1462268031917625537531404586555522943036
348934361876087383519747894686484588649
184756633608825131947997014534612361192
731843349698582592885518297317542635864
154960893551301851287895771228271094859
74538625198183448071703304232465885767
90119253595207960975154253960874668904
490490298842207268008686130110650585707
889140105386997859487814936426528161290
43837094936280578263985579219214889596
21559524196283408635919208676707687362
3529576530345945298417221655330775148434
712852075292428575721897815187631307634
24578674501084827553541010792599279786
4570784435885162492337532797511164013250
3603963786834120548629323680531166781647
66616371968011463488236217341353565483
172042982311232159162807402897097088926
68914731194233303853277600721617388604
8070894851656921529656096748531991779508
576557563602860485559699320972932955468
136652791707229544328637925047513568252
412386289786758673383176975149852773348
807648678355777091478428180648827208139
242327497900961452719741941210013858779
996268493145220283195584438176221793831
6548769180704766758166848869493026921548
498777204048122755678775738765189328801
710943854814767729259047163926953194613
554510696477892324233655505948447031104
71068850208583782332719284584077205080
773375203116660442117342095209681862906
325224681474654656696410203837262038370
566861619491047256072304457145325588886
19479496222136884782278456845434456861
144446482412795825049113953556854376035
6259955483075193662823714347823822506
249264397664788810328964814468631775063
424273517710953277893239497538032488
5519407183773168240086112710988086577224
6480768340433738233901938184213011477
2220172960439673606093346992787453314907
862389476757778551477772813680631524492
19743241941414162639511760610048471574
745841363115924438575604811797035918987
397897956949492865065728875349545722312
849415641600178779181998383299722990952
6147133448468971217052183242416113767328
66307298967592318374422420266130886575
3894320777881005331759430242638811990
4001695114609125281272185980665468857
7263582980957600120323957672402463851999
908449676343451237031952296339911432233
9648824319067529165505357478563018790
116665455669772492362069261197738392
88422280536058138158536555127525221182
1221157190357877765836243434804568691149
6270890224988420972136284064992742999912
1563547937431632790596415191460619078681
4546477022699069529467632954996981505886
4674829646059565334959271353552442219208
5261915770672439380017636193438251955694
77642500524046855273628991667834335637
9873206294197283222741853648457295824
698684879096531187184738723817906255627
434337287501843377298391423899297050537
233320975190892295538959429877971166928
63075093421393576907472283256195936440
137133192197651891338110891110435698151
5815497581114941213552868307669373284
521399325612893245673934065904976062153
426532965415280965734421370734688042077
460073946815360819988566203423158839452
1225706277502146125895862300815788521685
245125191956596118463582688044614365108
9873631976987831782599535451979115487401
289826712046754349754174046458064385695
7848631326823797986386545716782368474740
2720208085276051690763179695436512186306
211699272912944235289368201664352118592
484984703712115786546786133479634007813
6170111914159759624285048260635674546344
987560836410880753643846764763250346207
9028478984222970279493126747738963448370
7036481839759097428536944212980209527
770595751885066169552659927203872108970
202970852879897613736780185220983156157
8781373682549288877734791416564242009
391471465293301520587465551585414528350
21114242605974736494384416798445028266
921253926265182757892724029805344206978
2878209719738587697760764669936946745491
926696983203299653058754212121830883326
953077892963159149741305341143449637833
631095864407817348933642111131281385362
4076377632497376521706428757357236473
657131971146878859165442332465850003664
62355559046108492771837308952572344693
15164239014483829222183349746882993401
1570178634026056644046368301292888143276
286847255615674375722450867366786489403
2768443538296114372847005547717010438317
8428644387511905751562334122513941415
505715447905682504534893387398189353574
725898176502593421096713848296710099812
287854561077677474251236762690770259036
56481032312580977073837208361317643730
9281505478958196183611438393232950632914
6409683680826451106988845317998624873466
487490427957937241318417256958130857470
84789290867649987401945400416138997516
6799278989188393176105280375864448773
556584746244252937694711576518576562585
2473407150106849375981658580271796099316
799641401135569515145563690312140342428
647685764789532669157343206816013706127
9738942264027807618242681876725093303834
393314329709245357106250632546655045709
60014134282996037070178986517275936294
94999844374986768352622644459746942272
886963525523556348362063278974588463228
245278446084128248056364661805582758691
8509542397969543945899894163561970377936
629242999481215934724041891792893113865
83969049300187836803008396237644943200
5822686011064847632059823202864544596482
158124051269695587762864558922175713078
76126466716848062389407479358957783361
158832046487110872327581645114992913469
9246323642619715818712765571285757152357
4981253266167879224624574383745091241910
1421766654789947286947141690818594476092
723925929105395988565202497881075318561
9900920782312646604876317864573063544070
5402781934104280251014455557852749543914
4832218890484228811789632271961877281797
3267883332916060273356338586523514448222
5553120910048664953574957672794563062742
134819351037552848270600720629822268
7104927660746605854713918815845974104595
4465665970247621697516437577652010937922
189514959261792351622845351846002842331
210424968097588328457372772300249235361
91033198454298647077281770539256510229
674027423959159183484682928866947111
6646786827043233824788178972939445865678
300112553581345129189088066786546004873
436480905445319487421989074338110809684
62169472782929172323193712398631977
319130982274253688169756604997141028990
5971914598366684540223297336572433746686
3576505890024235180279772981336768199935
60418142188952958462411159964074436257
6602928988796026794094118847995817928758
403466605008962681875968018914928868710
4580624952967315999983384101759550778592
31830361256336918112264358484038436817
2548418778354250275105085247249247837
861986796867753696621478326306887981056
334097877425681086658555311923332191581
8423337295624077778553305243612566245
4967495919405866727627665542184316737086
36008600518095664053304930431175223
7420823899708324256622114457981676436124
81647153467685425604202738121271648908
86003783771553264658468356243015166990
6776432877426635130358752897511938878746
225372398137265979856298255532625486831
9644835352490462705637836885618215711
1067551799455882409393578903542552843006
8118559069719983120285311624745028986923
1112774478684957408028234000798736423487
161152858038729042319873726717248867872
818737711197238214617223110911594826519
3456318429839095224791879194930863725120
18095267713943716136318646515599444675
2483809874633640545611517094502768354541
23609912181241342049184043349752892695
158742406693765351555787872178725701355
6409998852439989754034564168346299433
462931207805230226750581099162380978
4490593035191019144016964958788031952988
7496776377311205115363034508840011333028
502511747595937713163121308379988592665
5814118666332430925042363529632829073724
261013431417303643516216860577471046169
735196104248861722178339668830057741504
3476629583834815386645914994759841657117
397020141485620673305202476972208662317
138286854251811883964196618270223816569
741032481157489843849604513443256416990
374332755844136438568293885236194309638
3852715034997345766494855904497947151838
3583624299930917042899249952975811377
49374269014554207087251076722051157045
556825249842329923702505861923427535216
18765274657656870683689679131084736863
618362734309901523195711745548598889099
1427149255611935429252562447278445856
598441544252604868037400510174030923008
5411323344808939999737514856488846056490
2903455366314747876029572537954425106
20014974949793940469631655949765108462
465928146858874687654398583944970583365
9321320561391724522130185280958833492880
19235637516237056633058544888859849
44351579178551997454420497795037123
2234472231559303881118259897641166253
5512800058091935666153363754878679668991
6075273231908075853137596572318194344551
255922725691268911433715554979411811864
3429641746741537173465585202396251239732
61635708333956521186318844911252307766
779654231438389999083291514490536506240
9567644533141669654672778160810393982675
839917163893039402993902937946863086667
2417851189202010660428149668721887191361
950452387968942248932567715686792876807
8066746269955091508145682808682550979487
7401498717221944352763769162326037002423
8574333647003323439494124301455729894506
5775784647139365118433181070449559991429
1180444781113624394664541519404785554618
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]