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
2053810568063008868753685889987491888
229629771465445222467358398676487577878
9947419297953646603132777238459188454417
6815890436318972663091893751802657178979
493597735627914750983709245698960043283
6678899735925238314217851274702357503207
9073635629834884242959737745394070152887
56560221629637320374351097877185979926
738295985905141081298779344397915650583
17414641434821502850496291638114418980
482089696670152345094626254051274711420
397720861591187590496775062698786627191
3516558113597355990993197803383429712161
1496621044727250714391845270132162991
838692777288735814070043044662639492523
815420456697326742245805947786823342228
5510528631133181952540809642913212234713
490952774633187993971353780492879445123
91653624276390465211152194183941369248
18685443941382732351527360747029148082
61789636100003754880781598476921134289573
5808668428775409363332907395703484951090
419047045782209964879064209903927532839
85068229923890887021155680976931509955
851304868577440134211230396036006827
767858544045522179176538409148544831569
3483509311713323561681012282147194659
62981802305430274425248505497073738170
247053055960439619286388249041549532581
25335714573527933711393365245966214245
200841151856264481238472818334532679118
1978957357435461626541055638464072581281
5603935431835467985129022560614524231
966594061759317533014015599198517221034
3671209976353758395539087047737918859510
2572858182194449150727007022983970436
866408136736866314793869863569885826695
4416634734563943156243656392999931286553
53007000851057048915780795256091352823
832355496009751462314351139651810504332
5793509495386162641562674120936551116300
6710133220938966338024112441672274189628
25175466036917129281749280317119526225
882057068488827733838335940281875376360
4934506977807435977415315211876706188
4844460286189950118637767882230718496423
495035671528207630231454638426692985468
37006699926834615018533188549014799885
8907303162454125411451512579399953649
35628747298096553859466429660083582163
79414343246642396084076547973863984371
8508698666547796568856454837320887574031
52601536756501924083323509351559765108
4431569810362188352442714625976194198483
2831137583322875888949672326576171535
597118367647757448911187244654247123988
4399352685211911360747024338922691683676
7530722268672379910391731884223970649820
595988638266085348791101973612465223720
67074597721988684965869840747376651837
6897325864742145682336445633065533736
997071537854324552587075238405189303937
4506315488837953469871889740402366952423
337326031102961412887982423499999284561
34787323823612078354967134968706779610
111425351779036607338825458344782936938
481983173917709287545185531833388039953
4050726459316837560632644459770058017870
321437182344618480667792894920673637912
1765962555549582933975333267595557635
8375502256857008931130156779986653488376
1178757440804313207854028884565698678409
126962774697236666494008471133473706698
839080666059864044697221849442074168255
455583955465187129866017855286877927391
6844913532877977175369646495745451558036
594371121843724050732206790162801513505
398284723191996180029322203489483777259
6115705230066341827770835430801344548
858617236136271582481225257792832783147
1328266012078406802791491701137148504061
78796870988515366827364306981794633787
295130030764115206033566391344815979662
92523304165681191768905806663063503505
6256874598078446435146642738962664315834
3912345382341081397855458531744390981565
4539742267843115679845491085264660014479
951552037129839086618414148823434837336
223854550267674591360634262906639783999
2580935517326510504078931363942792585927
1970926652499196169692567700154729115228
3455910779858195162872738044855256017225
3023359692819442739619653814958378703546
148284251056771012097463825518672164556
507520292792993855384136827876858891152
6961644456206677748667241427176754088646
8463817453312786244328315546501343644757
5112867234374836539147285770751850265840
36635128842848586349856222821760326941
9788175428477661614151485682691512937601
5881994619031609945586923829242088343373
5844453183266815216488525992204996525378
947984871719776158372795251642767352618
387332293606771177844837297962450841147
76319148317563110895346578384507454399
4397840295423967214888385873837152136719
254499776000574774145644558889996425765
6220261928687042420057871724936813341614
1772559023178163714753804129739464281301
252768337989197571593913194599693898322
7002898182893367691710556781141326869628
11516426467817853907708496513511233456
47409922877513157153426568437727884708
7098639587131439725628999385382174557265
885737431100726740139476558486693651121
778574794106412884549288769453725208053
37745773604732266628361297585223663548
3901480907752631465348350099847592391
45991195249678485228024293294014726346
8697938827198017175359777687385114592369
5029470060104162815161924575306312496995
77899252931977952222300765575047579768
733933936488922789083603433610287639794
90235027016564570258445454286790924889
588173597234849339597637149348611937683
161882914990940878449834601864839726189
174522906916431549951925260841363896120
4119664324558029399279272145865366718364
8948541067893100633636386552718058175153
263979925857301148854649423897327066558
5467911776061284616133339020418151586306
474594183173116479146432517943231055564
262163624207958097671146379652264614254
385939013223973074453103486966751208899
646312414617270357660986542245974327076
482712582475426841269440458697987936867
8612630495835650264092698870441084894336
9557409756399442457556178465131453738457
735453715132059205636972098346184751241
5929395855491097508545189886585636087992
350907320825484632378005553606153827149
5080333476145128640648606847647230731348
5307418124715881132669134689979473023937
43221513205486986987692497691266639410
293957827831377287129038111916534171034
1991327340227513782547256580798431356548
7780649274723603678788503972875711147453
1833188869062335709563419418745611376224
2617836738336127787952982551384258547241
407468921914467549792316440436578492367
483779648845867388762514511553074104713
6961794162124059835540445951395424202
677054036432482503893932862101710031042
939287585854357164661508343932423914622
741611983984462226945085181378142136465
13271369735531798609350485015484399748
7084401590119079498558554839855363787084
951741572080558176274809698196813111140
4926786712866031637085817378338862862030
728894537114594275708532557539606546142
5842458547965008165653731058199364087104
62373342250372653889212854740299133411
407573576594504254622202482868493783410
221199531545251107126336890195682607825
1374970667589897559468542128324394074689
5847600799511121627122481340301118214704
98321725415466107265711669547079291622
3821407998365724593752701746674174453835
676815677610933409088415131327192424100
2530863371423681267365992741227076606526
8251486483674894923980464558366741122412
364667188416415896765627997450784418510
6374715317912277652427131147479083511470
314426899856911591480726967650124360
852362949122863168615845451613347264825
1121650242713914113418404927811188532110
9650416526896327362891656223790278699
936871486798284446570232662729948161755
9855852055178560644012517831556815263460
940489602412511172841665397755816941770
28125296751203616973415161343014689094
487831733700231813758645379458190589643
8516512813329431557674789401460238408428
39914205647784547415536393703912267022
3244389334119582673698691397193871293818
89899794285969968399396964189078935193
9900403028934917489019498443811043410
18845131553412082583857751538784185363
366402291294194498956217118946328809234
6517278639145368141220964454524873497586
848387527901235585374003875503195867461
4429841969691833133310725438544561421934
9913232832563419789091184178259566984037
232061256063366183450229165678129504620
9367960480802830351751923164455910917885
1208916214196852364948276012117072584315
50305138458725643279055629922882177805
812735342499338150891452951866286812785
6713804778821333552342234905671858992
880738664844662796052037829491196875232
12211007929162135723582586376168754583
123490907610567948786283492836979491709
4595452673467364437771219058517880591910
9331421011201318801074889101865423911
334318227325970561967026739861431712655
101449651482155323728396180096885154342
465051718271882232699228230998153686508
9939685689321723207961144005755830889658
1622214238456480470981689246942920316987
487576175361768922242380752552638175704
2107252664047704359834834369352987068397
4835630820303575399645068826426712419174
8472836951005947580623058149336812690
46071868685232967496138329223172873735
7732645812685769997635514855339538791
246452626722671980619849110264688833807
631922977625704392077106368202032307866
53672114170883187472099405355144321497
20738366083494973416495840288259908846
191030889135124624393778722237307876027
6803567436361491459856988706450428584717
2885146121961920410217093451310942944482
17039292794969550889108444753040036693
128177623199991232221069909505654563060
9690508798371961650350923772551355614756
784643483681021902930672613883083016350
6439849972812583752011975361595321615655
9140244337344154819384711212042021324
1396164061367670583936586955218920462048
522417331514871882455784398956611803116
46415818503494757140574186101112752264
525750667308319091043822907683142495533
452356694382928584533017885805834643670
9575415565346698763875559420086378757
3251498116719657192619745957459785561999
631948563094917050290173254498521753526
41842668406696464221113535323745567334
9214903332272761829477747039905246892769
7685693718666177541880066653745185167495
939019963775059312282554555305495246
7645868427127398316649863383840290074125
778363296283602099485607910340649982086
95078781724737370919092448948822029208
7125386757853592932832425830585966503609
5806815316694814107356672557802638621878
3638120123701118761947939647198284165199
969577228211870575661529478920315139357
4293575743367453483693833450687058206808
3642100141532232583339086676355091687015
4540704670802402247474544825197049626305
5699247612092170366967363973273696369147
72414315324965053031125226101597007415
6913513074758828234221221921278232652067
606375298998495301906289141741219982
9861615730155452228437616437839317374
347337361343385390926814510471133219809
84394484660151338605075460786958249561
9619342333465619627934615928368684634105
8611698984563661531637675979841596379658
91413288598232191736468461382287647237
388066314751565257946750718197623591020
8023755433951053944953771470651382062969
268854022873964021230913279660779499
188830641289425792761695658560669673240
4518744395083782607511842362875571981831
852123406475131982059967673253618984217
231843627361280964634798742554389075779
588842471130214998512869996998322679138
260933981204471894940414099118747149873
148579984091427734128709794918216521387
818954717692745289415414888399667448152
5680771872941290420483905672394621666150
5432103567371077335274384359222145404745
428155314788959433588391193465015045825
7975203252321325795996309526471051545
637540231352679377994591105031416867646
84179706813043294529603171036172373547
9472760025731831300893212695791915787390
157111839941127639025313550953539703712
488997221150833852459882676325251363742
1023784614507734777086838128370238889327
8529215453979308881955374548252834615644
9299367687687455686832741032160185683895
2161246816772455763130024694699445901751
794990033192109848497432060228489533918
4199750714881908742343889917308153444808
9095177842835953198266655890776438268533
662581056412452256956388219174604172704
5997783775342713884232319817489338584981
7460578489465260886165766665448261734
311696913722140568635046372122944714893
99252211800642649473226616283783793013
751158109673531062998697161590197946512
138654052836955178267887305833832803628
707743214679649301487321704138759458423
6430415161394540733047064623840065861921
14845990626052354204258312973322155667
4269788498703459795262356671940170541856
769699578853984056824978990993613356849
87718702355363797415198048756115441371
4168325760702466119656405040151376439425
176244706341898611454129135928424301568
30934774151525163651757195814892587993
8341708736951621478367477887675632979686
626684859389795149926077464111578618687
8343802858383728965830395067381911457741
560735838974788259280244070774471563550
878819846571700297157615780587928874994
9218839585043241347291312862959516717769
65422324353015407521544081483222664669
18654116930098252389514147474476617289
48287355241371068581643478565373009195
5572371599859477763510020287115101217
907951337582405525126478185392295832359
364070163103197188891758322432444530589
564859999708274776758262708556215825717
78296137755291215558087954887045201579
1957304143796615177387463022936596306266
305370525313757839676011571551065421572
4538669794943936993068444235646579356646
78392725486782844036175017469838253517
32904186318083698040991069321873696596
7716921026799875279552073488118437025930
431534209598468453340028794703167949312
48738608812172649847726449762754047144
7867967982382808570778138652848547922375
5296610966255936147689923770369820983758
27176983961368218730409069339136147172
62648840770355498459593685100531365518
6317278653511528821187947295252099510
4541701794622779605351927074175595763461
6876380975638200857520616889466255902012
8057844068164795889818758655509650702317
1768585843419901588759422082896149353425
6800550233222137860573438535155683035802
9389915779033126254791132053240689936200
203853113073307172618535631815974578431
27603919441364887686221040655598345145
8652900153201960136885198736351604147
4530833287492995695263550694334296867
15418574576516919115234995684824226763
670438044512438448996669918058793318313
438843159755445158986735467819208242348
9930587196711304172611734543662539478403
647673022631351612838044887342876761905
759179124653356346712595013110442858287
478236238862135654812212590898426143186
2486645528489466730178719839437537583970
7245434853365303945487979405488695993526
7533009418961082179841934950743286073
6470924592713682950569802926452543276357
69637245912109922935246509826499039538
4703822129112321609596546682614331146235
86294746562410286365003416701581656385
8800723680539744581177415248988845572212
82285448793707570167761187239258907623
8622646032674851591327785399822597104198
930561572696841238345186410484382355673
1550204459023339943276257683995436487428
65739105528433424143658250117250899613
5456661234881210792935021184209395423573
3514309750280191767785609741923636852
6004560082681085152082349740101665172887
633867702147182101946892799257413721226
283176725153350834645725029591163021889
942210171968195615714072007281250441304
2392798991075656940837658489620351983666
9779633859509067813850883326472246827
1651216097819804722542905911468722457625
57921754572186695355419113377947431999
57866260512677932522434375648195858333
335597007697422354245817197661816625365
852096239042355755736100856084066648364
3011301438528662723230316741502390277968
974257194328707304432894762602093771355
891330713198916116777772372619098281
75310743791682494240226881730422445816
63762012467426786616756005273680135056
1643590338679631684361073129107147197448
6715524187994509466035427926599557483745
253299815208459295663998316122781729469
988284723297879667702155314245032786
910773656171753848662809105975284313676
575744037483644241685685496642089279821
239080709211403996411662841766638728241
3462276910038695654166506449305924876914
955431485889926176113459639565774774930
471585463082213430283006810161651162324
14918997877740161896270690192357511508
7558828057177019213081889766873695554984
554232907387854814919962640741415627293
8944141813011871771198206571471946604
348731776707966230819946593639274799176
165599228148573263012263039589296258489
4717684599028285380475335681561177932065
9043343056085859473954746581531886558
4422117729409630731345586080933563579
5189708428101454422250257890996314349123
9535615927263734959570362950602963336921
446669837489739285799856698876378626132
6056460864648299556484836432243576025949
9459948621747484314937377164836761579602
9964798741981534455468237791439013652
6669912812006246376385279677343774573164
3321517484711935705597224785448050495529
4416107876937122747277555370323992017441
359618114998790864778282310453995489092
3301764519848526990991043593869093924291
704135919054732597518081909143516697109
756664535465937266935604685135898356105
717399696522830437395428580497994636055
9680499118628216313124446432023885379
5579555990215371194530813197822211272841
38063859330313115538111274542316449805
9499243627604937343357434365904481052074
31487639531660138292709869868474031289
34744533583438442291579464154907478140
94407470601826172145638880289734945245
3378204775925887193955917407988171554375
3900115817364642782843383492691863386167
6861636557678804296341533241312061344038
23931794839106699309841761896736512762
1323182720075307438090422288949051442919
9968419128398934231173681512682686741
575558827847827336684248884517472433895
28789964889928438189105460652220454949
76921511193574025338618520981664719866
7436418786536095579623043656980926018882
5725821564386375747724957786587287878448
831257408759140358319339807421620784395
25678739173120565589444273652971348004
612012866695665472592643369455047554368
642696913767433973715390805599312448158
94747776259855089554930836811089286289
4150299665899827545250456006984354196583
3313695489539778885192155741633185831690
9121852243559788639213923031016783140
777791335193474834437468545865416284368
716553225513266685985101265869852413431
42323445995087173461412902144991657819
556466972011904147906063143750853475108
286470335998245587067793046965954468569
14253998932300126376676392748203168941
8353976456114381597978471393701873960
497889489506212638447023156713578016898
8100406369878979621062766713562872804163
5170767957102067539463722298994245875169
327412683505529782123003783656522675022
212934147368586062647807899383873671933
366577752536144594678836876262186074186
3392315089401487634452463086735227333644
1081629573415128984692041762645079603895
26005002540653646751337273051062464863
8801984160471783431345075935523043818
300550578383729630039803522682589977641
61602127958372484424268513765534528674
2323785890656395207430301614106649603635
488853787428744952747168541746711100263
4065243198677030274036336947278981601898
404319601838552954441805878845160032089
6576133968688714632835046298579178645917
607439652851411334829025798388472302889
5165271360998804106931754750450185874096
356189136298800600836332752547562326342
545580525070793042974543757263058131609
725018371346313723516076123652942486567
8932474842183663592147849928513159457685
682978172538973795671040558816222749055
693832744027710316911399903891620041586
4651114615462364977359243714360671695933
7622361869324963744868129309150295285492
88618596788880847506284471912793882868
3257742567392789464073393996951775285703
9057852074952196225471838514499294964
6260119568458061351775916576289957618204
394517519865226549427567430651947042452
277760487129006572336742560640154748591
572287544867539124415018044446818086874
2817846348743674850966128996997817703714
8554472647424524570792382953442716155
2140204589388086394959746368136649664208
1919706699194463148766563212697029151
7928996661306649825796677161014628120
645992417355682946463083993600379252170
3568263572263913199436539872643569828841
963482382086692273822193647994058796102
7056785686161800924552874616817738934810
575168969106864318269614676975385451603
16603168921774126827138952045303098430
72573068362596592269230695326734727984
46494829736750018423515856481563966547
9511630943673630125794814475946146115
3764250419412292684656373910641517867122
4012499471567763864518396689637761031613
86923961581149665381941473277911875866
42689024419481884084326387485515880363
930652553795635934936684651275328484868
8798310673812308668397802834257374214081
8484710275768060982876563998909984349376
633191738503771213014321525774482054003
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]