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
4291852483668931993424732595199061074589
99545653466198211410789510401689409674
77117125544411211529263338173772526854
190202639083422540635058305160580984533
752628048040605259627398304935142007885
70073767529802944181923092900011197199
8902568812779091463562478003726523895216
79088142137687256746679411632093116967
972718456561921041499391041382874922820
9313720951247291647210289518156241759272
5826064229144269443271790552229254449
252389288037355437947371160741067464831
58807212271606045356105618174324542479
4746793698776249621084894188178254050
52861085945363690015891567951512727287
1591781987468476134814573037554934167998
377020909440719169135376496953234759769
7118287599202489327398422411465203739
2280137535363503931643791279678488842166
615966704553687767598107243859750998887
25332115990593498698259441606800215152
768623481888390840089476823359465197510
2783367533438504523684058311877445260
6418895044192283816619606875932615721844
6940772311473719862724415855105213908489
1271435368997142795744171757925596182407
16621923253210116445665589240733209863
67677050445198115133993221278550821615
643253402814311584931966997969963909153
84042398206934998164575519103911801337
6181586711019819668986504614819494172193
6851595648172311990048026951118080584990
4728645245621594632235921886194268358270
448893672335681845350355365336655158
67255482832148789871621706516209941724
34832441818413779664944045225840204
28279842916653115048906977373513253157
1430567897482790486172704243642931291254
752997586970501375036817509938779806340
3559277036145682772164736224523431718835
409286434279933847266885185147743649399
28646219677699209505236312647674275966
98368833702253580651204135527355009069
56257581732349649167707357521530261535
90789842836162956194887684513439392714
95862389979992344796389180993885086713
3991379514158220282475753506766882189780
389231546064485858592462695554811994776
851341936959475743493306834828224510424
684019820578295526594894697130450201491
13026367804819165247777250870126795361
305747586546134663739707087344911493604
319059063915991281409067170186514281710
52058697309887105020707862635629447497
797860489503532824718320853518321489607
95204555226842209224237676352092974753
86403324129118311732311264188376994667
111539941755002864497396668372628816727
203636529711492798453396667715288581683
63991954419671490896731802885261224593
939081106776873428731557324191063435
9300503333636493260853147580744393635661
3413507082034057149919789672903494131564
541374465276941176802488872792645954529
429890582402795323358288432819116126245
4771438619664252233015945423158996583551
22650582124643952795766841249170763778
5851360260973311566847363771464832439676
2073611951408897482823685331709523365449
697367156647909125525647303543318063649
503778638605846034892474520794393712285
29754574502728633875317340916804168947
73316487477072140427581600210620612896
9583329365914454712957237556897523369
6092837749051687559810375121948321748323
21716382592592627065749144876433441877
187376348807579434082072450761381322120
8951887616116887722091184251128326090
5435917981446935273063474458645369619272
2259984711748116270297711066886049299
9011164771531657504495827071564096468
9260682918842740460279832790905674219096
646389973742230394911822750671887497972
2340605097521859763141388722394647048378
54480982297216597828298716723559838450
8102273442227747448928574263851864073871
81963959574601921612210165421093359975
359598228440575618767728347544548717560
9156498412332661479326595706376034228753
9608673757449600305080215160182920575987
43801874973119913774664438750207141421
729151156889787436046396372289787878481
5875836826536268617998954747510069629437
5702583354179462429893434724367743352942
954617557399941118514224766259673918840
941066464875064688114634315053967146
8384979434984658506806958662132867013
9757147484377609897690305562559881729505
272291144926860537313998938208368011336
980124698187744231463329786536836393707
546228303002976066481349455796036222314
4344248918739396110774421136853893753785
86907378375609526696766716569181588254
5027349427209180170610764668186110223663
1231112270399709632314176948533752918150
4066347294246777938841578031240773489928
9432105127528060752610235555776031445760
5446689248629174677783278812595263108881
556712849905677492125416696138733014969
87088250927346894698265756265393036680
27514924765252455627021771731621631459
12799592240499191217988867828969961480
604779868278852861761324547267588635997
4865858144461035523077719176285992866292
841737683514667759919721190236738281342
1166273586774191496856388086740186184722
829434073199794760282027406965081802722
511528799523170996231060247843772805585
507018719705786186959573646808971725693
72388967778563882668774464587175703704
4728495720791499908711215858866394504560
8816783245568138317269170732546838636
156372406972338322481746899112179455158
178227713407283735923242272567233322679
401630341725368125796925801738155711995
7898362159413703367917967640923216561703
6701467826963487543466042149498352973338
3236324782068124590156022868254694075824
53061223510135136827512621122936198567
62414398105322011818939559858547018707
35421116232954946539502456880474314671
9675673319693290662655972315378531935127
3154795844311228290884072953376289934763
4301142963954276636911489828245223347094
51435244305348007688716196180649599651
8220576270757578772765145850838859088713
4116443396178155729454437264560176459537
868199583409921689250283660461217038258
7775332841217771145394877883652329714767
1571274496502045498097502849187766119297
9897257055959222868919695065183618605180
6721118673671877451777022777152676409770
913431133867664112914459368689133791502
7187863844622994133646621542229462374
778314726121331539154283657934459903715
625599848515623714168036703671496413809
5962447017527154367036425050236638078996
27318005110416527931497023247849929820
11471885045935187779884559871074716386
482366422916180747464932598514019459231
810213217183236280137541074976250222906
504813185428580959843256248846661641158
5812551652566141564545595699803983404422
4246723876387338800912367245202365339505
442054954880527578418326283915553154064
7208883688159695314782841439224255603126
64112944919429521816151786789974396745
672265789352961771330147419673154817
4629164722184098259691154554982020022157
5080585350012606342129132813707876092771
4322683855503403476680352253661439919291
6754563524413367290326412444280970678460
708741424351620779670314517188594752714
6279136610212562423433653252291757755048
6507911233433393227266336009711324599707
48345556962948196177976976733442651946
760288731941831459549694503235236297498
8994445487896277623443141458991842289800
2733988718198729372780706472661831133273
131267488246976787865727932516396005102
65078181741118213007219318428963338177
9833716589936920237394185024360881819286
1355705036207930245556421318994161314120
7568896241038491988361243325844892533506
648758834643270555479201820352822987617
868320928532901130118699322755150381925
3425379103830641378403864753170629134
42320538117170166087449594229484806693
887176921661857842616079586590248912892
256271926923221222148091153481599702222
214934048723102108121345682137426427755
27023333451265781810697945921231588284
67694693626909936642490583588219297838
7948472359293667689119459850439278761412
69039513833333285798425856881653207212
66328711663793566921821274585124793507
9151342384713932289361168949628736135278
667694868997930596327348122715956097382
394833383181713538679118798338726784657
2430729761954303986266496292528049077
1507820136586152431768811981717925598205
4004259772299451266587804425748988537013
5941904828084233181580513470651862935459
7387863360552602645521819425849134476464
69345258318264771299191836373456696169
8906843588275582951188184272472560687672
969433752814968745073464333136736160253
89534957353532573381954497992371110
397346169514837685054547840970874407376
188431842405304785215058862450663862966
3656550213929468216556739546646818077716
4985226785074292333351451118206247799464
6445229713018252532150427954830873896311
533136448223808698281942132725975756928
67312646815494562347132610794646406156
9247419775794035185436887822434060134348
57229059582275097520762632231126726486
6672294736766508787681932260735027615587
7223331441262544889914263684229472488924
764063843828732774062209832676854276330
8781777435195621258826831886602734013191
6549659121015031257929047347725377514914
46976244524465721312235816690088472
895339947642276135038318685622595616580
2754270601564828662543363754665391863
18235242123358591625242557670670935678
92018248433615599634535283723763158960
9266946820834477621872763856276622269796
709396213521081724263539347791134677459
3386710575614951407883411984230581538311
2069250520613671389573786418371439896
26481437578361834247226247676641759117
399431414848851523801627539571022209740
923954087844907728474613849779323515822
390495998364999861025436207157063557466
48238944884374117062226527416509906504
288839488061660259889103548111247263776
62718194589283680947957927492663258789
4689791164778241546113412711168723074874
517312444794677274124818972501968713600
84029967611827781616601528643184463521
4944870099967031666920539577241725307745
46887940498280281247158897828773856565
670769654878793018873015285403887141
2444522666325793854411901154296519085453
8341903921672820954736646831259469187381
540871743455746839653225850254979599489
31155125585410705626751119307541354274
245480205008240773716681719282054467716
1425366543272718432716671957152047970
4792529545047389291439358447744770832909
973245915696438614281663147579652728
495238225955189727473476169825482139840
7871585313288374335036461840584786696081
2451989625722812753534075038995928453330
74617701674493591754694136285299708350
2526679826951554627837721867672412318788
225990107324477021952221993197251033072
90831569787396116115719791132156698
5694469638326186483085441118932266654
13584531109486420310771603978848706941
44261029458353690171065393628141865312
1264216180627729139518119963438294097428
4573580470213603959299824581161053594936
1444824156853547719313912204635641804997
191717849692701533298572582240499283459
3982873893624129951215691688521457462488
748656617167411148529119074730154225227
83073808557771583805951790076136812781
7347505384992273293646639355551746771173
474935579178152554439358455668138425077
1695325414694761755013285000314969217706
6203295166134234725806805280575164853
43815781474662430090493257753099501784
5558399965167174406065618842661819162928
85644381461674528566179445189696089320
3606371348369334587614388527916435721
406896061657837125595679712775849033857
31208015648820260537563316481897065865
8772825472547772503958524393269272864453
871464892855367837424656217563158961706
87469825043705974775798655473624825804
518274192523529906514845898275343275868
653397184745208920532912911999583078991
99729493286901326334025849633755234948
200832018333161170582306644990665702910
176759804085306480568259152856066014006
2914314888114727542127313006957427452206
838343366433138653311327751768338764409
13486034542714116749604645863173948935
753332673825590529023326366610474017448
129820049686934950997538866641750448395
311070405416438171256532395135832007351
7512835664621929424358994670986157569469
8474865484258921758060749314367754969434
6870109299427668792343961018344686151705
148467985792905284936721115582570877417
6779820242873765243410244712729672538518
94966745596754579696438318269225063581
412414589882488974847196544708260208224
264434771177847337516723108514533368524
332769901915238579959843670266328408032
1025826024724464577997779202789356488108
33453776718145123360467248580216566724
150968994047277737361493614227938849771
297576083824909954089278195539293547483
34347995514969364557573999172578085579
528285219562149671892161721840952433168
1604414057242165530874155154340797221935
269483003815705025427618675250827922107
81645061346695275150472594633744826628
5760772951649781467027877922336291044245
8718914837123334657372494980943216178248
4656186810429796977746657095305110256362
710113494717519272461178267531693444059
7080971554816929752446877872528454176216
12802439385091419534366994813655105066
891472561337789255738953139146587109764
1123810661527832117064306489141251273029
3129908538435401887455264532357096922214
86141817605608104938892977453185683163
30313885158585456163917112543453891687
8331304761052624630394179032583134276022
8270123695715494329882655518281073001
16512827725354448240383077118639994311
94739951753724664480733976803299304296
1629376172326012710773457431236062402
146248610567131864817191335155374548915
40728429116793491741493710764944287699
470169292407405811543618944520134749770
714977686985439463958506471911154289
236771931276716581195703870159556445043
19967616194836731825685628262222104660
1258498469184546378972716587685870153365
698460354602764915224229498281631694850
418645293137771713742342732529088689874
820768210572659451580885571359317961817
5526556723112939117682662189509528216042
371075275585321940641287364267069324679
849569849612480546471982687464313212989
102714289121702212623352432398618972203
275834784691052261996102512553140371340
743688732937163292786311704597053115612
1412364026809797934780858142885544909738
28827677890892043945592335842266885148
8864216511775332359724322777478383163788
8166836355887425877217654709756891863620
93664161425375135202322884752648585874
4758681332704273726372444429485384212035
914453612533845158649200182998924473764
595646301207809505522177692729095106
8451303177122160617768696623673026541062
6115126178752705509563144056322089921975
555970176872831693768897098258576591689
4174332641855613836113329117158660532582
1014507842469501184896961211712999146793
4070933851044588179162961552456064061
4735772213959402150755474436396954518907
607545818327193869278529335362491794657
798306698016722142451629642359156098835
167368652280387317985442123804016547729
5864501890652319947572442868203574425281
87491990817910005844561149719833497134
660551886014196146619811622940128904122
7352493617752616702419809306789063162602
5409952888105796944939378503114377671005
26644233702630928423583181330457689621
64469552213284184958745456089584673150
1203118087476039894619507552273538501941
8910132276313466303075096736714273085009
26216767378165072769770017185141498
78547278121426030631683726425724928101
8519156469321110496345555243184920247602
8841940578874508648724413099960171213564
658684133703978275608772498523777597497
918993513490206534116019963544497907988
342545909749178342601707037949890277150
4304896265528638552389487965613477992429
34099920619956559519627545148780289003
7791920873518705772500094971481137202
825994196598423988795121992924539074
71219909000615394069464172205440091033
82041340330062825942591576428521164782
2524145827272228507436558345452327285
230627293733623025419698540659742997094
370873851853272456617351013560922436165
285450124394014112566792976826327843533
35174482495813017055621533445376249495
8021991929213192782345802428670253311266
4588924145495213977967157144461575476961
8196460291441427747160707993483489332411
7647677515378491685794448489796351082
42392740734380454327543190544779723688
3932744546641620667590734278539158812738
925083147078690020056065392842286006774
7875714466167854269361461487547170951756
51013668500770082158871890663358601438
115436998731819284029155738767902768336
937514013167339676952981989882858939688
143370503474697288877988879186920315713
138037271223232142197069972810688648982
2932212424669701227430466058700510325402
632533917963948518946794451949369722222
5268235738902127531359292605675910755254
8493107413653490881872279689714691261410
123484222657147478831104562824716646744
9347567231446579556280921433686946958346
3122839699607205228733794183218755273546
2870974467617034617119053759122075830
7824852421717809676468908030575496157629
349855616711531447867777222189861437
11818620544479814251513118324766374095
8074615643694261887429653588173826726921
2634205772124945163572096938620312946635
365417728174120409241179182961143787053
60212974695099668667027292211184745514
12784187290527467590794845024481654315
447452582358487658024438477168592835
213628255893932538437976794758544086452
6623881455854670818969965617924490285130
53063711616925827816895648656094817466
794038816810160440559472287894468505567
477619358920841181233035951160182266080
1045212599176356711962179280385652286031
6040116373949770610587484536328216426289
396274653058020699587365782839154718484
316459498026759498491383730673465272803
488810242206171570410265174650888652207
563064222366569124936015138699074296974
1845193281938525766036894906145930071099
398525672225941831774023777058079862796
9271647085068412871912269908617139731266
4822814954219554997188986035993518537318
3975806145288863345779979966298395369846
5170877868360486775932313177970136473
9279530294231540303255888295211362777502
52057786546694974469448878629091008561
3268820684755915853017136578988064105607
7920579327135904867308671768768713200
4614689495966252283491855216557184037746
151042286633627998283961710818511018822
111077865735133029567169831051982101357
770864042656015332833443021374365857207
39571457436976091191976810679402981886
681522273871764917892641267870939758471
4924418727901407411926773879200189475179
639373743045868140061193971335631344
643687035816992943006132853317391146805
326580682967906031483989664411165531185
1818858289805134476428516725504878285962
3870805896143738678447258667385136480
560831738470839214818759839149451769607
5154253376931923745730141844659521891821
333220076886393210952631398468948675
870422588424718349352656707802169978071
7542340755087554524163604900523476748549
22642439127770435388363084299638274846
672958307371803800835224616700156823827
90286863480014876378663799416629352877
169064756296233865726226104124843951934
711538046693893495891766786810556436725
1579465160729849426730997288803349527
6000870247009616313848988645490017946563
326908497269099956995888040166710894486
37629314579408291729098646307668062282
34726962801157948179323070318457344328
82002206968686779683108228071124197489
3968865073399048102537655581960019187189
2826696332172212889739921828411566186731
6827926619269972787362782511551247367203
138221573529747472554811876799431216919
5690422965341279754442437154403659818838
7077793693121882164767183794581909672
806945229130996570319443977027902120479
2995753953758204449652696364626597604639
7801943025859248599065008772139822477149
404329069183868186072707358811179239411
2549914347656264815548935194855763472020
625026733718942531737054821308317687035
924957516554484287695398405362654776537
3615210079582154192649961799611937419869
97187504449984710537942717842714776229
8221769804346809256490278205224195442
727413883402151176768071265134417062013
483974643261775976395422823506669059862
271296641934200260519796953818874948511
3018481254232592155926221388964095736730
402619849209389980940106312533968721630
773221066355306442823465222683953325436
6987238221421771130242422241558744753050
464926616658885245626323786211626631955
999835033147858132761015969159072978053
286471883075287066355561143333136296703
901599937338751641712983851621288514792
224578731005157114629006570386139595469
86413694786636892231239312659382372152
89817273237988473995503720691233928122
2671598773153902207133911583438597344817
552993852820196169015844664542597954507
8783525258764471998737888521918865392056
153340841250174811411222516772925628944
42433668495079986735893363954880005486
7747977937499081448755466708687746315579
4562190014369623942742336147220193375549
1905761077917927817821735824841863317340
5890948156373188351574117973285165935190
935649975763143024049983617721430958012
1039616472449178835211843504526275461717
83170429838880754597141196469526032551
88487578354445280122068429677010953577
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]