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
264346710933761512044572492423174061615
177794272638988382630715027288735383
336510108478758788362856033607996869318
532950969842266251383289875612922663781
62894752626834673306266116649034733365
7559474635591630605192929898441190269060
2636679266108416869986937382197171689215
2291214811781650261322890833510395351
2003672074019965594340686048158887837346
906733593062586248133441673953191969462
3216268222485655638015744503741439196287
174195537272818419634519881037517076912
180373593765052101391469732169577586545
64271441034160935745554125570534115514
857829077638720824884735748751908584577
4808849958139864435463544065959154188177
474723708142814121034193638112382865329
737939141931312920361366819142929573475
120454462163536583572764352193872011265
634837038561472785227450459617047651314
3426764421323757268420527287452562261200
8242798714062620864076957864989841407169
853214478376242602988696178683685267
414774618553299964139482664093956029
3311931131095384587937508713720981278345
9234626857562195127135214630079080325
814351718225100988616477258522726430
233930224705814451962501052890577663927
9823817820960617545168949604215971787
8471305672267671135519573190099206901
357350834311907122076227729840424972591
1791195067947022651844436872812110869454
995635851629137374935859597308069577772
5721862290479856536763502218257489107597
3978992121935242500339341382344536322229
4306919895507175733566671405218432432298
43489367194341641539984383253436937251
5437657212576785760850361663491293876893
5079146695746172441768725804937552206174
136423474176855257049878694147269455926
61022632686178298257425569296361153781
5490159966675755114731008143320293285614
651962595570183902381937709606852078484
721519178812374628463448229959849337497
3493381114414315274963977086239614789406
6698498013155929268685996266339390349838
21277085296475986422372974727921398423
1731464030753204482391177256840844133142
1233219786573176396339872281855347092
6485401070179060723858953881232027032096
279781636250600547749653403256696117149
1298660799077682784552299157443822617943
64877536931922472574550698059628436726
9487495525663123687955766815735011259
29983615158659730796322130799516138452
3413154564186954687757370453663384550
64242239165180426021467407351238021982
5858520294458619560215593002360040135511
3592142982371677849836118669224395067936
85411894415643884418179626710266652607
89231950365395106881069055319673342809
7712542638218130309751851173146841849681
7828453261385798473227386858480518434
53357891497995877548200616673853142818
376882226794755765132825520755124365156
354557388635677678384430703110016841908
817415564748976128607869274557548452345
9561715611733967904235625524981602745
89894146472048421129881030719062907770
92046397903150687022142342333085202354
650314984236596893948131018970971075912
1420596999643394832676206152149510572834
7298297071081789734670285144279793146913
852254888556365580456248513589819406130
3642518287895832275195753051207067042189
9823352521352219398740221839523596994265
886853412787579232139634967573873803427
7280336942214739687191232978269112143724
989635472831753820345981038891512349787
89927655025988198874154534946398557431
3666117869455259536334417567815592114429
467872178590618457826883389997708881781
8277641980708552962641179875837983195063
271440663854035310257698498666443172
52161466822938404121416163441387573961
683677775108304573718903435448610969465
9869403990122268552617421219983736582902
973655682003271488545461165055384973726
117688576292686814225024684547230625432
2804231913336586456193188990679089698589
277142719410980854663247219941145347542
16555353586458282385815809967685131504
7221872745734678970102224640396888727
176244534792168747154440116290458578797
13947507553492025973596658838219042257
4006505358818270474383005609984523981469
107494495367127443269380492998057503
3282554730357228794831382487682322299
2078300468087370470925355409636252159758
8220842670558885247937016140532335825702
20624372478687951477054498637861401256
185353874931436771627723425544321138772
68949573260494216384096731384524455535
90254542873844719966763932141768799831
83498260851940863825198364626183907812
465040269054754864846955396576234415029
7974734034947738942544161877781641747260
5413991836409398714843807797711985032926
227216310003153745349846515145045847299
2140842181054311325883929126295231651997
893546504423961768704287195812902245682
43368730189197433980526449631596440444
96834501803733659003470468872038721180
7437884670576469422091359691615289539807
2520281284029075988169002793966813149845
23321199562379249618867653312161496897
205696442693579574429982237653964005
6526317797265395500830434442779061796007
131579164632345275523174847672149855250
7626302897701148699359204200530132843026
11206226155192192423193964894687352599
8539483574925604605242021614287566425865
1517492750731706229817167686686014617462
368862635088677616908949816645372229699
61542547960632665309260631591052952420
14161979407524752098057134171863726714
761699018094371717336405630392044425116
520081850475751536259976160635072968542
7383855258625766411484675240426842682157
44302398179950463676133192396433101594
236951549405328134993807121052385655985
558384029073680670414701988196512747287
982326499434550770329215619965424508671
566617879751824931788408958361490184247
612173759460399993435763811559657251494
7303217073239102784737137949729763506959
6444924054933103391110568679868642636462
531774138411390824965071886194264173130
392245571318097722078947904304090018710
2596555754975557575260478205299194021466
99317270284120284529551193801475646
7201703313868347513297911234435460116453
42239579843227431352762695160560647533
7871692948093847467768479785662626292380
870839161375825125240609620468912725829
2925257712340567948937466489743871195
250739995137171737761671662349355362555
97163977808931236377708222385359951420
59844466192295318864551961406259435525
288744079907356951837509877067055846167
443988151471252479949468973932081353141
446088691537839200843598577321334655285
85142384880421147252754658931495595569
444306193016410458842934017814250327129
576682427786209949537575210868425324466
8042121786898358605821729643766257568
76443414950895512040957222529084691
889536329993534475970175194161925237969
1404516380747790660737354954614023958487
858846201526793608466292881338689741295
27784515131473453967276175462977499939
76533029713725686752078124842361599489
20549080771445505630901050981271988179
10881499064924164552847720889806141081
633344387633657978833784732538443414555
4934271570826145632318463891146881909042
303524578393536641065368003688168952722
64675307792596966060781999254195668714
7716463045199864366282322633360635902402
320216014748206253036434609355644691957
437930692633619730469924683727131749290
237418076728574321615947698906951561775
77578026247839371186551602949512989431
37237543609579423230009032943313139834
59025275820098270582454463754051888715
5465695333619473315723677910908222229057
7384868213924111146767202212863221311367
244167112426751824837995305472914859803
404043388042381555028042849361348601864
4606260239096782451026164173688397488920
334888573854147150937624613542513714477
35257255131759372534206359939384762
547636379920319799153837565759490046843
1097188481399427551363325153058361256
21145984161592833337546391632805572394
58597953280446632656824080815025561442
6691506944789017679093719176657582923659
486791175475228996752163608838265874220
259337217807488838538108627906289754881
982257084426788044661296769625176001984
270052569668130598767754419716214763063
8597831131549326836620561445237868734287
40041664184630930755229044578420878719
84047238915223659784901774210175134570
1756144379644728394761364570591859514364
611132104482995854537034488231111034584
33474127592639120764033100892832182805
5305139709557178931426876965986557277
592758307461613151524751647384185102453
533593904584742126694793589132759719276
35691253847973277125353327442268566970
9804271433976353827293318133384469926814
2053258137268344428143651817233329376198
778024692954604313578992241543836041468
791129355266284585394226011807530177466
9728835849254367900584324330890315384515
94581411801550047078288440418089129440
221725933797359047563004681677087957655
456865238340191897476755447952112611667
168843670592889559957788587961892297865
2280445683445479792724349328370542182842
64193218507645381646419493626016933033
9754647289376940420757803193993565753449
8281621295732091606396921669788032714653
2003425318383461449713258284877940227504
82564716540486359019715488686513120012
146428319612814257486981531056895605530
5917394844283076852996088909704966014098
6332146792733777857497944408253507320
4191192474323294404585181887918634041137
3021555238967077277566552803217160089147
5950406425045381286748289291427074481229
374323238873217540622107207156203559448
803458049238923851997938294355282176315
2175502705832561101707804709811145901
1248769271942639378676610879213896164
43054524321578537426221421554419353413
987456516961407726057768772605362708801
554075453727908567205258572101451361799
890624321277209750171892514264893854677
178191597671702949822175855452913007427
3455918966041368952561045458458649107765
47125415269494525416671988496232717
989911718417713530226014547796489329611
185773043905292764785859729392914869
1384596235571485916231982683481645272847
29632663902507111539089921421220172071
553740851343213549016348582392557649896
735451836203604285428913612649297663182
7039536716639371538766575897614511887181
76877856792376516573838802600124340
940030908291115655957033228482532264
775217929863824754059177630371052872262
2493108710082425314720775289161556945
248752579082324648117203885415384171218
499974886752385776848727802434619102125
8320125179602338619722577039201745413685
7007685996116430466955586739652725749216
5007801433071965284939854605694563161
803566191173751732278268661365586492972
95959726847379302447554339975362172595
212920628340588675737083280689953784703
41377996450468599764752383852972181571
8586124074993722492014314188934142769628
38144044233548223826842617968679522501
741114133125867365365938235775076274
945183437762560806875316096324428775859
18509091957114591512354216377937497670
8908232328902327435414647550969581716012
25784919636340646622803791427785881387
96319268596942238936573968342111792
28724671459787253093971346478846622036
25889926681763364239896980192576749022
85706077227675870697339673291682134546
6026223166651780928831755770286620381869
131523429909164167190284073579359248298
1395671667137689286211561978559975026238
533452427380221638004397417546967424875
39254763172440488887581807693359562656
1082716229994607273143281261747074432330
1385161119135695715159439121646289867707
4734948214746282063036754734348109045
4596421315929287604890522481659374854
983964012663934685124843952630289062519
155030198330649421637154562219238989674
514904665604714877788192040831515204715
5365420055146090806211197226785725283092
88808641261043910955140665785165549812
921566082189910618841196607617062955294
8909641925245153864259892883634217389183
750432298541381253406088191256129699123
85096782332291378586805065973374682492
868571645716949736916508031620864651
221032994718260528311405210395016189192
200468131649548856513509258769114186291
69753639935092909985203488147496237283
99817669986116853833799945312762363216
1336817340631666640918409170916720138316
874060198105738977346675516187942517018
388150930655787439334451078420640405252
172811962968724039511794197463132506432
304767941506647861013223502087878364666
73842471396338744222262566766844851817
4794284490123317774276339288583568669064
6933623285504663519883896861606117383154
51315102389542389785736938661168196865
6075193515646829609468429748723094890
52168944600265085927796795438085947513
71102236680364463145309768159556106299
8400871465918244867265735896904878912831
4711554969584657777222557406692854543078
8443782574749201492592491697262864624385
62007734379147893178714122985458402756
997156806391361851349621491324556996388
438092798147403367663170446265818421840
65696681635897214727928244306039189223
4424213988976828919013844069517369666114
555088481405672974375056661537050615870
188316199793978074232957131676629136774
28063385439862462418139752332041586674
3470785537766161883496388876947290361955
7803851349308143790879129662662045961530
8080757126419585949046443089572376707124
635425316398429326824302868722419071
6703905434485929315523914788218344899878
775257613890869383939852854837739709355
751739063510983484781685767502080468081
5163966653988265577387045268157168385946
8470270113054645179062665073143869713863
638397673814538749691871703664381871330
9176416047498243956285233817554313897413
876835251540342881935944715812682484403
963636043640231020708685865793358002558
71778263937815668456399551777641475184
4875852987431509852912369804645155946949
974559122895947377112675206947979068648
2899285236703311324303159589820331386
74079371361476726548507016539475695480
92830266088885743369584873143912626198
2773344238957818326498250438843284729
3688483687646280884672221341220938426736
8981974411599132482381681999774171021971
6418617632525989747710604796582652277389
50827145926820121524282015038387423491
8359948314468420501128701282588976206166
89493375149299466456508791741884693185
938989604333805916831783368661188104580
40151592211166834496560706377169702304
600055837196836889856259911384338917840
858744934459446254745197812742583494211
98915068866946398480703899147173886790
6179915459992083544466242468137750776053
805136273531442417974152561500786613042
952697746331688377048043959957051366234
583056408742519375966505726690132155927
1193109243916073703667716433119480325302
7563736811131888247447474420889497893268
8436141345062120200537754163725326207480
27862682827641670017007583881917778896
744964139446187744040846873877519439791
11576871645154550471057556425887465552
374956142868979094462132510366044946907
58304684973823242475834437896197602660
8925579080049113153315116896165815429526
79516391433151376599750339466614909267
458889481371998975126027758030937534737
242363338731268379722466904187559629825
4113402710183646951262732522473743462178
209465302766568664937227257974476727464
5200338545521962951790918219881631213053
980388626127112533597647754346420255833
736128774001203373211905178765992796693
485787272002782486632632152243114571578
367297438542961567035114494560944296041
67973467768833325225617829581881908973
690163353635142988481480326681084065831
13169413449958519798677307304413896462
9800867363947800802199218874129125113184
1920291624901857285430034479338810582217
626430331777664888161242139496593834169
4471619911425505371439075930556226756676
915788993592215166564299443324842104262
6607123349483783515617763494304296703283
7061518677622251515447607870161191445
4789856437469166271330217921573085322971
7493783613805358417430864128413082358941
95919515554475761456119445849130029062
955284385746700673319121960906931432502
6682942057605294456510301868858530546803
42711175556228412058171939105952443424
529751297996341960032638647144651376268
547613351217641901276371077873820381350
1914305680127950525094302917368935644059
2429477020092205717918649560593287191295
9809355839162657258605169524993607322
4663537868352274703543637158499276705887
867098264963594460576878423512659609999
1541757148712523752197365862724022466424
8424219241466192285224965502697144671257
4358195593482445733258856903581695248278
760870739079262952017112132954983162387
4687315168871229352677817485491126547933
417769723933986281999811857196516963775
8308713511227607454538462539261713489789
4848868653171297725353288065709028096614
3603731189258572179638886876089466948
443839958877393974743574856838573223
9764517163967877714817814125588925412
707699439502899971563630506431299506269
7399446073359670406438984078576665425601
4556693424632402844420052662984170156500
3828474697988790871825936071479997638553
65436786598161342224612519947010423186
351217928332434471951768400193934901750
398445019318172102175523772670134464614
5066797462589210863625268534894156544220
9242866746919741876478517569871390715623
3021116436833580520737236834791426568051
999167335256643710265261498729317863708
412848789345350070294825800499494344953
50392983385567899290444301171418114853
946264037071728188902119742412423763471
2003816149935907965425695837256843913743
8695719722096127843262952152460427442040
684776218945541752616666896364525947679
254146439096158062588555284352895968080
364175801054950618002651588682050874427
872324229387388327039894097782424803737
83605931837678715150392454963360042969
114453199320610278294662721823715972892
4149645935955258584530399211968329035524
669674313961307896248114929277227201470
3836260598896588669929929772922205281
525554844873676950755689122330152374452
3521807296305487822055538726741183557469
65795033650151342252719246965526714987
52924525722919778888145362918226142906
28839154343272546465749738210860227739
87617729521445861300118070161148677686
290497751139892357423437103878026116373
1452434888682793998476154197783737463
75655381212073884335254564621821332536
9123753443948245304770679197236974896992
108657889558446091799428748813934254965
167151999316496544253146855313876511338
86155324571174465776245518262635141581
86618586435104698976919846407121547251
3958204285717796383396057257520968406664
3455431356121828512737758143655267652161
957220432004973987529963893393020818660
2568356812367802434351369188366725022576
923890846277484830439261665382630344870
6656291241736975605238002616731683529487
142088616818963513690559232866327834424
676838763883700569967931178246153606446
646015208393209374207298796389450479724
579172827243936702295531631950712291698
5229139627456989265094287416534464930
717111641148337071198209573786799889987
5970470852022110689899376830759121125033
785423821197595277031324360847893243599
550013418974716864027449630634179339256
7044565811403458668265618458997316963625
6791316715877870881860304772749459287179
1573261074368318406294368813351592202406
953218717945163261681797307261473984417
814663144154296302928431821661419824169
534292556773979917223130169270962883564
780281699951542858936998443291296124291
2977850756415722978275949423945529303
245365596306596996637137971971371478191
22182578726659254293727198666334264326
2002363290928377431244304899134742798
130544377106822993032640199180679533313
713238857597708851798952014120460035097
9360697573184070552286177716913654805111
439571171513629923835429087920727066933
3886773191085003926955707311770861203484
839940487039710433468247830369064023084
6442682046571308125191056426890957962733
6158706221318453425677833134861776527538
25036914489689392031210102289846934276
4321983210867758033100684154973926691
10471269664199293716594164358257370
123432967163963632931851367237654464418
4759436888512554562817611919638055979869
1927147512281251335129312755238568195443
426145733597757604944652611567236403206
72851901270648142161506349970567848719
5950311757492974943718477001418167139383
5707305745658819077412092837452424570
67321616287341352027427358798822684846
1452150935406222680541052980432365478465
2853785121679711613891089471404198381822
547079937516501185407252433922473164642
6399251958882922937398213269387717912
3084279476064309321093228913499811049793
608841358754742721179859842274341479599
577290676423511385468336612236658806316
6685452215808220913716792170785066671603
501253415327245352606715821121742447803
2341812073179143515766621376481191313884
88032414861305210622285526830560634975
874673697404105845669179497519793832151
9882165899507683675587572018279782830
2854327055461041806553653307643616545073
8745942016945899332455963595947118459263
764365135227172155719498230864328077637
54347367442893742510644667657353299710
95874286573625401821103443999419115936
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]