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
2384893755587726863643170878775103628
5913903891465232156137123410515297572310
891895316194432447418188355661559489762
200315177015679553776492129610281007079
498621832076644997734101214288416394155
3769569158841226991571653473400312087598
2341353081187309677624491868582416271361
2018168046439820208344584521273834423904
73936453178645213896756867279022232457
430575560996172763824146995541750308133
5502162680127583545516057699601228702768
664370817832643302633173505211772647763
4120358731331627687586809333656836131451
7857410356642961480009649726314351415
4575495632036726938474481229590452744543
329248961906625342884998418847569401548
718440531624874792524465285610752156789
7039228787644991752638024621706122804416
70771259825048194799413768653725008863
3933708455495560182937208347994595626737
5885533928657578383872625836899417398828
9065948740388187823733426371271288424643
67803465956049698541938796463766033130
3272867358487564764035366408955973829
3496535490601996372420115934680813129316
736327806208547274299654377111234714013
40983451367866747065516559868879867822
6246188260038384108868966128177598776404
258381325811792235586124401510362153068
738840555259259619909933958826632723995
210942601775306150179621347774341969785
9216619649274047423199647820103495537273
363970157901564875076587538702597191824
457029871938159746537574289161869185884
1898795654613791193561390010491725062
91048027229178459356628149534815771476
812199618755818988584150653352247199633
2926162332944228013428627740546333288
6471667676416130611558998614640981251975
233353655490871968086823038177984946779
5461921988507846778467725986217181272510
152832773171427128238752141232304973805
1990489843406199126337996913890577492089
1307152559986295433191504453950144963910
481834559064743833380278929969860603878
287387431934794396377583901638043066099
37613194417887635549256464960756302304
942637892049022983298187346777323286182
655086317152358753437618575234288285
892790802688314074552425697931979765769
6022145957913741391585726308526584232438
29697776593668997777981923347392533568
3453953632226633902460172836373126705410
5065899167118416701513395485481436514133
538719586325873723202449276767235589476
4649487585018599205355879312207212317
4964888711881671929437373616901210581093
8822916383352226297589415337305882394366
982216024748525500442776849314361884541
562473047591034521527836845403046973814
6558434676319383384997408370780637724763
2873879845192399159391214624717440531
9969757663908400711379251101259534861616
1326116157549311222032796553215977067894
3599264855014820944994181353382416266348
5352794486428683681466291346374391552
908466083166693237395996839031527528716
7620608925576015533175142377601236101034
7212194808176984096456810832198404674
9896804915085328424939032929251738887808
7255812549515074939779939614173035269271
5290610264624511181711219918912890231753
1590773159187635605853943028941291421552
80164132446790141878856736882085159334
49401302717762698311617499979799451897
2031667474535232734790729199713041032179
70278603159287424140622285756058045219
199891553706783078175158920592936778840
52667203411291015325358445634714078246
874386912805694384941237138283746937244
5254521231123892397925737643264510711478
596699165637855387047315286648625674831
5669730356172465856701215257117596619
8240354663009521254033254608878856507882
455158161775654851428963722631148507811
3610787425296398533319764506530566587156
3760184424767955533148182505186310222903
773962008994560980390888535842638658819
22294256140188309416727649525961526534
94486216648725829029546834029195116520
210054991802492917831472388965240665978
6073552781597988422611386101629690326104
548199016761753197456493180124394676462
3490880167613868612357427290419399996059
6891196964266492424896395148263248348620
5725595548404746134019772967444141534441
1121956282668648699840774102633124553106
5976395048539045631272317773578115762070
3528382684041297478286416247011587856
5842289836962378628988932228443029916
3719554974005744715389103892624025073783
79524651313289298346633114250236675697
3286740921268062959678911082837399603059
521533361365320393074126356472045856240
215067883441551278312479574644698341611
7744853914984997208220709649399156412407
2315585676852533463756621256921862344
90429188319795376915124723783974726833
1235408674469070119996674133638897528490
1780380967140248226838410967368053883
1445447518208639649653863449544236688706
731666358374314090361709748356920693554
98415803721411196106810887711703442661
3796664785593176829964827648643299212581
371435394488498742196660117698621141380
385339732661049673582368485558915286772
8075501497406171370532981188172520213402
776861582406126223319984260254939976500
9083283851951804847983593316102290115525
18174343822547944922446251521352253690
921210691283563061156364536933253005742
7519442165077388945781484588220925244310
5286917212583178866775528360288085554902
293461276160732858178319860391298520900
379514763169221953232412729713761299017
2902124222115373557590676642830130266609
67090501288580909127228477323044072605
3844969311913350135849762298488512433760
9729664076005025454443408582615539786212
2488823460775235935199888796823749388050
181620979513834768837139135901724553939
4704482735715115356999746801465164326
55284669504072803213625067532045276846
6221663036745182232445258284921744397
606248264633873568739002665724461942333
32568464095258522737358151795298745799
9579571251034284114721301790859782431226
203940963813584923381250667328335377976
1677818231441207127148881118415749365465
91997105347534532206393785545787305396
640398738095671367166925692261565387865
4681949630989953438865579330703494653451
831427953322042544351166207122793096956
948789305879241576855272529135757319960
5893770967956287665310005617296992615547
20647574859654707954798903942819216799
274344750316291865380362679691623894764
140437109095301256748808607898371521826
7273711965632218621040254118233463455793
7008279271975243274469853203201298708247
415883360205131904459901139708341207254
823868991073430991795518861208165812668
2804221917261778816170892067695092658476
827259821832998236227621441943718933189
284842431423077594218776063744894128249
2078768620128972425360504899956335919358
1976604273643988880365882891960383168185
363197057836350331699030161933534679945
8876245679158391707965577701191083873931
70828614972140617169201145196492914383
2424383882225848706969783185552952783930
758361831586306354888340862196176475774
1257653984496237614399175907627094877
466544431134911854753769947391244042
7755639829483568335636995545235919697940
28117807335860740010233041394118533057
42052863929230690155899612496428615517
2179940072249154622040673576752217727219
1949268867773575316285797775302672817984
3965603847359433440460492395483888542519
3784602294578542515669311863817429828
7174912235366003398033082877930346841
2074504234551549789121919194762789957774
9414251972788029962225951573910920114393
90560239030154870588206886326488805679
1272152218177372404012059503905472447120
893620826728364231431151671330318184711
5988178092133775943917697437454397726584
64656317843660194122831219913398531343
141821679494145750306294509737477285209
9688426770967385682750663289113480476231
460070241736241833511718997485050878998
947838911201977473616895138559427454411
53626295554344643940294222325227704385
44991683186752514864742478721714209066
6924498808895011065851125188002926941
1404157382686589119341141612466286136099
185598334580336595361220928788371683725
4641356965815822605673269997344690515234
523720714142205849381346713018581987978
50031210887155474375526930027522728258
63874977416449762168467671016992477304
3773943117045276841996257262405834282021
202232864198967056534978156072152391951
7662389073668874774825623389559391815
39934836001633196180599783679647036823
5298205718874086459332518478001949941
5399875737293160620023136360694072756916
815897167146358426593691486261238204052
8012226092105475376417973188769499676449
6489361839962521354656842332784539112573
758541684542482782482342736075061721970
4122263748216034307239229582400388333216
616769749211184755034789873792373854709
9240232564855595332956728645449903260
308081346306065804724523943991145885568
432076457105889415881712594415004594128
432344021835402538653115311601499994798
3490443249074020421573296028662678713002
477511939029151950212757171705189496421
634597635740327986383089877311249358860
577828723588186228301545863067136742378
8149174096963825536757467392247927857882
2518141730383648107855863067322949786040
67278623029630849784091348641217662798
5772530856712227796150342386271074232409
3673723872161370838854216816136244259166
241132375726865920881074847308398225290
92157973672335894167781836158024826339
9128878579237202716893503147859407759
9771266316023498497721375242240931015955
5133180361198653962880965277689427391664
866681193363605012465246187875116372
4146147119912642458677813419327998453138
8891804442515146970561804641523614039765
8712394853266015261755718903302028169904
691240183169536994742753658842562665627
94054533719390878754823334679853643962
4358966612925865359114591005597410597066
6843836118499866466850142730421337153947
7954804440908153527507493715349371223
89753604559186145190404189673257774510
27014743715949278793825435422151189789
703863821776592881699597688639045351932
3703171635334116742594638124925975291558
22169212944037218491422188147748753993
2944878074597852617575298600522199562581
48545735205443993881890971469029051733
3062764896473411335391087852835847269592
6255959460367423428959288104571820428728
4785304550973166463640579371986611691266
5835361215749905706546809292967184448698
3872735356145115704952308568266169162839
3836954364110281625558175269773229140
617533439784101538016691372415697114068
945778221502491625280774925709333214172
165833291077709255818518925672671458094
5523189167718109209076012348444239108181
943962588518369271708374840455035596
71576488716675425600317154121577771598
119871091815586346631575937291389025903
4860813368387630029408237151487553175
3951237446669020189389032547451054356044
8787261156405240215172848444924339998758
398357297772106986442206676841457163912
904450706710688864144701111111332944244
74066239453214767865997283301345244652
4547329292911608135858803174893795328181
240479868194236522044785916414413807673
28407211835317923839416826839527072667
6626591922448266944453125214187620363193
21198678322510669828993096351281607956
274182844999686093974096234986491385308
5543370071913847512455778160921527315
321644253568761341937091627926462367433
662298419301476330473984968710695708038
7577836991534128570050469479323530694487
7787648586229900220817372021765874955967
9554812513421179254025555148786963857467
494452867857231384431438721525863574951
102296122669565148271512413661637033142
75318678737719271904171877299485953869
841527897354704879026613918772611994029
2780851589229232952096788295329274646593
8217408517214061809421322960445335869436
991226718165990981276324314376676682093
86549910219800217018221008482174097660
8373668268707336298252034463944682381804
525697341735542866014728700652840684
7619948530431649921029556665317389928770
835373882322788376699981082140972705594
469144093195217773047039978434370232408
377851814917005167182494218266868505227
6232129821008164534938908584345647086286
735313233431861455961632948638333435558
364170767974593201797964097645624366481
309726715852633244612853362453770459260
2078739763114844970524586269543618977104
3963341820133054852395684569135161471329
239721404114735856252674386567356071078
5202944429576139612573092702801884729473
116163682535518346002289269488213872623
20758097361772831237342171231444935108
928049826972798557582127509462974047636
3054119756678449125663093280998313848578
433631394995946062442007668920241647466
9994186989909464355378668817929033701
11526960371076742200797528370564447402
29328462549346582897898380141632637581
3181221311928791972615755858395271798426
73662262894041889231204773723040648940
486532154107375378181066968950497496845
114150702152037490766627210783438935430
8243206868661095256972242881249054017
16707632115149274523488989637239017173
918778863197869955969882391626675507788
68381689478147550267107410024177531
2153582375962132602275045720944249634631
9170366761058353622268784646915955155288
213139033883371554759539597440328699837
363983858347399848728960953433411664540
6212263043775753749241378876781012317743
380731402116329116838141200694626184515
1176991944305399444499881117912769464981
3842512599915518599042878221990338194873
413975145359257855382453449939523671941
227446610401081667664255184549697282459
36761685834644064997190498323891545160
9904921735843713647943031867474737452778
371245614607680432760241044158475247786
983192232432906071048780939213584828494
2710263484688302652229847507325041852008
8993438115887744718915977569162969821041
578969063758342350584417457647245416298
6537375224716392510869621780288575165
128654878123959044644875794470504276245
100171167133932514876421896345784928112
787658784738527717071041570518374277310
650564606875664981423759748151494345375
119339087461271261542293571342617174789
272050188915462458387375213458993236940
338841149554840837026689892936012173106
14428291503198197394473987771089759298
796515162018868418685998903137157221466
27845634962234871223274662353792158000
967312135004879774885139220350376354861
9370186537151995353915255681984337335958
662623586577186192398458278947121952441
678284687715131532020235032621839921248
62476702300829364170386244577189218766
848740453769937269676997952581425231839
5911754172159062854216078075807514601619
6587314677376382841215923279506716716418
33456413615691755442385943090994231168
2027979099195907563819686621615425801618
78369056166751516528231231514966064412
921662178889297838707001755915841695252
48337566926532368463850989789928435049
78999715675740599218999755401242381148
2479443094406513318836434130370811958501
265611897628461431084706203333055116623
9583567217795143877836155449839838892562
4093462332779951156683232412473635982100
9109400075612436462662826260974179336218
24839283016696899771201141362716636541
8600832886789376942320793330481848484406
9541822127003118240448325374045138822
3866823075142990947533254016641667175807
3892362378104897676360986134155979484
496360934981852314989403059381898266899
494730821912714589651006482137516036052
1022219014605599826341267619148380944331
796186713290540066981574628892363441255
208652920847335185050271707799720873398
673399404674711239118021149748217715701
745871240876369918883899695406550526154
780352328339463136596125872162875565607
42235497683529909254911436244434294699
29671899948984944478994193392486951183
335990378156569018113896700484321844029
49262943991775783644331284745784769048
569651362384484455751441225567669812474
716572726737731458456312891507862136635
6736486874931744908555366351174463311858
1176877182652566653359034008908871541377
7568186416796365884016683447252848692292
4397746185665591332911986121277668900
9884780256368678493329686667950065689
9193100879379023810156702562227096186326
6848595420489041971124179243441533452058
7750147822152034120911045874764917077614
28496795153216496771080514449081354300
985087463496378535058199590093904266586
73405838820547839234816091631209614330
74896945750872751929382222552317745750
936363114757930473261938011754443565943
797985232922147980972301277667325657
5385236996182251457044198076499513721704
3165707595161282123040792133351130693427
511197696097918844308153778380728628897
6414772185235665343666959755804236485498
164272282770763165410359862817114792753
7022129882212614508843824549982137184
4668644441107994199290312708244251893995
872727887638111165121978298245138294546
11948466276782447796145185174773958655
4589503284631928127215696665367795398314
502246985653499717405737798678047538677
1282938785639643415486281745658374886335
2943265912852913242869015926868853865044
576865951669547837633366344967625575323
1745575549807693181720231816847871256351
229888949874852375609288698425791302006
21588328945096279416870574535107931413
6050104532678286141092836064651461298266
1707921632395039393457819301120356212074
891395556335315912421456842572304440112
2407734661819279147179528173428210738251
8591493728651477354785001681623467935946
348011902038553513240659101316698224283
93559833369460285912044479631802080
556013811383880935876276512262847335526
7445662114143752795846669030885535393475
1580637798669593582745535082932673826541
688153934229652432180094844074468976
4667649699976062412227625685188443708111
41567236691179453172225950361808314675
318974439450477523051348958481022261923
752229257144965901261797151599376089423
1788529429812458793931928976366846343342
1119843028086647602170982717944136218204
86049569346931560834357183953346792211
52009188437745583755282890111446238145
579013678173680013583025793847743838537
960363154201348196890212806721557727137
511473353922168357830366200600226377428
734618866631385939116992966817822093970
891020039036207032725739746195681409079
310339715251627955425293412937946207
355831676017359679062991035740030599089
673135598364825192576763800446789348503
5771365446453887315376212735475348187
353831008564444069506922908282551174522
2248299029827506362593850523243934096
92922766451747790229951172745044726710
89335375591955201788322441476478154558
926836562028654644089998244070475888389
7037992690883549431153662162838986132544
273047956469168271875445741216711171695
728485472006651578672056698713245474786
73356368311364262722686503522845775480
980920367554201320027406861258456054361
1794815087468590930182365411257810473598
62601612480272515652706771866512872401
2034265574854424567326928971478004059
34429125767375497036235571883817711881
9232981063522451317534864572828960945521
221775579361545298980367640490946449222
7610831794014743450943702504640662646479
539331592542556722812081448221718906323
3617803599945849156362143748240562928682
8318997390765855736418305716548459366826
9975584151515548319288971001751569515766
8080276864025597695762781109579296099943
4493515664635040768555508348910991873415
7503219862633985882094894980696435443662
826851644921524996001229245582879359722
8644413162933171927369011685211722965890
85939497098489681628281564241737487119
4161768547865518210693031785316814652141
211813737955393977603986477642077763407
1510163055123354417095025791783450082384
6882958218862126895379881249880155201348
821168198831127122072635246593463112057
31425786618182744339907434612275478189
7018918696247482216450158475482137515244
118387747490234666111680828885857912793
9362656414501190327142541525206423729260
4057193861347166459635282880537385249077
73731440743080494412834199509616824242
74949149186189545902187120458177723197
47637286667515338067203198858899419986
6027131427851880980617307679894665234668
380782351983633826349490658690508235969
374974617858880538720591149844218233002
937465210019627768517786873835591519218
88881621136097909165921045907663378369
52855845077167934728457225144948374396
381689268880721640785857977261241182249
1283865911193482472727023431858555952047
6919576912158239445636821609499634775261
84648826980762701102382146711107681718
531490117791538757839329883251954259534
854072768849548633968706967220693945101
6769184188011980921926091931698471331566
17062972694694941716939120934003167
1740363078528408468026776446962719328557
590494251052107299842973404390650429081
8962419938937370305027679917553186912611
8094551425125485256738486006774843064165
978940012456108213140916300571380224350
3653882958258225598129582804172841445427
9735484013443106557431812991584660467634
68396627812622586837499343536863313151
4702312283842912997362474703555410924025
4848881287757936217251728390562656004
509221090181091292696778081373876253326
7446543420357969186072313747185228435160
870426067294450384277889359658353879
33875713333965614369286900120821814766
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]