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
4942850726642941914421068151579740946825
2185188592879236259459999530462322125684
105426727093974882564792300637578541804
225567534836141142343128733059739822469
673869779435599728887259679580410033707
2708380877792208383169885446680734965740
210371505880247292714126925321826132150
8418250975113981678189323844100397981845
51602775884457847683731470036636984649
2552288168026016820173275891372928431789
3936948293551026338712839075226343316
2359127745243006198785775517203986479256
7950676141547410516898266987519241094488
418867917801448974755352428183171545510
6467113981771242475364179023601114303920
886848029778818619796117632556714285369
55682706768690428952627289691912908515
3798244273997552467216736117755550703763
48954888943932612904506207488315523970
281289276677926562153362523444315603990
5789504596371129997343463722630073463699
7233520385292055858168003274135194134898
3534227658882617126715791482697566386998
686082612426172722067911866188712008301
93428215852881055539921497026476553606
9044631278146141447686405356173967448377
7101840578448137665992756065780025051473
7362281949977954175824961491591131408697
2218277687399488509063255051534545321723
41854084949173271457871652304159309735
5362151117723239781451169092470279059520
376358185879449971083152003989214967023
445528822362161047071001425401693789077
717793012093237449388105409035023518527
4165311278763177960538998415857692349751
8393982744067108788295942995687736382245
617854619075256974835317647641537089617
461426578278712206223714501362274134750
500688047066349583638604305726973708351
9383792375301217565528798251576358784360
1885244172507584282367835188110797778407
197603578788788478944418745813164775107
433524653029835515554851034249079094740
2560224579324354315789794938409755464560
5836238695399242112180792746707296617411
642388037081933950823080619116555631993
24331527086666231425169338832340872197
966039773068066335485223215337599967
89047879709925688102672885474165674229
689724493274362365434823291160138335
2319184868988004207067333935376799442827
925616256830683255812604226112854371001
6968626258298224586715962732575769472756
192884031467083454163461938853042011559
2380303475555420699845824504859457043432
548335274232694731371026670328725680604
652249865193229557946889898283415191
99629243398992814443760743798354618
5911848070047851437929789072836171993901
9817691442982426161956611949867752351010
1579378243633695440599799509174253007646
2845190827910213538196511632840243464
856790951133464568282258582399423521875
1571291145499369234043298413796828183
1251244114742624043379494559411974390
8258128866112999987975264551816372622759
3104675878699398990279958634716951115343
805342510483219665163469999762958554070
5357171238054886893703596976660016395
9177740125878498693930531061946443505083
835032947175111524989619990845595174041
642319167827704450704504357252720873969
261338544065354837236284544960682558239
96888651761379416863803294952419193147
17849185441300324283906864553941479036
78323646274743921118297568185885910798
5353253981418371688639435768310121646865
19344625812130621241159957671873951138
6297544399565564860935683875276931523098
14948848476837692897067803924835811996
13744173440571739066912914893983591953
449583624382790675546832331244622954756
14697479383410683417697863972992502758
3158336093908752997578631628163224689519
98973819433972146461501732391334412927
92496879528134477806439707706418957275
2654471331233727525673098437682732580
976562462807635996538459292341621146232
7040569868704706571996953367606234274774
5536590678203489893847391027700797537123
4939755686539292849242453313112882076714
770864274312434858862026825037582908524
200645735783190651885587978417558124710
647339797058106736941489189141451283793
319212616008574367264362034891285864743
6734578172863073354619019832757377484960
5945567263719403413489148832932684819825
47405070446866728789557280854352589230
6476344425208282833736977685548177794301
2513984565042926621136451923589925831967
319027919600781564253237786146857372723
5986921285345399847181036783570321279104
2993666948706478869497279345223538958
73533549969657277537936892075579421379
77168412152941246120391630941576405
12273588785065489471358645026946330083
19629286731135412271962517273068109428
9877547623353069274697076477394713666572
88301842887272733546787177966831427772
2225494193092499996696233210233716056660
7739932991427659605733613844991521925
102930838052362587246285581185036675658
5232932799406161352162445536282061167622
9432258981184722370661572940382380851025
64467099924487249416072562746718351506
45598713424717628261721496537329932455
5620533347854398547948590598264854
31327512608620452128335360547646002720
930783327727410256875819908965511014956
3056750470186506422935934264829144057658
2219148674997740660015009715551290117547
2592849523651713265333433858989062952690
140951777147683930873561825421187222796
63928072139215080099207843259573362420
7785817660506626582976821842163939859064
76541979486651741228164285395148983130
8684114193674705772528232328946347015572
115175323338574132185381598197886
6053414953147007745858783931768216704501
412370326753194787823065099271232063685
6162544096411087251755637030222955488187
1462937032963792534896552485204214428018
76999741629270704033991965549377716431
53505235234903626637665271520766252832
342630368265927399838236275249567874278
651649137891101666681246604739374222506
6519355341237838815739749203797573084882
256899449502748758348674887256655879645
292584277978029912162915187260322546212
14487019210639977311219948280557455865
472475866675309264027899394515030651843
9991303192511210369351819937435998311647
3286477146926332269836866502186444479
406249176428742230339364206627259945897
5369231118730929876861323382736425237
56189036381243391850461050576628727419
4998712821685245683039644809603995602974
835762309238652602913254239704049947656
30344346761279318977129395298619229827
448914355274265566896391331599359647422
1307315948223208402937443605570619119312
8526697233585616134286546237649699871430
99501714173720656254746908269637262596
5524223397239777469897857140490542459
5316326961011499343884597009985612261268
8477250559108960487178818550362715672101
9145630211798571752628059713986892688970
6918932045941542857434162484310910038686
68438517627067234583761360603772549203
9183138113651372426417887124748796
395754925343234569249872813174738746655
55781279604040952103868749653917379048
52361097471491034037127561203994733270
766667687201599433259405211724258679255
3019532673869684895318421209490994027721
7039905342197327264183647119408986807783
9295089161985246612240185184382106474
981631973448406351319871750481374096413
6457430377369547431836285877926978238
2719834286167842337452885913146629097655
4272541670345671393548852167117596873035
4563406026371979555083818351415867014345
724677612144992246297919600669242416406
151991554641655286894723252947481744
6266356117266792960013233174216749971104
8313811818372677929563013799294970234287
58368423326771583423993402110438779202
5285993450473011525529497613405083685533
14943485270639853519293445560697655846
8974689411671836998976755974105130978252
9878724867863993172038369511393838077774
148315553474777032934096795997386627455
893146538596132263891742057839818094526
4296591488295399715739073540195654796
28836264296837237686106814021728698844
2704641387972148723893135655212385243573
604629841462633483029177180538375191398
6481581892093862331184278065112853531592
3705158654302101139161427635175423777852
4952743436878999774012409247533699399676
23510526914226860132066764982862279418
8358730978829761463892792242204772472979
79938708850491242412439522956319197893
9600983615642491861132855719856949163918
105322427764941857967494798979062417073
54412405944377178535101960348213364749
4072990178489128410151526384521606322
5124424615296444688691239743992894071446
5906798519045173614426991648943083489516
688798269834578548249219136756937513883
2023121042239700550412874803659411896264
778640207038716728043829986363388058861
6090918850368492299795202787142245829834
60168402188672467317544149806199396060
142167637231920449431026761719928055638
1936314111056796919752835036506741592809
4062365367841845371771642641036798853
50081865291818280005216692566467147781
8922908669646795906563401998473625959468
576550814874388465965935998537756386099
230934657699608167749245156232619875052
42432091631517673653914308857798445237
25342505430698044755242925494697353631
7179713313134121904522396693338212182348
3932937254019777707280322736789724217
78033849912057827681722893911435267426
7556605234306482847456715106851518246105
8351623815419021250159007130604153081285
7848723320999828465735035473792977541687
4114277687157268871268841380187766529016
898961577092767861658047957380322287270
9252315050659926243926206750568881978671
9058106541976291573584728042673520163907
223695546758224218192311372829019919590
1385754462206191319112038686262066196844
790078761419379537384983933427392653
3198567323499274673889385904293716125670
57711898775798884650392977541396382190
7334276442833038531525803816182267725450
7979378956977288463893681112356958728738
3874125523254634957614047693763755345947
337162391132465175534725277631023236241
1421978419625143217078959711803472248572
96247187716593419557238965782251588389
5110620492126404148027178558206246672812
224457019378244974379042979749696157505
79576627841062144304947390940783819433
573681001699748529209535875911856694
918915386605014817562227236225532926004
9786258943792765920787699418621360831278
3985349446829335447340333788932823659
3694214283758713897426531007160069477950
6876517785945505752199689489455153909
521574646489791151611648944199376845592
275023175585745256733207062408837169161
5588264580536374213518997660112280011717
20439451246196554642498975267947203232
469669389395457683259973616287153926871
157216407538221575224439589111460836703
9941597786451063700272081639738213596996
4178762735313091284170582036322525285413
6326154326744255180885416725628148367764
6603190354528011389578591214183479355
40841951528048069798321645844171860284
8086611860818829346749243574527485251519
2248547270216768945807474513735669799
8372144997065001658293867805930582637040
4782251974753314346247021877862259704641
628450711746239372327377169297132234885
544126914780352853036471868865853973155
917495757843633236469533394662943249168
9365938631336496663637352396703743236014
236220195858151752649766070449558533161
6192257750486462581697024286247042121945
8181882888153634658311657187262298195390
7990562893471896771678527911675690603325
1622743339099937978292447434981207456
6708519060728345781552272520324352706707
7045191439951809829668046438249956301591
8272946192539604393822744512207452333319
5315844058165991287923594964170563851222
195978145139600885485030234154918543506
5717807978772349463194869847848619018708
8398493178228877294542102973769364272
7662305792795798477782668971135597316533
89653289661839265421198991100849247837
4691232373665329289249112101541850221852
2858504470092114979897033518791562624193
372329293374567253865778244249055992844
79683455248549407078632074719646909110
441719042156217929297128682501360604708
451654198238692045769429762256077609634
2607708931023316241692365455276349736727
17611900492646385074727775339927012661
835459289736536216523191776848578819409
328642353996720396774840414492939509432
4496727481605912575579287335181632003772
5714475217696209513696154572766459295141
70732643070681295351307181506121791281
2928320858213302467653865512329841213
9478747340592134761416083588747937351483
7434579473768925709342973580281188353353
69595775735778965335214187958296928641
7192256950056821619943276155716041943
76552980646548170138885231385788989131
270875747791608367343742191180024896903
355249396665735436183975402828503829996
326055751625175172522449774435324403595
6753571555436354129383722547756472596803
7351867384034662987749389766548511145405
2454817171258675895647666742143855890
34098512621441883927690271225528464535
45876989793832432530246534735095008230
2769789069881387793078575596551091216577
782256784281826938835833257753926477
9816124697632761702195822347207427309062
7147280531511550532674373830893358206399
254349115765717197196627544450265096857
8683245088293087994622571296292932809166
977190754349544409663977259937542128218
407319763661763228745783790874342052288
99671573639150253970997170886188406957
9957804396328924808064856830786020115999
42318210459307232495260171262732851219
518519088193581247445316118463926548952
5450345758126533823529343789959274509518
3711601338908979270896291542769150346613
7428833127157790309284679608246334179308
32872738579847591955211468376386175720
78777638325956086312920314993069615044
1625560361943535835333862152603489814266
5121424627859746946188686449154468172907
6470725755176176709828679549789058304455
6291592322048882019253727321924946539
37613113415860033539392964311442654189
848316381275974182245291571673723806
1439152122023107379698732307653879953076
5534653077867360654592243866210240825420
3058852819787251482156887025368423027561
410478032887556842114178707959899713432
1010181687842114622586946354975669971393
1393338898572557843149685991040819387
5945252277644772111738911021849294805440
38794341995199912894744725196981829182
813434473580715346555644139449876664007
6336832325844413393312835621256207551
7681792476404620104511247245338529359345
8686816065186956939989738815683411320
30698923720932111292639302128012711284
57302377636744799660362117814551665668
62199338382620112349825703580977953856
744755841595419123962894667321001122636
30142973672554185621341481445199257653
219955569491629916304395652350223224081
97776535931286962635671701774659189323
1918387676289611622259852675108174559562
9731172435044066637750185202431822844864
866406793966103803320758609330675893144
3504744465506402682524051116863553059525
85977318340743561585121242415044549607
5851765852732933506033619799433221083860
64334130345499257251274601428380295203
7863526316687071546671246431489457525577
270340145633520345022198667197789072020
4863939971905861099810907254658121911
5862110212898917757464928772802388482602
845749884014736810659574284527978906222
3003233482050255053810913260821260
6802431577842200901990563267576716780
782251145588394515072215339546289458929
6086897563613733406355557778455248146
83521632600630264329265186526673136573
4085687889438839731332795161812358758203
312376575391519949882176697597350124796
628381466804757962779048897053247103332
66659811293981510293902164698781466940
7751353560446461269497956615149622828329
7923476929414301965592994421342611193364
653022364081465023785117634026286013987
426191445755589640515906981625252953967
779856372493182867027606041902589256629
6920523544506626152473294890907846905596
81442705456325734165318410815943682505
8117831870745858606719613141271495398456
13768887941070046660246178884866822672
2642413190497705104889353567895716453416
2033541652638305153627762503993046974638
7884973473688304472367331111453266157094
3079364220289909780841422249205151856990
590183725204734642777334693357319151412
776227333334764037255636852513935513
3553610992203719869624927455325027699801
801960147355507690109802657467601366360
403433933747824535633839305393164768434
6485631163442425911121494430411764439961
4273709157516269855861639073680091928827
943328356273858371931290629853642195918
388028085620294112691410517613031276671
52227274532728195058133246878076057689
9998876349775707924443049074328658376115
7800160071708996953386844798934777802292
1531112981813247619739681358584545506511
91614963790154888424135330070874382514
605446376073474695726991109866498205849
118151924490532456977162713058955823533
457612793348143486635066362915621395001
34784258581947077303788697911983385937
5430185071983424929264388755153816816460
6616482127293642725728110198686368111
7397844855072703734370652181175748014172
710042887315579189818123914991097956632
24708323866482733320562918773379697894
52399886569339253911686064845157495536
9201802839588094226051221182948548874406
901581239123858240382392500365136193994
8533773327109124618784379856140620245
8380828779657631835216318539281192939
432519593735289377457370664931193483469
6634986193169667046649741585523951918
20002187774510662953521501523383807388
296868567137398943802611371823916112566
114351784012619085014068921965724027612
53885700712211584663723391501960087306
8459592994156781265505285737296849510
97865109363730145894372594155587887550
935855314387378161933359282410113336189
600221223774811244531247712016993221742
618182266417349716355238564139386292549
3809140866716892247574653875770539505
336942715247440395376633947992146882608
86439484213906132788607656904430813389
7721554311501548301312328005396668181267
75768167513051024646091333834958545752
2645368711316733275975756782514861535657
290990956974202490815771247637976717377
24993056683545942416474039732587578929
2770118119885614672531887292311299972995
379976615962474231117765668023541565463
839638158043881129724075121516358655596
3335225597296146307183029183455311425
953762971320113763226708117189225202840
2506903028267157467676839444159360967407
44007519061578814267191136770924112
548387488938947986389176911286180618036
181816115289229209618698614391114821
146521541546846325639401249476921099783
266867215748993337301091253581167014718
4338437413395732691979045750610424723883
1943447549066087870255129230911932728668
87778820595506914265753613453137316693
2055789312577656754580624305417851145076
4709342662383686228390613491441064907832
102161368209995317623886177152822562615
7888628328152023222214912265740512592955
6516233115721544721714984257750183948925
99861547348398353234381191133867449517
535414674801684542560497215495257114446
773614246677787631307604439361580863017
5869355032801776715893661861162541712902
282158976852714735266767150126986466956
7711948671533289915326312414954525154201
2621895347327211227723193107414186939150
8710815860436692553059921530906279782910
150260328005411520795055949128420837853
376541778650842979365821559035842870583
5313126281508656312863092774933868594718
1972350595542417685658441999857058128070
3215747244251808344683182094675922246
6952429479192786920312109941323282843638
3254443548277808476790484303440180803752
505438235391902194360724233703283039007
9138530359563711675319661251275693912782
7383163893612251354371167024162413072697
821811925952389124644369357526419391575
6924326394805762656578159971359197854933
7256138073025548260383361205163994048693
113741571281488053487238171466032172754
568665294472110522539135758771524917892
2148599251429147315089142192856871754
918287714641511901792659044676459939919
679711139634402755598985535110776076187
87609215577990512013994613667753387599
4893256455712946259699878104246756724040
285656015826175968936565305932959374077
605999593776382293301822221181213974715
282731491297212441638036536281216681730
73441433439584597059077037348828827186
7031291513534227732593535553155642108101
33487837898194920193625284374473597110
891984037830522686893620611275782205608
703912528221752327718338957399519135023
74924622467080082575263520579811267362
836732998963111322647712307046670915824
837786263065136108831027020875389773905
72737360973266517863477998578396578781
765894722081863340333963092144260967560
6301732045512754300686969443225427165045
698733165330234752289796415324156434
9999374520222318840456892927966453768937
144480587175549137667852296792401955986
8465882663152600515332610145367037860
968665944235522718584561426189634149402
6650564992582936742690317714415810609030
1502821670629182376449374022235295105914
782787429794741687155546858661551236250
1127799941275757782984842255104558326501
76384593381533895769178381515339801765
7908720128827052224198270875277272684
2302568944553209189430311984784872705649
37557742968837998329389793896013354782
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]