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
99143273625495616676111750527676745518
7354159248183273456675965709843883856790
9695641983971676844752223409625150426814
4689944061731813349924875290261880681523
230736503548761190057161579481628637079
4532287141579943866834737309643491657264
73405004254065393632479758583575442592
741062151238014398180063913367757888569
902345454886473836076806997543345943588
4135985313486832841822794836079999634
60922272468698171115341997341251705795
1114387286125127755270964694205358947
47558274970745019024939075793294315631
7654663669454195661196387481128465849000
17463178337030902074726172625887374836
53480572077164289269922046996044718714
906755735469094475362288005374371357309
574949452625108151805247522045432032476
9613364014102807582912968312774534275138
55808787676892612126025747972979812979
1187663121928656821329057098966019883219
6776918561913219902294247488569692731373
783495557140185584951290672435407862437
8362685739942257117564682049330588806490
57374858207989718862887405375888650929
67789582358573971813288921913946288751
354149074264502254835238446383854811152
7226689690828832332316318695928781918915
5357497232363481314212319894776976273059
8789922859675523141381604606034985489
2498349421393846648162161371187937313413
8492372176333436637537721121633409196
6582383041161229827571831497911595569900
7228139754461418350959453802762242809277
4722301643572754542326216827528674183690
137637069291203209987128557662459853247
581612561779111181987373709401975578943
35186532359411344654369316880831742147
4178827145365627573786074067175165431774
1536677652726110123892667941156128603337
683616644377566685187857539532949852
453936536055544219876088737422517734570
11987037483643218972525589866927845075
8597789987032397941562783326660557056263
826761692444712227916681298040656749671
71221345568927022416176296493861089462
3603510387163418351560215867148369359373
878077757521230014812285044251599591195
3860002913626698357389615325875806473
8224487913841604565863181767736657562784
2046876171119947528127305640780349639767
3013537759865602959774554945481698285861
4294905490234262254718309712541681271993
1583755787346584348759687696706694979275
119928376424469569612995593775756887829
51776842580322462936944451269292169889
351928253449270372864439854829776553529
155568368547550221972858435481015223557
3838759857668570769481072499844394302766
6647816879651951085665266275646467737
7885471010233178232755408769296817052165
2460779794422177863384128378291967676740
641694141976245461181377310490667174168
7869418262626591828445051059326658019486
978226674081066683726437014376180365190
997521523244101929854694089123576042139
976514133508518681915972482888429323700
521093775989114915264564874940324949911
354761886956415897727046768883056425558
78604812565801826895675007745968058243
506799022443681826144279341112718652250
5457269496474118800638748795861977044027
1685807480844321133458838985990792185122
2878870690118664906330654339696272336232
578670171070120620094538238765963477841
957455891571290300782083656823344542685
191532565416767885678583652724435533604
103906822987155306856687485775454475348
220566529215173964274578427539525075903
350886508865477043287714751337288039963
5207353176136765203088357524508815609320
19619429488791125957284548072769276923
8068748229113458198192836627567329472251
2203636336325934476771019840659668965873
2922840820883222802870913348883196958374
215720324451887143942418751186783996316
529840465814948098027210588442121106626
970505050421823967222488229334583467749
5811264131761307188183893986310652153006
5262867052196283326645008935887694948581
78746694381411451262462128556129159814
890968216229373761015948398423599901021
97801450893976925789148679572828556875
710885116335897061346851524559434738318
318298576431797594227639432844033508167
161425988883461479621087113644030904464
648998064475859031412991984663624214932
13175007346626533034316085721858221332
579737032011520267299498649026725261750
467216677295862773718286463591262154784
5159793075316301787764613701808894602255
1833726253387194470490827730146511996076
4430866557968667313033987774341823357272
7136872772929872263480649495931654195
552937389037710210270117600351171692526
98915264599432339377908782906368308180
185289957152685192841183770503194665026
2010328761693786512197948731929994108064
515723741143146764489306421500537344232
44333503386064634158909106955788574271
74038258964994410136915010775298896645
83224853342266250968222496668924823306
835958022678862831971371687063075036318
175997908032414423346775434640263244735
9015809136353480912728542092631573042627
1526622188281453282575516098468112796915
984982434521915620265423100686129431349
170217015155364593914463643965763295783
101154504123322273297960490891067657509
5930866168993283914961133671867719708349
8565588757829011956813136382967428725304
5216927297655476968757514193748263465399
63999114764447525724948288381416783230
140089166505760077625411675332828434065
165607019482462405953836025525732583
823470912083692450539663792994451975859
5057982122364096388847148689761399391
7157669882911007256947131312316164835
8039840380204222797730888864755612185824
1791510653516252512197561025479619586
433559006403710592791968628382791764132
37377109455943754891193904860428614954
208761851128427985243379624722683204
2630571827956940269892334834868268072227
1478100006698123488782432825724729080
3835114050028311728567153327940259433745
3055421329888969817573685055490971927036
246713476311981728857958362935654108994
5031618113506246376857677278996283614564
6082666571938029872670057923816681481398
9968886980813389981746051337188277553294
679726075481110655864259627386164306858
779653777379184500522452834974853131562
812675882686796266301949174261056638255
689852948726122439067671486712378132
2880166241184666241843446793109830083789
7427212048656193993910506597308664105300
9129355743074509272448805797118440062400
483338194330156939516420829793564268277
45317818654957765498636677517869604158
12723596121055191083712385952625495117
381562745098482884358380113246152400443
38387789420396394243511391823831641751
86332708598941334692864599434976568885
22146228854239678124727307596481209375
401757993768563696245641498291622812879
59622554496472194715741135920799511683
1822928132566117370160994989524464257135
52322773244625621889326055399237385625
2959796259361420231725009871837026587966
28037531886872480475120151579039138684
7636199542564783734454846208780891311294
26579149179453083433215477968496269325
140188043353239169046336499376810276409
77695374883316963724207222098089901801
828153482808406093672825172432275284767
1814354910871037495143099061887034573915
55886732830568388227087476809657106891
3992492475192164212381348383912319463080
7045143884736930390733248710788875484985
48120158578823775391534270496958731
5952443415929214078603797891598532849
1941282068788082836724737888831771938054
521326876162790575959372346631870179006
10076404836571666868353866362612079011
39953655671721114683733971096565153525
30866861618600936249765942202792627644
8708730992991146673670293098314849517677
7711648059998451807527978906151998977730
446017521359237390759221176318714907515
3641810360559395940936951645669440747314
7591288112906599533259457574228849208102
5685764545986142834953332296725814865
1560608726778642469175044839256859293767
7507896672215418260660972161882072064808
39854722478191428757301372363501599881
4396132490956435344853881415805213204583
832159608871698776525057623881964137163
90084871491320645643567331855876284322
450865621648558112034683377634073258
7627997094159931279199298015336633551048
1942487616161200946877127726171496533162
4716927711576380181338267654589870769443
9999457692292391498041118777939622531
5784654754488315972252683962638792917
3652527753421757309062885966967356636506
4021407578577972349449557394538440051843
9762868866361030802088900619917515540
846181722803767463645636625819815252273
6696475549922682357458584047591320806359
70988965864153553877770760166883268748
6294357276138059311666329221257256638544
502552475262992510434492969166513077787
76903131636291705548837131277615346390
6712513049906421513687369691606691028438
48381663683138817769174263596115709124
10295369972565210356373530787465018025
1693885966516316463550605494308796427465
964135885319943140108814613604632627922
7008517943106306444227827007320886977953
69241890176970357909721412049089188
3704456444499764741141957876801472892924
66502247278036689968694486287684052244
369666596152385699622418852612723139575
69195750642827276553784472211164163018
6400820573157221456067297954611047425737
6293266956437907303841323851591719679678
26193859801315439872853177990481684730
9776238456927655906321669976165235889042
2725148667433404824239434307817711218509
366925022342629922167950611838876753136
2791535989243007978723735081655915839
3780719534504161967046982816975591496494
572673521785230523419118607384733862650
1403201519913155743226061348587050043625
657168044239400973719496250269170557124
4806480679222575931484095962295734018584
4894878442017475352623147087263810715361
387026171835978429287445857288040423478
300312434865900727907195275869315395685
651783864649589816363405817477049541805
2066873338694934237723225329817115654148
9180627380241137373425512156861228215
320254276794803696625655941301467599686
2890510168761161716016048547706014235760
911074303391787171382389372979598513166
6254270130377646444195925567755863779897
60959973220683429837262693511985809807
7417862769938208843319038087738664201211
30981123348155532362464699074971303124
657323458978223777156729613656787465962
88919375939991236672446345877894463975
3457385226896789180198496735805887775730
7246946571038426941056976571160061263517
6176701493284426886672459770917153475920
800116290743729436773514784801852276
22792507429385790093695539526799334581
4254979640377765957379507560950088391412
5076618398326272812797564990561074187515
16469561171886942174455960458711301996
62849677746272856246676175987230237696
84634321578144443331363557546252005399
717665920196960460213861507113062041584
1947976527255966644466667945490471168
21456995863809059752348363799699032423
1419755764449128681564293637471578643644
410759339884859827033547404713420676434
962544273341732805671556987948040759104
6939169344196175771494827033155396116487
28523038976917037653632843676731952587
3847480189045865820418946665678720291139
7237570376006069806724664975700851874369
64546668170973031914840245266595717446
52417162792702747573776648298143382168
3714778620665867512994567405366343887494
9783679739299498782452837718765776713491
4612421320408792337159895761497598008061
32651117289323566694324695534898711
835752795652705066754284588065247376277
865340309560970663014229756629054188345
9384905516532909899167215439576880713642
29308301170709391384839675483285002333
67912436123974229579488675984064452534
9813674325276988732146493072192843121088
34641820430923648264971583142054669244
8433533793903842860558067129635844619364
8709512015064083466182078474672228979892
728922507564938979662995037849081382660
1554473946293670939863374388994956913953
84949515135362152469712741315650761908
690253959903681125948551365919650184030
629929452069693990177052754156855158282
9266951654398353335443701425827330611371
450948514877981724914828097391811686692
555727945425310246858913212241213346567
25826516551434121201932821829387904436
6310239890737005801785922539495497207301
498993605990770769881067456581304154019
483086842653023182442556324609212229579
3541620989785885898516887356425160833425
3448413023454796654472574505512526968772
9812945432805294101919472954105667629661
2231351814181262524822588671976580243718
7955956343518510306643333861830547009865
1398846173217762109475418362886514473051
71141261507949979254944860549782503389
582024193329551565425533843982867576284
480663504109545382379595879108342253657
946266851992687489791061334191429278747
752568598382239021911992783317631988
881731881424417140739341189023393826684
344268590199431986920338245851899401677
8741284555326685376632108464738795699
5129696458387537470097393870682218181569
952852585879535518161103458691158879173
713656947161468066613359170658622447420
9937618497515645904557174348473962733849
989425839838956471786391405255872227072
876836541315832842211933252423457542425
54193188650867667728409075212736725665
79387317388339092245977949420001131779
1890826250032876123747593444275180391216
4860560654271330498119651433829191907729
766193227665667765767340467763284518598
78794970216067617418572254276227781631
2015861034582668652989151594381374711427
914696322697863508016955534589874581225
196465347076527186816275594628231707451
6801595340651466796909653043597531747
737997098423232886152600258102661635879
6933248586158779318820022228866277579356
444844868058433653247967232172429709719
33992454873919274065264393452289898443
827331808044673169757600282360697928225
481103976121655087950120585746771666
88221292152954612262357478604466884644
8051463847162062895131593709722311544968
19291201115208782813649850319772412115
8283796676896034235397499348903095119659
533248142019475136005012493470237107529
285645227144836125794755384542382256671
9842287112417871956049618214752074207481
8790686551414270288888772461513780697339
6419982272521621096925089459698324775
6093555966537977609415932005367230025041
5811151610278866821829333018869970997
6957335446677305837664758801297129435159
500121636549785478164867039750561544600
9826768958034719238953194220315619442453
6353983987158946192457435567487679609674
722689907184706272099692260827061914002
2275312992532054279042484215359310589501
690357079499276194817134415198163333245
600012193517424361023308144888984535212
844118707571752780856902840167338916407
9240844815605574448058896479638925385665
8321989371886585297675391646735531621623
6600610882064328613437197926489739778020
96067144148671452540536872770971018162
163726920334382816516055665289211138
861746181525996680158741528382313814143
7148320674468571149029856165393494295473
8971523588121585912315699334495861337163
877287336484866383324909292548724632
9035913167351312997774249108976149348388
4828391192845919282142901735139278665
6952263524413918162750284446640192696614
970910248875330488650826232715412118060
2623334109297828287153899950749717248
787358804049953100122065074645256057047
2300607735908279513063426053936865687709
3972447488598690754789826258785416965075
627736677887921879495085896251682957
685654949458364965292648898271397133446
3500706400388102057641878812691996858
31678835988719827257755717707459864535
13452192270134364143507895218307309251
7354267516867285559985534371588187573933
4743221765235778494475316684585672708038
5274472844994922758388476493223514886434
6308275515903328421839234382299275325535
79516707527278338945609087598370124747
1212433398343161264573312464414905022
51832424451521349676835411301634802440
180537308054692814153356154908760363079
785550177122185938505758613960082862432
395888606315603397245136537884349284795
851881167903873042587108524415338848279
805162062945320747193676751214959659306
255041763670982047124305252916571662506
1803259916846655450429911090489861614216
8030834097363108213394561364111615273562
374374203282063866786698513973722113443
491244222720534778742186250907978747104
7874218586169693881322812029103872005879
6674349699765681824811364696897711298029
747675141599734609750141450369951479824
317552492128727324811017483773290766226
374889017832484479840708509640959381138
3394683238583051604810884006915246318240
211951123444201450923376371687435297867
764610289523256067548929134895538051640
66764029236856313928973660767560198778
1302448826583318370494112999553361726290
302602998307224975978704570800187899674
609893496351179435059302437143660875637
7197768875672993698027521767731343727597
38995329834966132469990294173424684509
651526328710463262622877490953256768104
85406398804196885213023713850489003724
47046999281684194368385365932003765606
2379860453922014229657963858428595555
363829116021192711622390814055426408477
98028735810621599596571728897652039717
167269616382201569966929715108347658897
120163609076519756578668969391598034807
424915875424607308969344801323339391880
4245273666747245899188802685225382482747
4087363582092784277125291974818460044191
24435756485548487511826450788958164884
4385131113896163263210633994753848435635
9138657091322989475356129436646283105026
74668741065940740435457455671990459520
161582695165205273059015652198559071529
67145543144696519464575801888999245209
38514957310193594641323542558621696827
1635321960078207755224895998662167428407
3239696274857793758719339057189430524078
9397919417582621540382096017153592801538
933343182483366766768699201705544504887
204580596287809718246686793959088686153
384843647995342887816870852666585067899
8415620345795129718491831690934179574120
29932209917809219214778919242527749817
6956933946793175442039121504637736324564
15699364738087645230241513624992363488
7637767585655166949458237683241398756475
5206209078636606878980206782135658956109
998356384289211803258389476718929652115
9484427649169780593051266232349537372980
650054932046129667745865679863312362636
9979908781452581256769719117560724669243
8473361456996314286858618151453041719291
91365672227945819045279659812143288260
907889875438269498546458405834844276347
2678114814532304469119674272438619507021
107420836983452226414921364986262581361
56665212380183761346386125832815908191
715875155054242469441989877607188109312
873568932668461541631114969535890282487
2149793131337321310925494542804939652581
61941903853249512403038643951545822153
809852544631509637946302513519483965582
285584997563222916171063725568659507128
165887564539225406710637192911068929006
917411872401679235597144646414249258732
77723109851958413549455287419138111741
55018852486205656283618439486532462119
916242637641880376857678613454598613348
1115824490925753244692401106888377797528
61463835376279887403169373055835737955
913230507164417836455794244989251045309
37938191887135110413689011313811072218
5830321392593452566475022178017403743
3463147620538028723729566836664436253927
6178667495701533412371244040724419354015
7614378999641185718528596882514686574820
3246378538174169369514686318719433341909
8471715179893602183816597230161875600
981491753850305285715381107262961296335
8574417925358416177079592733865753021239
35993738672674221387668507445977517412
96628389583248236994068836499399542724
603913855201239262095856436194380285935
5616491333548959416589181738330451477514
174244737020811641778382437241049941074
89495912931812971871393786332611664766
269635834895968221795053098589950114534
7338457513616324926995865336465988053
8734201379544971886225632202613321607757
3755347627514260655777063302534162347771
9404062719884032736825706832802796
87024731503133273616047076904483392353
33598025246457917154614421451565472418
555367081206760180115865814571958963649
3543547991828166855450456635172377312291
5588227823717606334031982384777956934214
7126779236199393614202674951295533774
3338152646607685279987577119514563378777
3535608119557691159659321983952477268101
405158396204960469314216642699111239171
5834222690592927515066637732667457254119
469832567605381038962569253843704509514
8634550457079514595472871028239249703105
26519293732058805760074133314896181975
995016482026677338565718985302065678244
2459115765798865230991887435313814911024
127843255688113648092855099928229569944
69071281998452564109711952548068972281
4497545581552629538169006853635671981460
83463050869515969368923431590975847916
764383003809010133588248081377343217676
200846227922441962083515103749729325624
6601307757522076149025121157564708517
7615309125765241816511973474971568182520
2600931739963411508336521924286148491117
313052026612217928997831908949245766645
21916885754276526682100396982316428447
273423643847013191969933080270793836406
3893620274251570989638201529429470555577
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]