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
4723181534561122011579254573273501894
7818156137674249591997479518941451183586
4384179022636217825388015613648697449952
51797163287263654981255518844073164131
500713938623314656838065581772845386920
704263588411128833982134341710143385870
338569538486972618129874947331375837366
837646436329675253067523020967943581004
7565310097822209182938275640131233359663
66752300439086596060820136553349943864
68147479277682545513058991040585703367
8136234431395945217745723019716675619593
6987304583716225710455489397797858636502
9971765595283731775165957189305694882561
982805365604581820335546314374654088661
53470691250475876918669205289292592494
30391699911531298487402395311094292954
743196313441708219813829690354074808912
4969319958144145676142489204201172672862
7488446265551949849931203963109570488919
6342168414255307660792948428490454457050
4775247230889036838280288871902634492214
889987797126562120581083788446812805975
39497448135686085958674139353316048500
6428521450149061345230278612860940836574
9671852339153085716115853348758447016165
8838712197809688825188627292143164524678
974165801989646393171588195160875161167
2527422081894542495091881954906856573027
3842801033827082958386993107063803221
170746197504508114643924990949545607365
2397389762043323775189515096484191639155
6007437679920263916302684996118811443
98614552117522521047172811677673079893
99935185294624320179096597513862517990
75164432669735453169603312190153220832
856194877624289166294169576352931810
9901716485631836914012125538845858665589
771286570097501155586693440852938524232
646436947444632067871237446552465256189
566128076673882011706188931679685754570
232024664403300129421773749434834889891
2516710287328593925334524386168993478988
158127245415364814160367404794750918777
20114996702278312234538881122097499411
5115571886556981276462384406220295845881
4322699196108520178685646701964449233834
12063591740597485996907232542381673446
645757352342187523983713642876862141664
9713783989814436398495116470900979179162
787535994928297035384340908933059963744
8219467056666581840141212357979679013
41785364449931624317369778906950497685
430067052098127175293012331491938505059
7948737027608985891953491414769221237325
6525245145476223720290306920933686187059
842793156128568364445957590347015889897
6665577086738660616418245430253625442191
499904582461077179154721313292654925985
954360558032726754643004758218778893853
5562412332171598434011334896417845168872
3791648782753993059988853079161177129
69506111454354572955624431416832608096
884240697386274539681189967045362275453
96057082869578920277133219149210122728
1276931251288834919081651758122220219012
3389515236892966227645261853768952853
27328135163788477840283809141659776845
62728519718591819157032653579896714
543533392316618948632312929394515431718
9892916122871931610382187561453845647097
3569881732287571328863331721195139756033
245369387976975150368605776312872218809
9904822790303874111591281961946042883127
76722255533846335677027428310757545774
53936101746856436834978871924464895123
9604745774105046186989735914666415634984
8568675435396631427159649907804910863773
39735646453604975865068267633278964987
5555960286440453686705224912121927810
6826159618343470781651271577455886962361
53438994515911439148582572897047286565
250926862621198887936107654069839412
9300119438524145180685819080717111289313
111824383417764111446142231315423352426
835680865538677712441079826140585718636
1722160369177036225017417220830965926474
85491893375151775466776306152040216400
53189648540533568309719318743299061869
1053892634747390572823346924267780684669
92855729777342330267638558970360196400
9199805465083222221384325028689961527259
375811779675614097062181694166294243943
657507673622619858564375859847723715482
99448003345340484965821734973634924570
1788575473272742205697873995423673496724
6760436736674385217698451255974891809
485846242344924641344172816254465143461
35948198267703992037605496224537918560
395998039654218452775379025141428099418
951185248495557448807593212235860785565
4468339094315912433293754235885972073651
753929848605290386153742938670819879128
2095931434984748899229263901561694681944
714580062723380940283177406476218871485
5777305723734900511257324421721362368340
485200893582981142248734537806750282678
541765753495134979889247299609373044909
98774087375256617264014869913143794466
2646508051577728732268255676847989075145
29568873715927442780729348295889914114
4769644731521743776316282344365265888214
803810565004989020263122996585399694203
4484955842511135611887638995492416012921
23024260418580187766917569571676997890
6869230482498520227569592515496382355469
9741210655875604876263415533486942245
907344094861502313513369426388658608458
2123849417822111754590429217525950636123
108354383383348806024904047658131094119
146766627595229565314177174814838616650
1834479818889934870738164536738268438
1107808866398953403013929870538517186901
524926532687771742254731871715176402787
508715685417483532362681345439469018604
620370012544180824960235718587891563107
5891953912476640289922377325359553993638
683965751465721696889191659157186655227
605259299998374811986666611812299078252
1936361245008886863854055634586279037765
244202086843727738998418260117996965583
6343773133417298822260979688384966519008
7946796699005627637299837989476781190
5266394720071284992652654766867304
550994098015222879728961215376327355
2466465917381683871469716774753066500
272718376895291725262344938754366232901
4355863441446140027362288460054648302
652689627578977972749250671724725852
651330396178580161889435373386175337326
657532455859585957912794188530525013790
1170578778139082412366764133252917727949
2466485015661970396991267399287325904539
6333860362421177404859085813240183446589
684212629467884954872946210081638380
5061491671321948455356919248890159538561
97843140238333232399682341499657892710
416454084269760236759124702296814896030
614518582151354311123155879381821566376
6584231881691744990434626192580883888468
4140137885893736795141119486257912947639
2257637758906688972789973470304714661809
264948476343038457289216985168875858156
26581926461129973017949368217089575962
1966543447914969102521815918836971611771
588224446925571689644974865156133501192
364832909347328143396158803311715637293
7139899489047269669821264171814536747737
7445426377729979116380537366274202682
6523550560552719695855651095653643977145
8806797812291206974296727156875676468114
347383327763680869836759513194946363279
6047518450626265029319213894291282140
55374525185425252099906908276193323431
153438470359233838651020399266524126
862718532853214793183329281839464205977
6962666484098346515420479440367595070
53593324749034471131421194322813663019
6761620264469382642492264913701558489560
9343228845464835381865282756797621122930
65214915966612054752690548641351402090
2445751024423794586554481893659443870
9781301033874403582460227897618573671236
322298380763382196146121104992761671193
771778728226668231503267159550821099595
8453717499058111714239325692890340497576
229653047781958586233142636566354633686
8383730232518387249448243262056756506
761447284116440734913386785572816098484
346291186688687868968107150820923364241
4317939156195862656292244718771152373233
645568541490665338559236892757326596
912342651328561345499209850540432793326
4773221053261717838469662702484985156984
8866077119154237299901875992899157563
8723894911392314418876637749460692323752
489882286871602595688142702946088819817
6198903461078872380186124192271057006411
40867895466684468353057031712820458
4462874653822032864564255599911939063875
933118547411947135022511633428717596995
936668034091118430445261374817747716971
9825916442307781266629436202890646495851
802438733779590564110753843254416769508
12017348528151378339706085553399150931
3013763285418306867065149985732846940
5154571262791142187304306653777251895
6557207410614813178923787526426786818108
3594595559785920701711644077746186964
2840761869041857844351854808974844119649
5810126301768373701802704235494105803
3939988535409538210564946284742838547052
26427237910724134562062910124097243975
913836965237449141837799237588391036788
66381817347323626745683937947239861279
4014625937268432207472945558630821033877
188027499884838067616665942414661877660
8526124044279955878109041324110322263
195282306012109656858498346502338534409
387624885084881746848294231498945599350
3582820928016264675200932961635251227
3637269651923238831877485357950019527140
85492124653689476916216229238030934435
50877603960551615299308213284503292113
812817759553320145964933944302791271046
725774919106271848097582242922513633390
299071382266981693359921864851760852335
296912757055322647467635818257603544099
9033653859275320811629662794317689483916
12176957167101628127362757292117345684
6189140219931827224325540335239997449
87570661425248456752882996826259695378
468247576050210859424418595158377950906
1170147142079728208122372565728717436653
267991080425255112612511348125735155603
80032917457866904459155398283474906565
631962464392829457571657550951532481406
389271491552575126428450546250057724642
4500989595153598281944047345402914919993
2725665327513746314915925664484447037897
1970869565704525704973089103177012204654
3915622527644446193467133252890283748382
8913807243162346826087482206791910693102
8445402740578560777698914154645054166426
543480778396437208476859584714223866519
164740098118491762822908097404326908790
5858579847112085674184031167591398677068
3460793412109148189523383716815622854591
5426730783124104701668302521825043142953
3898272717213267683725044076468471216813
9383475131616480873477626437273055231658
778519986971266965162092901930789159907
57632598376282213987781235557584936700
66591949157196044867690841556961788483
741399152467453878299930902140086792286
457411987895555199429265617308487743715
655673797058655925138535562538115469633
52613725163848270353455469987591091217
5457191643522859130512881649118061447266
51972530598537862218432383427132075294
6245557512537831601268431878656772375684
384585792629735302043537147095459888
33357769764128432279825689939227387398
26309095728758645064840697216076937802
1908424630493796342789953379350780172294
4065650066258025443132682090344223074499
678883072541251553671781115935978459248
655113358192578357728529414799153637475
8814945369334530921858966416390929443
901491753847816854383918912987684978333
8966463375011871251276027696526752157923
520188184683355921221812699660914666898
514614430505189814543589603820353128907
628727960753661853122406589798514484938
452489922622531852828001297158098964102
73966110745179003945751514410523397515
8853277287468923683483564378435566813981
3741743446914530728331703918909125083844
2162445660817790681925335512700220396470
46788125606759589812657582704651946301
41414133648019863311815554538877926
439917696340906451694532848070847776295
972438437966677594772422677841642955374
408041714721492371151459731826417781322
528649429562765356434109634802346991878
966135978063851727211215380762040353734
6275177664771671258855222535295562588668
6749600063992303723296229907570216506417
1000068664185143126829838298849774352508
3253462469483514800435701607210346343827
698576444821264797646967331846296819062
7555915355334846916554477711460119555
2546100546818358328130135267874829865034
3503565946585608922271971577634583677488
1675371097841139698532459462254971885921
3724689817206445344255621113061176149
383597749424619540986374266977134001501
6056277691356553279529675138585124339953
1940108674325991705112838087334326287602
6613465748598427795888703375628957734487
762413313555286509440381595479337915557
916240366345584260877335083801479733872
721092545799349585138109610925787361062
44968626445105775639217437150731189299
1049209679268484591068025014380724748677
22964943657723793567476597689196585957
2167279527328298987499349384733384888112
5595727611354210718853846928573338586966
3601771641417356448579992366860767813954
507736751861866962255637284762985849586
60984392706844226348486297933627861905
2763917555368595715028534258289077504890
2319621450952352691128559257771247905
943210447745844519356748681687149127384
1691835761463345924545966768916184179694
4782188029682707756588465265635223474237
2873428034287302939353213296227658964744
1238323155803395873748233911113735154967
321526869337323656421193619324848196050
4286479267039511667061584169718535952
675974056246426879309604254735388753067
797585155914317778556427375730524141054
909552961508678033818897648960291854723
796612777105304180720218130501674468665
3833492129875246108245427594660248139571
652894195711655756397447153916789517713
898584450717712838477936685482866822644
8824485994514244802726487772435570865361
916528214170175869694143850950482411740
1998901068147661117582125232250352599022
162453335659219338470999389314050131419
2175553472923880766295635329153340729882
363289375168741640672149271900844994323
435583691675874994851551989792165286741
3216772382112944423140253099260689578263
8710972746395854848054467360533346278123
3085474543539445562498949049567189135115
74417460665011629559690138894362126082
9924115744078730414951906344388890021126
84860244222713559027849113141636801437
81131158487023748121999602253031374295
3897310874599477507779654461580455466691
14219618969614837411005231684864803970
7632444095012023824172904018292780052107
484742195877726806048933164768987338802
917990640451879372913771579279015337
30489804675173765782441639627841591498
66339537964554946662604315382614450749
6965852514672367128940654674843765434
2466490093275428220870749450101458259710
5247379416129167565399237660844755784
8470326465881921207689397792597250162651
2427214489697699308370012055906795263267
371940312114714442320668581233322226373
959377971922953916452302284598910276855
6361324567377131909097895329895167574003
6263842985169221229647787742241297862267
95973732273813313783164812237252467927
3043937393723590553932904801205180254481
796042001425998844093939425193022138646
52430646172730265262465884628452779316
29692443879134585068591571773607754008
5887233564998044532210344805235988096285
7435429116357726789039412940189057608859
1291640595089670763276464747775951388246
9339486470137923936521617534925977332
942098777228674059526159091548138112888
74634641177039553707694889672456622825
46245621597223264921114048026366289636
36129253311111328580152789496161128487
89083461969143558427745187254348843410
903887918032143212195284387325980182492
7846183474703583553051859993942148933216
987311073749108157276679602506859612817
71198808717250587247309286985095989135
7864165176139571879321204089561416573246
6511926792213504999251719519115527369
5464841536367649552089328604343240871385
868297349099659229387491421144923579405
414743236439296574540886781795582215
6938218453075001563670429718148234837410
648562994163080501857282651634613073909
12966375471340479506998201261277929767
7928555684593842557529215003377994884400
1533250930578994180911464614661622277166
123573377974333680055663040769050126736
886571385164915938440603266567099439463
54285779718975528651452556967139995863
3287440186841374753825912893459331978821
1808146197106465970997182319872710112261
354967622594780473841413188410139316728
157117964408818550616225438304782999274
2472331270142505183146425121592487523585
48889785654425034129736825934031392223
623150315447609141354966372850082877514
789881753839722281133818083836727234684
8263668982968779605990626149481107407
95363737490339605071128563047848423828
896883269737316448137389526899970312605
7284488334893005525365586193183035529388
339696898564899306345426672573977715337
797926444674157246704133950967845306
338482366539327222978016839212985146841
544158411346594987627685704672880501699
7371548914513051761182813352992582712835
841345694534554355773239855293927484195
7959896556666189201315276364155447856313
8977340068844408229920728712940269421464
460690821694178609157191558688539735680
99287096186879916096295459995698596754
7868730075326271564037612442303570861669
1647773288818796963976674915960383655826
75552976876761465138022374145628602776
325591215796664672838136990933126282550
427249254640802837370249376314377868457
38855630398488387235264322700718596415
950654414798553546917645316464931796379
9538638920611394248630155996733778228868
2271540345667398523793046376295511026105
50544137570596214061956986313116922605
5426949699863748609809595988546674004
856846128752275844814062489494401662842
94474379133484099270822584747348904551
12142106926929710373086425775506504963
576594398346103241120312959300383256794
787288722669275282850129272850054899501
28175174788214149533702936400453894263
765238292709100553665443924534684164836
42616068153351407001340443069016404259
387630683792277148271432642721244684859
41062444933047384724804116285882145342
2127416882147035225077599656543369042777
4758206339173543544836531244656861537542
425169274020679088292868227118912904620
89119893463153771995842582699112637101
18608396658610749215706950773303634452
3110775729435541699243679144540320253060
3812494323934082246747588535583566723494
1611982676313987815348942730396780846713
6042831289731214105428287831454610564394
244755261227578915620399340887847119233
344582958880363695199062906135694781460
852384079550956440441468041721239485107
5732501142498023832190331628762911748207
546266045164828578744462792234252152030
2529784687656817236010311324850165878118
7778632219854663799474273427718761098139
640456777288960661199586281237830868453
549344787906383136556202363610664094379
2504562863989340286392675836691787973921
11621272159241499583985671695195085111
346369028161423659758226144932379757348
2653252676547426385718986182081281956
4908747285444381456836923275837464429064
115619290698274656750896154295226326728
394756371920839168245197751587172275033
80484858434278094104840427080475273568
785135304948291225519022209565664972035
271946726700898784897398174517028321324
846171591225613551966997238020891696
1296464931523681563489815204456886366679
71912240887892687760116571381482711937
45248740928880068669389650422930289690
771239052820910316097903763938721702297
38575292446934233872081943014764467410
14402804999031581547717605613018177922
6991453532627745312949863942686271679587
3075943159903940489296132316313726697288
5902151543977882509678306228967898339367
76536386483070875914513258290746006652
48128563476777961228599898929955186344
56872224206153587991927196935669316217
53791582522965635034214921229845635316
782969064041750520310342694181375982721
823059941777349513752068266463086317901
7390324058771433362716799693113985142492
8272806861117865628013641302847285894256
859770601827448823151712640872029773341
9160974234601556118866924595737091266925
9251802673608126452739729814897936617113
61803068755727121478549324911128012581
335685279131116839403958535958089131347
9857153712854712353316182467848243418
852811961793886671453403736602743649110
749262295625206339471479984235812586528
31734663909945366117323389513264855940
25041898825935814092455049396315966270
3953877328343881305818848988210710728587
74203388794883813584279856168970353480
6743219319034855849745634102236483541
832291317193793025992421268598216915949
7290250286431295397736236150947884342158
749177018757444968987407997487484427913
715621442444374798626403463613888986517
796772955125233339314381950902187484827
767358676624222136495269933796453797637
6720936523016514389578091173429238345951
360866723104083186611318410305356855641
183216548436001443350307814601145356197
4326163837934068675331957677709458337307
132930713779210835484879617800331583219
326417443297837842049353727813025862047
868170801391717263091733937699643614
1944132862044826607984485927327735617317
793274004331995327465183162352465224499
1710560537958283936458478251715622428924
1383592386323434086130272632754582900
610427942520684179760428767469610177459
8962715858247749592666749848533610716560
7141703853542451912111426765284045061606
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]