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
16095563270255919261980428675883905493
90274434342337944244730210718714935058
629937977059921533367417693300165227730
829977541741069329956057132690510403312
2809738647397490464024709695837746155506
4315864123478919494988359939904175496531
1938416642588571496242743099336066138135
896725469444350147194876967130856308506
8746403927176153364714277157329343847
58092701108648664503370418944217948245
133521348441985073237452773886018533430
8462789544647531523423397685962938761595
2305858710015208520793823892831062104406
11728534402840254806521239872943408687
7839885983823247203637004948169456351
523517937299604430771019863684217271614
774427974937562470082536043207493068657
8018756540114650551822737247973699287460
985156561493987675606916277098711601439
751742788160712950459313602598008662223
9142786074632576850680623598199949144820
38945874710625937184309594615796412104
53944409532529486112917380338981807626
683692175752227560638032631192682392379
342680757928756840842617746184586168937
31443531382143713782948787444219031277
103952454829410392994526918379792742778
500145183086605921122124321119275423819
47528332659017745749316316235664925273
765783671739701514788813661820682014466
396379366541029523224926361920215734
171481076290933635257915567268115337149
759443187061259433920340264199672577
4600431990209153579523586463486344452740
2760981150797368325483554192338029808751
960097442470375665452817763628735106638
618912516912301616647357944594169459377
779112098725963101627582462599914481289
33745658258725169906112181719215619705
823214691666136155118184475782438704044
65067643638372214501402942527282205762
294139478054698813454871547692871137200
72024464025611015266450848183469348723
8306094865252481337671044549538076097
370518850712160430681779405249957869982
8059125838147710216913381753326519406213
98018040548448046244666441591071639418
79777894551136488675779261411592016070
7653501767801921286956937185476351952147
5755650732181077795593041188763231481381
4120550828809400810136143160344365779781
4508145530372043733424453080511899877071
893473971488884295546915199611860935621
538340930799471116265447953770748582149
5184297859965562216946153485351376601147
695299228338289229216674895374199115
9858769020905678314361048334416174415615
91491726813783491459009653685457972604
793637495901369015862463425688288655642
6844450185065424703186097935956370584434
40381056163941255928295419548996306568
9291184136487456230498447530531079124753
99922535822270611759912558453234105147
824188184444556446465770282131754108452
2943394494159163642984941398126056163438
7611625773272013960873431032165751118918
707237748466224560121978138880287767035
8979590254246245958828316380439465168552
608196441470947111117192671737120139673
2239918153128944285436853947793389897402
37691604199228264505469792093028905341
96504054708399485505385800859459486117
3926656056235562478068636866883963828752
903612522659712438094382531738066276685
527237234782120725307867833120852507716
43792389891658716553765674118078691067
603671737149981930656542632795742839945
993769496934980221818978650679119899961
93269436085968319506082666936831877997
1934857424122365841449016670407745858991
7405646893166353780927904824124738881282
5078363680388202925675127685608024313623
1863359431983278621413176612329870931013
289475724514701989317714302778276753497
768676056640301682055736396459180967104
55751119989761830761718381759602902
7057263366139752786997292887465740482129
36332584865683293528433792582316104125
2380494116627545913387638843549843376096
121346515963249330747583891399862061142
321539076573207292444386531975962183138
782989913226694495965796385254635283671
8550442541853090815880607774273239544002
7289909689852032557839217699691284993246
9676758861246928752426823335729950164792
6901299757267826356324203263519103766
6715736989492648377618439645135263305287
730045558858840051002653376529560749534
232557783313895886954733954565633451192
1233876759677358760739947975793760892912
700642082880335589626783871817343499824
445122459286259864274300647396714245971
3415483673565613570794103748775069988291
4821288429656582479843122734597894767348
698763314710905566355554458546555557512
4017962171642435429614856008455771995797
3953662637956971628816457059163187717383
46353182516363194781633242971900854741
5842340418657317162698761742166080762817
613669406714772697179333534722369101834
5215885837826594745505932893830066303
3070593781455127500952702919894759572283
163031636989356601693292549349121497
1864360383317989632540421399254651213529
268517053229893118935427273922152462056
601842281280355691835616270772261661151
813256462960913941316664219935664862463
658528480396312384124274688564915964629
20355251749472832119847128635349838752
29779564337969768649964045892761879876
9170914251692079415613778005341064232224
10947595979341184952869624353782544103
8902857763421362609727556049510342599423
4505293765054995994367160412351539530
6895979443586280057288135810480576469
234714029443533325191718851735875351213
2310266612278794555967342786450990069916
432084960392527593021525404645569088
416799235390860417515872508151410824688
792991354020102132604156848259282682041
1518895775186464331947989306359968815857
52844330412164959509914385392761576745
66991434940692922232383227989595809203
7227343382924288128957077457312248578975
8699272726816139322021758287335169087653
621361859883727161359045384358322335862
738922479819714363835387501064123826328
6970779565346826390949659435961227947262
64001029171915215690791462453479089334
9153314863887062723688727431842761433800
60173131619914367343149290424394562589
53624944939873144070654203829169154172
2844201585969201927416805835595693418838
1939110779321353514081752095198161474986
9014147625566362268579415735504030767646
5153146727051577672245411186884626085
5611393899643273204266465643824310819523
7236284095639947252895942506913716844472
3426751855251616705852723880610385935856
5815694897905488348875276400258759073889
6809117585117981615123979207855334516429
5508165075711232325375676994381365774270
9232591370738021769931520230381841349
6309820312132325791548502463511476104
10000924340406675155655847838256887319963
525213893206522493151287928335912769078
34412341879488069492121694161322305530
10696244030450622198389158361761705076
29124651950426386089034260900667183454
93211024872158036115751657594101244213
44585769449469164219289234727120521473
778930284551260846060881550196314454424
3406449782711591664041299190531382237340
7130979911395643254292016395976974684596
550049222431244864365944318970347711430
38915047950612786117443730744334924770
7300909181097729358363263375100792484031
46311627967122778052834733257833876638
7296804633930341690954099617791896377
412157204428226423904388639960708093
946526653205657169525336183184385891061
391216115386660979920594252426782178452
890855577149811223133843234698297079807
3951640272091199193022678976383188719368
79346182590337958669954856546621864866
6148314519483798478032838554565162521034
875919014761552543240215995532739729
330847958664505158457739327896118299296
4116192306630018621408289784412792793
7261902859891818812085509996762410293159
2188640680553528834088858629448522729811
469331122983617455472768635351462014369
9190372561043670216239858537988629952648
260683023757681584950874665598036763422
486660648269912651017984635825372778384
66622735208758662606328999823124555822
8027401514339364506656427588241647935
6636510856704561455246367416677331547311
9149109448579569941810546960726592395021
8583615688913869151754281635633726434
6854306733219903554619896272689196177
822968422556736897424948884974899766355
6565241270758134256686582996927171034947
7887243624339131383662762477187688376266
3326972762417439920955797007490723998642
93839807562880118170744166123770688301
192313894537703418356711167883505444948
339561974285960239666872879705046145106
776316219874466938235234601406095408125
13785544591301431678969998440468078137
69233586800486385594790970733604207821
510650942165772681190581604483389115646
778510195458825017232386646392921894385
8459995884883992491648119858804533838238
4754599198423414698630044564792290373519
1671101722763871569853507270735044916079
966795508805508189786341133648628918811
255655134798235789660122524672044269095
510937664633126850882566287731911206519
1653518433815063105657616052704182063452
89966405136252208063594229455347554365
25112286778617817538136882766388856293
9265769467168363146784787920982959859485
759691351395690896127563987756750218008
104688328504321637666666760332459013221
835386722912598280872108186691657997851
762363320127280435477937654677641275003
8405859410876934998082775057382139320
56888985137357695471979123541752508646
77498579717537657046868392637296204639
5289400888145110572918862880159057336492
876369039384447885001349142714743109764
354487409886883872541211141505918548762
9661891276192026791164152132871249323833
215118273270787997643183362012201462415
29098424374920299355464436198844567673
415834221512370960439318794579234081955
7202309054261082861331233651982050583890
707123183028901278672890634781014708338
153915263604405905036864795759638212522
5526614996674441311581799755068764069
9442755178334072387859043707514712588
324038365084343185151850498184219335031
4816121431337893927073258394373882181839
88117794406721494191807344549336248253
35375350234802755693689528526917731563
20769317566714118705394286739255029432
1682152268037587331937624959536943709798
141942684243175338775821684270655469385
690310882293117912591808820758963381
150881777539103443927378352386064741001
81901988841252912289179821752527321642
480983905229902874425658881130949027347
534787748197639355653408122387816442917
3076583027017469973737154476123169828
169689481519447961247088750195759745023
17174835183636666474775442545028035289
55816030698296850927322755266867310
554082932151738092024251403667921259939
171347061165872637846600185904474688481
5535954701924418227936144609313935682
245885525746701878346052938365066731755
4722582792792776603034746910606940873921
460935731445384296817952656209018014097
76357481265038076605162491764114298426
71099467608713781666423944083094786982
5650597481357244276377606384827046658030
187180296478409829569265920458237694040
2214425642775685378016697200883057897731
318672808882480532498571365916997598435
848059348823238947025974883550637581145
590231509912406979871817812386524840302
6541120082305828297559947582825097835428
3542835870527538836256183521329362419683
758228301467853680395494867357371952597
782116934647855874698055442434591735156
6118144032632607834164712857932815363900
232273156142621279972753711538721877188
260133582927498179307400424233453006092
331468607487159999073167436741470996318
3124461445876114545834763878920646236246
2333910590293047634595783212474833546
2425239150832298709749202221747613597639
4623973989143440903034356566433924594215
34711313422458647282498787583351367515
16854765571204721117959039832947756751
7004306761681190805714876081925222962312
98345826408838343027369665123733546070
8705308241538596665351352595496232796475
7529900935242916336551250105374135676
97229311151082599986924111066761358519
818216879071365969917975097989016239383
5225891038965904810741446466363596806
5447617651913692188960775567508131314040
4501872486923603165428333612193848918702
3906377678306344243554874277762230626705
9622273285713156464687159000775762458819
7206453611766903235761169271747537562134
868450239395438214786796925464693192390
449121124718210201271762909135696521155
7460935567717943702056198291996553615576
6195565518558618108791821671140069928219
8040672960992117573544648750539146065031
818751330307622521166946025413726543002
5910735012264058399076246753052827916
4025549072335769133070392236623089854080
669721379510327984428513483158167431
7135629348479523557376497426857672606686
256163217126926704894967457443645950
19684615838382429659289701703375221293
89431615852073295071805631395599992976
91695409101379187000461161338174167397
2929886830757727524739959375801473071178
2036315186244691480974871998238858330
346470206448527739164805599431767207737
3791997390754654283328513610368031141
2780879855107372714487475072482426682317
5241345359727330482498225416231348572574
9295364352578967321434236490820057533049
74967763941363228693692268626455213515
6335712782127384285818355425536792134020
1168302231591298274321262541605397377
270065763634792595567783880261975608987
671942813635839163674679430892927092526
255735549649861045614632383673022509563
39186229898360694864921192487433257913
695679751353679214631921784860520624407
6958300181211888687438538913225735553175
9599431629507191666330681653187773112382
541819674211570587504725818821248457334
1838506670614241493868445748478615304750
535149553577415151849516673093606468855
92811219731029361468615976651620299891
363999196165399962543235568890979955660
5884178433957434304767803236321553043853
96263756525452051610343415272535281506
4716516862276122620515486515610552711
94738533851457427776645201715466224869
42152295460342976658667643988392987540
269254776156363429341276404202649107453
927368338015767330948368368824923431540
8307752653865801604518346496882728344761
1250707514673091227036381644737124173217
2152761749459159545144367265494550865173
6821420639545371237912378692920828755947
20796038227321691066567730443656048254
695357775147587856146584158312411603362
609988828959827698881164110352374793899
551371528834518744013375808356762108539
801353325158086725233026946147714401849
9154884593436117582473882456326147209037
12102352551667226596269171678559921790
6730954130643591462229852350258260611665
433952244912934010483050700434317591979
626434352462102060785751725513411665390
94944929286144379699488932353729713234
6964878930674772400430834307964893276515
6129422299149629182913005783058115277
116543538637949743073435711573397186899
5356691597382035820927937601493793861958
4085694330034048910318785655205713661260
90766656652189283616836866849359016224
4658537259362759783096472943253428313985
809283248093498875978704826276295548856
1951360453581571039547415920188766134
8776747256696956148476631126769279951813
54554113918536071321406375573113343769
2715669365927221362257434088513108429
4561830499117383432569753735772131599502
160370392145866759946156311861414915349
811923545907790956042766788014670263701
1888379819871623470393435076743482039
839471687065302264768132928511355747853
81232858335310582496818602022164584972
484354964468564414824150873596249632748
997360452025910680988538100782640646718
9419271943441816319117454757286448365939
9347915331085464957582724106297999428579
2088677523074137794758389311233291841
1687868989291118794232537370199527776225
51877699632430610183471663596295359018
983994571779443743567093994359592384280
233711862924289880462961573545245007174
3029439062126661333668213871033614349
689719136297343411444324045854017187285
4644852110000445582282438743654117277608
6947997187008843291734859087308229416756
245491522259855319829780630381169205974
4819181278814787831322448916177671718
693781027974522363559354139726547115136
2944235596292282468363688223926134255065
795075997889615647793878517549560345006
396952526059531474573466860186518463113
48903163501634961439309277731196161358
331607025734074536663332213588019748919
9551147022664240305612637404767538911582
664313536982428615012761341248539891126
248190226163622797032683691442863433070
52513343330812742028340375788826571702
3087924256636412478251471792584162301664
465744724585971622323683206627003811999
546295619220355154121492127663738231785
3615693662918575178071144305249777659604
5627761326754327832261359885391563
849244334281881857301615875234132004790
54983047672356694220427159709892019955
4227395733366307756281201398701431985
172714848483945530028415418705874346452
602794485639864365775607886328848844352
2539185078985207950910663153236829917
2218245472328285199874878792253826225
41823496682690161876409475074542492537
631519352490444634311998264222831927060
725161546477243652797318652127289709
713860994456663345283916258833585535718
370227226003984033026714583831218356325
6489563967121388193565296793642022224599
4796301419065883859820994157300583173787
93068353917280663768768507759478738090
6211420620477035424531446733590266042113
5364889785044105392422121790237546432009
66928165991916997585993960925791664734
471255562553506633562710576890609462694
7923690398541730301712962445855463896523
2618474380453711884241955067686345167157
5663710957229074156850146954315846565085
85863884525563296985047462778675059
919828096867860594587352687890123175772
3028138018178786214984131999784882883654
603930018117277794514423343773238167122
6473125033141097968794607709226986108706
1053127547151760684425206550449226407687
442511677908556313184277270434073624003
1269930629875377638321819791892621934081
1092274226548772259835425887230276019296
247352688353663558821631793632978488490
917221423511169318579964593624071758222
450662808966698216822264583802612791836
49169698658358610848079419953449337612
8519250054834656983150959312749315409408
4904671136202977548326057467330856069664
8563126180723418724795821377341222505846
2176176442049903106840424262637833545673
853823568106925092424565953236930643725
7301054935151667614872101059678395766
2243336063124722386057919277804936903438
341189125025762726011518620466258989629
9162984826578349405995699719920184781967
493816314242205638321239296230821165636
5894408125794056375525286247023419477
9190952233541458304288757248674119523190
7981804224976728595257667569752827503309
2188570862602432434543354275592214494673
5871510910533572333532849313720681234443
796269837292367459397654820579185829923
692997308724802777009870131058549482
57613621579999503396469104816049201526
1557450525105428583857927208771510494403
784860966531626346898737165970831974809
403299481719889301587824791210238203
6615437888757039305041869590528873638146
971310053845569135782313773892422667280
600687964002684616177448776444859462322
1841576798144094797315682878411884058346
238079838022473363388908275896887539016
777522687639497215939008602500769358421
2389104719671470402397525292360541195668
433279232301001966434209892120382317150
6602917054182800262485693266156095639914
647879038466544857131283244966913096313
6208572675323025884683979525327198668
6041907246514327845435941941486854901538
43732567980999377248717471469378030388
8221571442915822061059424087498181605
447798524001205642316263536888645881969
674537923293550677260741770531019417365
1134864246064055463238831402739569923641
885140462524247499883299117167535228730
4876335221081272740379506853796465769041
44412406606442094085655431453069648187
5966778662766338710116853321389743698946
516959494464955550028104939313086037303
382517890042034822452409564784847599970
60203022566986771395343233231949161856
2428559478946628780651392663405135093229
18502816620132883445374447922555905168
11054716911433309561465558408436516898
5993209417503504793915610216250281674
7604526613403074865956543153769744504021
4911480770027256713128467117348070173406
614145545153217902786679098955679661857
21137406310897084178359334288751363547
213552226292201547475913373802040138293
151990478034915179579527718592685166187
140492758553177931037397216133062715373
40469520943117249747531684790776207929
6266720531575106163165791310132225975272
98563050137739077568819459379265171761
990219182487896821290558278401255682704
178064935885210436624635974334927767127
173167005561257273138738041897775612486
96842936799210295383682078585253424440
166763946185420685598578673775111332090
601489585999663371962956093782935143591
5658832744062283776114122189380147821292
5626423889562693986499377119544365204062
115476669231278604357568046531089988259
5892260351088618507552902173682199288929
92896361302840952235691873462983577242
629778811116567959231666692808950456409
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]