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
3177904918932154939062904482318932515890
539573444018666090432736315163289818615
251762929953387360139346648530149735853
779155764814712733853491662390138093
588572376135641929213034822507022774612
54391578344993693885206813540377032423
5002546535237673339395924456358061256234
176778722766540652168167485671259587290
4881112152601098327241379611337441185303
2583413075775571845315185144902690075085
290068955227860656887089856065716019609
3132902232809766642488127384720899917458
901530197147209416948645215881429096129
5924337497539246410931371651769015468671
881353025061220348719233616115748243
3653645783659277130559996959315239925755
8366216983312170407840524511298673052939
901961571162960491842194872560858917157
4827742825697558486734531237121467254
679861586991131072032757142853950713151
8468918866144942864365517089587394235980
80605992608853467098722254195562582938
995267553143465130528878939266628964222
93666620336877873817882566466198711070
910652456343189415578524382696626369915
21585522397682296718402012197461060
481761129577723315874942248613152685521
17759441945574857742444840659062502405
199084891938470971050172764225376254316
2214667771481267448891072017406360852644
8172423761911746434194382731151556095676
81247003278542765293393510394795446841
713987404756421414067405691156255253197
705143846469177473144630298101327246
27829530442265398956934872524927937656
703511793165155234361026596845748431
38754218925036228482239442474482894
28858822391432271916403824971826951525
861582429334505453613414612894058905586
1229171030154638674390166916848979195485
992538993083708218252696803392073734718
63067559768909719529146383479133072194
471138529134194107397337277626037843409
9925386096395067915176912441495422954532
138312086159830296558747201843790693847
3692824888778709142963493433366134344
211770728887425836308962356820888484959
3851591342673856177561574036635348134118
603566359623907920037129794181737384444
1212217410826902397274037904107297876648
273394027051371803723787313294415635
6464191382287123483265814638483641719525
57885524631906985324663293197271117592
1790503568445253124680323938698420986207
6295774216523409736977684265863912552816
8446615013264416983597568714528526883448
690542554014478804318364730650278644355
959518868739123383779742090948921263517
1744490033521534480878736848503770527712
7103783866858740162240804258621189491011
557721328295172870123248171197593784173
2559639015742958459818514222713142532480
6858507398885312873433744455673489243
3069703965188882422715846771438227525139
4822158594689896669259463262398467406
3493831485641296612069382211829270697062
532611997520786944155947288833518761088
9604655919881849768647485394194512058489
6363867427884696189240853198756766242276
5429130855235444194938539152464424532413
1975787799151739928732269216221921822102
429314589059671755838533391896985331268
6214367896594718891568819959638776956497
3278430998452390628361136571530984689470
8954430984682476899946475098441543241
4682814449463932406691389932140165798351
3796865265287248472790021763763811825871
345055077944978834855163175138359679044
829758487529165521275487912717334122148
10399314349373214394105727015767433282
3810392767058956681371897113495840338595
93531188907847223224158538881112677
971186951759355643037787658447565297262
680715613742563672349586981241823295272
12846619559561111238418828678873795384
486896742395796094039588516291927484687
278946464820445764598216593822564008527
16359971761959556941715145737872328563
8695566572253474772289436759477462349301
879737506056691571922933592463479625280
762951461047291305599385495889386579217
4893568714939567674258207109979165307836
1063574559465026866983857975282429431
66720715188571580220251390412914834866
1690571765087736757065904457709490471394
9264226063187916798049396946546337481818
3379746024757526463275997774502349155523
344212367047612733790515899825728697
2912879288804595582686541101371351715193
154847419883741081164896602389558315935
1252332930351220339913717893471718879223
9522717332557993310219664457527458486858
91812384791690701793272852385149921345
950753559898310994247826652115235404152
88673175527117823578097024485123195499
979923036380941851878173866179894277669
136723516987625852928205518861951555556
3708425414243632216443422214971341415900
88792064469122011591538995809159521413
9084273869496948771135751147290974329621
415571937815799943041198009373182162216
7006379157931837536482992592467273275222
324875757030334674860259546706155826307
106394342518552956318717044998872059580
574883726429786548997760163386888957279
40957035576798855978764801552148486979
1235311942763039752588207709806336377200
5425699246243590792522859247605613388165
47852299247720545572521413884511706427
341489231088345656906545474718929428728
919223569622960231260688238241696449249
3325783144327242503791507518780268311267
59738671314886770874268867493060813327
393120611899792426571187321704935516476
7918398367039385229434926624605525129330
59306243123653221934529792167872949758
663877656348395200872363473492378586741
747227268922898742212916613561496827144
39321760772843310489308050194666829961
965033985274972990827414067116061888474
3713647360588496906249212113685440202902
6750757171933495897884134615790550712944
41659989857387198989195564842898552359
353127552193422447412094066454211697358
1379122423993656482124953157273192377423
6421536888739681253913735216155950811970
334298599569231993017039472221299059537
1905995420966314329268132317912459472997
347564711453069805064395602289406454
507883261574343887259915163810829259963
9900612360659217939569075462267785822157
868590475224757046071898614294111782060
93879461497339653441567018886553639517
821076233426184015790038470467639512479
960995422984690189785868683048292978150
724328637323973835429729103271858024796
7942429848414773997642594608178313617957
9006209110557966423363048341597838282412
431626169817343623835692217554892308850
7942205459367993296494405444287753211047
65026806240695549643804705425366967831
42698480610363959195496721643654670770
7115943585418534564635248174878364297142
8840452822037175358923191986433336745433
7009873259005574751932817748338316991674
44256553333581574772703189377310066329
908898172282536686977213276590920673229
79413523845182992607534244116883552947
1136903352516035559684543831875073807738
6728882374265192041859339924059223408
74135895850431263936992428610938486287
180225014214554459482889884765961166980
7628611884522394896187796025163711375011
10876165434594936253918080839104748082
968767914210337923155126959391390518489
39037438148208130157940111527291015
889429658929885886497699849460031918476
1360432514757927435556382093986325454770
20754472364916244367213431218644242264
629595747099909810554078983873193832686
7942409460464520339340511762905036002090
1607300944177818664251552290945114539
447098562648838423706270789331767071617
832032694020473840871059905820011749612
9971586671915654401692856934676671498509
1708775093518386187026972751137805606
531727036492498584625766314484017925483
721792272579551525493544516322441181
9365434526766184731987989309253653943971
852787056325479158924858100464052603965
65058616811859079821327714053492853011
32833075182915728509354463348898728402
5847982226972043122943668085177510067320
34461942173887390038012849426431232839
32243111596283256986458476122397748586
4997549121107389323960306524343317245945
947172952164790040784100875493282514371
965956848322978215427112987911083001210
5960101351163133528568447365437440973618
7255857010223411370862962741203523893451
7027617923167566327156946106881252065518
393852824700426363011355774132832771315
59421699911262866728357791474750284109
261721072561163722004382783761907587653
875425987219979719436120869614897315822
602316139493213250357412100312557338913
458322279223829044979302680661752941503
163487244142813265003637745139484713085
79813185877967239323322672872625224143
988567958418273890186965640324783539417
4590737951634284404462014427128756172813
546521937073734220222037043980463541484
579470847429710843085785667126166824627
65977461071891564976938556253148709657
9757380883536835921534604732378983026
5578571131848127501943768144527281807580
87227971287706594351880670826237265399
9421431593372718817019557724502360859392
529476209232854018391405182497919224726
5943543314808879340356088028279549022100
992039402924770297143737246283211981996
2653151539385654314245004053199832864745
2394811544193875832655003496981878159429
344353666667435619783821939842094105743
4404438445516237219576642602105551544711
4049172955365056514350448096534617789258
9198236489113771332934611072281686234114
4423791583187827967363658988496157240
4390643191115439397748948597524051573216
1843401765986216379881402826666963724546
2964625754963992495837744647091363890
3506557923317501140275419802714328435
440214123380411502687824242897328841708
7913646571617166415919018614873622917
2293491163838381133183811216920962889984
9346603729127289907322154449655824505
571672976023320244307487843241986236815
84694875553584840269244346376616301436
38896436288793482849389224206645809250
5052477261115568113347777012345880959667
517271516943456553277872107450505158
36182365362644943641794060925886912591
1872371765774837464837681989746616949190
7338601625326924991013832856828689267752
62839643387267655472946108283495329804
5613122091186390390296334427730244256574
867056611359817785465826719287927498665
253191581136569923260505483694198599379
641176901845149382579568848512983321231
613430316648224702880448443874286771
8160854708641834832803461990219988193
68546236279965200978679869982677942798
619411613346909435829054890658412893634
318572988540520064798029722755373392484
9027455012447676153452926356289333969577
5784994899032973435247832272826330469679
190660665919933956445554682136424493
4229527339817420588547635025747446426037
4347475160684829218887274725294553918218
2598636178717521935426228811815852556882
901281781603915205454628791790860714542
9701683038131270623274661992172731047098
2191135711528390216836108485188286045897
54173104141762968192292730479699544359
7214211913262418275125502991268339354621
502211270085241436365281025773312532934
7634395186587893802771666811975384606401
836323134063967525986784663546134236114
6387488986245045394776937033609246364346
447287241712648772134132205787620955432
81253359703518376335433141190225555438
9118960889989694292245158913590145927912
7016148368129276201642456386088308522
85469389162203392709617899697267804919
11648419222519825032434164376335568172
540969354632367792666398548515457453
921447815665522889843182355153154433
462148707043563795067613487406331822538
3845563998841245343093151765187794709613
505691288666469151120769508975627837413
88844868706220844758681682375416383390
1788303057498927787058481579458569497356
318922601944478087563519638762541158382
1424326565709175435675214249591861101
781687048208137556495121014633323211034
5445739791301394646099108101739956515537
5733726231417530819098566272666496052986
717457754145457440035034968600631587590
2119861463014734282455960675054016259
1661499279766348731625365214124722238682
68272665727847365519222423888997286129
730630827751260967113731276196763837871
663094752321462664136116442236397154318
9547665830813955181170956325615998766759
248441473546230947292565208273097425607
635867143856223573576367498317441805653
589979906079140488468344962774970624579
3115883151556789230750642792856863293316
496887355604374789553578988573234242281
138345004951973048529945534287438215000
2729952047132768922847475195369884949120
78983052508398713064583163471952574358
902798704087137082098404537526361961755
2197347185014606767339392190230568145879
784226045287844756593763070181681676507
921519531977793395467347243556066882692
6100608848493277479958223989227187993
1376667867841064503596008137464859288495
43438326949853815186566153597233942647
27863724606549850256784331936113885532
675751252399216598382721775775244087605
6109734149034328533266131912647518746163
569431065898396729852608654638654682652
84677276807497989232101403591821646497
186934858372278472322209840413467206429
14133745271559051666795121809375602114
67989386079999589051138314342336675086
664673679545727463288804288934317912012
69501534486291341651972040089575233507
9150410250613485333246988448778684858973
7140365735098536640498212203216286323015
114234977757536028424723625559497928795
21494433057870856664559186949547134448
41631011263301934428821175051197128194
181696143817527949676038362758417988736
67369708526180303303827296777010459526
764612138623091715511795246260933166665
229444119194855351172949333847820909148
379192541118960637139013123549567262714
84106371476210441684727767669051901403
6054281354641506966925118002152685932756
683689735155153272183852516788256069990
92641843679972866642341284598995585244
307282314258218616892494524977451217962
9401430321049884299690832261432776484160
271518772343741470435106254949579368217
94882579442296204611822049327491265938
79995298309718727989510730145014057114
9095106219527972639350801826689810069144
11852051744123271882013372607292712803
3429192570795064642088226626802998955641
4807359526166243134927252855196249112
9269172317132696943791091055498351996285
20548835804344557598007725892715122046
351371822116989871617585387185159514844
25215968562782792233338736837647577447
67353896630468831435958517632625087246
7083213976532520176547396473387328312358
142492941984011441790147063326736527
606157218929057287172463937893153753221
282243501991788888480515719475624572070
149477045524957525944830440564487773218
56072348564862474849241312222891433992
9430103039361855657351082177170492533408
2568766823067296491689245094869919006352
570772867754652540109679432278645299603
5334123027421148879687829736965646876126
4469191224386473929714352504517114166684
967285401561279580404687414123434273454
436669437945717577068281967989095367772
30491622995300810926461901377189529340
7281146377256027213908723132662847771
7532307162004925293146953464146898081629
852934924775447593607201128911249273982
420250683498754694774658362655577409
5479532345078475767651172388364679165346
669688806593497833718123281151624761610
5448622668401167339101397659022241833
64023331183142517141973768681890913245
867393973367106855467388360343287606636
194666468840856498391826966770281245707
6203615240587902405875988430581865792832
474048098153558821136465237698924159382
6068797659557449760279521760807574105673
33378228686505617989244474354825065688
246983807445573197073628886162441044160
13093160382190214128346503087105472527
2099289398762411595597407172200583748305
9940642453252325560279117192836045705922
150425838672880292830164970280691996196
325010826401773723842795847258396565567
226893639793716692739943674275897206025
12337794680051595992128272770064646997
8545451985457858783424997202309864443667
13082029280725950528003673159292401491
268895455603881682664267721473486301728
3775638631066302510053238960887472747168
93255667256241531202656767938749502458
7411864668531112410735609879233295395236
21134761449287369250013790662156301569
9149821442438983411614229382292616624559
62264109875353871104247736331125197621
279577238836399673847740783423673477584
7116678881778463423133462107534582580
2425645593463882917346759022407045619391
563796212743538671386876108422338242283
4646918464995091794032889020490571666761
53168045726881893775192103551530433076
8711243177158601384218552786450440431202
91784828335468385850767479748815077211
9205338336061180701041823922859690647457
7406476805385082997141541813659873559
880457434554014944537624226895832897
172035189678023584998266344210994920
7829753083538606581835614851918280773
112767193664226546190562728368483286020
588348257160454795267295663274073731725
718553779704297069816734184845865183995
41829251298240465949842350781440257082
8392341310462413278958971396952836297016
840523181616949064362688469588597724641
510816277423027458224183233787823235473
329632838157581935050293659706727159073
51213536923470326072655783338245841652
5026716848961269887417537630778240688268
8334116051305362979959373838923967902497
5556533790536289422179959709450395951338
3189882956442908178480178904722767711213
893629941298978946636017332704098917874
239962399897828296628899789167011552
35739795433994446980965305593842974135
437426531073593925803229860717951847
4794752489583488148183042028395354506992
183354523198438685209939651977843591282
73233235808577227069201692491293384905
575876061159733525378742035952722251077
452112195625717094819547546837303323980
7124683419589977357165938748759683217971
928151285217625537615636526363715353661
5298988432261870176616138353853615727479
551818565313619620152896193947885609744
8552319574779976646066093894140445699052
52283968980749636872726930417116808095
123059234532175639028873354654836775639
126010876343178035364728485180279989519
17594435015911748554907905682686625778
801666479027230599295112161207863469341
675230639205335578095908681610814505903
69085737706634443717468952103023445
728697537431414865597595514164285813868
7083280297667438341433263173740795607445
8284325016578301729488652463184465648008
9062615233331289153583192179390884343264
92758316705821632303716124263247997192
402647984660731618194008056531842513127
534569315441607662143221393620036870
19685263594091027118765597213106768611
757073469468789988214136336161235978335
96152467989173318649066624425246117
473189012003869569666766903222547534762
578732017387370798577627261273262643463
3806345146731800432034631414673976035165
3687621452464269302576937498696888503712
729888359798908242161688696812273431221
75785138887329415451160247914029721377
190364828759671123009930981615573141342
416843398618985058253954142797831847414
58394243006780069295636390320156370956
94194674823737339314000567815652240625
6313710575098095229886957095150691489277
9425602374786842937042608843490483769325
981359805500388545531274596614726255026
140373222364695665811188485238928179577
93871511879534317367645262408819257653
278678059289367672025571111313688186231
2585101698169404242113418513867929206
53833385487172718911217297458827478169
351033578819166291918721281880163871018
33394551600456732554970413715681287042
26962493501153216057454882617015796433
60924135816145088647058793594063148606
68689455211489863628222080894701772607
8257821151337501131696046315374312065707
4407957168801529536245506154357581427216
884767726952728570338761696277243467121
6002147480937561701652174090365432856620
4916168258586835578358103796424148205
4877851027097938661142456926154974345413
47423342094521388503369084703382944231
1200785253558138111077342099959227572022
877876273952702151368175508778621997998
1501594978564212152678686102253662047319
78255730290376421880541767657983449607
751776663319222810517494191513281466951
658529689351906561125897786432996077066
3389428866499290887388124687362773084
222726625571306782657640420674867883846
50006410139529011752051598453151482961
651026708876161244608732615657149088141
60477500441386556820796839149567678729
342817758958844110228754303961298855848
8672710995899697351946565540599801408
4140187932518310461471629307482833423516
9096555739674656473315238580305121192574
11173060690381066294834886969363781805
4528663438011764999692008918337019393290
487182094682605173183841490287315195455
297385103024949797431572330505613005145
2715533519659007580069603530296446762448
774942483788621107236689086315177
39333344359569821074139049239112005093
1246277465514262422470632287717812107855
26194202599457985380777441945412611644
3473914352518718585213561376285417426196
3359271698097175986185927995631496936569
78655253457975973590593386607017343463
511361917953117313232737123987270095197
68165860170475312869539388441149805809
6901946279207366155027872692507269042893
239233354420796289024957021603276656423
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]