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
25731019688769975442847315677335747018
6329652552139339646275807917255679682782
8432954052724031460552466096421313611203
6692225760333625164037739698870622949970
366416131065471467163506128442202651519
6323456392433240135268229693340117743205
725382824597738442108533188735077820151
5117321245311261612341267749470996868163
918352637066687444498032643536036935238
5521573549466669936026214595848076951960
696437533621462930854177356949815088456
8112738238856482135278526822700410588061
31305050725567167485207185656112729934
389420958821586895014701706534512431323
7968518434115115616058215370949591061113
786890234117974481378788697795767950
2214870356351071456332859018340242213797
8637603690696978350696877105685049106207
5508479230868556311798399499848514512081
9960687539863428384069705950150065389435
3794310576828935439116586730575858036560
54399141787430747838961937879841222885
40914918502819131062337572274666833963
5233184728745303929512734604394789839815
45657086509577060694926213334118331083
266096391552643567773608100619155143153
9833982337567148348396576853432666760
8930674870743119162585742584371429977371
882325567248328697278926599561530688580
623884068874739637473568290911535378295
5058580114815786795375562116830160273079
795831693311514395810066935139438526746
383171089149467742372965546580997869920
645981478457865745430769586723712314
5576474155448287760812631813657363378
37072609978577419028082982324163909737
8116473811757318136590735789261331704135
21707093941551033825395240881558266191
829511268007422168151262716775799781528
146051892487932414836118284217032009684
3219972120594018199592222155338278214724
223195393348151138062321720658221402912
551289647675265709219696726660486075588
9554509573341929326681347267742464681822
6073276376336845305992973512325298854978
25288678116487544996679030619139769084
2948860682314671482357399564639881216660
897313232103262323339548788196910275149
5256243183536772592614980955794389013
8992900109978003627189647354916367985
3953178361957155300222226925969559573134
397766873367907391388915306771964664732
3475901324648934609332966292319312999966
56362452663723933532817767035406758832
691076792425807073699383539451714804318
9714257495114108120612079687797101808
575658136446969624402618288277449434863
62572549533435788784565146112105640
185330554009774857649159408619784411412
761788374332356845483580613879604007303
4121756888154345553894057914221984823971
9394690010998200823368742628437764404848
366494061282447487698749313700572912879
726318039273385952729007602792762608067
31153011899682388963625514575766047898
8188883052266764985317856659801116743885
2774258253987518100120145135980755038344
67681462632715149551821240305995682272
582842897690576710024564178340775501984
718687106208226255946166859025013568113
19864441344948347273127437983824848989
769196589849211247634045315247741983349
984015314070715580347614638376920678439
48839693235278336831408557024231863
372289620535507241954982900178016192521
17138039389815024809576320430721087619
3938332991409199264563933181113157192178
791055020228895464435735239903713314281
6986183871521903982198542446952382969660
2445773988142352611340411651244982513823
6035439877128414436265655265891191112711
142057063275586906926138444124924733359
996565375947699252643469684045567127
3110736712482846830988379942105741959698
7861436863842209539347665170690121503106
3761762889776808335032229474317038416027
8775208899541777209163659490521195688717
7947728739944881610761766560522816336610
1645406982158845919130634311032559008
9360447469865243240262089703616138266392
26731425882976316663320469626325010844
5259150788717979950328650141478228937
73599621724795684649583881828816992002
7518976269973210842937477714385961824106
618769697266944111703631702815634599019
8276748732537167647433766515849273257200
6242682111638852854471559608620541299962
90748192123908989845059317702285663142
183916467216196968843257606783194158172
902819338517558486747054013756262338133
345384136895129014163775742731588126491
3044441443328534746636501767855168671331
700140772062687493549002563841474954303
963253945812103451686141616296389526576
106021167751980122656164971682127803897
4661902724259151639579037935450569914349
973228787826906737413981531211368461531
383793551427446779585344363648988690749
5058807478028722216228674860954795537993
2161949380362893203544533926431313068851
7303905474983286822487776626754285089959
1416898757655685488358377945219671232613
9374927090091742910663355625453196424249
141553847695836318998302556685688459546
2634952015338913290820322270226055097467
59467212307186625807710112492454709697
90792638484471598650807132568843698026
3722457585497726806594316861444297542603
5156217289494079048910886421231372788
785628942968749269814468206120411832931
3951105831971398928265028383524490914
3463568086755218585498896756144385534
46843517508840803119698469339181262616
210581678509268845226297920497236244355
1965180471547643911583912822809877215232
68015650387260697142908121742014927929
5127473781882287859916372636902429773682
9487864362927882352297893181292142257121
526410666556525963447950610725249577935
5864121712004735351854904861478731225016
920054448961373494641628606322318435010
498114624564956971263839870687457124258
391829003466254857514374129571796756080
32907500758921863739853715541429448844
884513013383175214205325635934644827632
53825501086690646859246264596148988872
681807011109126850711249978807897132048
330244343097615316338687159917947167940
207310242086115933185159831059321198980
540148494344966422479677402944317275737
186069999266782510981309731205215034722
6371220922556259279725884950291290719344
576565634190387914064249631832363036933
123239762698618469194703873593544906670
1205346927089267376485884078869044583235
7228970464485122610676765864751968564894
96728575731046946534259833746312214975
211463623573575079354489663058781148229
6066114720938912869098468893199648198308
9537441280504149115737929986725994037357
1005939565965929683048663140442522838383
91032554110624505614977974975147681943
637234713741599776495413695633957543225
7558509221347684226910563291247351250
2371921444498592115789966668312788041349
108054121474734476452828951835226703590
1044445733048674538699185877337845031401
7514293714142264877214313133152429511282
6022601533705824964929149118300667933571
844942773663633631341746255867637268858
9828952831531880743226966550826263904262
926997379765824146030239578284140872049
313085303024577759020303597648816507634
9758933649443080968592863712438913619012
154798532899899482125527083543734470
3586249165317466935619302920180023344088
3457544719025202251938367780757773493105
35078491911681255073615855073522605243
7208298942537427930059527831735472309127
9834386266243666409543791425654968568660
164875807462331079701121501207664687123
720042668637578556746425299317246709922
5401964914039505960796714509915994165952
332562583909001700763173597441094822267
8812866928962256467759704271290267015279
269176731470612971035655400731236399386
9285371474989406773549538427806725762846
48913421647723861726858803848025819776
6567299994862239163243477920824879257047
1601687153127796324062377355303120911592
654267783622589220922343234552535153644
7378103112851925219467049117302382961987
3921232262633624151148844043857550145358
761454861973485678357988493755768303562
2304874601040049378177202739847493606
834562579988259792415321642114686835720
9565197464825553527943344888233393621681
39662963877767342223638479644296585819
91928216607971489928559663672949553752
2170980719762295392085315838113294058291
396899359742186889866935185207557716712
3853756064339059405891486341360541236782
611999931412058286740506865641257326912
32708862188954642009356148036969204129
5002450428320828523997418246123748159
2485374647925461717410789916401126332133
9212579326963992456222021306323085076559
937836036295158844736318953289065889734
95252123984463746752668127673874618649
73685117808326624678318757311631225408
4398811084077307119098873255638410929950
146116972162483555398289544918699791603
643292421707724400741926852708840315541
441093871672959742997261743981960774725
89375751184816363140531156894696574200
85652601464915011589764188316268074185
3270894973118745761657783996655240873090
239334645025298164226677728862221211
1040270363413156400414964028374092913362
8595512130852193599542524791524970026815
101855317798403528985886648696546369534
7095656961513097597444090447844876900
522848918618708154250194786347840195364
37877835348140892385646782102620901973
3412782546972620565938707199585551766780
4432616169021996798151163869529725985397
47985663469818475997360890015591344021
100819401523242319716099757501910037615
3006919841325524503837749321368044185191
416417513025101269858474378169237051201
3216103259022970272184591153250259631040
321635175168178916944074886734074278647
9123866126018218414187956874595966284400
792845265072428554034866984183204197315
9363671669188365353844644106739285789
6429782593218582537886126548803762275571
611932087044033635432534718926520449993
702663916751648170080282210315918308814
14084144830119310621663667816640918753
6934335252897825328881776390440526036396
182072509968684724824167574517812836430
38041827939777972835358943151934583296
6399912930113556539254483707377394829480
6183863566386708528420813248961478647942
8018339332817171976628763889033817050
938915381453494288436259160634683646077
2166984639337832496250041359568474618242
5939620636679045577728437854824262209959
63625576918955457156613841545087036324
6533965461614596425669695268631343678
6904689187441209902487024816742186438379
5172219245258504830690157006939183927
167586581179427630618975774588106987408
8580904277879189450493703836413473018951
4174718755481840657912956931247184318958
563729627701261835490698267311165791143
857179965199565397243934830508634212244
5358182114585534527230705016929374482092
6945337328833562383397607035339142178485
7113688324784763235970191285189819372705
1332421065224894700734649252263241947
184310128955582493048483295949848946452
7792646994249045211526936638010631438
989627084216122521895238586016022018233
732753692201540848577965407156133297001
8292997624832796696495917917948743134733
1693141957246641547611751668800771564375
315996188737404564372185205631989994239
271188422320848145174361024749699612427
1583050909922426574013988827110386693
911475893488483863527768156551027466735
1982802937214791448279271937820227296137
2190144110702844733569888574517802368
252515665546580387075774716728789336320
6071315791105642237389496749757456362141
851995807095413311567767281095988676179
33465887690126724741754690975269348736
562762064715730540794064077314713369664
116344925079769725268093206849197717907
79212351944562569553857972379249354208
7774829719369514609226398110198162566564
4205111032914049738055582320181521296021
8377869391702336911411513036151928466325
1900309462174681295975917246315526823
88686849697541961192308528458940958743
91432099269621235544127590689657675297
9490573079727804156318125361849028938973
72997835150902775767456149823330561775
27455414993885688207965275271094568751
311896478183836100848384747388750852950
835893889911666319611785646211241431051
69721212888245206446939280249494717575
404748941178380233553222288373134206447
993448593649715615190932519673218425476
537470802424740526377886954574831633331
915277812146505488782988134208459974358
900733076062743531484462179313890715365
919922609489357725455709287866828928460
175065974813548440376323152183356489396
8338557555494705131584473027211492808860
734180139929919038308831607375698433735
24984771189139631944179788594647434696
92719315157518929779767881491113643261
446164455066912062082676107928834640219
7007409737184814615951881579604067637457
89576701287955664354426224486886989551
777949776054527030113741208471750196135
95796983579195929098073732973592354217
4606979345979030782840983623789972903825
50281405838295436779407866537686791222
513629046743920136217836124716538184976
4969915729882293519868317401723237431444
5671713490968784657676274475178714965
6751668334472034370096147928251176954022
281546933772424849124634304238478858365
6655924310402841153729598216780769712554
890558216534196061335141266159485697551
7083675137241176965848991581362025952795
264063331813951587886860193867241987419
77711961585471766728304219926975326344
20761363761074793901240145898076328
9570380379811906997557284466356692614767
14449273091520424549698123405145493014
8623967977777762683492511587243433197025
2980251755631248427937141141114972405613
127371841252504746853490611411521228
3699897765758115705585586505974955805461
8730774214505422157197125130247734415252
4064816298781235353471983165521576511336
377977983550370979297851287277962392315
995811055518294172561459301859393507115
39296554182098725913352089314520828151
24492775647121692918659729649323091023
5255697012418646420471927056105545979176
8195938894806116483943908907799447446869
7127114353699215423472304321638739311212
15914434598274223132857131694807494689
136962504335545430761931998985188936814
219350852315168369625569989575642641701
299650607129293155713064266356627175
493492842916116984855707574110968078542
4378111207597548000481977377241336428
8748728564517270558116778907902518884714
296723751655654885150796586897116928304
113875477567906726456592879254412378561
145011533505135438727953883698268306174
8874627688863907589198867356420507538
112170096976448432596072038636268825763
4152779363344208812376247559726890863731
813483754839917744699916893602389725917
174885159983421861126581891213513306010
4508895767822434955414396143194165324956
8359553428005101882512468647679488752947
7579132115828050938577226818556424565765
5656691813825031229593539449379851013
886082068804459413836107472565420466
6231532260185791595942585414657578919402
7716303350102963853268782519812054522197
735080077307418599686354547134917014430
1361618763742574460930549672480736688635
6178235322816763723966153395755316355395
6487247293104912111855221301411101230
248616182168893199396763210251983597267
282037237938623922648734524391258606
3711761655695311606565604954714466499625
63779969673526624627504375982934896350
558060509147829363144971971354433823110
689653016545624445259403953672495797894
9540740150393200955741276304916177072462
642290295385802154963345682247675654529
309535461585378142265351552676177654062
210895757532928879897477629670562626338
622469347546259349972613484043094907402
55472671941473356326476316806347992807
1190577985208811158350169981817575305
3833340134098769379384106999357465778658
5182228375694299319857432914721126259
3329757826642753518576339759682085745587
1654155769025665891383428497293037876025
267665168982651187706691196582026319992
7695240485622115613225104548906444555096
260678287053376137239328683296056328536
589671956311049713929085006932346362232
5991398907545922974735585284116680385
1323105240243804226168863921594847526101
53260997055164753239064268647929982927
1594789894494271299559381944448179727
3725702085742666679237652131386313334503
8582120132544639579930447966875767274202
233839126605442153855047734947276421857
12452713135529160998644403452271821854
788748566049197152515041701623268325790
197345473712230471614665824146892485358
519051152042948219522032658592140923860
5965226890106824492670271722575577774068
1871323076127456679386487806615218423803
2433998321661149101762983086341779407741
18496576962478824822513445582860831276
4204891485525211309457787750864692211971
33494796027728102992669614700061458098
8597544759409321159873939008211477784123
6410184148592820783479049619315871071105
29777743971829818063461589557899776388
2355697938315190176461335991775780
1612680078116260617518073819944832225337
151087072240819099014785556492014134913
959231983439115248475154353051071389
66841106908511961649719047823936144828
1962285722506063840731368440740640189407
971622953334534942258099569188428303574
31601972952485743974071730261695168229
71643940249162126577385231337101808804
427787712592127819723572610545671270186
298622597216116632938627869858187295567
62885690777898243773346266696918582464
268167518923410697627815650351311283266
4179616756992540531776733689720087566409
72827569383145255650817114868959393251
5964352229001110512021128501791477184900
504224788213648763869545269242065233043
6614846138626141424742829743873036425170
487749679304411571301053143601125454
266583816383323252784574934814432678993
533863551404584387621843638699956173572
2616547498133340262956408151537748112
3353116658488747279726822133658270803393
6355703656061288673662839052359855798309
1162734153944702786833971835122719971597
3336540056592459157253711596400117603758
7423636974347027766728489002844761222215
1498823048066588420459814887326379277829
7945995217957451794485835246231426414
358106926547815738598935067958059388044
462411404614664352176719975305196548240
84756867612488810642645500628994206894
791051952985328094859398357858035517978
189857278280211574285854724602847309972
5013319104428995542216243932136699527
336360935770512756341962348947273846686
847589196893723377104625563377886015813
7864575151786337316142428556413117992591
54838796131831472578858588120464006355
14628221447972476909284508891392128901
82865535669986240598691557633133121054
8411857931373486968845136759532425524681
4328297666603060876829726302371360259161
6909968047417713352762679112742822497159
752952732133215094997264356574136414888
43279185786847524615703998769269152314
4316274774111264491989486977358838031623
8887692998665830620917557713779298374445
479560265915641285599937998504783761
816973163572369011077881753792346303823
53486588342210513664391315830388484086
656829765535218341122382230516627216553
7657683212925892751768176044429584038981
7412299335389846950566184374874985
8520560461572398756458350307067867641
29898473279428612355994019043227140267
109187937759225212940687190219155451
588385360835813129950019551981173453528
7289520370034979126718193415542699335121
45589525180342032038148088292915947984
5980947743059874899555213847451570364916
9138539189361748762866933917098356508
208820064480957263754494321921882891275
355826233528288056787461960483089492715
3145768455025799670877454999122491802989
50901365258640373169206406632930002670
34020604394635787035434922498965659
408816822201333695990424122606044201824
2525275672144848777366991383910945980
731855913947194424057737542899302810438
38644036117297506094273783107225429707
52174911521022607414372165278092587418
898062379627563398084394393666213994495
27643139704407910026791690506358445963
5829608417094168555033101633913567307027
270514569788701428175924121863940792332
855373153849374389378722718826339990
31247475472542136430794248534333834499
3750918432719019993819645177923725986836
5030701311977062481880485135871748163378
8964164942225847601532717669533155157075
483982140773448647969276332882188945829
27537441810510143907919775877565453718
2425120881883373168399663165163046368
5195827082186767918633944067648487065342
166597402283538802447635400567277755404
27950567418106963016949490783916506094
78847665657419878921165355786816440675
7391709641979060322441767474499547708834
38037438806752379387547934286382949889
255591773962593667567165622356581536682
189126873229933382767372754629791938392
1413574982845016218239878563641067533
9229566395179445481853213889999669613388
608818008149746726867476658170393904573
545488510172358277430709146886391226046
1405660350091353110258990267150965171
399992172269344789517906278398966751492
6617359632031511259294375736190295631
4859702564141673632487390725819035159
4464959238866334555422471764047237915
468942045042563744905985540665219755247
7490864431878890960574775587505238516290
8183339176023134461829559120273469603572
585252768046767775032155308057579312
3430867326881728274767099906955068221921
586457784749986137099949839734897757062
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]