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
2973270833263886780420289827301979461598
67763229927139290220734140419427192669
7426380074258419836315565381258389626003
836699417767269470279479632162106229372
2287206562234052456726464274368716571587
229891984197471829124595202544147676447
42914106515241031017696930934763272324
7461576632765496851492552934968955627794
745734709654681270831874220998971835738
8727995098785613606873807005818233273624
293423487366949736269688957159232322235
6310129667181085751571751367071364528
76914326150251081389374552670244102211
701651278566803578358395550617555875315
833678163707519879429067498882263248083
2634766782676781963346556769737913518944
1556545728404327532111266874170838604984
165866508505961714621463702648667834916
5242906168711615535241042666115784749802
2038822321175544546488001414128173638439
6638781323014991991965332836998825627
69846162966073405572051145182981674012
45669051266284651325435241542442291894
65449380156568227254470866268290449373
44660425410333941179246602836469841495
91252466931226329297617350834417017769
4980350662655992867574606853941785719826
7215361793120531169584769716913194934
98067960505648948824495670128292584652
310621881326691433675115518393739419913
8553700952561038142033802520796840843430
3046533433056605106480566039574790011542
7655556383060109873833512652680582967
629091816006563096186594844371982237853
4219685132483407929820117023223883856997
219439473466657670549993493269569115547
97171219525607592813432342311995125523
5060670746065740278211929299179660818285
835833259412990166675913649385035671538
9219864379902585802951340239535415639
34154228434220116654097036557363932608
49417007751740955975205375425482494670
5601801022877578665559026426850557763774
133911041525759182854102764764038958063
231096392678453872173131520763413721310
421712791935287574004204447798560748141
564050358155330773161945323976298317864
622231184582676390510095616488479607312
359657368064144179959355945230559017684
8476475720459222935133566460317441461605
4158869837695774839760164855915618194782
8034240433682702246779587170246871917501
505619914438169615437563614540297125328
4173229549093967413376043109119079962048
9458238572669997839534074918514790255520
911838703301954978587374782907860506045
68165859210035134128698635390890037496
818530045716296868392040958460339653450
8785997857638383249012728121318739263459
7356489560369986625921159883464864418991
2955700225394219259928581615907920942125
720278987662832176589737363770990111929
1563190123306324273841512163285432346989
7746566421378153857676386621354981614
44741147113116564941456971067041134486
5543549575985445287436148356590732289351
933066774465994855851816571292243358981
6089742492177878763045138924985329107314
9975120520254507916850039480942324867355
570148658777856978137842507736685389403
42761643604617115712308653736519747962
214272046471113081902020303324667994600
460237187697695667862419413371584342745
763623757801982570739453825579117201490
927241656671486075497767192063215952504
328990591605599173548302688575739862550
4087169981863662102124846825720232411737
33134994772752831314865224181132015385
586085806835494861405145899424922966166
260693939573419471749265648256421441237
824849576109767083307313782714211587375
252866941183122639015620275805183642873
277245008527972436965578492804728892194
554032279627530841488797355792852938607
4978174923498708835988474841161193674808
76522012746440159007277320071991731548
6689534869806766734892237012540995851214
8803946176201169253870722710487846631293
3501981753527448592890118911100075273689
99878357332816532553056788520033492402
526350404263852815314402494610925841212
215614035114065553767582691544849775884
3180508890495869212856747764182736391
18691987785152771176765120141483564017
252415513602522470447264865368073647379
913195926919618529731254500022239182801
580224462670318264475165916742921627515
2961507182961215885962214059527951573540
538378836129958455432962320245433399000
578256193578548758174775462989656796
703464827444629179122545910722264181998
5304513253676681146160362970980547731861
353615708742800013469521854519632892162
8395561824306064441482122679223289507898
2070578397787587148973886736157221294730
289752128189562449348753848298185465620
2369506632437783495084330085465111229
3372437532732607696540884922568667971631
39566682984474089509508095341126339116
158631019319281596291172281634674228415
74879669664188645273135288330963668195
121376466309193788172728739399181768978
843913241927423766849763902048537113897
422792718870828613676387940959881074354
325716252167182313263142731666867064135
7254122330597959765495567693672179671527
7115784924818222606380546034720656937555
613599783575637427544101426508410367634
4708782179962865881283244633769013269421
95763536938829826651676165719199203972
22996901736491616291178290426480808017
52296168825020884727324942581430679604
10147879987729274092243503873903243235
8046388186433500422778845780546857488014
9923775678581690191713212731628292165789
2859222660908643760613824346833545121350
597580326037568497552334987871236040
571585353676205676241106731028438724994
80685451488769207525651747795558106603
893663639621964210957963244568323918190
98416904048752445740947679639426627
8276142049132169381773519280838098149774
3684200596054951291482281635157686145986
171952214571652187369447463460560584640
4183971116569345176367913267611271343173
82067063281448780665495563288565773858
4606698952736023163185894135393576681176
3960907812945768549067782547239764631
99889050484977126997432482561061314032
1861215183537815600749854104824815346800
3486534414418790120031108381828884509708
87124225367564237301732854543579249363
468428839273182751929564422876046709388
8878873854542508281629728916459146732249
12467531326975460423538475624560207183
4260609457219148463419407817211839169387
120627833356508342611599983449925275444
62625909129804788572968020155711857127
102757355216397173301165743108072575067
710772729015864464511717280245961851669
944641502592574195149071094472998637626
4078870136031183120435756523689632172182
15595570538496793963446800987278904795
2320926286407036677653714754626395793495
798231054491111775328836300286577766004
64772639421537955529801757683538228949
84349952426332766747193230667173164868
97409255636645802437274955936578974264
8032125163529617533135909141349395508480
9645596519565211863665478677496342195134
2736939089264074640971481739465934697083
91767235817822349991148701613115551756
8491115516045068213084142867594326602818
323663726626470794115166931390241748678
5589548625422592143216103769495947782272
37326124832890633884240218742152605007
62316235162864930944286063963643233517
86826367233849114298147937839455474704
833172691863278613198674464773210618134
3523787564672781135720797393963819351904
6320645370126333794389491663751785528286
3106349383117335365115715206983578549690
80305311964221729629491122997071735535
1931147063026960306122204455806585277780
619410898554489963522158134565298168909
87095373392067976727736175672116039930
74539876437756614283728056921645621605
5438637488633152452883698462382034579758
1734654626154775294571515578157141198601
9154373326047953954770895607287759736649
461922793172109694354704650834127373
7306232532946606482260141875980713657455
96099056201767840805006875439994306963
55243466697029916527514911948488397836
9448172932887579449338456369749131210
67466948430645460292166795564129854461
80015742165380281546811995212368364115
8615638223295935337987704385661358176428
528390832858706814947159593916828737
211541012929392479165071108285852692371
3411771858943749288791668780518726398840
1585849512536147921746977238645899258
307558247493895102131638509409865992666
2673944814403924683820741418613244374756
648578975183468382867768495671231715435
99304300600165692202314578190343973700
664262416054448598912261232851069183644
809686283989868602268753653508517662348
4911701671817682799426065054996932991921
4312545202454731436906792980178241438
971951973549595760733518151043996913825
595322919850855014548028342490803839019
8927697819831206913321429876283158977246
9267203379103935543611362310430337055432
880973040391612891587511575515576552145
22173681117085263317752906822546105207
22563232309835651046775478289988440608
40397578120485674706476267110062142195
829388533371118720548204562348475409237
20335548686700029212563415635704509774
38025707252413100829796032735657288556
3632784682466273227315480327739453999
802932619466039152338803132729947957932
9290712934103898127754223889694170332610
2754146530095792450215760806538528583
942323276142679937795613090631708567
324469205432798857998410647193679218118
876181983357088590178459732953780902725
9755667191905307457158014138876410793353
87647753376118706381396237936708956882
2550724964659753383937941648248071005
119694626761520831677485933269649304891
7662685504274573101161606621816068551
180457804639340515091348521522443440319
426247308087483847622291795024878143792
4613389872488997635141782727302253628213
630820721831625450057486100339986455308
96612177000384254197846485798434448706
7752333173748859167918601586260232481231
3948731322187933188619715930233344729582
38533513478366348698915654973616878
7346644079880162756757422241227012183
5894205555353351348050585380356874931502
921963904583478125415751968489822958499
5052562550595205202929999542677225085417
1907217748566668624321991107630293669699
25499372636766503730597289747630543315
552534788378161346732463931291862876802
225183125581186918044469743429037922003
6498546130113782395580626773172120683
542378491415204173944641379168192086303
304991361639161828801476514132883856461
10145440168071313423481743682284019032
938542516642039727595571678389090267136
510333644843161252152314685137764636198
14553667774577236368396985833914683200
924685957022428460063001767211978203780
9373972643395359922029338901277999479548
6939727223197110435454207539144678226732
93478766393113184553127910535618124827
3368334562601451672639875791602436891490
38542832349887171168530012326975562046
442939396858194272053635646772986539925
617345832154614641688954862243126177026
20212145148295735164329366399448831092
49638437565284553546638290452984343995
760493632273734561164329900431521066637
648697017671648992724803115797017586382
51102654388915289931118240169725142508
8181576326825335599313223126544982317609
638038178455745552896634440429627233914
8965684962927012685347736505360803155
33328547655384543315252654864539148542
56491323814375075497671766163831759992
15541677479098107937562115550332965833
642343229614760858875988532156221123823
793923585598844074397826772414071594939
3304222075213646505120137926413947796701
8177370219997266544691421277874877863264
40466099562468196712723287479715827631
38975787559328973986411731921548248559
14596544987699078244134645147986558781
1933855179645945351025045884644844604425
533970209129745294405288218779085109
3782435010769561435475287534764840572699
6603436863414698486300158194920974632
629756025653747333484545620160566658581
84956169126286435712538908352429619457
4331821257568424121561813136654213972773
6710383527252309189343033999616674989
3011106471204831169381967279503859066179
3707736744002140738963133914846921235477
486733569474745826507980399438716462
37075370666308148552505119192186928616
19979856788522654910089746654833353089
78867563605269549534779471396232155431
248632415635287152962279491490260193948
2313846592573304479275585816779995913149
107573774070998768804977439674098302384
220990384311907174802085243319139552052
15442826588195956954164184058955597388
99782254815407228748180825280463586167
421457521863117614027306469129970331373
3372907121093298292379869251198119234592
17850401248893933287501360868177062270
1683214739485552765320626915149793749533
176490628124744966687268250878080067199
136155604353487705627629566965783727
3577671427178907364973805292608653228199
789276919001770385665798322117777286448
1019148193072411687674337088222029526639
5548838773464841410348854939778646479309
33829241740612530371415890978629144630
3290282277965349178170454006259480705715
590718944204271751336052905864092869171
41922195672485336482256634130282947656
62424208562737429907255361731637379250
2638226870823535884289023484566168129108
6497743674192643291982371337283183706791
1511749589869166428686603002757744833074
951772664877999175833227961484591873001
390486856661349074332114922483842522833
6566155482687265568787541403165035418518
56817727210926239267031649549171983147
508598887574600848872313250337578924195
4567351161336359192734485805128173139871
96305968824359785648553871763033475470
7740198235474511836765073281912681825510
12190249934700028151071116925877959410
1778255137339285695067378336538453814803
1905183761726340739685617476128988642301
8947767940009786945877912895898190068657
622187454228337732435058645838693805
4735678057754094389515571665511758446026
6995217953115744969225181819545070909448
798835134387147437078206796130887215219
709634228529477432378142595313454443
444894575565394963121382897515463623960
1201169081387992340692452404185245839602
5090906538726448659742493983562341659708
81494694720897244083206805558686255273
65739295297789374836215413661314173554
7056440829445419509889597793773475628167
24214076464958444347436338357304344919
3108656788837248899633355059502919211916
2485469988955108938497063868334988986159
7448456348918612225799593587792428758397
727579559668383846449615298270675325428
375336138778816461294017989687066119070
9154634718164722525741125447666353547305
6403868182999547801921349967143160462401
85324350258914735307452557152279840283
1343866258795163780781895724769664954857
8882308247098735797127758905751642096532
2639842979501377929825161869571315062408
95685893662676226116251717695791888928
14456056625167495124024536737997237278
4361357603459624683458769186369951688
5790980684672442948964313834634610893612
8837238219993649365722717098715566027181
113627227660432529153425567798229144450
995977152527423073707629318588622466824
427042461310771956892933547304358238906
218518641183242359271259838427385905731
8991403919109349279141244648749079952810
3059184840175537967561515521586519708968
3972707320363650532499216601081089148
8699282695071332205116276924963742948409
543292646870768610533805135466457739297
655339069947720234231531320955624018938
574234909133857862030956411233359961039
2039600591709451624596183447900148809473
203923945605795263259768729300463866354
88775988738997289360056275392667441790
47489491478381486195053113846067918218
115798631074117247712799354848598041721
8381860159305877812769857812721117621314
8152269622287863515353272404554434429745
283495856091688724721098475083147323
826653195055171820252055288475611198434
333413527413904491551108156105429489395
753277452680653873302085105819137604155
2015793741899285336573444920830047825617
510311705635513211261419930165849343528
825348359664234567170229665912795103255
450653858217396920240139841447252367807
708375647724970728647583486826362775676
460254517560672830554936243350079666977
3314226056932486918089415514339877104486
8904285424566249957898463775883850187346
1229149846156083937132998671112254239160
6217305749196306726236969294743572428536
6518420898605426284780008153408231118383
914290196536708887735924264617110701731
8421879065945055355649939528934089321660
7589742064071424229591223931350838534377
13554084336739371474725398751226541467
8510884021768233656363594101290911721792
7350937285648235416185453749219266876943
6838861882218606585041146624794512579149
500855407723658769012829629842991378494
2267463694217159464459215838433092496
165371019386626364499688104516253759610
921330092417474865558213081981746758
50789321295090395045158249133945285973
779436958380954368597241062793420776541
6695193047402897460759534701880447995247
6924986460811667318577394493558751017590
2821254527337946617254751214627546730
5934194669705419856694764553648089598985
920872858784506243033115842653757613872
54193849319713212775563820509771814008
58981660434955201101898158577618927590
107854847056985653252401664290693836831
73938233890232911564545603719713991135
2000470812878209675836752834507944333841
403416459608116157293386938968354871692
4795394517687457944567467000259650707954
31153540448285585262913987451547034947
169826096168713760436232470617582731184
608798832169123793476461641484747729174
713572264725324274868827382536558572
624344655131468552319087880862392984933
7875432338651118135497318427393478442820
969762215262208554075921013449175379738
941537625892835923616027236372089599202
3984413589479635396220814888764612059873
7548821678784782199090295964979828949008
12359969483860207831981154737763538266
596164279777407777427578379347591207751
470577699649462176739927901195891982423
9204997969836386509157189987869343085489
617040447387845230556439325481497796444
5761550295856840424697427842709198270
4971831869769169967621933385687030585777
957146575549286940331984745191358959349
585174389711329410534431986552967439397
3658293767307726278073742770410131833366
3075776039779798923050529457740844578394
186192633526311193215588214951689666395
695143684465308034951717151167324624
1301970758006641457758171070151314193650
1584327926583095822026911703571782422638
556553313221134410790601084911842994713
841432713236844865474509748051861189388
65379926576757757072876983733277684564
2005315735987464774610395169617595341332
797410336783909483523695538414993620144
75069897394577709214537542968532722833
49605322956357705036492314655620377
94259248945663036897496777248616785901
37421811734936754522957612491489641806
4765969792253461826240573880916779436565
198379726188072545731967895298126241
30283037490735054343741342663235463906
3528429051591168369791819726425492478531
66059681633255181164267170257883488520
4536330630526387565673782556158658447641
60939366172529655474985926440471495320
7127593095908774779186023630393698107284
985142454647225769634495447281088997810
3502727637159743559636512738401613489637
49446002125163078480732218635214918715
8035839171253218233520743354776153965575
233154375026670795226715504792587167
50589659767391993471468725226624507237
375671184429139056594219782242042734380
364265417355479632041382963337159059228
97266640367060836776789450163151962738
6945666163537492419875298753585419592243
8984768123534382774487564365841066483016
71269491963107848822493778526073441967
77545693594623583511280915268908701548
85065254639430817716788236418146455407
72587279446994927181317003271616591703
8147649926004035524765658313637619649731
72459014248454189763575791815263714057
328482367297905744249636281968313728822
678578638007995525515718865952435689769
1387707264154012477476269962546124927216
1113361157917392775033599432634251697482
340860767396790583541496420604220026974
413443627490724064168405224992044273275
8356637998166331123759423734787260789912
163089231476506698760177404733092558559
8422918548151101358754587413511984109327
13624888207303406064843368877944531432
89905154934850655754797718920092223432
29107402918499551052646493546575718565
7820104589312431604271351400878327163921
5962426082488681536015697658829348305
289439365811843319586546198979088253092
256639596561784855816445980246326993705
505264105669977616863590468689978007130
796056381745712453007484547973822664087
920653532197161488464889939816253601294
5123352448559441651567442125394787437268
1139991173659720565231402529483045595441
35308147908665575629757193505549703610
93479812593049492552749484514428075603
75591288010224513782155736061614614156
2676171920105034948047305425244996021917
6199651550437098572775625713988436642334
9272592980529411350817026124147910888113
2346253809059383900370641108027281775
5068243477969662106294892639269052061043
9656789369559234826321795938880085659366
703541072871595732993450848746988300727
4599992020411167561821877753236793238141
599285137051989455211282922479328718979
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]