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
4620496885332886540569392329206989712774
8511519850291115663015494213812059923977
403284503528963417984695302640426295841
1511647387914516302767983418760939778097
6297984823604781830938116756407399493723
52937339613955125624525199780843097117
186951089004717173335575834376491496755
991296655873936380317664941497344383
323324902485625975970212131786629617
9657353818604677304790466523141253661898
95857498353324935680085374251965277
91665467800057509258614322604770475771
7784150574227527553728558109637269413188
166316426277979767146859687769068841933
986575389994208795951917139700799234737
8605518468606584727686223479547052709230
9888323252981023533737201389535472781836
6519470549045682468839976270707569697192
342432124152871286321093625113816876767
92188495434419515330725967377746095266
2184170949901013802374516633408594525869
42437227546613450987518910129641486796
610554399648147495968752625363604507860
58383354584849111974993710602594901917
440633925937013471041745284366641171
2840703575854548917079111332463724098692
13136529725090607059200478964570024371
29662694648314298156670233478149787064
2547104518977467470765659911111425498961
1901611731491237455369611903160999465742
112894248045712181133628216645517908883
421645378793422320358821050991375326859
1116512873192681907064499172863178953090
1518679284960984521951518892746148067
4156117920399049495812411294516873304295
6274584579249259355232074409228055974673
399984993271418396093008824950296851648
469826053847592179993575299566806727143
2247689145177666157271753814895203214
416536743157722360390595167108926503558
7044939616276339186383515203313075416120
678389607682434661175753458829427821334
5607334312554017728836569293747410395943
49410601884131315334419856085939833932
940287302043995393720867641242016408647
924872477917118488227752795422317278909
860986152729331125391397562251349092935
930998793389818715419414017325645101215
5824604454398525998834108204627181413803
1017838985713626829647618417591972939121
5258805930921177698782744935486124529721
963468491653218116655511829337598402142
27142743847405806841181092864737737634
278420245846830615779302942790972017537
733030319140365811498058387879325747970
40326629310318382738594416532383006452
9717743116774701703182063214641870942902
750850256744151391551653739120712215640
11338059425747382723061385829941935
1830157856586354568813578817442622561193
67399366924591144024748168104315842467
457434693238565450764812995495576435530
2470455043252830289055049949695421946605
5217828960296628477728664386854537952208
367088776740910283945292323622224725372
112219037606504993767872402315782394456
4684821997669072373323142757921735399169
3114286412448034516548062086631130495803
694733866979628382495524641247198426478
410869554967602561012329891412057079423
845248907745305781990654851699364298244
7801666384889109634298116347415534186312
80171835614477345067509341563422206378
907127231949309351110647437335415132738
1918851583445002609523115500352569496253
3978738592447864627982084979973018315329
7279870198956138286957057372075685525
303489649370470585993165406554728547940
3507922681627076390655768053118973298201
62169514185481118878939124297141922128
3864129782063642708337952024725632777678
7888509159846783366462596370761449241363
835665249843134385787381985956184956976
445554459487596265904265125338893162263
3636991948231324624649833570966043427696
550819042185121998899404526225192457247
21139021468343097581311472229176832573
7087723720346405671780598826526585644686
1153457273796096436973891758973644335
61726123767454888823467114486897707769
9876294430088175514914288600403018732502
3308768991706212208881984299812832796355
168133418062336986266047922771683384107
185512587368137808803127888373973243
665100316373890792090507798419369172752
7393300842848478726198653632970823427196
80644542973585061345151059174171689290
139442566514795923745850328986418309761
193236666864147918501185951999717716734
4819998145989668395845043934274721166200
688461736498909933361861392168082134274
469531908967113966962022252065875752797
36259269947553371701031302657959688909
126631701179636419555316376656953624819
6333722349363792474171588084555753061200
807334916710296429035085746529197058561
1422600220711563397836446485971163838685
5961395888465496746318024395222186082
210427057969965533311396576138442190
344076766763749272177874928928384955132
62975323476020013574868615180468511948
533291099057431585265101364403216515749
6097384917918143607750305807552385589737
39077727904468751172170629587324593971
66624376653789994601730543708251550174
396653766638249460738353243874828486667
19463611836452951551836982940589772769
44022415379772605915629167924821644387
90378725393898209993423231760652161093
34188583859194132370196577818652644025
34635910751955699320344367179548234977
9930884593623151137216344200949965901073
414978019105889836711315089115257494331
1201488366428864946332552629322012655534
4752433689531036763655759989404300231
659642086322379187790663017812129131538
62314733217737969279447878376995935669
3342604628821874863027384499500054856874
4443826277044877959094297581869752232077
3642916982575614248520556220803492336185
32663433567845208932477224941064597803
8998124065167724609260916013710814165229
5479462057527512360312191367989721928314
9157114142453125286470075088896443973111
200271446903564996176029115242830497802
7585442815389287342767729109539082625089
474125123191433655189589763219559688093
619166044132662354360667972859044953924
268125212165569641467047138887674282429
4974613325099288591362051882504216451
815376128417895581276555467947335042
22612638769480913330878770877386156461
81817335849838330493644035101995161094
505755013796314996372946874735129116414
988381919888295589026172020340452218553
489317951974523249418940301333429019052
8200734127902626974840893222365237761
533675297324118409362971938177960149
1606336692089572633063894534372092198437
117852031231974171457845840224359543998
73321012937584751861343620987862750427
2094540480537395586055232906519966823850
798652011172332637848376365210170768870
129574238042811591353290679744808478405
1619330024705404890818009645399592389681
968962699388317449398629152260469989858
1456848493159236142691036949504472784681
9657132649999926658936981248449982999703
710434608149633694073583224476324884462
4670127184913191041463997585010281330
65951364392554717994248623338263268059
6139383694593239302165888352276374582349
5081724739667140534198328512271301304
75664682793614302328588143018073048157
514744491342874236317634141639722550418
154668541631883146728118551443771759641
575379345560211424194181700871796455073
380454603943404199194659680839136667525
88774876150356306551328352598378082460
8753108173099324545122291853317822514895
415963334469525232058071908353016813102
1013191436998360966425612049847787008805
829749958667180793704823140926003813
96103677863246076214673409765828791471
91811084570182913028944830351750671433
3972867678214368565150382042510869548244
7510980017772139922973018571586092053014
7666903186036198374099296885957716908273
323999817989450429805411717106860186022
643278295291279865219967179903657003762
91526869530296935013241136632186857690
995236492572858064203430324868759263534
943580059629902711819023401924531611379
774463365883258478521356453319613034163
76031840877137653371602073437129653492
302595790215542274678618902427225326857
62912246530555127425744279851374889006
443022419193151041697654895749956741700
390211091619608805051137480162278087318
54535019575814582919460310351129008362
651190839701428615386419910617487548512
59142178314212375045257883334189280376
5018387065461716519437719766556294328706
225835519761194853973217088952118795575
3544542879551058110834116566827247338639
772089384634227667184843125237276095142
3578595678775352556419247881828251976475
6840682965787503838523975108563656797513
954622349190216139155976300454756139115
170928999150335598733356200361923453816
9423264462922323446476261079632928468395
58194415210388596747078550977922384382
862021457368692088362084431899564755822
67883873741342462259809828114639609
416827618201650146438205573130131692526
7431960197877331307678136788532016930
3196930264895169816014163094832320405269
1575140311385889883683604120743655449330
384898086973852986147958972967147953276
55864143340951037265059144087852292105
542022512334819540606268380842543415775
760374825768706424885108273481546593014
335588091610889619185142636825879174163
943298916306412550334308753708587916322
524584364068175422076839862494248128711
992190352783379393192543412219231834067
430444312833894457396248916411547845115
554995488079874279524627226665877095371
8066451091904982951411779325766149064265
7964821500394648289651372356010592719
4515637626156903344711412067847089109
9675798815714094404715599415672999795890
911696212829793689869055862451494882
8323302057676472577372771775270167511296
755621452928220388991617506081699568587
55032380716322439452108867572473364529
2373574230937218447982163774693459551868
3070215174547072493558641800344340788172
72953498298460922917288173558620279047
2749649347916937329057673296578275601663
597765494386059447995465867122629269023
93073686571294688512137345440653271202
295451022060377667637644794176493355909
8616540778363769268330894688643698079244
320091617927126265131429541279270174297
790950944898400763712139873117577101267
1942831093484891520797794606343642463735
9623152660491962202253217590642432383264
3172565316521123117155958155091130615
84267664782020168373886724379508680743
760580778950487446699929907499487167875
724717026177443797791123944724074835798
2872756436261791862390615897508167085995
75718642835142464901140543211148443347
62187244614396025149611940669383317393
620024683628466595138472227174737033976
6766392103595339352700588933192096111
9176604950992914144758336752870333481059
281211721712445292744089129185941544174
487249482647820462785167549527386686804
50683863776173761149038345843918244817
879547507461289594597411475637491211146
573421159891866866031903407570365339905
386672758278628706897616725989454871692
37396308411521619183450652425583958680
8902763416742373457492451278214868542660
9866639240094118880580311989167392269489
2556122715705083683091248346663118617396
9612711725198015502777556649486090009866
500714389818111380802756991848345929163
8330697587311455670692901766765331718370
846422112461972260965322289418247401393
360578458186639153584091997983269666861
36869487764200440421652298486218111710
848698444205556224915367606567253128234
424353614864701588673196736275959665
167412914019541862459243026942523967966
2271550789723339691590987560701985787457
3779801866656123166317214454623282263
787279363512893464420271212553312902752
861986856164643419678448873274283961727
8096536944263803340224788269797067136
9846416993129386137338374673675016619881
859319291744807285274105778753576228972
6050373980251634499669922980318299947488
690477855005390399821663365425598130479
2818358098395702646949384747264777562838
1545978667017134179296754727256488594842
921418101773192217952328128758255740831
48911259396331072532231212271492213420
7996383384735385683976765386557468742623
6211478253529571501021736922480753266825
9149923350311207897785575410412853587058
8140565165271508195045445611796378567
497140006300430734017840737484798778162
55222236723674015016583361964276517163
230895434678974098388019801325786072222
889194217649971853243009816731898275219
671753060224742453582896828385039485
5753254129544991849114239934683996869873
6307803635278674174811949312355247502891
8367596990797281245136449537480316923843
5500442726842443577891801402980241306
974660874532468816127005023493092655041
3756374245605908544673185734670920991038
6746843174885297979529846927712918417049
952137539905909158330304788873166476615
6238207347111263920165996187259912354077
277013964908018523658592093740577737474
6952225477287760897938794846312507743
246639002304755090215102952382161289090
294238237393515518318252746714056248328
666041806755789449831115803869533369592
266751410782561531825529661213776727511
561381112735379932302059893630891995280
240486967373723685591314683144539679166
94768265317786163942939698652969186463
547242838984465511874291334968747293524
6213309041458782784490852358767719993533
67564556316165733742209108816991710
422324427527611691667055514122841837929
8749307460627841210670515729683737845
430583061853348651688629576707473673663
576258387333573791921412389604564166198
2979379546738482781110451053942853667
424532648999904615996187135576821788312
2789290635797971711394282534545912783665
248140011757418792236282049153663287917
114381265196971198363412373727338483315
45661224494972569118421414749567599309
679991857785221283973436171802183254080
900541448800842148235303603923799625483
3656273372350010262484799978753207759
88423541597938727243239714353683112041
29127592931290385348335718006123128432
9764144446132384264929945816573533629629
9584560078274795437221651210977577147784
8002634283078372195493306247269694639971
2315736846146985452279029330636865982148
2161771629695513424197701246959066934843
678176292893170239590731455443841076111
48048066216695568646822432872287942692
26481086583120896095345939466143607671
582662569484845775123289133942660759320
39216898876192295992054634226133488657
8653950497462377160317145303210716302535
79255967880215334123768987236841424258
24497467599140610533032499077211353103
786658993765367622094302841599331189
375623359639645417959933688810370018456
258851416557926864446842005130753952121
531248042785505478255577415936936788710
59220436470513280442033741230496704270
882063259632413270040554526245462435602
30182285751278578185328799441088745049
3197944621647674935839964150826668733737
6887440660655705840550519738426034524824
9163313812377822423075545925368119045066
6522957136071180133285451941916420479184
976825697928243453257703694057488985858
5777193591725776522911137484325708402
76378503805567229291995725888679693316
81215379658182151093583235338945147819
2761101312716309540111849935312044706830
402208988004949157836993613201131012666
38033802763178309386964524099592268
501124139452137956194497479854029714326
3896634861797673792583892047773853301
9350646632623463447274905875788666023890
9054453966131269395684788486604972397966
169034383337783469621528910955019677952
3815204291169014561980897271320875623
7828527742165394256204336143418908328
753948967983158980613808518724482328083
6973187574398521546532826577962742882002
5292139454831231940263676541967318166
79275882248042345953974949375177067610
92279734979358290261516528290040055020
9499488871136278970016404994305634911494
8935327863444359846822362177418794502193
2628658259139726924630425892355331126515
34056721663017754025567760689798357243
2567101831405022515515243302968126255771
99258680223066089490928916849593572298
7911367622966239281864242595590533845739
37851074242112346429059877959315685771
51657007638320570283858892743754888630
7809553765456855424879892845425965123055
5453705969236293716978221975214312908674
2759152098996683380024257421108530548249
157176746268903591899097025374431521900
1161686190793474321510825597886498922570
37257325214149151243332655796657689920
9385394839495641312338543495669432475027
7453488520575518981758289658892164985406
40049465514289178600300643738177984419
195338255004602881428791490865539914245
594785454847535760882847292693184460870
916622764255652112962536412446839498652
324752934964946387890163954538984276898
706422948547644170752488384912539803519
2025208420392117626873796983720739249627
66203718560455869334851670746621501906
701129556049021990362248119812670727277
655508774405977551144624426660652924359
7111101725151084937097377617394843779338
917088494047292718218636411552386547165
22169564824051988173691234792379226830
310635318237877028856425308670416067602
7408565541772597469295241408382125797516
4450850784818529738642353430498822063953
148830878711566485660216133802796714883
6287875486067419584519895191905894853831
13398049689183348217011432506219197508
436118849223142112995965848627029755123
975510787847537163158075632744399462990
856341777997177957974716531586880291842
63924112602353314878446515230162287914
474935083373901274135565435384583688714
643950275565291853599953546184921643162
656340516339486235531725163150178982312
1399786810584265494725004838854935546335
6090157462194038343636994342321514094
531327135375259377434300586811238756151
6532658224836765146357753104509192116178
6430173221679439321029821826298841269049
16920467956208713799268964795542985318
2064152938634437886369019916442490258295
994946224493147600367394006140071382810
2528903767854172563744908230126359013367
850246071676553406258328291136212861361
129259342798410035769293560147940528048
3479991581111351712357999180092797631
78642810180976963374754935894890151091
74106243400589245860967656625385934755
5244244417139126765578088853631446048980
3473840592119755327160686634602616472854
1947738034869541540740715845934283666355
1110367295616035739860407776816224975462
9114656496552635325965780947431214015
74835830686898667965796683631526678329
56754089526560954791238071561455353096
805424711848112529027335367633682155398
778755387261065963451562088277598506978
9833334648327244364767093465745629725376
587822537257452265221897038109536304
7108147987231519727448646132494656893703
7620341784578319126122197003445875529706
12098113428354602496378353892288264981
8849355286213620936692778038864446772990
857071859927756417075438847085357792742
4257473124154684807453519290789708037
3796587580079134756477433329558473661106
9672957685666717198221188577693228897296
2302174240559613190334432112463434522489
2490655958114336448374336156750381642289
711949498901052838593638287921425401206
580694343625497230246652529659051899523
8300707749624983819762035872759520912139
28427242496293533426455781147972427445
892231356726793466963943755610928676595
2295893567853304389859892644393625573810
730151108656858410593848367663839841556
6968501431026467720141421166285023494334
6256565511163621880040223078144839798947
926840427066290580214835133618187774697
155238212188903150827022115242398370517
1723510076472789803996684223427420384592
770298894374407722735463523318790121836
4735673263355859940713941674353632517340
746654432872719529287137964306863944480
7225271266864480261123336179796568921
4603519243497731835137717180548996332537
361294514006362638679146356603674299925
5365389192172396723863050288837221573
426625843646631568891059391324987927759
871780188606404642299473773546664898340
4105230777659375325313937023148143722139
666821138469551999487899320287482251989
813657852969503765788047163223776120
754391808101608324736992589935527667689
761151027251187166076577882037073365
1098337852834782853063854727670858665946
975574463662722550660025894694052256006
97631663484436699434376397477677741603
8016975255404434653584169864297919282
2780322517403174592541711224927310622362
356294775799777585081009335666542879727
12657705864723283490757833004830611030
398898241949347247443242433195130508082
2320559246159446844149457962025676104
16863093532925575687451649638468768193
7643190422677347545616508926323971818415
928894784405178942857560299635284978123
9859447310936479326076109624216526183676
31341595348351163191636042256646613106
9006718745451260441635928699387429286
162091262297867524213891342335947269851
944927041709670146177351926371142026401
836626934104608474795519726960917443460
7631473615978889231073209804992330869563
477178159025089376655159236549424028347
90736371330150284357069951779841346672
92286488279199840425695603967516647845
917146355034836387296101451258575696710
32196850261921999732469659449656888081
3788230867907834557354565252857298046782
459974668081586585822331298094554549977
8550866899559431621450837325187628701340
44442304460089705225766993854760786320
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]