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
865094838679401655411478220049259985834
4894677131687595788760666987494263316733
764530717665940822966681341370312068364
8868485554622950862154874212357385721079
7626839937757513613760011134271767453158
6578483794431186916914312171653511912368
5450981343124316831522096613757472608377
321286883457763258198949825922371759640
2561511948254277476876848189869118402
717266109441983179472408394175872215423
6934341810806877695736357796832794446150
567389332880460461117117706186576847145
731533188618776990118353645762222981437
1273967843826178136682505289425134933182
54206984404321418693559508819073867161
8245111232692979319653858250961922479855
936269127124568854764104637099248605623
149127866876228318278544324618739738056
523436798551257268995353563630974062970
42653929211174596326315391294048241992
74671827733273243994681837263336266845
1517154285721860632798453834841614145109
6424737090440047741028375432611223917
266981168574455682559512290508512398086
9881834820438096812511421974857272553975
2600829436901968194613037902669028269906
3167675450767984366041168371622687580
42435462198729685859594189276005661800
7588227222226789468335766934500299173392
9449779436496457853808741086373449863
1499445953412176672899295327555493112945
7529865990433572757919908981279859156983
624195575775161611920014253315839814210
3993356548491609269025026971697122595646
7319677023968066311544973353203842174
69012815773158722203202250916230202231
72703130488564897648646586593953398527
307984588602381249726474905669054653076
892192742271272513539596909144607553
491982718427849838454681543283879985
302071119051331141869356792212180547497
54773272483258867115929890596762182361
822837666130672993733594310698697051190
3148861171295659281733883618788026631056
6384426280535800929076367328275982227757
876880759185598598516111099556337641460
25542796904745695014164650769527542992
82801862195670724447943306034332786301
740879355013893361354232503869398724733
801657849598142670890656538706553314134
5170252958445959922623215443573151933435
92024148268560572653790581133183927153
5321984095866390727436177214468817702496
484976732508892919495854775251487402062
528589760715070819485684468827723344
130059379367431232003206898476353868611
38853187012731051186945264631263984
748654239669672232632049023933891476034
4650327120331697752954759834142797526195
693825405112869602862529575636628291977
3806405991365254163418206772561251024585
51768642608841043059158802126465536967
3122781693975309815650922934212941241389
443860482241421639490256027530541287321
88478974331140272083383950742333321328
668462269453845168094771477836589753215
2901926732761463445018329629653055952741
426742887443160583182693701779476985927
609598131015524284978017477373675389497
771370087731227957639393848176149117390
941962983536540452692040133117871751860
226835319409622170893862999692214649704
6956762745675791404624307569441597335992
73611103416271262738499398813323441763
6656114923263675377766897447618991306100
700072093531801999259549660052058739219
6067919021179461646521515857714064827001
54726009208334976383645518753386131173
349665936456451394817988251550993987245
5910357559129829622561929603825086641963
223241718114278888802577192279684896074
1084632889204727678480213274510074675349
9498355320192851472572409071270630819739
3909539969264708771481897818471154214162
573212154960276217527351661665626881913
706785429833833888771812363850887341942
501280169821748954646314243197497342056
9978942362627475382722072577557653295289
945680902039270636804737636634440439692
1874602722473407208147338830513256874224
785257224011834481891717781094466441877
6183844746119910376331415146325668719555
89506174664060326453852134090076176231
3896089696421531904815009164515163551
54844383205297453649495538291895677160
1112119368443839100752157741295033465
719276259142153876674238489761164447828
4568997837457310677297921761734851266559
5273565398571230744232782915664891661494
600664268904335212432787642177927695443
4124148179486182895545869146843497585310
2492931850278831116519512865164887988032
71593565903790051897228088810823966396
1487765368941212976696231645131630793315
4196838258169759801178322551278554027599
969050633661543818314463575958854680357
8973322662179686675644197341641626111
2749845972417874124461737061332792084704
1774113018633901224796553817637219667198
99412863037309724442082637984962914113
6919556196104212869106903255468359676
86995656938270051278728377838760674489
647967408482152587230083215254977515653
72326286061968846234287980210477476666
7930769372761491931057747460615620177814
9251560592147946829486255151440833002123
5776945043710427071843012542558513537
6965336796463112240132814820262473223671
148988722125978476449771832652683402948
6858898228813309578863195939941473748347
276875191542626943542897560993798901710
5290727859618656113019384047968080127771
1444328257513217514346701348351952598069
5525941219512763606846415590458756026819
768112964209606768476419946166953237862
6683881235243796849369702356137825595446
8350288690102136874392828612644128229
796097911767981156565938223428437709484
6515939547774257792050168459831968109969
924327363871222035391531939856872563732
5253277822458103984727462360679690875280
51079231219918510113857189613982719124
2874581725384845135574912109872579742170
882886546036284015251707296737733516260
125639651328276960285928812757534473225
17022112518649116362257147832359872120
2365922011447014331529272998428928027
50157297764250117239871029288887323645
840345268596671750446184108725824412034
120161175171736631305056763398597981230
1887762454939868172154906019331231096411
5057259729808597272117969297563843255615
3961733283063595929898899060327159313189
8455244447863814322031804918912253326829
74658440952765866948716418478203321
965279480101749938078969714375776583334
922022081068816315091551004659268831289
70706159633992968964196677255560075299
47105008870623659507418906739341575599
732830542851956932030055796959452052
81577064615049247272788379734798204488
2830906255098734532393511566760692122470
23236949391424116587255783374513046404
9177308730823701972674938186476765145626
929838881753704845342294884527789914395
5414224275552133374520373010498481128612
3291566512451882913699026632597248656028
299302047447288334918732938477577071895
37028626598884864682748344459643809
336624528223745926638986397239678386369
2689846027297457687426421656491737493984
8941404986477812961020335528203582442
1972700510247209142275059460287775886001
7305175952886449832158245788801733601901
224542635804960236671179693651963864773
6266574057866988789645016933217252214084
839172526231884617141956798657632084418
7873589067978155760326561736591562267839
540211413379118375949003461756716348160
55092283152228837669099186640644107172
2834871054025125567827609360487629016929
31311854754147964994094385388938617340
9931413541732596379587595378393389474204
762595444201191695718180642514001133781
5998809022264661353959819621253735721695
1516367869022860381172383109917557274309
521663542232969714712213667768766219103
9653911970805779822176265108516816251093
64431483564580419237894179274538005400
3310649389881658567421245358155474109867
6167520337982661833397968912744490338799
59449873972633850889733454950421173647
8030359631773084665072633720312972786
28053795727988677343741736065856594439
62456204163611567622817410220772629765
6594855220585097988515559227927131269427
95995308675979625532109444695175748941
5032606692592199253498635354115641153504
6751763551349974978465081476296613841339
316997817335960173013235559579832432962
15414648192060996266159889907828357405
576991343588737220099598748568763504631
660675715994357448621042962374240747962
5228564812191151868317214486758246624443
2036446467168332539654279341182134024
553693087702323355688184432511119285421
319612533348425335618217902885186647837
7670392128053192332771053531468778276293
9602444540593921561653612384984797386
1289484362642598738796523885635481688
537237243850823752445538246420112095529
847166222777335695367925977492885951303
506280398161465126781534175880351026874
69169145473728189262261038668389391068
5687986092903148764528482791554241488114
3267301282022943714360546345294966642805
190565569375211129886467600971492608820
8937437192735778122766015068284457462552
4595269717788449143751453521606192039604
3460159776691151168099778292336675844605
225334049607304526092021345241513481949
379469669285975463665311212868315374792
9724287211167055903613777499776044825
8353528241151274371572817973078041123
4444363192138364447563354401691628797590
4213889325697780729054699604401953774208
5897717727111305319445545616263088526407
5886608959112063884866084372672794272892
38108281380895574563400766961124905346
46438381440269835537430789585762954115
9823831843511895519747311907567796487
286277173026109907557604187263989836244
53652555365430778710185969359866292483
7433545640468343120348064965466598245348
51536576540103967736924705293135762659
93039032998732575741931212170717587560
76105294204291310556128787545148587453
1342800538680374647118525331662100004943
20037589506295144573403208266266841412
267634571055802623705956119798647245373
275660797391742728357731940103146951955
7262358722619339500138095507770468071168
192180189445557775154482971849087637874
5539979778560705805264325539067094533
738156002740579990292958887416509490750
472361107157571166571478419956424226703
7134581841135180660975927813745938726583
283865379869395562894358768018252495
294746241032471768473078425455456394312
673463366414920862574987829737283609672
8620460790372176565710746317640354321097
48242842396443810103897719939761217975
27688513703556794032384321340682351491
222238018624763734199745231794851876171
6410487179151643883111553275666752372
811315495147817861295169482488341671407
7537912325844254985693079619866053855862
9459188159348190506842373671516667087791
365994691948769135143888238833243343241
66026320483855138705825890995343538122
63861768267686097934988613766653791480
18154016660431680463448662282512879220
814835459696858309136421958540665678579
214775684261308486758708530295015391145
28848800358456527505217857852336587755
201863451890380094836576601272361101424
761223129024501713723230550846416329241
397482082372407947941203403103766981691
7764933335504211413168562160761949614892
749243315871647129942236805660253328992
8824786770393358252439606060501913692979
888863137823610111851293746667514886844
700341311859962715498371719567327214645
875816201162190658371315819667813009666
8222495541736368803175605645951974349
9122996568732266815649991417114146627
90136519616137664438112169264096738293
75648985102936579485995631076807123077
5427623563341416122632679512983336872802
18775781701218862366966436869117485799
26592333510866685148434795935579984381
3106177597212855452728785876171335289023
7842363778896916910864413954257110863352
519941175528354724965409995990715959325
6196463677997987655949472025916592972190
2382583941565930532252875177629884121888
748069127756719118456182759560462254896
3244354671831637275266694475293340836081
8943992179915359310172541344407418199
785917838056630818028718792699692526424
49845632652487193976755806461514153399
5391212779274730729715673596401638254390
489814363569477990539400560871422297071
7993387970322110896582006527245324528064
6169127918695113587369954156605992048747
54805628473844288178835535917180097362
8700288651184615674771341903304721889575
5069724632874699858448209220683723936562
884011158803380124019567988657785411722
9675208767712865492541281477245693391
8836556543461531262549157881079873352
8653553498983411848354154491264752676
304150179481799634364290541715995647514
16642169192670135415352250410828826277
308265552828441559954836585173120241885
6032201963961290906907604765935201589
4241181520889165589522758013454825999443
28947343617671125437240211921868951145
233060891594982271121384135835043018537
968115664723829386245091429949573573194
414345075196111759017786679697929849580
31638116232374545757728410367649178366
2106235891655561996650837088496713023300
1345250070174839948069706179817585583987
569430768891392693647892634665532134537
180679969876389294110917845753479483339
98528782424492797580589099116865106104
357115066304733451906136693831199453347
8728701485607566705172279481157385346343
1431150268737437449790861023540945594851
927269071718491748764820455831358585
81899596326828162672423208814656441256
6877418467705851945518137852381292245045
1267403265127775900049335742712080143112
82403421705953777458444360715643642992
7049113098032564214793222619915222439785
8805880267377077140174552716279573759725
73823956831461652693562873726965224977
8723275650984555253784775921838977859776
307330086352520423873282044740334434572
502610419698890459432729292776236619005
572422689141325611374502755591639659937
61047978050430533664513599245892085318
257855009407863858145188282842574165983
127821861664302387239171372031227391566
5799240231915955045196328438124787860
288251056532546198536972544273669201776
318635975683728089245798986236460318780
625925687341911024114430733732663595785
33844951358682455311337898346026722561
6981162760772544622273992573691875121
701417115759844610443127309789653295553
40806702925823475722055694167642087160
263463404280104365131091841393055986761
6092281211837018317369536646546202950
559618702922178211599224466576125441
576770362054493207553226824674538211929
7565658638279911762365149396735693041120
788933515836231460839652704883586978343
5812687727598060906831625549900528734816
3365835818408881897724862291341240578602
7630612118242470917139526894975890796865
3794445531361616279747657947767898069012
9732138720374842363135706571711851395214
1559450278392607125334349909266346435568
604339267305524242138818338299230594
910212558491369333756404889943915483353
47219454996773035379933151412192101
49553734659506879843160795298878074034
76183167857111953424893407931916258649
1318545329429335866021747145141347
5886912751296660466741429063425213763567
9947794397232925166334306817343444493491
9615756924461641338558781817800898617380
1137352679792161364131542387314781834041
4698612598827943875653153979533055787
9615107759826976370944408711852721443777
927311303764165074047932616518926529272
862840199171923479313928474156796596997
9342116975281998495934425447022164432
548557281242872652572939389633129454078
7390409049693363460220891753873826223669
4027432652603562847663035693364857204058
62195552658760349085986476162997978448
28070674132740725426119223202834977439
6836998125869518810615726673911816844337
8899272276303051670332646503436260191347
3955732214047833798832594849346290232750
569492508879559626033394406699002621928
7200534862503356014834598708761419748
423045906595684793245973563396161067663
6729456860793184411566191682320348485558
894436612926363290385388361120499394556
601246252254151969787263240262684088923
8895343963269929681832025594224781123363
3758521081981935905137492763842930118806
304757024134305632958578614526369382330
211801852211859984215419239446447553
8025600086472638048992122283935809772
111934937674094351833997427457947361443
93365191536931159651492927899257911554
766639726944806739644354958680795696116
1591929249331629431860098131639847499848
8950673211346253222710357005887986788335
271058907918952274021754707865845287059
76689239639909332281557180611131463582
633862187706839608482161445846764583134
356087783824360087019674327275175263796
9279342265215540325916388102657843460
14421656367994947513807936014705145460
8944159374653886549576904217482041631482
2659444270603545483450416739396679958724
648975872022692863351356102656355726606
596044663758109212084546795870261311145
9630704498399912231482872436581887205
364861863796591887963551204733433955138
8406662856099125118166473370630062802215
645059982324371613875605288777532523585
27472765380177225868379536211674240332
2759796377051997644780374779909885738
183408716897998817499616529583996763080
827777992195392286267291816935328104194
498543496968683157448227420856720148965
7236732399638142689582172754554710455292
1727637689775381798817703925488110697124
161338827080240530696018470897620004288
5391544137221396546122612029945239682506
54236347434564733228349359832217400287
4291397599655769799876553670650691019368
954819304480506327708444457619031005527
2315732795485098900960458411943942241207
7148876146473302702110877858679825843046
990650401888535256284528220377076877514
9943821202645462872977771051371053539
6186350727199259454672834288924190597689
446874193377499657743105561285889957284
987846956234284284094237815553871517842
2334607812149848390531247427874986881052
87999009727572869577365748948403533174
66745250956042855469836990715217977920
5285148760147763678563363915373294519168
88678412515094311712497572545359132981
2431787155076243482767217796889041903089
5314337186054087669364197332383174883508
82576978152288107146118679576357933777
4863244671458278889399012742209924342080
736365748478169833553127050528435861173
780491883346039964330878147437222614154
274330917271915552049119220774145731079
95236182418780567207630807446911638825
83237476763518269519159596308854435703
24977466824250796486153643970168557552
432083177658905542517270707821737891511
20756351607674278464742821644013248477
737345910927735258017438265217463443407
114114311524882031206262148710798261438
1574894563569649910444387426793263523985
7259282793743750497880835945954495847334
133418372748571520332072792990839624262
9266735971014661553328744672275642165842
339396115795308550427279863585271938293
84306947479476227453912858647379064530
7168761094931916916657535569542851681446
574220365952464228687485190609013136590
995177098493149520126962312742132529223
2265125091184250456257683180747511108901
54567029011561911454775702262921613956
71956232183384757563656764634829128374
1278348445151938983968122331014543515
3692853546078452946127067128783217042737
850184497030983593575209216774624399979
11417984739233507322622155323163897328
936840287426547870208401923619290945624
835956298035526945095303436860041492282
311588045487518982862089487314935196771
877495745445458785572191915991921833994
6136681238959800376856108377344417684155
22336503653183526166468046388583164309
54037665929485957886514454728998026380
2801436293683894558163821855974278485259
8282807212717597492985979989158944958946
48402968918229168398765837670944106461
14901155423483954526201025054088383282
35568745098972793971474146111241337075
474285428100186839409835712556064326943
951810936081742902910404439423370197046
49291711920854754378032936562718297373
843762663891747444316304545621630349105
36707854255695515673187747021737581381
9825400512726903174849667876266716878785
58598146850171857462528682056795660134
91753275927749211005407687771483818548
193387443486771662842509227127936641813
10094957256450763869029174808318741219
3970231554899049235933566591241464955213
196014287478808814215543495536636806806
67618447542777953076325406705456438828
3087526437568014606421267649740728499712
561125078353924578324831902869442967083
5410962175505151730241221701232783991577
8454889012449973688380433105122280173803
93385190614481817411638870812454031992
256913506706936459938094624381402303117
914442275855591934663192637287642285
89626273670286041329818480890853229652
761969486579732413896392966043436521535
7768525896848131319628133690424281312157
99118879688144663337834557671532832835
69674327191457025375132898986725725159
32612311126144833811576163485110762879
3998398684547848190244593752898590328525
2986316261089205158192439228553526629319
6844416778985106928572124120400238207952
5723522876166431599086453954265019697606
384098164742826052844498333158414318231
3383289617181671298692098972337454433214
68983470830569144613496701183864081991
67203093246310643498389398110550448781
7185833361257492297073057630330116318855
22549034972886445075888301203398973796
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]