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
5291255616448584903973157232025825853
3951661578568916189063875065697921001117
6749414041275548899565062894489717317083
784377091594606654619736757632068889287
523721322726313198363414784617799715
13623326117872478992185332287417924123
87797952375331477409152218151488726
2005573434455088777142581013908427414360
270978586626470233527876244514324062164
569897487640853096733184703993064338225
66639825047257186574526096724013319966
844713027031984753307192211881395447425
799198485793749812433107995962237204671
65802390683242233025507663818014308
939229514382442357008406664964324247336
7745565782853821872429236814122375257265
55923596417961670742374640530293827675
293765926788277924788183930854644448397
916463933909936595708601529799007661787
61269556421870131737664145122163723
208919708126573994343494287956279601841
8795386348846771331629306878362160635847
207088308509800626468655956803084488299
184738845959504744154611404058362203508
6275638997965648308155465265670913668826
12276321210676912337641615334155157919
75417789446093405606444012126470824618
490156917966929615557713393411793268122
74548983114611564088356389481127653574
685872983330750859465231460850265477987
368567253571365793050448287923361562925
324982445379424501572771986347564296965
405382332322469869090439446100956156182
5705780090759143837346762655561361797008
5078474779271555105843122268449321547
3399864923856824503160736031743359386615
5734639487473688189020544179197499544737
240533156699618122107253823375963427252
4720900054701285100563745905926620901441
39707249426437145039879202309536816715
837154553327373018499100831857426939265
7147592453394699837870595306248923538460
39780388322427786152168145966663556487
86015655170354619043347979640286445646
9438794460652755181362328669900269829638
82121118729234953248676373375451820665
756781874593758966989326701967079177350
8952842716062418547539833541333147209514
49623054188421394504744755231212093503
3597180431388941489477685153798292496
53728269660747341575675278737358769635
1071742990164579269057297196635363237178
721141158152133073144184507299356082904
6706319191581377774779170976831029963
3651432532483131650534838091722234919886
117749193053240938512879276592227355250
8104918656279747663146149074682844424163
9742507842919572290145016157123142968897
461280919961199811546538127580145723874
2193871029866700688445579425209051696419
727554029637356650806633570636241904109
690586739458824055213856832391954607150
101625995080769898321574638289579543938
5870894156561253839275617226405655528814
993877015111502128125228139328445078186
389614979835132434649996062411769212720
6182738992684991909328759656717695786908
3083827047337958174433453328636123812224
862167935307966050887313560180777658510
169265563337734683188777176133947291394
8368527356156010735212466888298857148516
861160574912098635680171053805343132598
827442543038816863628395016407173173112
334232215315743529879097176732393017134
490341315839696272156271898922494009349
6222448416813431141970132594533852536
6168157695143531620291656360876332112855
884763410289220938538349845876616789145
1740787043588550969156889702686870468129
77756302913459353347260943340958807386
656433215210955697627907075661995791156
65793676634278655644837166632838609284
754912032080230133663266046921747460
405879344265392596541668280474177336896
75060846166920140769899697824232855417
620256868835113646284769369897788248355
60541913358538713723012870136216622534
5629784143968197681592174395516874973805
5217757259233591462570155693816758987508
68267153964548414612890772379195837733
955623857088815381748142303122025767347
570667463985140953837489305649871518319
353318781057983915655446672982489595009
10632119702303131323916702769291635282
45221683411815112954562872235163288715
452386803206463144118528301631527049400
1889477568857769157049085628238784257019
925830218684888574088391324290278609907
52288407902331901352447813339012205608
8518606846134396368926927179935096597869
12299911474760981558804736346664129391
643341414415462963878607777428476952548
2147182031872882808072277543654445679208
61284660663176393028426084577098715189
3139349283713744403819123371828134278942
6848556775679030594524781104167299981132
6296907171127547364795562705479486528154
4991373291831301107284081502973757406
628351324399689050556356212146524564941
9910699238328806520988382522494628637294
9015261568514485636279741479146114107556
548821183578933453626183209071431523594
1758680565781137633684583696894771834651
60268438384997515966516172283651408578
306532265398433883989711499048654779897
6447454935345901300573116275307069522715
355038126086776923691956727703711372515
71156656806837153755564381249337995430
674160378977507278831056803230662816024
47747338005595678387483617862314599090
9044836618217312206155187939723644817810
5198648978286968815444083018517987322131
379890764976158768631291061528486539211
3108420711479379670276211562910024822538
72745377928740462713018179615963179082
353450863565563919898306452574275487668
910434245601188753242236478412925798488
9181690673553741217051598984836462351689
587490123069187524033280735098679159057
30467764697367097542681810952522091640
4704659658664859984549969448783650392281
271223883781512559496123785464989083257
57534738627520285845437732381275408058
956036368317372174541085558579942971012
786417522112988162079831635627940559716
692952865349470819382775136332563758286
393923248166192111245114387621468331388
1433921848562841362050068885356082796372
7236192030936838684936917963373823251960
77427095242469569363157078711744763875
840317282858097222788738578663231275
404970327493375483152371145993289647875
89369313632336884562164429636989077785
4372749214956239503747881082658939767303
9296322610863636468964001797475942595638
306533067768886403546314967245234945575
512484686113615472765594839243796699242
1193181747725295481877621378240617499635
777491169864993909421917039295385672425
4575423766935038933525793477535585021971
2987590797051525474775934756668336688998
2827190623429612618792513964455682835
5625321949921158767910156345667319612339
877639051873634631171537489184925529587
3016949093176634498124836053674278218602
980821343768628810347742246794046214
13004011716975792445380753092861294413
576663742789453309380219112994435535371
869829053831559321872874017888644695544
735626963014915175091731665833189561356
35882807520398690917183923387121841815
4226560377151910374463746653989930824312
341028864487713133140522640333514226437
9288946530767921704925297604472436105856
472694731779121038775900435855053815541
740286171087630078493389594754194124569
5853197973741332313588925517319581455
3818323330471761438576497472916135
1342874873599665848448045823452675903008
464649149163563984621964720838256633205
119416606587331099126138257961139583
4129855941732967984644822271209736949104
67733510917671316698546673135352449740
8793206282577033611863132119273789462
1245410662749336423911801283782090829447
525818081588961328725784552976568573386
37602786361490162845712644347160883398
949645615499903193990696627146253364023
3838183311811658778749160530186216737
5630605935983136338424312314123013201259
974831664522990659558038794324008443112
4935147861758787652728379800823440978410
8629984559147315312757754257570174772494
67054031533810829313945645744363197044
9844499649403349398564523975401660946905
337357357765596246140981316604798247385
3375669849677436243959075041836621185945
6234128861011173995245354430952077063991
947827396677868735527485065318453341074
1046285078581821768650326689281000239
9706586072869675733085445016124766629
8356817037824018161672328254587928301078
9607302546139223248185174243752075742
1843593830145672958267141835506362584750
160546747059041896971566292599311474
1542444591038627692150259206713069941710
1975131256764441144153787413656519297722
550018858225750189678386102636270288936
87614515430623772026336552670811548135
945634134162412905989623703958155791214
495380613198617932749444971786016439698
1459380566065177621310429453723893485606
2674727143408354239390395886841514531731
6004987044805438444411719287950614162101
8600749116912310697230096140568188755156
15072583207491789134708562846916232527
490398108657521518311008654883529458778
1384220737301675881959539060373894144991
74763111200860299342340733683410842893
42840634452630190654095934211335285160
589465176231019458428212567809383148955
921320854084383247961473947469765245396
18369566431485385539950586791333996463
30819718602882579704196912934390277658
2432376439484568277037971324977329244515
4313304176953135218058199590496857411658
4506149629404244831014923807930060875690
434154158136797189096183469590420986672
602122165470265684897510854515242128464
797618731896869797389843796727195555374
4057967336996478742876287719639270383305
4846978591469687943224507901834227552679
558658395951939894331734221939781968953
57394604958975978853354925999029496511
70785892750759077925765275587918084541
714623650559217410388406904832453572889
941735978397224549570807233122375885482
918355974299771927758358721188350339863
7354843855235411943034552614154551012340
24559344450654598622161987254176053612
7943782465262632959583549084786183991
722686839286701029619122673936477331932
7801635730196873664722776759915678717410
2653794784378362204080361900908369641976
975627845965666654497679493744245158741
6494249279214226999522945323455278958451
830724305131158301851386538684785587545
43333990921029368288818094861651677058
3705538096346667250232659180705956807773
1596780121191152703387097012801520443797
81051434414464793293547242771085620316
486380973826889052235089544949076073442
880458141254732185165899403242935491098
9114341214371536206534682951712380676108
354558626219710598550751120839650713230
834064879055799173559534726379293164827
614430855035066282777491614192820143011
113999634006172786715699108295138923963
301471813156944185167144256637557166106
93633953317681438174086171571768491293
7435761122396575112094271771326947931028
7052505537491430148625161585339585079096
62271474532167820976812304824482218562
89672906634375446734176776550172333094
753091199826720648466457152201033029580
4471787388221273924711446674945246802245
3441510219890865014495028284160852846
8895496325117076591336085634143918009622
9262500783827572916226177170822467472372
410467189837365628728305190527939008069
66466186202729306446599934276910352836
4582263793584936714938748585031521594
590420457250871622701820173575292300195
427911621895722787041771808917578365
85489892134140797231506422734565797812
4111748586824576329793727164363329032
36823045737161595059756260690154974468
6433998442663993867287406666620954076365
5300864674897422626820801249844565261834
862470151870543183938854164691585241262
242643334372131874998364627511875631670
909172069604719545975359404643436244103
4757204163679253415291115287422894351922
382654454846838926722907315544775643111
283928338378177372153735735885154623207
1123670176298111667986775830937731776367
382362654854106053192091215878699502340
3158564831093229828813333810813991537849
158166036978633311311604661874783438
9206446028027572185198052777743398966488
756179538050830370907381485275497067028
7326672113821852306148606063563750011026
88998588651813622439592838824693295835
665941075915695871559381977811978457
11088315948900954544398154961456949267
607492938170344035677896211793599235250
9723522074104945897536318787592317218809
9649393096699209541497144959967475403182
2609259092275402385036132379402185489142
5760105272339482684827525517542962164961
10065523474965275541046974522120192909
9333496521891222729596948748673394289281
8241770331324188287590785819641786847721
540958197105564612212015541658028918664
7878969594222584632563359100470334414936
3717379144534611466616306770902759283
5887341160013544151738722715945950693850
8557580112283543495790784637452134103988
980058678232614938898993275625021973835
116273699406063770948672687618041496545
81535239285887414248644982417457422
95773449042411829348009685933523846931
57415917429517504197105938659978468840
1073361410182115740436615275606973793698
396452185538689890874081547047979885044
817827764662766139731183327556844488698
3916657149058955770311187461243713223016
8186959698483676432248483888676386328091
4939253887509516232887405914648674073139
2748705738005841662630867760461715598355
74938242686296910484448689878983576874
9651567266543405433072652958748938852618
41362662821286394508464915858206597186
904378398699341158646465626114949926112
245452128286553398621131957608598107782
7410751332316325936079328288752574221363
9585639559049356581971628732545035252167
2430705438446850682087856500263349522043
884715402770403843895543135019983741
786765137754787576819569669432678631398
5520320670495460287887434394879783721631
325528414414137687796851752953451558544
7393985616853334523165105342788992701448
9005117715581927371891547984973269048570
430791103027183885476533161131883329851
445378714069211401651385132492376371425
6958686299185491931671767611707218962935
301679779337768566454927019919887644028
4742684971951147247316232204553361646842
363347864181273818660577731434958476377
6203770166693444129110309315836375667931
5391644719751028847352083657046826922
25848791278621848639787790363938008110
9209729950074799610691575239418169999
7094282924198433894374539569448231526077
309553153797127037393081209332795532857
1685479343599522280833958164646632161604
8078582932527388571977384318994753806961
137921957515781844638629565854171258409
6027980739577585840122114717889395817
2527448310152171237171233632378671615527
691172581170995718292776300106969574198
254179219253682118993569476366622227568
32089151948069490040857877228511403463
5576636470574142251694274859200966582723
91918721809786464527525261965434496457
657347625783253596015141304955310065752
2048466290509402685855709530749886445571
1863460570665312913429838095481622962281
792914828944456848149077558221425973969
404683748932590848546579901183878182171
57509907190144378087682566295848526040
3430178648189640437325349092887482879905
760124914932948897578752260212998908188
457724528482794695684889947246601863299
578314708106820210449726137290117282673
42605204511472965253015700373623167770
673172177163285825847819628695796984383
768414665777365437777923673760449325520
7788812998018210415283593194394967591639
399676363539936586625853468018437124
6778698247541256645214649960520840008678
2196533542018580648890552508111563893423
213429357938592124397793719196059628495
3732367387881715266751387561822528426425
663376101923655275519933273980987612946
15559672965687170792271950350265388265
864796818826169346827089140692381399383
843304526761174623985921978621483325294
312074172141263524845039774518233515188
872921368524097973652724989924347813227
99765392506210344345510678218249361477
51091406357005467046822330895177265511
4026661847861158830766875548288662157859
22384900717878423782066152056611171037
6332684456516362205011142534219967588901
185633191794437257112642922914243385905
3094117757141970284761957442155051857148
178016901282940827823275115255538419714
300389355706801685744453515012249616963
8845966651144937710588149801385320204005
9792768216041930638382561846967818518693
6214326073932019606471355633438143367429
4068975249165345583102278423380624664
8898282821853231904411987560672577119644
8751850179841856971069113975231164082791
23211355734325422837966894237106286124
8474788973084601670073138664644283447724
89627227178648872730822719778948774159
234922245730999596198541712796561414741
3213417656827979361579209858737458702026
3439810994456440365048584885183865782530
4616291976435232565761435107361281244326
4830384161541474812350177134502820302163
76087056265979894693162711487313166560
1713677349417982550044269403158430987618
991418976087821088951941048243379931989
7719313824876502510378689774996786035895
12818642226213735754462176401261761397
93314802919365366621976569956571428867
720167631566896675096355341073739191550
8563472223023857840920639009210520737296
3011495789276701368838987020795830142761
27642941959788422537701628662793935629
68116453648202318184888886769224428993
59452172154860630074210923815828424134
539911651278425787333308527266541939314
335586824033617160389175341058646485147
643067672172735155221155942979741239647
4999102915857510931563381800886938671912
27471369294282467053540313008905620149
5410465221349625629697884580427528679301
5049746298852982452570057220650293863210
508220353103494733557592564633681756043
6151178847228877294687172031424080525683
6754678197775114284470874075368319874799
80421678362133501627661722302281741057
309655863344474733077192404161042634334
610089349822689953772021752225380571027
1028397444976726204061539310603678619949
93014155950720835552850862638462512057
62794614579138034485888314899499595347
36366425682716258157507456539433761978
7331502985712137757024418060114864094
4574450027669053072912118809652044296
513613746001374592374918162715354991993
9805242475934691651133562060701112007201
5114570326617069187289559809308619473216
5145288286345518062435506514014310264
521825016409236702951032343920030623387
945497163131545102079646838958767638456
621127289436681181318432900223198804504
976925217384350965963672834285359337722
4638656165713092232245622046408149185318
9919578261412484302472703492314921492441
19447922264263501723119361448232912795
287154539586765514490341509894155716545
2949389244098425108995281565150556597730
50018814851540189513292480624074723812
7866849867354652153598555022371210275019
68559281071757838986593260614755833038
958282664248213764617278517683493099341
5540956189285948644794967241164148316
599731972946597557289498513176156608746
15101797268523629204691420711017725900
1191784925306019605618215998720779611518
8447384025172865510634478509275574677840
784664943133749548819231600545484006058
50889943511210242209922660478922574753
4378448264305345895994584437612250308537
49245345432910996565775237171106075047
684931106078140029847027222246646877920
6763575691196210844948097958634168531390
693817884477806560639761346231069553365
4033381718598473286967168149502416287704
3945995456079281323072271672259986597688
6337904378381126906165327376466330277557
151778680750822929488232754027943480
27058051760881988118659630891908342131
1227419094317827703084494716865222518820
46246904642872955888679705557620968322
745312715903966946725033521649936517413
1462619297657493805771466224221465112463
5001233713199942216478445862045023611
2107654693223563860567371223961457811266
7142638989913061593643225428499678193173
301497826186863171314149426358591919278
331515807842862233299065498769517954061
4472385571842247875953229344985150406587
829238023704254887687475926819544948126
51068969635653699189852193176459695520
263589634494111687153656206181391534349
2902901773238681582794859182593717877730
8563130614856411392325445579261558971390
586804833175481846421063591427747596
9241674636282573730972979585220428194222
205956269486322813536453960162552379331
9110660644997164117419399279968834853747
122486943356024969112512556140731977586
219928067306325676594594840289386512830
903780389409307150738191776096674271905
450682687153679787203845966487833476781
241489606952104189888079348533054431969
9980657818385095547966024836542890485988
182424377750799749342750833978724726251
778393684304903337984723385270437571391
771511754384980373971429749111698634459
1675877148851719909650628941886459469396
3670916711243616772947309684827925098081
3749894448445171413784824067968143156436
9625286131209727582660018711182130594122
276981211986323735742527689197815459
53363238462421135454334937256998420459
305160235675823753515183383489292207205
176939898523663271765325344889304864401
4155701755474265446520831686821037451313
385230477255016275218382102190575201938
3905816691175561242131053070549075095357
7050800418851601922817792374733045744148
734245301530516865723441427577959512741
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]