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
81038210508946352074421256362671438851
11933283348253293252736976137056066320
95873565881325555524516582377714521663
478202818303190111077957281897089682420
758163924439527822716286924652284167623
225563754793265944395264418313522416996
295782651218827364341547281370420288457
679967936082370329501728038742766606670
4896239547172781559031889655936406389
3940327642952889760112605157682721274718
5053833384952994227349438187716912024770
5072208399195133106484615168149938132434
84449824256794919248435246510355503285
9951265970986766224245188737499509220
298583052474180850052475919598693317497
5635637444021381203092253260775390573100
499671721609503750947703625201131094395
590028796813728283567117890125598903221
160544301243519636452264372604998264233
496377238284180452231052380212895591939
1875732875986019356539614272978735278032
8483912520741178908343766723257640483880
893843727292603333296472636163509615912
73464933855255495247793895884932103363
948550139879839881769717724357744274286
78345728145938333975466954909899383796
99732704689930423621226329579694825702
571799292146707010071409965381023943035
4688174426373272996789688485280668775215
7572483947958045511676948198432725359570
1812990972303087567946681372342910925793
5211860096546444193853196201315066312
1525294570122919300960109836211992843854
631951108245891349019268159126685248130
486316037642527001176347902925788415
15195281332470627051996737491283743927
5762553050664041602013051385948544369668
415597713884968926704186909365174338579
504324567020296392559051914012492348700
2759395082385361127157191199432615348907
1354908955803467419719283941185684628
930623616079893638856792823278444253074
499417813037953784915879969533479695375
438138108953225636253001018840325895772
798464439449635768466069164465717688269
4798481347874758822195133291452168325233
1107697075318287577882967311410856591465
43532174356216794311523777254977388
4607466224927699701547256237635328793276
943858476068692347769861849675460206159
990237923917678814802486962792068033663
671378161114963053126437359781399912142
222991639211980640387677458605818475229
2519302052037035534585532736864976916
2844355343226712164461494211247262798646
44613477700154195146982627526283375
3345678140498682505438567156276113096474
2870927144469399741214716511434279377885
5186855518167516872274235808593541956770
79451132252321247235696988912524752396
455984169606581303942728486574132429517
121943590418534826412215674117057859972
12376277875972520143323323906374007103
73687484396811907319626233744066153307
32497479416916463465455354161515005520
5340198298983469480325883009752257185110
784641463920856011707399787890817953210
231085686872430145783437692279066209445
8862732236100387597468082692454867158
6164871535044007375716075852419452695179
269028255935326851618494325234159324662
9213457949154596973156679438628551395876
7588675859459452667750446763360365773038
7508614598296794413414729063851223817
168110308361629309583179937879618369249
8718306236084609925466776248979582078888
336442607958200144266320733024861912530
865737166584347141831753989872416012194
248454679605976867943401558976758614438
3112405546256091016380817421033526312
1246681915052701963690322822599434488859
489076937627885044486913281112783638784
9867412265299839119599876741289482783453
7862287792681439312082246043504097934089
87627882862773078389671177947951279343
98168571974117809046136664686104384
364737054128204697905311972871540245326
41888786562789638635712847486219907904
1681176424074128222827396437847820236893
94494436406522964292376576821019763707
9821126151312816863398226031378585574247
12396554936017231628120684694935317416
7619136017734438521107242840645232765
6946355223074597169335341165209618015718
6489976821891932691850596743476732544538
4851971164328944718593525970875127393179
2294520444474169279446578797371972164911
554775246327102468880264066948919179779
962784734737900178975086076694564901023
2385516444088756323296233554804196422659
147976272771217858035183923752132601269
9379362077881010827360681262338695073891
353148154576842359149957242542298353388
888641978601211863545291369822155745466
3992576879148702805080447512329490295629
8874114538141134194568815489222792756260
1335509675859407497510772229628151887278
91493782813946064145784487939461141488
82807026866599220549184655417876714186
840626453897510564353578987674460666302
47996796751354180071043377894410985543
251686334362623478436248156980633882657
8896229787842456377234844600762931338779
925131834724685131504204110119574846624
910569847407348820056669732877390875109
10747850428514548710509241322964828
8798813181455171340687188671637132947428
2457290113743484288949997133795510955601
378015918077670633143118639833634334729
26470921897602200833815994228336422997
73934298625977472489064182712301265816
355559831006624538435617466879858482190
99992552753048082246951842524738034180
3630802488304696856411209235395443746528
373214891405768551272966389415493515789
3242878712239547342836316847920940056919
169145065485229436143529136628888596182
4892289431519901130639183600297675708247
79785931301828634695061596361518437624
49786924474220677597604556166515694247
4185754285446037638508967082994326029
5158627685079505427581306720249213517
7396474924885822960312312785268486711414
2496919892727102948214748626717698094227
273133213423186583905112345613812415849
1273553082963126324220142966744367492735
678089804304287526264458239161139407965
495491298037348775880203682327595946009
4675792038043009862939204567119816693489
5548460737508835998178417390521974119000
76797224819358997256531555306967398581
763090543432752995628932305306868479537
1177417758223916967633011117482405262
617979764266291517337988919353383738899
585740771588519393025143568614920469425
62701566708555511862515548739385384925
4026728965934909633697676468525625674257
59989156595120962817791572058406888455
362990446761101223395902927401576572094
7498297294905578610586633205302881024640
9374215640396539612486944167621711221475
6052949568974521139728874445145210278828
9310165853577899538157342519231439821
558296102798727643164383866682356358104
293781071515349888028237740342121565192
213181485693101422567631732503614632838
957565736999063104838901613312818887345
3564804944453329957471691290214057986481
22475845356396908912033993251134022696
64598696524340503770042346466374058919
454294037918618271218302407896524237260
2764907728726558593112381625173153605948
865796062809275338826151641790975523390
369261988079183330055483887882112419976
5752553558875017426725884443107291018302
3228282393433277286638441321449864946869
7637437429796578914135408622145723913
228359131227637268768612480654998328134
8532644977496098366788504901626582501368
1982661561749587885364879313651556354375
6904555863365905474246981054432487301558
6780306083629475811940368869891035474191
971615449357516158831885413200969829158
9615547622172197103353756967208672197863
617055943511579740786773446837919658977
3781164280541712222562002469594585919437
815257723563434903663903108880356142343
8658184854426777435173246177846381066051
83102718567278179075308380363372883233
90138214184997483769426474299876572005
190480855361867295368235849889824571127
28409884460288095602344914126257539618
1801999616694731369565311030883564827439
314951624910263844834169101189085219284
25829480388130905490851238592890263731
772497264417721863479518396978380923445
1468569175896939770559527303761422242
8694350550184661657746298957157573857723
425823544653113414244872690865088312354
747795858269617223391145104879472645839
2264244472523728433795675959072589203
1102416851679090546474935079111871738721
2925410994156695374329767633175012573223
696659998335933042849617531161178092
6174477613415482620164267969522768576310
1891950072808376484616057414610194104868
945726045116289225723493240922645887345
1420208062246627302918341469958954058803
8350129415348895168569084442945949686729
37656957958023124732120068778780368044
2238232671885644519981988838677898737061
845992992426144396715839855802713488760
693392967966264953652095374454097797878
94242612465457415234303013237428488764
31712981807154279726298165211811484573
325340044996623722017625346645482529374
12375185192983767581484892974393348176
972322994040896366153304504875055858395
636569543940905362264969533267679501852
933920994748598653217944229210683021356
9345449381180787910163072118826612255
8836779362874031987389074531338272013274
887625747452556118221990183786248472083
126631546314709325975434161775455649756
169237652332113229537907346426230289616
1148854482209520940627757598130441412554
901965361540768656705045577761346756852
7384520247042473431396057499835032364
9954662480669092240884016198830794099218
3459964422785269128978152388417650081450
7495138154436469912372641893080938400
234338579927806612824835350134845725618
599849289423176089353739618888376293362
621050868411149384633503763167448351393
65656942416632127102601970625776558310
52259426500695067564689353481953897670
289944808746636318349787181658783404816
3915788891203679205625034595575916459899
496163870155938834319603962221941683089
534764513300267380892962661711644494533
314840406949537087082158149577796689298
9649735077408685633442123465823893349
1227458964835149763253785919792019294029
5974649838596692821827779339739835704676
2059819597602866010692866143092004410
1449381419055232640477871402762321737290
602933395382738673860201774142956538109
9174649959160602629684770872848641993
29573146316135142934535039411182644667
77628953690030489945171817783971384616
5975676267062692960129774257849163915
4521408838246648481428969716194095948698
602158568257237352527125931595942412869
1704172554910774713595986570938928278
8991650473988525737033978595631393959817
488787784318479205576981905385256681066
3290341631087268176223486702676304
1392843690046460862638168682541618341346
610846173015136969077152320430040474316
59785951964653523233432185187055847869
796538849945485719872545012593878737205
7375443687965715495117148664948720862373
499626995622515816428250945029894384156
2067366969533727148442332483169494888022
9097366226961169948012575857807268772124
64513652297245290712562795410130281048
284176258454511598412921398204711268475
885134319337309024082190679787311921485
275313639334640749817541268947167655186
737448683884764985982367818139664869214
3914832979345705568082775147892416971704
2716915211773887870035827683584570553935
472052838878548458401568841720922628342
283464947743699072752919484707785671185
861386835960742134512154029979835772097
8101590678715972269156890743166455434
844332231222537922717967911560854817055
301289665159503471902850116241517494631
138655356342337483496872303471375591168
248983995789383855718796076642975636291
75984617393067930634144595686692333075
57825817806063684346635730192293074211
94018811301555427198570161919429561661
67344782964965055994919671288556499810
93523792667790549071876347677674573745
814490015928694761383062997972508408
1911383819955748244572272775424408969
43199052323131695416288860121537702510
384040977435719424787840685899177020617
2121723599005220376170498645626453678903
3426150529356992512480634630977655482252
8982339393834577560068576625726975159563
72317380682657073954408449323380457397
134856844203879739372544879212097409844
73535867196544341501992901873653604965
720723988257003233652373723387393516610
1090447573116516411760976483234745903074
30091789548064698296519312654875701899
61701819361495745660799883391261977723
22571247686025181021196454762085365426
947229680304982747171127126429154089406
549674937698823452717665499562771081999
243784688337943782279903239136884030
404798218535193494569865856678030584331
9214417079785733556430773057885145013197
8401727269573458976078175417736529488318
336538337698380173417339170114128028680
223460366764510628785092484965615794677
2361467511180370801054414504048403471
4979526941774829663885819847229822731665
68687051631825206275144243265410934201
575240188543026500592124980202522351383
337190999148030621383692888675679758377
8361434371672357375033053670996082624318
869340310548795150792756784770645432472
7230484648202531321761423877183437651605
261519698656710895317955760346385285746
94062647894153012367844247144149822022
39067828621874632624229794091616407547
817156637274306675797399844732605169950
4434932997982210696424466626885556402921
292271313169130928038037190649468284799
4250781114802560473086046022573435755505
509223513216688822911848122253830682054
1207781343626835305657694098784636212193
456182232626808042628329195927589515227
619058959939528720538825425528636299746
97286207635124708422940422807437222792
59313615513232429709705583401587724591
541169821148968665538128236363793881671
815898385999207669319630330947006986297
2982880540246674564328727634740068325
6137877181687323483623854684990392582723
55757656768582973833416769375663337292
1910293394971053256132898843424933212
1047782635824048730574877833839560029101
966991895055205598279856493328592107113
3022337118395064697892318688445943245523
632332438131621630176768480592176049623
117478852843363827267106496374097893
835717021888432735599322073753686571750
49316234516125840540991785211575467724
877061102860469381039445197336674301123
91877346650725488975726390517302112897
9213679369345052372612019127474265814533
5713411147428001465621479408584717858531
8332346953598316086177956145230897388
3963514216793948755759592574395343884565
8895347830686210646557069333307277966595
4284674545221602748181323361700631236722
8435849483317093939922226458219920954602
7157969520084350634941261155845150384112
5388176034284218128321355640929974591359
8894061719381920848537609505993492989
7560537917603605863410882795316198093022
653172613972256640425897023298246257861
57472491628569474149395972855533772623
368504869558213640478933083833111552722
5045182670626257623422873005748131859662
59515523841669989365937551309722195153
6111104213032861653962219200434476839861
262311444531312333896483900579147881418
69205496365559318019563888421773122108
818752443629616481768257517496152848536
5538422947025420259550547698581216396
9189166296381800673283402895451999577117
7530401539157915700391254848821049470
8811513162125551116646335423560072079816
119652017830240592862486377919830896929
719361007619340179264407194829230964529
145591632256755910027435815207997558245
789983187757100834813013679214639633324
5176389775916697322636647340735181401612
6061314615142066563223161677286180298367
8920633127617731948138898819392535374443
3068402739073149689336196330921881225487
9672996578502839741727882210200612346134
3026415022509655325388448743962083337290
9634723896683243675372448630699374847040
4967585099513830169124791112949111551231
8877845995756042463826715896133805896
3754634246149115656494329697104715007675
170238121027760180398370897874986055409
9142295572849900193051198367710879343321
140190411125935640749286484295348501922
533354917447195707282617999511775341053
61444216566760727264082333975020421421
479268121846613580815221945860272284490
96781271791331203382188526194979084489
7779173334559261633658999129676820471546
778635178696932627361267516800280749026
806730613111194993793226956839370389797
83918285676825090095624320177319618477
8555295111443264469822785877435123572067
1913477832696663556416178861628375977416
6923499422456755923343845937941549
760650094434535072404656230171687619651
73694157285748179925805521771214975527
6562708029539099648037123826842472947735
69246103146323869347962340298969142431
95018485425332659371619067599246892801
6594452680249562661144262553984039351174
8367089687755994854992690886546721612
353724405756382168027332105310648951957
5451852324836323809278708596309093476
135829277265279194394113023993795935998
980493424324677257201237386800466136948
918964844628389159184072767905193275063
40412160158719018581105184752748944842
979472999902595052251035374499873066794
1116421313566221884378926160582748890
643649251542408598946314441953567389525
9031832191835632270209119868623813312
34117783898076584416369457276244339735
1478237684293805570461633023462830853624
110686274155289219109650590220394519026
4234725175874415493672488437604722628174
8918825068129061971075845291370373532688
9724867916378624861253018073306482645972
49664005548867276598624658105264165478
108204815494010192747169685478389657128
4170792562182764836898895557308720201879
686430193681750084419608661731123737579
9596462959367548491491888172970275981272
1891813716477616118582809079809657762073
67083333474130051712375601490561404646
25918281881827563587845025129470866797
1317161461788837974347745748739755306580
5486730816833104625512653077576611218757
77426281513966209563632556544553179949
38423645157569976496055711672288239470
8720664738531359862523924039112142991749
7994976375815710909433611448707544875888
1011273748084406921823604713778070118634
295766977746487210478962542855883353716
105782286534512865955553719830173787344
74285945035190819895986054246014036271
94488625140451987477191817723776647987
2708329997801743445506038340438243658
51159922661812941355745422612093411142
64323115726071043636670782343821023207
435060864204994672739675048290948954998
5328982226711697243214587765933565072521
590121797534149870289153942240714863332
464014694518386588197727970195398409578
50867572301880808012315484149471382811
217214871993865775456335979983097645136
34911606125083886332083034819138135041
26631987014894634177698583862929726074
470214473136181915453496435599137252
8348816339730355913808686565384585516
2834813148898670861845214004554271339323
265561837390305287115537377492869429261
8331935865711885422850207571594350214828
5859651277317879397247376640791573866
927544910276718501115172094853890915974
1220401892257944173170373029006101483
8203312081079255896915561055334288861218
1864140842222915615675511874422669387595
1282423619334260273274799010494214133943
3302087494480561087769216824317071827
897411969263602719565121303552724799063
6701736561586893750933369011219694181856
99641274699992487231108579868185471432
4022858335738010907261516754665468592
5377894848378418235912712001120318008630
4221326645094504971699693468796846707542
73546385141912607527784762670472693820
177313422019559634122104137515350445996
472313365635773356499187996859015332315
1814527569073674394164916611154767384
654255253662655293818065103253630175778
642975701071452967651991287519651092739
8421117472008439280442717293621462759252
72821689998668838325831370351909199241
2804666482104476587019684440100753566808
3283911332675088432073036896690487518227
8104748492203642171453683228428360578873
204990684704627867328297974266495739133
37754780503170135869974277817214363026
939054855760460479216514552516626786882
375825912806356828724854591260392231
578657932629686201529708797110543457611
82715208874368047876962326714377608677
1720127532894923363646414302718843649626
1058661348228599859045988661794017334591
67288978881851775664644354145018542180
5304862937374029537421717399488345989530
4681437167734340422886257488116805462
33866889175729651254926157975343129815
8546643116219457605561594892303096372430
5348196925955371243059341348932577204897
51152978513466732507263900917316681
731795189141827360473030679438284053619
82693991643754572624473440014825245325
5405766670486117290850076283576668286724
3025994662141619806525938177996857847
8555486050211029759218719061597221956335
253575657373332460117638879112894748465
9977133741848614184839322789408795798021
2530180912354917598842717692204265887986
9190696378764124191974962507815109773
4027142354375548436458801136556868474439
6257149596587286879371712838921374308327
57267321116071015028476619030352957676
108776928926891901282285868341673314784
78901408107897495406661411607080124263
6067492248395962370097816601914911064860
3757146163976790847636992844584365904366
5386141158141027475069935827906775215719
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]