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
119875899666398381621938880941655155433
1146930641745500398183624085952581002879
2268493286315039840271635486139633529254
1574955015253090656136855133508811972451
722092651817379497727736798250874238
2759509150354630808658621504378860932022
9909531622759987134568634055229579035
20793049179660301847492623819679694256
5754893263773211790441083901540373567914
754490888601952133726989196815819223943
10394693644119788226101974812696484180
79557319006814223468881869982939778516
1429271861642687583753793472165479187172
3322654489531070173935523231560347428
3215769692347644834341132724247716822943
859073624755290783694703250520025151553
718730801308834877336105353164795547606
370473977251786164061691168993493986298
7430827792777233537927315606835139804949
348770839397240544584002118750467556860
9507616234495151989880594044656393813525
625031753464957412097798154728996239706
639809181761743738175183954815691077426
8318757959531522150271601006240750053947
479186926779957218442926623806775104
8848485717129531731169268660760645363158
1688859945368958666091849586022032864
877012956752750539805603161581469281355
374984561381735068241848246266923776409
726458372714106197589996784612055064836
4084347036635507527431364963317295570
59734667888280625653548531414094401065
81013617574906230859512379381218378616
868550208220439376691895585239399109037
3560328336923807495092765382304983757232
7012947681749041353049714584486984913841
461734246024764141910093266749213133189
3296949865876363158769461761453348623
701517405405115832792019374982535036688
6583952625436937114449440120395334198
92338591327327592528697827912534847436
32123926410691642763508104888257883130
238736259696111321660511977177161394591
7507810077474293527951836245992475167886
81186632327354488741153500302366092001
204036997297840315296577849834447745195
5186324025159442858655582052572871384593
658534713882863121887314475695210331101
54832485538675574915290824673986108706
3535855652347536810672423736888742449
6960408991768798509091019429961717932489
305581853713142298192606291828780033791
9287392310313504786511783602939273264016
41431252682439853933812271350386882311
31362171051925649466508275513120429856
6556829723722548995776712551378770339220
32395142563947076909709574399284795686
142191292137112957015465571824029536628
6844434573733417293733342717652435794447
53226747181237687578289356124732480
238218278979897354398068407108036355181
3975908288947600834873002865955930513659
408831757367785384805296832511241637548
521779311481436669622752071737011459800
806154139092115784873741117566979629605
106146841876996268928447142498989595463
878175119515858145242109808759692964095
729776225958986495954215901871292673422
535123435806688219186762296749520818657
2611936020769333654133189662465986362660
2785330536467731446941169295615319784928
627383749206300789084048570961719208099
5324844265321243673851592285319483661
7532562437244404109321824826431133846
612141835315663020449733545196854292147
119692157513140996991765741072811873620
45128231381633437825552541243799836660
321295237163851498586222381859984144940
515117131290197377868218739129776273634
90555489802868428827020333514571668720
68014771963993892671454420845898788287
812519903656742214837895026614739748399
9197208923882317192766234680828270416526
3025190087965957628774044311905713435605
4717857079178520967753744101020196016
3022419064021559462634109364630472382613
642496151816484904469367590322559164539
5032571456604446811536546408523756171130
9931525318106744696176854772873799006455
2487254042156844265662004353680938804330
5111611330954238166230184481581865184608
841333263738587328192059803917438441094
9594682760942508933867958803338013482798
725195716873034853591261640547438523
8032567959193591646193838553466210484912
330919741570924828416623814706879591164
3168161610497199512949677911220213192186
637756272948253739872615307171341147
507202854834684510593433218577593569741
2871689591092308969417379861930965266452
30352408136290741204307192148379189098
5673634584265691118767399718134443054755
209336469417842341659125820373154153966
2742622051421862527631808907279083813499
76051883324180641121965884968009986973
1735147260067542521545927677566239643948
179493541087064388673021838807040157323
8354927780144308378035473152566578472559
788016185466979330615708720277949383587
2918494384782026834896814403441365724109
17099007951506972615942744538892198505
38301199571499454571578116250486874945
167561054565842336320972646254812162084
5950975869073402643142665309352773461254
445135188499279182176487213613434287414
45405419968382978902736241626235528268
67668458335563241119151114784775685319
2438049958898368083252211434289924327
19861752022118742689469041156834294779
35738140561815180699376805692632250875
18597627531041896502149369791950941036
2773666177463007611029884101240986983754
6228105245248515633222241881077021362
288936343351127894368189122053717444444
721621838129724169575788787829631993206
520911270789400713741616894327550618300
647951924627650758438261866133403134276
5329462997484663565936982477198530734720
44643638642387248143992159655978978019
7243439821286580350679841267718360231695
7660409565894534902982566377734061565
30979120175538098047781128508642387209
55289262300993664992747214540174106231
4760301319476805861834097624840827573625
500766282876429965193516404613088404956
5239646218908437152254641217846866195617
74788860678338361011933339833159678457
186482408742159152233246651334121968529
24519996324028381146146896463619527
817283487162052999559833151195472748027
4056348733768561440576864046839979664331
232251028216784369279074830080695758527
509269866853300827085639421769874275323
76742693289304986374225673480356234818
90949987395120769724701409341114160732
840643858421472151168187700249211677653
3715817769242992858965222099491790275316
310646353488384443547764755141557637165
400890775889537584786656578876417730
4448887812369729671216716317189673110
2566550673262811876874156533548663811615
435986345691947785669373226855013183869
6383336871449415197048066666125477889261
332765973387470918834848610819586205586
9177915987822683747865616716545516419
8756346530615760678246768908563256775278
7490460718893693708297153630199235697990
2991568496774144230783008965458475111872
718250032516459845023915645530334974322
545641724863909973265556551272452175836
846857595300653061987003112471984411496
769151847985526391741087700188377264989
813513283466935140585415230996684861617
270872370146269993114426338444979651719
3238415576401553213079405967589560561921
39034829293589110450638284699937704122
8219536313923021182723892010925158912291
882749876842917683769498329316413769
1228436850331140903893098832944478577
52591316930948359505472637914556403392
5186939975302732104974601162936752663208
1586248294107919324258702268659678798
1827286360467185874462519726201169252
5626913574102048525331604086875290755531
6350307660147430806766435015616076427327
9063366116416016750569281465469873015436
780019101749744169023389681151328311179
24963395698091271561447034116871382871
5728419838955098945734751491835825166
207662329251210142973064831391070688578
3719781339675232494087542901160446319624
4075266569263126220727606466377496313550
587671996029920079585964555122474431784
147258146299212544589281820846688974561
3511282580197891746525762780445778583200
864197257664548366026931534547320169967
774822943948694859634921263860197537
303264709687566579283149792501627764582
49659472589623017998379773918121873818
671532533281291988619129433283712685891
9719459743045394435429334581612419543173
636384981120487979142672406165263220611
7848252668673375585580506610787952379220
9889689131114401970934751032200652055969
80211718990312424507956364712037149448
7043391525134671850799223005457094954667
61812032563934562542873714264069942301
1073204829015929332713653456357838639485
654724357842234864018683191260718084160
53451039310642071981221127716607884401
6620536699103167193679243794577793777656
4665294658355968868510206431247757685004
9614877759635919703094122240140951437312
3989801064055617137689351272304045124122
719764252897523403671701660182029104745
7937508662869199923631138802269622074211
651254814102307858562491471584942403931
777386789227209877726767498149556044581
120850896456265922356349987422303225
50579541406329374793899384748567799914
3992712562876835675513499960915145721588
2528966295617275752645444454120555098800
788014796578422620852141182302146256775
947826258881760706615431538299694086
9097177137567981773824593165315672857614
8527354547667272477424419744923654945772
11649487638986932711633105888620099422
53376757249294254474842612944879447691
11564200320547925963644118064659174741
8401502997693543700581337179872641588990
1581336585505441778826101194556260044956
844446686876297314056575734687562431995
53199268072705375432049203600967068644
49490101616536058387609209526080478804
9693978430306180348710096909949583036012
7379915073642002507644372748863564997975
892317664190596470942676410875111704802
244684396138398901667408312974739744108
587877203652168739645670877070867059088
9592778197764565652678342043370575773180
4393062395373199898959507082791543361
9190932497421176321418523848900575217339
535527459927360234733897480385282402180
607912614421957637171208661656512268281
642859646107211591720834071956202226
6993664361363451531195778815760327878757
554056531674010802560844003269943754007
752713066828405667718913427503154825635
92885838887025757197901784055349582911
207964885185849651882237654324567544804
1819700840404849664455931361874243809144
363950577471415864081333096616786827109
9951444852446927768057401620182399825352
9571475530626941563216369295169047842
9608409397672360882756067227624446431899
546039463795516899839832403804128843722
2774296240413531323737879804348390583446
9324262577009442818649866982669779271990
821195174632620653175768535804389197704
485373107066551308774022519963771079693
6605797971114495649295141752533214003898
69756122045837463187655757865778949647
2920115416122623674854318994557843398914
3941947150798718204797793837295420301993
266985405600436697543604230461986311561
66001714598616630270501476103194006270
4840696109243323402778305731785315659
9046602229549177857350049528201134102144
3195456728486249190419541825838559662768
321225469753788380230943301798374164460
631044071801801292497936687075230503946
8942809829578209137427677015368951934446
955945597560368811112403459294181213287
2291238564568956978362799484105130457
972668534326549849885195000664375007591
281235774225537145933419117855193363977
396268376076762883063743220957878351536
44916690082515773694187016712006580
190357961370541577322646818534913477094
590167878646592757944899923856821974459
940087267320746689345891614358433948760
139459045231205953981363153655385968338
882059668736669735523623951564761788952
708759978226630781258536534548975637791
1121873266423639406231632404834949645611
96387054815238464969948841042760505088
27864342216435483971029356214067206080
496158347369456660721281455107285646602
390475652049401435023322761147539807097
2266951745274445915370718935397365817869
4224204433332580487750133467340676337048
518484747762380062933201257541793014754
288528806927128989059563317410257856340
918507687676054211317353723106012571054
491856504619339028527739885315185023251
472680071854345073253697331516704810287
95023412065292597126066028601648136152
182325822435164330638351373239348823403
7574472031954449953038108956585742303987
9232104689653867122790966910677164241306
4808509183036399978343728881679258678299
4838436821799030240098715919465354454000
1269366920245623642235845035768923448126
43487142946742391051652591227544536463
469912569402731148448857630414072343908
603846790793331327214509609831668179862
9600627816507487343812742055778830415066
4648568196895105744899903022748027063726
1027703272644825920195419913443270689530
107366643729798335174786161703667497887
9389280235455700554585083410940289909805
8705471980112956214829308979699294307226
367756032002036858416339339199586785893
55123223417993983293786506345698895737
8008245694017196883427557024717295464868
480663297961646011979121249585290157140
4615173034327095750674963575895530583793
6744890175409264490599284288590096281923
61452464481091714734909715177218169732
16956036526357120131745698103933704565
7450112262584540903968343303440339652538
195077615752281450146167648728239624591
4750467052614154613436441225325953113369
915857127132841534645549189247237364905
2607471489313604314813016254133878550
437473387271180296629079103533442071796
4275323015128145460665485945885323652310
70051394760386776166960588460913648273
6410794935324169425169249487710615527621
7930953159819696750518518592966136876098
1558432748592319156599254612777601607
901041511524755522132557313379842637674
50956648185106928821829889137016083180
3477737496444228237049009626650073229133
829072034639717850797054606651265261683
11012211887322322108014317679933437258
6397620336789922274284774046992686776075
28298848406648543581381398938051617372
7703743087479530546860907525142587889872
1468698635955555536326872646359956436004
44199492638910842151875449185737689116
401428206872364733312426237844160453090
45656575806412132276621436332217041525
501554563907327528355321302053692501
6037987699383736643176523065776614863466
1995860052448110382980936890516273364183
7596439064111825616641798212394505353
31088361913165724551364407160792999500
7781436391027298258553041850227762031776
162255218105696938598928763221058281823
3724895253047479380426922189523071778515
861352864414150622268505891314093894432
2668218516277741436880652916918143113288
209818001456990124349147746235519457622
856146519632725024483012226288066826173
83807398861636788147868819447981624538
331490878825957432703919497891841906472
2322695494672739354572857530742305630
583799956785533955538369639225196129304
40824531304763274341999677128725146878
3226737754327337425986578021983525802632
238434634718846792248532238491467091647
4201645164856293918619642353046983249
958279354121493951282672778693183929773
4327201747942308514677397534704657851183
920111008949532341328301484085308783623
9137459430906025747949657813477372296173
1838656063668607575190856942734296358447
889747921080380947079815438225918312580
49267899189396024727709745730063988550
1144424556186727650893539709812499709478
1052821088145764244565527230662545194644
7368287790391296911277042898941022786633
146999138451880126196741163315079634583
493802831619800421874566193575441833636
14437316353729605788385527486284166355
1706743899498182865325859359890626249499
3208204132958477560785175011893338521204
294615984617575066725787257797230383602
460650820848083124684816926631132496482
5400793176468924559479508056982782284862
8041409930595695981512525925639176052819
571271127177268288422991913128966297
24527391128466082308089862167521483006
192300074693357693021393774172920782340
480112967532326126752524598784872372856
8155149108638994134654134344997733939
998999793836365528571443604091956991986
31877840530896499878132158181161209868
621158987904060926266917145421635156683
14131408396091636307321749798324497274
137189384918880022424333709573219669152
4867225332713183742951845088976155615251
642846147696749664718983989378941909117
5273139460784176736231593847810842433606
110011443576857569170773839443069228467
599107609142256055409279217387049513
71835764514952896504329548114080775559
570492875562462865677290325945188449683
5100114431129810934735503494132078406622
5635715195378220365569321705088067836
21986366600708073696866211755931977279
935862885281753010151820126786845176968
729298787520487263171373768526292735871
431844274656966972320544667525846762068
7840627856719591785370835454333276476090
155112887746834961540344480714879871298
1093448589643332320463638261301810235126
4653833370132203446484674973762833218191
34431656379139148938660761771136974329
306539972622813810234311419153979864794
1895267585623137736349781606011174635
60032389453889064882403460734977991829
99986435157059855048900341658998433926
455280883812592814285084926021948360
5830402025176346272277455454894766554185
55804654315790640297546661834028189880
164965546051691851362513143143719429264
471965464282484914447634895962373381085
542936075580920480294709660360687868654
413360573601525994865623420898577508283
21245878391610431364643940435164176117
8792370418926039320530716480207293471515
4348303766960376823614463079129917292
20685966459448456660839102371992428076
2190690496132417479648615590051441767
7631912332398128488722479458819719855
7350640832686961893523152926671092133413
6473110914109389553554961116655387691117
456545623858761632316239791661819902400
2830543931393107986094274998890611418477
2984829670538361573557771779489817517103
63471543792995901592692470103779782292
64116129743346621553397405920617864535
91659914459887433161775875453292761946
1321758355792341756231944902578132617124
86676660713293236083842597970874936971
5134719256943038798186469749292591014389
5689312723953416861147983903505444427605
573091706590699969256844857855048493450
191520051949204375954091807534577458
432782101662667176003997807539669275838
1692385992596592874181034627728486954510
8906377236340546017588972637097104
4378922513708816965197967290830291912051
183647544183638184556696714729934911362
376193967281482492793335264682150695258
7453716143332366290275644768684379199774
2895347436588634451865676558674099326
6214753497915937783243764930179982948945
74232870551573261419435562066495519805
55776751358694882003899884723730473904
783010344438100723241016656128864317228
104256199934149062124641702734016697257
4240245473805187826717846970676398131118
616433895388645289556881917782261528
98236127393404055313766625187724331234
176522875721562928616371774435771442708
730065265803912728013902711815459125
566973474293226256155648687188116600238
81109174505237942903556441815081721310
620759585282921383105961189569381418775
201157827957400230129633178382976459292
2359705029354167275255371155500773755458
545741814074915706962015513110829202535
963284905746484775202623702512298161787
56291452548014655662160560156342904137
2822114592357664536746193361280412506
34683802575200142965012872189436758486
438067614912881243953764632414973757219
28084011904142855012725003337429386152
5405958589359802925641411787191527617489
3946993684704385404116855326183367319718
3933703544565195628492652376620091264059
527697109621507892532262583236832941052
8491724264837985592452429969381079681591
3215351374784319311698788124818230353961
739131987105556291551712272137740777688
83526215874688397199191125169715226127
9068435768639940362797867670545347916875
3404813584093339985777074667909934173988
798253439519121848112578622562721132369
9800221039243837472183527437730214594399
1770254759858039615029043386911186507491
551010837683682946937472473911691810
369828792595902147549063391984662019221
4461207825785117141419357904416419709592
8890867918289199912620534036798480008569
2610625941836938508584063942361937148592
631378619131530721985074022297572624860
39569256299831681351571690767936547800
197565568670687981361606788729364025159
84037877443467928666443352962147181772
801235219571947777311035794673167923414
328088211448336193086013899859353543370
2804356044557702944083694162316586574914
635511438198709817786883714445214744452
4758730426104290448692597006770212802663
774378988410313271227803130778527185170
8713985350807688542924883276491990747
3687116188147288240376857489850795361173
60061111494149761918951989390023211009
61719475766638872408567102596575275954
279440025254609281962198875803995589189
8990395424597939195872088590028043442
3812919924442460753785169814895419403760
917755064054339427824619025952254195793
60175531686921123690397418569539536995
97532332273758538674506874426428673660
582213128896550714341597407795586027297
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]