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
129797054267973246216439881855035737455
420429333626183949987498700459681393256
3127318457269670635279410281012518665
5180670887657729799647943814313444476851
2106136141447465232986764072610177132888
8311817428121545884143745173326846061855
407481697123896601038112633956092989075
517367335063680682735177674890083711469
3475828377765785643537904656167702667
4217999746137273143977274635541775665662
225710921001545365179659683929214563
3166116831255222761893264563330068486380
422990483704230355857589681548861038219
8579637810797049994848368731155513607704
4625295981687324478223991271492465554
8484595837924038529588718027277585207
344250294101544149362225935748554716568
464926253305425273275059650991245302648
3347653492399842245717975630217863918173
527291696543650557852819652670833421809
627223911148240748451859441137251984491
575234398331137479816833144548222459583
2279802355622096477617498497663935648378
33206129967761224285304863598181901491
4044781230015372570058971651340665569302
3009877465875409382534183466774869269352
6575983565053682629231660425295464129
55372022658955284771159892139043732727
1163505449077688259628024695369437165146
7685597430481558679773283544989262009416
7617602237699024161837556057653444456844
741974114154102666874847834199828948342
423358871919551164440717392810374186
713367738087652914395876682935032644598
6693996898807688955612969027466784061457
1548753280725549413611406728762190221097
4208882870212645190871676840216111895488
59328566453332413045695812552096731117
5200613199749009678790763253439429976327
687463568522529598544067452320461788339
4857719651801555765080836008952594484912
695294140612259426537784419787451875358
439393102003665862649635719275661982244
62858087416245884646874913498224092971
471058899444619290595189057694363922208
439950897289585043341236926526385248967
10493803805476183102972017434887317970
279951019008280099529136785507549512748
470171167465501177511339963754067429
79298033767457877415813564834761982261
548872799747454920479755695456535855004
9838497828308350798289543773327299188982
355152899569153684622636026504167562655
5273565862155305742131696879419452068489
5357755127582537726760986164708777524756
9916160654702698813410742201769677304267
277950631832274727211896908170752719805
733451008398872481144747703194460227139
1483911788906223407191998415560241219852
438514093321139637621518189515036758
84555496505148181476098527891823767116
46761559544195797816587627332015109697
2982435411719382495274032641551169768152
777888818381951280993938864132560526562
863367599199459093563098911221511732450
852937794539913949916205050884024481152
8424314513708916600685438831385334162581
520547283658434595022629813465479043167
1637322677828900641798832939125596341259
548988242534534630462689246421171859939
5395345287509141909917587761912518894447
12209977557749427615054557893764015329
788439151055738877314572721111394961425
92334208292393623488845482900269463010
252155689799375186250701000017791081838
4527584456689959850915536078274532423098
778225758885724994533693758116117756661
48699831214257524185700917585725044665
551753732769396558321241664772850651314
6115604214330119687102553118916336259
187073938728774679977504347566839576077
228652415388293869886986917612962881316
9843892251423613417322877322879242736
1710102014865907762841488790797442913160
9566199472386065142870678929841742128
66257801589466128332931672227083754424
9527570780963526326395433772529427569036
6661741263058882522510938167313289232392
5765518975245870445796199024967535389
3529733855434020496847958761885866009194
6078608897014152589385601310538458343567
8039145547981278186317949640127892303017
6950243074536875715461186051201031775111
5303625274366971432317328365519711804947
290345947654242312590042495957738269190
2937549313489972643081823896605319213804
6613149777279867598120804367192221555312
346766018616803455351502715434115055766
4921793712408176507153043451914484439056
8549173457685547307234287188516785468006
3074703794524863322989339296882654124386
765574177487617098175102595893740073274
8681985839268553494388278122588076550
447445715118428834832002761672917235321
7254145342216055261860485959181264879
54956922811897642466258915351212635042
3717204342943043382253768553629466388039
734887646411952384874802061760477665154
6650760215215527920934018996669816211
406682557790118735689386484478136064962
9713948923297284502292241498135949792106
643138588864955951372435208397814067616
4508177626234172709712437576627787119636
812959228735567105661883098801825947
1119488154055903353152424330610660942960
271396631376289431496031977867955093360
1901427915093483675592665861940491941
118994079706657450235857089140652619419
4743338591303815273650846929528086891
850861391255374858331379424505792312305
9059698024439264802941011017468431536572
831820228006628517225242653867657106584
34237257111924209737268774681636889162
136731364190501368268741042319277192060
571277446790649086662049917364171333652
28118105206112258328363561117141836863
638744811721825775175596420169005255411
336798209679538292659472143924999589228
791017293029316482154724576461724631345
205144196021482667216276355900569531138
76077270474533187369786027381446033705
8497382514855829142983209744922859552543
1930879363371558409460775661932722261908
6765477813516786771092498729311312695331
4859241572585619816656592203415394678814
288654165373552319262534435487361885279
260561701126220097617078543764947574001
2463783967892525428434572699993889162195
357179854329315358520004505347031293
9289291637164501787557856457698415872184
621768508531294291050935647239397962869
7997888596111437898014111549342922129793
1701348080813968224410163922494028846308
7319839882224208406228750544132379432
75314628311227696184316125894345407474
4531546490326132296071385288991432753245
848481785833375786283041771246662016183
501583310056527779620032369191385533170
27903453480372481336292808872955015875
577187929350116054251371557935736305189
2204760734412012652014515850100410906205
42026144478405320122473120120661861551
96658187455696666168745279490999893469
4739420927354376839285708409148223589551
270562033708117970966648711416295978826
644966467706380203850447245866950029623
105365365431813969872493172815448317404
32180179476255272838852715971380092103
2808897053439525719190547368316833779613
970557211553294998749734279204753416990
4930710316982221405967795834685530726828
1677802585316972849010341592474204335
814210931997824496752543693229840019674
2812974155550325502975127463918947028
6550117944465853252534858107993041112053
529283649063323520465085478135431637627
665796537417950171442732279965321957852
282614471641920257239462426512388487222
596789228582709548629646575994014103669
43658843594035366574952105468138596117
2295549184605516763061341640432955794978
1454368562551383493089719179739181856828
94054755588535360657726773474459688950
2334345252919588448519002721780436026722
6902303660766932946525121669527767182
98366537526662125573229970932982811058
566915008732477335359160426525565136585
305852923881142263306650362548480198916
19813996484343069134622557851843407962
64847042636207917885709757699370112901
8747224889078978888768415546230693823485
8228739976809890957412928219998769692888
7399694969141822898590732514525279215963
526770967603729710267704247180693642960
66133613700493662351654162263361189536
8890612639634771485664468805544618044284
8734668791247549248150726185875353526940
135534547703453745057911275207026854858
569454023947118097892312589394696494378
9740169863062751436782369212134338842172
9960140611256559512118735921128354142157
9431957470247267206502642733599974184
1312204991384393289298079354410425096951
6844502942602279303155917423183143326205
91716324670774972833213476197629549802
93027644352783724759938239868065559191
297468355271172307875598628966198178
62477856828826151535667752761273803245
110437847862505036280105311928892633207
8900485429854826023545059495136011954
873143495745187660608223382250975389532
874197641145216495688043258523779025655
868054327701796328739378692548097495216
41416226737190324974554730557263491296
50102528386187822383303626846920708304
85703785394108487993806784639188371520
784529809549136336976800406205814849918
941038193392271897840873534239483625180
4715154549115347897869532297750903972
95474394307456866543343820531354342129
665996403255650673606554756729908432585
363413134695654655748962780106565443375
72558774386508694398001149627383245937
2647235638059402160044149028855318869867
712372648064300056252673079670063348418
1798113760153434413448715813679316738009
607894678451124440676998687110116618725
18886255973012782785412699385515255968
801930642285309989446996945475077638755
74907981942863469573195449917391476314
939960746664229831540857514332635947408
9938447155842908894831105914189887837561
606853503223276137875897697602133099284
72872719783230663833194034126633921962
7186252129974660473955299143650134232298
9120207445444084374262478997827411030
3445753927961648559045778008246916203440
165197737262726482246628989376323451224
1232559152012146004354668737557529060
2702354629685918304525975188903977221
277364677714331950624384179567789429460
6093953388261385472526168841250574393144
98984804854797270860951385953157096714
54288734819863857714046837712393823901
9963249892722480824422492342479684516
6785236282233912895182063057690747958848
20883742211568724756590577785332956164
5056642331169682318979022883353477842578
87423354437146789941098722072359375998
67951479032368460383818525248770594352
40867996805831872953792469921517224330
501149871410277146904377142922183747983
371685452956832804911606382383767952496
8648302123116214240631559630176351734403
285446786584674180778876331474685192799
42772912156674939705135847905892764450
54361319644226656897214395676956063708
600655159362448646587502040591426132114
963726883280968169752378184935438738668
3014304921524372594391460798290719325
31811540665934976454081304932889574731
557112996797873319782944696478932901474
6396783594884099801085505423277042336067
484942682645668790315474578625263489677
352447103508199771897597426836227427699
3906624651192362830960242848123641934972
697530185433612428958489871919530152496
325517536983880307761153140781822142701
887525995874861415136571267751271347639
631256071559753982720298942150585992450
756456039993953777981765906384461742722
412159734374607214131935638588998652404
6334952069074879224678715005544238493986
467530176413166377947493651910185406393
2268561113701908121567277756369147206094
1099706310953093735736675866446473383987
565485049467533966540294730954670409745
5843828822892664851919554724016391013
808295743742153901096036368103470463908
124148509356565426694833741373221353630
49041376986665425614883605657134329614
7693532045378089965966355849852351926788
959564671403476038884469975787140683695
64317781554025334068153527966035708295
904830457607942994286961572462251151088
34948104797439039310632566661288876827
256726393069398465175614212707115297607
6331824393302883866655232544849788537375
2262796886943410600016555314539865735292
96137468359375734530427618469885583787
4814686959096332264176797662435411397301
7004303718427174927169008522176074294
816291604537425670242916809440231016635
97664077247746059248631814836472393772
140115607573252652660102043649875454458
174872237846904321040959172451928259578
1309116721986298376822842389641859972
4405551793681360473242916358623843617439
6186325318646955821854191442123974048807
3881712958755901414129052930133081798547
6964539773868081975911766222771840884077
3168639997436963111252041318948995258659
8708543892362642513292182177364896476465
4043858295828030986937718488273513503176
7193231864646540716647619407571192383745
9003349471931574980951551755609767191406
6273364162953890133497065685823447552432
12799218262030514137683925238494059833
1582765298684505304574442514682834634
3601625636942466942174832205960713418574
364759482173857769591358775982052258768
34064951901133611356407899958791767287
481664793024293398176111286100838778323
78327998675602343199074287112853481955
53808832504405775514479439350084624372
4404848613343294858714367608177428388384
5665196294548646741265099695786827923335
678745247529712228267482682512918644254
4739434937583628933924821843741647272
4820385278854417415723654733938137307662
87011399991798553603144749675361807436
519540341167609153679619263907954654545
8807714133886943671328894774234775581
6829950997673524219977236581990051063564
52783339731679814514512826616527791890
544787837104240655772613764762505641
32408659819259485934347846679757051323
715512654151890067259304156113067605276
363440253109485131972266532413251314328
5537462882253317707725656067160248194044
15914760630620385655564903530246741273
16232554904555185743911665504319467496
85891595745154061745488213245809303
230496388549401726409841760043609886686
609256246565888015099286503235798033912
9923363867081202756864297937841672800
302289713823362850531774957655344868554
644197691659964918682792156397054353680
483961067753232870269246703631887827845
3830582948349670234135606247398065656186
45216146505740822251651462466817942358
6091234464779438801060811241450220906114
166283389171370118142745131849785259781
7476469798462152694346933416468157622787
678459459249672814755972773534368937875
810998274495221455611016992265547402971
53736406277962581060072710795837361902
85405779276013258504786430449835839749
6161935049884356178119721930449912553486
869997022837903533813447547450879346074
461169304097102859394775806164338366489
20158011162974948847254577323269869038
4909910878661595445990915462192647071460
159754731723466494692786379194947268485
6399723889116079542387346389289185254066
93269349690989563644766117258499976597
3145799741914321354591661186322636748554
621073153609550484808996237031844809257
1057327355887204162747809930845383928274
3306716626241943940522988464642557863111
37763243218435603108753161075233813349
57517069424615774615026434239794807741
362960787865619220142895492072938486561
8070178939357206634585857403782780929170
336547972569211314613633962439515938137
8214093732182254660835776420437036110
791841459136744812732733868957590821384
5080649383532076492985646480599593269049
1394921895907571780396288950920370462443
9745516546388640389671443124633883721030
6593164341516988810749499410410943901446
1908532737689397157685639454701180722406
1272344020175780455412075443180196814461
1446627271965584351291265237568971383417
12808683576062921254324874791865539910000
1184638668524703312549291592295355358753
271694284818140218943816062235927823153
6138970840058352526577822330783544367243
27918381396897321245708519491152972206
7857182388871209187425676425950592688168
9374319683479397630120535566285185793
87141113993618110143101102967665434007
1261540916426791655235409183109963521635
9514184298657640541115365653637626315655
206660553205109754491033475404398586987
6620310532332115865175527771269258613
386992228576441188447420563378082399463
81803491977260495934709091957913603171
670962431662665163223872677807374944
2087881961096718988759456460177497981
6548697227906135915441587396551027609479
9897977647471193581430032852356358467263
882388686176531586334873874411639335473
343420542845111519856428830841157207853
4669961562243639253309917037489593300
43824451206947756252952047994958967029
386323463934624136413071729838333427161
967971983243932525028142891527945928650
762945523083904269577037736415515535651
1490348012693046340819707564627946331699
125364508633388334101973414688723395397
8205604424675161981538019010394414513358
25701663224375380071075812874266956587
26829368712568489544672792236036732903
103096148871711971852368340503771140
63563272712284969116939230807067069098
324486224961895992470474971465925684342
5601845343487064763846824015825601294
68996486263850791464237476338596135386
51207816953148218997696816555868745706
674368149578147029308388741652790428905
441812952655579914885389410358225806770
962131962709347166069181068477413938784
506323940059671399497124529285313864204
625249838353622806823374145119358825502
6577805968533031487613472679549933784050
87927279295982921008476946552635035006
6283924025728188447226528473404839799
989897478461403982291404222252093166458
33899155668862576255061503859337058372
359499593347398171242698217831207496189
852742408309353285772673760242746706
532760116191429778812144307368059509906
6825416977718689264237326514327480175959
1447212854244926811912394287122868192155
2376553617636849477183352026488210679199
900342318254895843949231865115251697477
918083395431256258548443435275094577631
48966005434652543084120097105693394534
60838291383724434126237341351591179625
104167666078467991692218843338031528271
69832733094333843589489442274634798973
848587331406514709210899040928978348577
81345461484729927422616747374844193655
547597381582271916413183865969640086861
120360823153021137478564203864419501418
713499367772814467088896737684256211532
67372342635931293817351690332346213529
1117904475711052119218922317936417106374
467196480321732154564991388069655085
2565601813028784675828442396331543425
7166532346479412286935869507534867110
2624386794427749358832885567136458518714
66745785601795835081582320764581774449
489574149119591224730478004375739344
4403502291045474886595010449480356259
381327092539777591845394536329439293624
614786455375553677461772985825013823421
478459502112320977578999938517780741960
56935924252425833130410106764263359809
9677796247189430284237991580481811821308
225715273068380658154188238421872624295
677621261738248206744639623735539395534
5332315544652187107850585039943615081681
833430335298398077225233469443188390
9873491653055630430539786063456378974531
841921403925852431211311894685995396045
6920379416148904558934969226841894415632
814592152991792022035368936108395449461
287033603467729963095087346751297908826
96118313213473801533162401799321113401
3423398266376885891460204063852858783508
88705419878856791318662896151556472632
8179406313225080510399232033258179671211
902042978028508213466461930926910874936
785939259933435352969524928656642704731
812121549497584895321866872748064367017
54948994997016285746536832509234981527
1856492946161359254730876859257450220
304397283843765701320903340485097733053
1851762187736671463636217506878753586017
63412651822261316665706739101575303364
150655762107302910325860250065102133482
5230377627224013501212117294153625201665
868233447378347592519399184220940013990
932118078215105837711351572253589156533
837599326755373321379477963047193044284
4963925484052417577985667459173961879
123879513324094796719944839497586055904
64569075672431661338384227298343166847
184059052892233769762433633953032649601
4320566317319439463825748828876656784
5576513262048682582557029802108617271542
57216158335776669461955745869552949849
19901484644273055841874856279305286742
147185061897383849423713008803912276615
823590122306550172469006863384149204172
567534213183897038954974980514108429007
760414898856754973009138803096151253814
40807230468953222055301848463719734782
555637307732570615039949227406739801450
88729337566300722769239590610773968029
4030377459462466328025603431865187728926
8771656669206449663672454498306259824857
534348014828105849972684910156026975129
3094635984677595866441858966766977609669
1467666852745493317707977967663911158
9048288073699794163688148042380832025007
3586551918653110376422564276154458466688
8801555272250986932895928827753995993
23831803888066605484334894007684253914
24065203312574223683610439493983708274
721715637306155543994803783230174247713
293154502588659097656636283460795979463
644595774649992046986496886083769499803
4826465702805912498280730873979863079
501581758698722023056916816725083635221
94579304886656084760743468254174788860
6192662041090585263571972345734528648
5264264853237980718891241554716447557203
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]