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
6083692824396894026414591670459969387
64959153327273918713688365875657673488
85117388692037517970533766750177549427
4703258281986486172936638625510412127144
2218710568143032864524004240389671292630
5829134193693511707215442806444644477384
760029244584725816696746775796936717178
146947033275378021526637138828247099
3419196657774681400847915497129256502030
3135421447454148754376259798742412038221
5443515148894516844141678133354477344664
522319832799205524639322657136742967827
375289623542160554092114977028794904293
9098286144053235818149573756802480041331
3249835285328896733184046665305813917893
18553746143726284238941366469525262778
8948391570708263590647275326499541668300
5573668301573255946433630938066705389
937551893488611241124633448901217579395
1314790045705429419050162088447926916766
4806737267433689579569525158696399694479
6705695884815652161162141117622889829023
819949213667423143278221965369891625252
9891207510435368826579783444378650866396
40018080155766198463199681697679708515
6182567760215924468915814900505072851420
7284280565185115394810832814378023144457
6485470171903991700915082481301117521230
72055231005631784243093926505315586177
19039153231933620720924037564285739962
74485949597192917421892993133028068888
7156552640085414250399615512669233297785
231281135383389854997687236333800643
154197242827866614033738077132393705204
799526377227378940054690407219820839974
837439881188213577516185453520920942796
855348237169595143131351193167382205608
629763605716772248143304493559968636710
3914785471685930424245224074973287027002
4420802898167806925934303329700399915603
189686189828979308262671589135962039062
435195227576206351285282449839909178097
6243537983043045116589117008452738209253
427682801360246933264257848299614665727
6531581824207832376817125797892856382822
3901251292783407546799375340223997823669
6779347349299906995269893275475454664192
3730799876687565384259951729777794719852
6711282652671051636888943879261121175013
908166327634796644142212141736713162766
7629886839399690685031652921408671817611
4086240443106297657968246006311344907182
166529135721987028725210870874347221932
150086813859696708355102644293219528
67598336967117764143397825133518634
8828224546767278164155527225935445832740
658189693256206902703761282027577919
85631310345410821372421977917690542827
85146567777320312692322322560920809199
356645635223348019781717385362784874469
658677406448666702445459655789585865538
581846293433659976788267104451949008447
392547973419775521948348663620079593
73732335848286479614136790884236882844
478258627485215424834445164279362698021
5555766790068731338457487901939348994091
268710909506154641802325459785532085792
864317701771312400241625717845157409
9117945223736242182910035723722187863082
69899227203602267371219145708080781482
45319795876031709952639289179048985884
635637274837508281689444989532221074588
193174141425535573470509697371116939
3361554912985206285577959080651510264303
662355234123038391491823741125091285
84096575428391297907065165159481502906
74216609860791161969428639978813881261
442493012525949630173358658901919383825
287365622166747384867228450608898385391
5965475078774171278257322689281263492187
395268742853560364848437006324474452108
97357264958024771528663817233267777558
8990455855534743668969653838162736287854
3199663422221241483269937722719165788657
2291897321846081498615462270473797826415
724933876365620751190825454797622876068
1063747366684219230427509644687464007590
4043785728481449104131102048362789195595
128853603078775330773573865194513045182
6814470765514047556964794434471347027
108679631966176344045504611860718058233
7492455980399235990298677822497242956463
9292335627905929321071926091122475441951
3700412746291133200033784659214340695022
8348883214609513292818753859503227779776
492963525880257093663064483514871941707
3367323178766002150301663252399463629
96804100408535673087560450878664751871
23218673164558462628538962814822674380
586057505281568250683565916326963378945
609839658724786711666711202802194856136
526494572601895518433099433574035108255
180752735494750513314269183372119079300
689360357447199606643651511768927572353
3402428545228405820915674754670447338502
9249464693317974297955221082813428589883
9817647398083945415384797270518563014334
605431527148950351446322733791997508824
559915585226780594851679204248115627203
4807606115114517784066334847520067159528
86958057933218099121167621685048064183
186651259917298885834586312719266577835
68354942590234865203575634761551317381
176625069238669519519936332644753207616
4126497555929914246386779526032701410
8422135347589599637153131346901593613166
1062436521235632233951793571217485704326
840877914648459441706839578346540699
540238176826391197373044099629261843294
6327707879585724123246179989882949791658
9699379671582945472222314475555731607
9393401282857337857236182538518438082931
720184870333054587492213791485278057108
280150775699910745568424457364033424
26226977995657440359197369104830101769
912944882899281661422543255832594617119
703640069050657264416849510287498318362
7793797999441095805536642720573529521266
1665675327617489195397631492646424776524
9113018940618012009146954967950494112
56739035600860126912137185719759312201
889893156870378116431743426763880094622
6897460753823064500085361125580225947859
1077871915095240679795363422680145785617
70258741447535273758278562502621342673
2418154442973446607453948406895108902
6673645762835544323237274702106659832536
950963258523801392373279986669903028945
658517085244437375226233585252235541250
9536183268147404902560655426840071332298
44243229955529021772043614073988886981
4554731089948508459842381740105461416563
8756283125784834800345417118958042497234
7777056847831427416343887313308945
4585425877033837763041536165417968409
4614456490279585427667897618505777639766
2597218220729745647422496202211946922967
338298965263292520534050582651068017338
7500576179247097777539677596907171744334
8190141315732387831535853724163128158025
7213798763917687711662603225331279268
37001419503624884323517417691932979035
193072946546416894794262162932468559529
48847600238376730981598884476113131
4359128533098443494127103248336369901737
224225826054434991481784618162175768600
251311498862557680128950348845063657907
641951816601845726566359640353328706325
38567568437552759995129848898425258678
9196396945774522195070370917781268980
83181272081335076726639299442598084536
8743563679584904292113088859459685364036
6223535596498417471365822244242828899886
23095266379473163658386932571873674269
6611157622663954774129004202889840531
6626236071784351576314875735889392872
5831348790009459447978385229601189644832
431911085958885160890584394743035395325
4141309476211196595878067542415996736972
786214199838452354022063586936886968142
664218166003556652768822036943789374745
814772525698241266329678043691963933
9552231853424467689382902546637581887865
32661515177526015661123493424145208125
6071558640413415193030424581319546954678
790942049543787942898398108790138354
5810615388119391602046422735500615565455
6312541557822568981249865001303739319590
187890657635184924391093850459347235563
2463247435683593576833351027339271234292
106988705909607580274435187433117828741
3091152110869003321973968952912316319924
248597587896782023462679435070781004557
28792630773979363416799722943139031
69620115100402916632975202221968723513
26441922874592039360507551797135226343
3408580054833605389096119554435598272560
75815226944791881391176771135088738113
5881516592092716187496368360925914968
853457619479541696173278339280687963106
73867249470718529259198153855124843642
513968911771516232048905420650125423533
476492774490436261094402889177369105097
158521949155899552059168746970729136984
3234531994839705902137071083253335835280
25104744998178770995014895794414536583
27503315899677413465151572185304076771
7997305850489565194028881114542321065687
439767145582498222918249560411719497380
503289754735145055895567403342273187494
307240212829401393780042219204518188543
2043194417672364767285872501352786324404
319189891148891125698442504451125588186
7662593043495806793585754553804580155987
8463664493852826268875758622871946972515
3660217373144459949858812883474315286690
563139593553284388882731353327260505855
9615862974483183596795757995500997262129
9741962456188048491528781359124548381919
472894441848788395127343403913794385139
99039615979134007173622682078733472254
464177016238303367516855466836034474267
7825405419076831686118192454988725259703
11664965104719824770566405672166863522
5240164959799767363193685363972476201998
2548426482468928784621519895573679722421
54916652127498185372688657331945547230
63728020250350085557126428819624521933
269953351840922979927271531071576555
39665542688603571246022368263668441547
4619409945262990104627063432943331321308
69363269914815466994809664579968858
7966958147627762524120269544120460467913
7628474395585210764233854820623192615758
784014512481206798249693380888762737743
290171551833515013368540321746416942651
106115575689500815060709131432186433147
274490394115118848775802251971293493615
2068411194944070714253725553934498461095
47201704826854797926975256564312448214
549918586778540995391365751945411214435
3574144448988709734487652419676662683376
219950451055569461675253879271465269262
34691512238867554943498433321627091457
415676665175603147869066116293867338988
2155878625124745726843742749303899917244
3567898377247871584521421670664031386693
90876201519178163732825957575826706380
7341497578663808458342837620205031283579
23017793492961188960774758319083716144
66855044766262123405158363586472389318
6381642461666167908215745327177950695865
987168531848180126166611977590316509321
8235455916753410106611991807326230857706
327596118160192257804313736245121659419
2270442384217074833253474215647598539421
9986890028929230323763444694362258890
643225945786365757045738861969677652290
4625776317474465747861361637981615628928
703347112310994160565596787377975885998
8187430556058641179124984066086774804
114056448838416589988647299692896935939
568560999900712733124542505315357654582
785252754276554979908219735223593861445
4610744758602160243672266181192182768
5304311651826452664864412380033143301
8060414286858439159163603772213738172351
263185789946669708344016914158419391174
28686252391966327755868671555619846996
8804966091393679732837792274178532969987
591264727649703321783658568988789794222
45811581183559105317986383949897858255
1946103543986316499588351761422314326261
3446451366048633769917727381496833862
80501417149462399892807805383797225989
617186886049828597830797453134231304188
9422200127364504911547077297577756212219
7049756519507057605757522377459425785110
1061581498722281379640836011429542137820
51786105122477328525055197252594316488
9052672275614156279786251427800135481280
191932106106555310989447326540683469127
592671885610254562808982292935571699310
3261207845238909508711665030432036319865
1452728655324121937379377891543283028364
50466974910326232908370984279055502840
363421498114620599651346948744429988515
167424133308428111069224386995646178910
4240418018088715834492381203290214611154
4687641970108796241313067587568797085762
23908212116376772842367112687719061314
7993266328448534883590283942133385287718
3358159735245134952873715344193866703126
244561655136903076258773810312950886419
9698208815695416236188935318953141989676
696938168472695843899073471834935266694
617168906079631783321411016745363814778
613678467689272253072426704190328184842
265519469192300414873758964528631992369
3114574490544410711228861215347510899712
975077099596699274795358944088266473
657278142507458704467612470984865497752
8367848489044204272690187805495121607543
3677738358549027854813054423114167836597
88073028881765878793810540698431813624
3255963135647125282828629022779812962179
7606914370055081448216471061594045501639
656284724818130218104636662722646216754
56648072503786372415628866387239018714
7872405256018689593161382848525938844552
8880773283966187590766288754242841134
887077018781807227107261346048363142968
856682088821884397181454047795658765355
739135670907196248454489209391561853007
7511551871912243461052475796619733508907
34657314526723345767215326594730471688
544431988872317390397782762788013606355
5074702632526776622916469713481449124270
632448832295929611442036766250211124417
62362128837186828076973744856475502347
4902783554969846491471088430988770004656
83833038735813731469448180162652480773
9545358021043744465939154067690846489123
95291791271511631682605569117729831379
39268495342639433684408365806118543597
4439657264791272047427997183541363335
498638727798749086212798391610402604476
550186908508579635724509191488201458
1853703233102638306541798237114710646042
9413504012864311311256079606182352040
384649637799557535585869164669166653731
1137153163961416861189392607546791925923
6612944498489770335013294375519619545
3475759645223970814218274037585919218082
7267832399649993715556003767424772103795
8370429628013457497191998100824125649369
2649997899512264598632872462378632088925
5659168759078288411291336406770325757079
3548164747367045455743719528541387240
124052609761551452692784298218167697091
3381378619284756525183166305191016184174
544943396624370197642378976838918312443
84247681185034602915810896171248317459
692419713709157649753426888596875712391
650858954119469199203389828295242479340
657699558246531559849247630194346669471
6446112577244381490314325943764255854894
284156122335677904446663154989635901177
72547033129149063168602667826377472710
1083291681482816896114995302225713575880
8240804159167947974167769471844562802296
949612695694889461943143113591412638023
7951979058979562528660456081438529796312
1343130375736927701719463242222896329492
991673058935082714350673605418665756455
4083863512252803672824366703211653525942
20906982607020174943256619053993922
82727969697434955617828000929212186
680621792041802774822757311987133096802
8966457911777626972980857822322522843373
8892634473819001691523694355934174472800
34358665728113482935645377350492045175
740947076069844913705804304283199151016
5328415668715339713863868277143878983176
4172824784357897982783861813572813693759
3352514642831643372214785321639997549098
8320823034233968644885022531440817983424
80118989707759173779491521845475583456
487213883526689100541955160552183649961
4822165617368695304688049416230966794009
3341308259741607844379656408572268452434
5014428820154858621210043695866660893903
934236263241973852318851966754718648065
27844866202089175782931048130476799619
6333434677205659334552049508810039598411
4924493462037294615257386692402852457214
894871947651761257391542966347852399
98163034878059943845156492679229431028
589396099379751732661962514913570554278
158598861206133687666844643816013004935
1325483673049408684741805573857477586604
495440532953899847107650898898714888558
9385387293289558842252866099137852488896
1686925443008582713567148567890724819969
626034553208127249499291165791983935357
8358956999956316411178037391107179607819
9181223251326544240721136687910272494790
2761691034817392788410785686948265009
85844679536658772062576143852195731784
32065970707747444329563475052179678589
1701595283986039356853521092705611416210
89302458564104545838996267223882458693
211645086487724451656713799407849843315
1840657364419954606385429884521352771
424295789111334148373731978440277677043
860333093319800996373092254145214381293
363083738967697648866590296464369481594
8651494666256732510718951295828217435600
838097858773431223634285218477333754374
9102746297233850506652138958447949416368
305413171174210361172332868855131125333
7199239299214658394795236148111931265006
4124781538741963364236996440302432747925
791091566414655015548635734937747145517
4003434512214776113684093763733210117523
882661983287296341643386784955050798350
4128159555263535380974702269477354651
891439225928478651561928750798782165654
37998975850267068061944427457387761171
4639411182338693325854723208397041068
40259363701752218580457692222404737995
84776752993096581864479510894827693
9166407095135551281987808942164063874709
722769066886688828819967385349788159925
3653974973208969414324454018764444882002
368237504048303692934351138694778239551
6735352852225603397786257446502277697442
4831254741006317243355635796518627408
31471625324255821560324832006748523289
612617842000371119570211887697365179
794854568600692757756091669933069726678
707216118454900713759737580597464711881
707552035843178591483895288255135062533
1736829520419794819051784408104269613687
31409149902102347637948860286555709075
285454032926110255452026955859567585107
985782591394346429289548442776915602016
49741305747150227903226346439082942290
8391366912561845362630377942249825903298
7474321357481576183494181791227717064120
2687780140216336662667292668543724693354
851615466935692293165996283471821908739
280731056201762614099348789969397391401
9967859262805529651434409258199353007482
785948327899747015102959288555290606417
3124429023703163540991904153546225697793
4068384177056056726520796633369872304823
437466756094117261564459700117517821774
831613302603440644833616598915097069500
722912158560273857578976263982161014181
8009465543058636568872013833991947185030
559838267127547561685166224195423827504
9558992371911865393819020587036692347
95142638486289108668915848486085699371
12912296596183189112662251961962149930
990626929541600564886387459975055988905
2181857429252824795498538529707469794390
9913202437795829917264864993531293412614
741087472898483723743369097906943245207
92813194829571075673461569227394448006
271480883876650124398959698639122996222
1706129147478560392720248897724739819665
4422445425329848404621021828703699218490
3864485062885210662718529353760943603529
2137638338055533248514591582627290505718
6808963511107535549956662513999840354604
99754392627420188688674634057212764708
61495688589917739692796141635892902255
9671788893987433738845818168896873802425
48954583084307649283528612191219185104
3143755843583705458063389557968362781333
316960746266246985572129648321841797879
6613506623936584738885789879132392944928
72527863131678813462842644013878616796
22499524120952347613959510469388376917
736112716276765241526736213650167285042
80622115464775110558554408094504445463
6468356156994971907214076139969782524095
335463207322140868814676986687761954351
6506150781969752250640555112805216091
99435668847427622165738669759642094853
919312019055787712341731775862641742283
41336304790693148364556092164292657170
94003692674241488361302127584525761198
4244807465481199288312793073463666252260
5582281547973713376872353625378526965355
318865953578541759675835296110630754421
25393434126381702551639044136899277211
74369542807958833119351250779065855422
5578995356076384703194297220808628913218
8075415515427518148959876563135313559
9121319292498465526666199071660964474523
7283438160476402559397401999710581215189
7639574743812824311336057015250668772695
6838401278192431473433711482978323945
3704797791897517316730214190823596997400
5164648115507999365693196976463118518004
4819477351899945647333661254211657585600
1304536482154592353128998492198888024487
8432375263819428953038394068902267757
509915801167122481934782580454311186475
7228275257204181759626232507451519639134
9114711890792235524962111206984830026592
72354488372814612275060681413397838442
4141756453707744983816427618206154048689
3148998042202380423759398583742622311
6972776529213367162672169324514121778066
6596395656226233048254894146255880178
2039976474485276397092069574793047616058
22392994629794734588551839647474121272
4185700645464592379840665857721370156046
3867617797512162755681729268592162218220
791923084916280134876488835373963031736
512752186516569937867978049947944828989
1392229865961322802160961352345165861
552233032492697835444386889959159114348
21024491434123273216881996466830633725
8541772583416948867768264017789431194
54459464801437834997643137451386232440
1112619639779371016991446744776052345
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: QuanticaLabs@gmail.com