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
9449996571911072323848721790950544831607
6390936120853422427622751094481492439312
8075866207962467036249573477158693376
97788172748646433529926291626868514020
2133391287468133447413602199983260621836
5775810781119386918395415857812339076711
99236908374714545346402400132933738501
8402624172178959852529457991326961070
6386482391493488267453977924763911325650
334854268675024435876332081919276226042
7226664663917442473537655020882663354
329142536579391905568236782777950505225
1065183672037475405756294634203397421475
103186121638194176364951428892064535206
7060154268737546204488717458945615123085
46801861609615442274874299886338534
832431724879937414829889229273884139311
385093599847180622901314446641754829221
211298045918757074681447408554251313
2227808121712659301695412325495479483785
725356158911794850934723367302415838397
9829620348177403795694011610290816488281
519196254411664198492159233137833685757
893437544711120225929386303630506142909
305165731580606888168669833296443036606
3761285794963553141179575390511775757316
68566852790472618563169135920458773422
160664106238802623246435396666826425721
1335749490828597470630761278273983073
22434491647097145199309165156965418377
7227895842568831605665561674228171552747
401291768349446511607732964749837528868
770882227404880971661417447673531365478
4644796593646664763656913422426813717243
4347117745146722295970795266443020097147
1734938572955042770298853114684670862775
49922478114421567086757827442190093723
328645104040579813514933935157237706040
6337487079799742840681732183817386615409
363150079165888222834985780317871719187
8487667296934757564082118265206184335373
2289850898528587505238457540130198367480
7678214760211795642815338579998282707322
7506459436958968696716608842842812593531
905388463288527725366959587883685944882
3036353198836367287579038082555518482853
9123400131058630927567508012343475326968
5297968319823641830711395743224676125561
96831528391804633501448136809653858944
30176955758926411139706955194808862
393320418778337773325080137468629486858
1000974474248465316246169413120794943201
8827646562191745527784444152835233024
5155990730951651130116927592909986036072
342859083398394532016434754927666746023
49147692275217232014549782665513627452
996211795167962169935786636344874646123
54319494902629878899736064817029167841
280918609542884043135274156504677214599
361382055878135266403308385881578658367
988175789894201051713632916511737037180
1270723620893778964774336829878071097557
610571241903306980710925445803949049463
841498377892496766614409082473685083675
69079983963366321489442562570388662272
9554230773499889991145433317317070817647
40588613199172361833169794820687999419
60521480372226469202198705161221434862
14859895520542465509800126247785708881
791233558310887668603807688317762681998
9586101635137225526468363986763367914686
8052795494641295542153903829302061593719
9425751741545078304892998834779497953810
7462682950045841618654931431108539245347
8202828181034356362314331071178289554
660692173305570080749328169199261615431
5938367534345231885019055835726458833324
1626706397279847547490467736484574426478
4357902511136180767987128361227962549329
17853558558731620433885889393524731607
52359087411979886400592083803571615986
987318212748521360134996847664683254305
168787242027443641296245634381641758934
853484978437920552807350907134598534911
48742050683284805363625743159522359117
660876087482842383378030251102546692187
962637067093343119942857327691789817811
67768214310834264286341215494073306410
929545809028396618976638108455601738307
56662536441915104636326256108865727567
17172308274056553398879277933959159859
612276422706708658192272499817336899998
9713965144043109802472665669386149339066
7550741723691162317468178863166188446381
745087054945928366946684742900257868913
3941709397723883818544023787971580258058
6554995074782751268042856755189585552577
7252321426038660135745786825345053817556
7768693951045755324553065349674354704644
536731552787490699819571694319843263804
5874668376414093671771971222183965570
2773651934858344127934447481958733266
67936389229698904199215306350213493348
4497137350152882925256697482790353421292
6808760163924732591650897173991277963536
9204973761089701859860342346124574797275
857030469945754755685793695348688895270
54643185682783678019343797970799121925
55553134031162423487059831987162176371
612754386725677881823774305543833963011
43304944147145834637366848789111617929
369280761517501522351775190815196471942
73949285389383519997765111137869945942
4001740796921011405651949659243596504823
1473493269216751371020636148696210377164
6872373485987480222469452763506376928094
38246519967184588135709755727048253401
430561194156599318379000312257671407532
7874405037281126869945092822789682018756
351651042254160471196324795272036599456
8338647025525919222889207466571491806873
5217272672328179744360038151362635381784
6979899516281691540562453399964962420
401029492963544748786251252250243299917
457548578908650275873034844253522999348
64864224824946726774270938992996305132
64476733492985429385172175474540994798
315475798011603876955488372527881077723
319652812000815495703241490641059254376
33178111488288184659437569268182269498
541382059946687150376776490389158798702
835873965269364183671837267397965106141
2221286229971172311889445130603631141768
5502244829068161397452567022913116377336
612691603894501729635045170887550828196
84804263323394865340617473714445806051
36949036532507835272252331305061004309
6356898946975429254530325533197923752
63297579339620897324726381651642764524
187413542494856916478925879456052018023
919519621340529591189104812037236623
7238782635911749734762626804278874272019
92826212425332992541449699269074555815
69542413209449721006478423228724231356
19785327275698281496465516258181248599
241070668333936891386690574579759643
97589440994756181760711775131829345617
52459099635187069644341055534712435779
729948611767765233947374365889593276727
191888654611617086037778578267825449861
9676949441596685550662094182376869777436
173544744025743496490868868704113599614
718935251860554864113220209670979451144
89971437255899416425489554175053742181
9979457813359006360878037798753038257090
892865029539155515547627623479983864113
4460450290237775829720159976878392391535
20226470856312858445247611044807000453
334410006842462618979450522621804746475
8080982268171691114286024242838057378
2263252627592260989011401429799772259714
8913407379541666873871100140619538226
2254607559846049398324729268390820288557
5078387220079317154164696219771138353
51327084998077781135965379601354693052
9700590913169898806526985920847076621
793973345340659636222444479889311823915
345554036052344612388746654869134499959
779466619719435978508984686336579259
682419085457240275116659819462778172794
943690891873238857444582589532772002113
614040329664703768662142413668734932989
986145618991937372419889811883054357100
74496541330273540857494781757458637819
784381854611485759196852203720894706820
328676437752320689469527198303152805569
41544679719546085426981997530694197914
754495405350135198895327846565137688005
468618763963255556256152558888366280777
1072205166376477471143674967602464992874
615787925328447468812678717652130194824
2189155424713840855690218431134482946406
287776838269489478673757201819128265818
59773558599447477836948667681879278091
12199224720955043767409654939941534765
7733141548596126352847596706367738881885
4471407414889676294331749201782027316959
60224293230237569949928198558740726421
796873094699294560415108842200622782653
352443979817087267292498287976594807910
7259442768122134519970345917996988118599
6184918655302404843281672550248095473965
38697379816123268422796635714012679638
502567702105122658498903367019042388
7905246388365315304490141582859454769023
585463087696153140578527995170598526504
8447574259984974677668057072692412334349
1760904916819788716496554270286917837121
2672210076624994838043136585359275794273
5596944749776153418222746724705098754
56107543704492431162424967975120091825
865232133168138306238722001522548899519
1897865275544053812889256052315936935650
2075205281636308241227901616504356933814
654946654863729641967982566385443306293
5342734686705185517545889737443253682219
620046865186765941892635572548512086542
3569829450660575105729564218741497855
353206443708897641796691873608425931345
4328579381584765428970438969960610358669
669633867580894340543771012643165476660
12988092203497682816709194198826348444
3564619443759183225638408317636967849554
2350263283981868273564132538898449581465
30375033203742963596735313181688775288
36291687948840887604984744934836688184
9340674995108620284355689785460832119407
14064255415818653304458959335812801363
905437169187782932693252808919244343063
25182472382585084925376400509783907694
9383403964845527762044035644933952040
28622557633741933397864107504541987147
92986378254638784838728833993013768909
992755278164532290172536239250779254229
777595409511244873983224916214478813987
244924219677983745658704326250156311462
291443107580967712562830581918478502069
734020506822326284275618298512595229329
76994505249617440211751201921121704005
25216966070120761216426650627599589220
3795924383517768680453049954545738304122
445673924610957971242455614888357783550
26252409726092039634603458282464016488
8255343310251344809762459812508653146897
396147296075813533147686476167535525562
6046499071642956186375584731359359573151
93051329239433598048825977207557154220
71583569989071832976691014805868923472
170846415141571833121107582747494837651
8357221387376112220353277206275088895335
3167710049707796553519694220404985179479
5891214736702740439955294348316519979530
517965238601135157078716703474021614372
705392896600705623042855823188632433904
249612442656705576019356570240362783475
9716108117301076278036364475571417655951
459668385792122059013980667123207757
2576141256515893563863454466157119276769
350635452050901311493854070990761434692
4726351325234246336137594259508077975746
2364991568958088530095025189148997522753
360252326955410958181892853516478339622
6250779058072787803445769732820372315005
264697642775956848763339972423835377767
5895328828678452673440274814314194746328
797641518236770056443709651185613233351
77239475918443478309771568424229259101
6215499527998212552835891709928921299723
28293743582431757267163769315083429467
6496401561298066712539075597710416557175
8591114980474335827277281521846775205137
7187396485907922741016753967724220224534
1883879615669358990889753652158943633213
34556503466422158857294516194785381673
1461942165746777999842683519357814995861
7905340638299156318620602020792498555089
66385395959224046315117274072454512787
17695156329739748733983035788938468232
8525609834576830618099037444738655734545
7671521491039656435021606344014646115
1231814044215041280325769219272614895956
158332708419727410291161593122909217248
777432681658459287915434169119828791854
773457577035965085928551259219504793022
2193839627941235687887371648970693942001
296223179373492765054763475345638715653
840517601677635112985503237209877864994
3158644440438286724073704244644792444
82349182935683220978457164331829293641
1665550811617928719424164860244327337117
19431892712012175283885464860044483046
7667644952376915423741057254138115964015
3596426879343459318793388789959943316320
31179245442352887442911658987857788106
6422982510806986177517672779129174958653
6565320139472519654872185225112202827
4406537640718738923178433820111058757
8417398831951615149630268698477682149490
161273758707951968318239665274995506787
353014464312219940602303310116313223131
6383145725942793509373759104743933113605
789030349826610215761776610720936101029
835029936273079587759645511505140085781
280246333978293421883073848378637206237
6673647531928410463295495733545173299789
96005052002931251854796187648067686631
8737573558795386317691811560175019061281
99312858531151650032623451618436217189
271843611985484594542059190326018100383
300166922174145952577541231699460915162
784923869165522382090827052614584148338
6189597457362596383917415148184752043329
4996193910754946985461288385192651900
8528340312732580999113292497102229044396
1557770782577801828218097875553340759776
3027178770628718247552946989739533838320
487863186904316375285290553576475628837
77603674917795458190722906751537042834
324312138422152795256981777814490275370
4083845218149845919043528847874661486975
480674312603774953292149840989921655325
4394726972367566524697737385789787044874
3609535822732950962524457264951460465451
17174041817743934055763795291913816502
6145158288444678585511597213518539018
3227244953313889388482173487353189963086
303938284148346889699027762349013734946
44317964593722675624635497767742385647
6439204044634646152234862013350573071733
196542026360407151094410384586102349714
7144977560755369238029739576899449418187
7345907424823072437538449546619458616424
9044280236276713322194498771915119685909
984455574601590479290614537126534401440
6701955995294527307669706402292615665987
169382991407281205587145972417397595455
48562564376923930415984183166939026376
12016699583869430574799991446955106
5836847298718808350336246749534592506906
7926604775643602152757816978979831442
4442927129142996501989357651850886431792
2079121870256651308425047215737758571950
8205366381268393538567193947603838950
41311149952939745026538816521149621342
8483789583576992101129879224161074456926
58387532219572016295599934184799785621
501114594566797735311995771460369069051
8632105183568973875066798127175264055664
7608232597707625162698562756986021433042
504836432758715194594365792392372188990
221059795111394921028322501819978189784
42445207938117836555041631953338038515
898798197401266067525802942112966415046
1121683638638087376730386020241764212134
419758585626746496785752177939428288799
591218899668645757666770792216813528068
55749331966132477236394329931255574891
36535969655121442044279771821912195295
5845462750741771417857055084183861353928
82961796886913457859832517908167041124
9032260973908686307690098454318974737894
899080545176244372781159692249064592706
2213164612994462834073216261948388962127
1797647811770671504693058532114631131
27369838721520227251343215958436109358
395447057257801206399401890182313265858
90999402886656697428037662455071026
1223159297483677574349356405508149713842
5960443351057349173271733573861946127609
666491392547517732068545483204274951747
4380973511936934506228574327330192056843
5513414182561430399243213972247388815477
312776914101835316570909468500035529056
6645897144365705264084543925485860936026
105723311557975928167891237939053534685
593870421564148651749408493613734054681
56859928325290533543881523340894974292
882276161689743115187942386113691026304
3469856381428385214138667097611955703608
1607717245933400228695725348911561534025
6584679669961733262776671681531076057
8543621517082141192964423460484935856498
5298343281412999294402255577545164421
813889599058600434485505601186637865441
9401230749865553119448709802642540228294
224943997151342282534461526178989246519
825033726945113837484387865191621511609
249904145222680978013383672591600128
338091340833403537014596520729646156770
127461802749914778267138217636131992830
5877323254278632322693557224918084314643
2694291514443196368876907291935942876109
7335792469874482742761222469209990733
235042961685501375994251909572339237493
83727267805653236167854633401607220347
8068950387566790430672847586607737394741
42628946439647146103873144729267337860
5177608893868609743997887944164231738889
834229532836495280822967763638735043972
23522492399500474743502881267643707210
3216905273651084586189004465559867714999
3948589215129001217065213978323526044651
478372306026765767991255339447557535694
16314461174293212017703863126659713821
5321968227495464576142345017107455098925
361464579061746947272937138245173054537
2411911214739970849975227709545434517320
8194777882738204133820394092932275631382
5889405569557931326185184737105115489798
5161843435383112426739499908606646122587
923146528402435848785846866952224011937
3268374660622597211831604305392985019401
620916451453489182714792044565870747334
669390239079485342678364201784543318686
882159636537961386560607393131558363855
343227742403841493811722366528812285083
539099045096771010577448808059992411277
47592305295204947681867801883574233250
502280267666442811877601962710653636904
82796148403847845490892048172018272890
656110076499873486843844697652324499956
5215116546614694805983647712219790524507
6771668467914468892197413596572090564741
247515942148569076078521252186098520150
23523167963887874199617988677964948470
222867920802662579684088518912812663007
76313293363860204041598737776500556000
20111466829951014748874726439917872007
4908829748682270362751006680943587215044
5401257258947874106784622850310391729998
1441716140093274116498375286639039717433
26238607078893664844463812225049202776
11349759230384441573896459301202620225
97879716531541902783203552474516621614
7894408339868106312137344628576318923
6262772017479409349613668949993953701899
2620247516331894995162872503448647961881
4017934272628318587617109364319220201588
6636564625187880870815718477135273321578
8697850085462483407375214937304717104635
8152412098314702994949096088113811681009
894030625332900259717524080701236121317
618090243961981780358754375248545775834
28211330572645388478189444537159473128
8862107692227873792007142317168499399
6910441370265802981031292178605959279255
188635133517592861401343957785986352542
3117210575637738761371362379921689586974
8050518284837427125215009747335416873
3281437743728851838198008494823853767521
633065663198475389966629167636837965316
8814129825948081999267552334183222320
68328815215513758632940383446624994264
585814964282543673941058672416655461
1678638667662912995499444487340485489
2017652179025663145139576042680016854235
47471116753133863772153873223292253528
6219745053598365762129905055963060002425
7234374027518449966868635662531117224803
1133650737611849205945969092590377471357
315094547156483234249268459412983854458
1832438115129164674496189578470838118
10812563312331611819136094832116795952
5318665125534441957045445608678257871460
3571262936819504472360773453352184432211
453361473148842257464472314261272603505
121787073149262189005827269143294382640
7554335782751544294715469301229022191800
980582844429293887298495831999994885391
868999459168116492062908747672649439373
347850853407657357436183822162892776034
36244257664618696919240633559247237548
347683733581499979147114117368338557876
3337168166153717626177694822664420
874971419784717127235934265596402085339
5351692392264397560317694071180075723953
1337241881025584928740214029939264241781
11856066413287830358824160268079298373
869737122991921260960045289759072456607
43816773121601406718197592792557009902
1022422414296441344899853498310562375768
3565910548712491556165227410262258626215
16513575749369573431881619338361113
866830619999240371329514511092525761
65695167905516098009435606872551597347
380192957120173684435673524403147515253
806289625425567293592681266455247144878
79138600538758467213679393475785842383
279435061693814795399317320172789489726
7742199775323464615419717734307615589060
45192331165319716642159832327344162871
8727476127476979727914450571813994
187583964485452552667962262733226998145
98536831808956837559538355894966847134
495777514007214388532531594173111379641
70887258341466162551546893827727422341
8762466377651710581188923442511262974722
7261168190395333577159078900499462029187
5797230940791438273556303496742392125181
938634170711261477437365568384756504856
4946882526143279421166301484488176379436
869581009903882149288397577467968525332
6308636554897238733476773958402662505
40742325323328411284877898940384071368
511761302577482912779853442291688764730
268271316984470988397763344291335645530
856425866682837343033598352605423744807
8895308016834429828456893742195545646024
330736576583367047515329665754325307240
4304824182952229701655214964867994195150
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]