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
82179877918432994778736180167156468726
36907759679309915725560171614220679408
1025681359505139384551552836584417026075
7501545547429734755969740110264081356
8274881635169501600633922092470981941517
8172518171192809576529874688515133319212
2146272477398525926015371350871166586144
7197329144101012109137087690262564783518
660592061891525994096106493455832207769
123184656878474351913576447830170367731
4297674836687120334821686224313764393057
5579331650055587693010511136688161758
5136224185214922140730080348354234703
2526505243785843894367276804495915906
35108263481190576385754975572280772348
496223305402701695072991402465995931606
1197762663588888313717955423186380186995
7552703494382534685092687840518071361780
952678439731668544355876419705242421545
4940138624414698514845482348597629843
11684881621664757581972212856746173036
96796081985824778087380978380386928247
8652968042836298734417197425667495834258
773064222190249628381187281964489388530
673058927362499175830492594345094828460
1435897357401868822299131386886730865538
9189286677343164722150718647695949656808
913735208114763676623156599526387766574
1151693305442286161237571743857722260
552557041187352971753564546646521808
163039056183236521565764717395227986178
598286576832181288071541050888052166274
71660249476143667231812483375555249
201861045639133467464224524690472091218
2051594043467456999736818135531071582796
988818902806650487507647344279388565375
6785944621937639861489061354945151191437
48214914401931050704792861690039537295
2187486082462626798246926440571255418004
926655612956382643571684757185028781311
8358984229295513873750823071611604244
5545390553062793937698831971640947808961
9086688490867950441010638956927035027862
5341414178977714294952184296846691253505
752738897368855089698532442565993323805
5631903239213364899911094265355321698851
2483443086011337664325806369881194833377
320413191999651717447652382435357475601
54852947525382456251133245896435402083
35931860443253222010988298473889792858
21523519857921181000024328975574837444327
6021688835574236012949257798405037247
729580757318916351706278761539034701460
6961905243032127825217739138554085645790
4252648815488477158023998010295527802274
16638933297340790793138038431871498807
906887336547026314885215164319230437169
4360266421356161489712515838518643933208
64231972334888553791695352234014908192
795664777365933749892653327543714804076
781856396385288992611554116359799174070
93295759745753102326556783240216908407
698898634184183341749758785279441964873
6182277948945718845221258218661967498
6833572829288487882614735867756735339563
91921569310463091571830360998833119339
1019489985933369378485169760362598811
8827564671742823746190282279841913090
44903294908092222499540350805758134103
9447963945539561226268931865172024436593
912130399515523634874794993529892071878
432717695975761697275412202284626951818
6070816414781961489540814858776713826644
5270181969016958401219721255718849382735
7931407252619110634588148651145446259269
16181244261537719974312185188018965963
512281457544954497582155203476962542339
4982239730646989728968911776940578438664
5082111789321339845387172687912911882135
26561298128379738056187483380764695530
54020687831990508498359016111688494109
679854746416435590099068209492640983308
9142568766499558737971399156213176416271
88560803286486494316135608693518951543
1373431140569899652260281841492636371418
3676469255272316331672858380631626402756
205762745921334710787434302118995571957
726749565248103776186903162440432301321
826510000956353261586060199354515031209
5887252958979508616525438933301195289346
9418507130572285408538957123252870058058
6452610669685987842181122900364055317232
300463409227622929699625959332147826807
8595532760959274346428196495231418055165
6002971411934108173028546700276889458603
373121871173118380831379616825977391781
196490215287628773388607116179824692885
584844008221586606020947122811830515541
3080275353269466582540364520791458424396
951059493111657733794729951154524954956
7863902352785424976189764450905855203496
12859207310663689703251865860209081852
37113021657590195259927306855612307727
696534997493316799121427280941818214921
720482051793476633126191211728744367976
350344943522711582917744776841924020934
5306719675084596450245303803497034148149
168123424879051580768248459857279776607
7358453272803634579259368025632068278294
6558711313818501408520805361521847726942
2027448769061884563821114695627054398684
4308552327304391693883511185722544416168
2733418261504665440855001585653677037647
3197237461884254175311268485214429392476
91435598086330074506308216254458727047
8973947693783392907951103384839928552960
8348518494475754536342185780708527707497
48281674586685761831393120896906643906
24926480491359354769005224496219093659
61269059829715854356317911125040299228
8872765921003183279973056007132562883105
332359884255639361271240893763440034802
9255823754826944411374631569383272780
825545892030857357162161945281988207366
6104556339907516781688199500362368282540
265619327240613436794583335820414887624
1354166511394631103818349119863699471
9273330484579932939246639868450108606
109130957549943568245241155953958239447
39888819840839327736472938669230669921
10460337155999736948147472577487674999
86718552302515829455141770940917859836
4375513315949486776129011020728724784825
81437672632267543562544216744047778149
5323323820254076662396516771410991491653
5185762092842251949840124376329925502463
96556933522100745323441327352684026113
6264803869639287888613133736114587394067
13671746280650001706816279881564875884
72453897861028945118515850715701599785
79967488305650821791699649851192945843
1554186243956838770647204988702187942646
489528931974332145261083390154459476136
61019839796289118567967237024871131845
416935417217713504373859840626566873779
267695918309141421992285739642976083394
99399403575928913972897769567090939045
5327657875878094955548481249397538462308
5189295583337538722728999387482847309044
4406914169849943384675161918937717378594
5084465018702267202269373589687915256746
877174232292802085782976665407726893763
423685744289163382431133793169982696433
9092402593749397914985047279622278313139
557858186322133581802353976785386531376
591559828040141735058351054178290262588
895018205230154289327309798896159718604
25943126308599213327279723973015696107
8786809067905085708220559196566076168596
908283545109345043278925450127668028565
7738998380643484208511269048613066193
5320899641318622307843591992993147269
7675803183039541509624099625643988775108
52075756130877045286179311357638733186
67657071887526241826712405111384316667
718951210132836984912588456527132859213
7063252468484874738544997213712153097442
463761519188683643217672548237439865334
1873651335412811725637911866648299839389
821466001455579455596533945461549045664
9801971584046295399956909869215056569292
4821770112353813285625193343579610737580
7038738491647101253064051715412785232742
1040829465728728916190963839425337459987
815628678523621539134891719951923816767
50951131456715598962547555997856953790
485674872348934899076376770355243886557
9900511960946653716252146754277245411088
941850642921223972794993683676846283467
83917394341171733301595995237343493734
516925132406353168238926656865066969797
9263128026806226462088133893346112643274
7664682177128032571594226237998939597347
4224669245692431665934068100871261624060
1647653830327224862779873931816660207989
339254232907947195584329565419479639
827503411919526134839047155558529835465
69910374724568326866481879253187732620
2738974135305484866541193399993008990
994864645153795555340146005620737762408
61848813616227585792933658286827761419
5489868384142335841992469376324620899002
1850836159105007977782955777653280294569
175454678665765199668893397999455237326
36659920311190328971359481784932883331
71464700642513941981699903538761756524
25868076688855872227376962334559175724
4395539149745494762354014926396831603883
7757705479154907825819885454736697572051
4257499296806898413996204627109774463914
652789748142338764465980695680249295562
98827697335589729707653682559336903272
1463717182193772506544144546793732558251
797765638482370293792605272122912128162
9766768034789852579018027464587585329917
7442129763976237533188028169493249061277
876210185974085927469555645311485085953
3889632924023958461599992604458295327853
37425326245174287223004995659773112952
6149292448901303901868176416262759703000
372712940811739353336024855318439427164
777984217505742419178167436416117833387
42798023646183211209371811835404367
41363712531479977456014781193578855936
3253908381772364183433195920974693544
224299433257496918126228597492559985991
3247926792462923627866129616505134107124
652523224829603893062661473519470671202
9858465940953295705847192120319473516654
55465167885924892333497804543885391767
877685128589739558854801293507537105025
511649441573479844142689402048568096841
1424556761853276455130274444973326639511
5134563010622457421015786742379386266209
930779998948564945112239205766067758815
9435749872097001261912949799414839567842
8270728796564012826830868087847480567330
5356718671199944139761724830189966861874
64833175499967180145554452831376876
3042601286851481349073709678929429926126
4998741874758149198845863668466223889
840462302642375580072816200610434461123
920717485989356531323487631519782055807
33274213112430069758677310967596671010
181748393806636337585619791160599971305
57951888612959492964698160543689852958
7982296189799254257992405370340498573301
6128123712474315743421812701247610135131
1829745978080980791012819299771881797
9221339197912513687560254675817982239
49346475697660533337084539528975885007
1879920293841589218013929334138161468215
72348469135512372016001228345380777060
4355578416019658695863523495706205605
4533844516605223663739571722497285469275
384447441270850526754544629834331019152
5962357677378032896976035986260114192869
196851206686414659255691037464852415497
8649337955763133158113279199877450729494
89306236952791553383257326949958246304
486367429302031228875252106182241596770
561571369795188319050776301257560797456
81478840185858774715933232203457059346
118097937776460750816213796490679519139
6153516311031994918764018044669090983731
5956990459766440266282392102990737863985
1407706085755014163395213990679533389504
226517944765982109280782734821691047060
8545431056472272161423109850163666769059
772836492111151435587784393262879323432
412448387132380633242585366875330466406
7469704925242684199242895077624059145530
9378222672929526251090015920417080518732
9871491451588449990414489602121444794680
6855145855628506806351963948168739614
7095718142688222940999112962370350727517
8297430710721998868038921892922872816896
124311406785780456063156343334864662539
629992823431809231568308563805826432063
241519416268236132845236434420917068879
186365911437228897428299137302648872625
1045715492338465860171024792297610944004
47164215205214528165158524824668241554
223845896594201189207737732168785377421
495723264581560120490988106525055531982
6065957769545161218185335962813048061243
1780511364908769356081963613721724076524
993335514082369111497404755620352983922
867263584116637355426847380401640934925
39414985639177405142864234187605324
94141565609435652758011886225224703078
916911244257133990953855081657079916896
120148982351661598786323907926721924
9694936838705587486871508972401288803620
7069218021939659709576758135548797052382
6661197522998946349368566687320556901335
52738476227163912806614217871865516969
23136386345939280810303945717999046042
337360238887328719402616696257981295828
694694092709821830098709302143577054853
658217015591889325131616398044751552218
923873034549394860833161808306694188838
32305185176389769935516274897353768734
7965341566602877782326821698212696284238
77094272409437269391528496444257081270
899080408534119358675241485762927517579
3885806269584826669353571229841665019207
7156845875314740722586506723354590881157
34451098541829810724724162872342657306
654775313831621699371443758839639802457
60999348824953325696421270565985676856
229085301122385471676204402257236644149
9544426317416440949944723435608583995716
44051273642252015061804725754875029612
6138422592334306170577957839819048432189
790854365112368375733063735171896625762
2722941354132482418744464271179314224
791040138318584590265811676415592081034
6138725439264890725557414948773283545692
411126773392335030857553976841841098316
52103113013167165452804949689762429482
844660554904128966104737485324867299
301753642482459850796215143645864391596
64195089113168418842555733632708088729
13932591313202857267066052220943625810
4003774261672066662821612683407221197392
220184725597315332524535789284319717014
8784697735804329195139064931396997186837
7014265149771065159413042279603838578771
35534026996443329710321057354625330304
844459163863913935585846831785669934849
85608548872731573330404535106256783330
4366592943188236319924304760867165472791
430116703037839580586727773175476704378
977876094442557195332563719238499499430
4537896668548366719380906134481785972514
73404673512513076466463177917364479194
934410911803208864325542432459172732175
7077872539856231868172616250402629211466
927123289682394731451965624114358120909
80899166460535671183314042420594982841
6722999529426428602863921862774932165425
124685347990585616078171862649211435211
2306652752571394486064466091221942065020
31622510811125067741155786453378956908
66547958378261396411697131328026405549
6855615169334810224898268786369955419333
70021785521661576655022055868454489120
3187644994955732104587101688715872297047
5132316040327017158884685833682093734992
4678516976208451882091182285539841498815
5732898351355812106560790899677591834
5407372969439568415752290994527219941
790999338617129592189915059598266728
9104740428689041727465209633861216711968
790939507785623355693042924877460246444
164981246843948747780121475914676461
6980118910359505469547117138441068157250
71783804820865419669297133535192844996
58507872685274819211677511767733307788
9569778822569911652320679700746433762241
2724698895207344961116563266551951248341
550778886848828958846295160234230803338
7063456540392056419717344874391073768655
2010226817005302276851331688814086846225
371275592351592271579744625380141358027
854178882211142065535832520869510786836
183750185993430508886641200557460834554
932910755463659765782921353621553566
3384194512738781195865060933345287410
5417516616127379764646869038766051226055
8865509790971091164069712243503728785605
339156327137553338043141671744468434899
47062094307034430453715387283098506337
1715835955847711041181530989238722426
2851464682982806464167043588101699041074
6687932155428536725514006021992251099280
727226391586881452137535845778210442378
848517248638112433613495147632023531983
4402182825355931311217963554338254775607
3638993697627812299677117762887967626604
190455135828083140632273357634983607668
319481738804192251436032901685944251385
16788599747935535622001021555993737044
90647789715236554349126725097250734501
44548838302582383189271391690121888542
483440661695365700717004562138311851
79537987751323376871365334227711697832
284263949429864480533989227845696308658
63668828493269114656215633574417454664
5616689165831562546043879008268319935486
497253257772185522384901422693226653879
591432348098306986919458850304025024309
235740067767066497372659289406546636189
822048903516330893391994225984641516302
323456242281111359211133750228727147246
867036139222780244383159239774626866600
404096879391804747120899817193350911692
9565709412287375672090785067298059544894
2530595968039737865584976497203095843719
631372062585306840753968062580444175584
16519934544532576044839399721016713083
623825011772584182032656741704177231036
137620054828776089913069354409257753792
544343305756342889882427642709768365925
9445737798225070545377669362208082875829
2044470055499150931195232098773027033094
9166132515918290485998296272956843462705
6042675499663659413717068153507282332161
6351430081162606155184765930599734643071
66556432675959344819578915644416785544
841523712712751882411915857447017221201
34432826422130705110301034355687491502
969848847422555255649152844624114787765
1149929501526475190509773175382186301
4603812720512512705799490394663933689
3907414079411917514223994567511884871
972025616248735484154026257712047385424
5150994564703236821528471428998442604025
28977801654193650944953599289208800125
9273953696529462885650503640181968606
854441815471456781293634627375218543896
3268623493036660101583113078803821242720
266696507977171791697115948751194432845
85897018638478219089754784460083337920
6586484926592440523845803440940379561484
741044292870921745268118509837536866229
1131782288251056406137575322814737144639
775820892414150296814303781742794768676
592980044137346785871707733340519028212
2567859728415498384589927952880943288064
2370770848602445190988013772185384843058
907233519357998648979941200344214431704
6487457975932800960775305245440530018134
611965311542143384091719431430916162150
518244472781242217683738299548793648503
518336405463849297072289629284759116752
411476873997686915455437203779060226765
7262957591776325404771178064271318621187
95721608200092142975542731362677348990
771170176290908973940156782374333998791
825271606471182166771718801328151786040
609884642386390903961614503253737996088
216555284014149540968573380423119442060
87561889210719561443428206898994519563
716311274876423384825355714677014766
559431828135956442434651477465290467327
1983462965923347653675889666166018414787
15217933677258518321832331016271959065
8900475742293232827756894004533567229
73009117690188736816489770312847619164
874930476839787641679649750203967176286
1575969795838454226418505037409190224907
450691928425528592763889171758957405977
5962497426509570986369632546769681624843
1513354389612327187692362050304581462219
549712432246491328619744679573065688607
5911330343594496673812354738996239405250
8686412761613641685761734465967574516069
492027773676222319781166262801776063785
721392536253439974534577062145459112078
9320653273444802370335365678396351348176
961147154539786976015482245295297078442
869418028739617268117523747300060306424
129995344574875323564845427061286173692
15261454828721161390804677313372282
5554811924696621353542403931850170962921
6871896618223147504313945594746012652468
7639563833429170044875020903446894126
103384375202927396851452951333693131941
67514128686623856219728602168662261
971321942601930430507440778352625563600
103880201614102800968828867709747794216
41737980152267737263583958378960301237
25223937792688292715779529617595170260
9748983914467991808799347626418856889644
5728587066928986768764704238501581839189
3150564191808042164311713145743689756819
602778869436419413728067016759749617469
560012747922371962719434539699953792387
2959612170225354574546886862315222986392
3559681241656095115896917179500392325104
141636194605986959602308731971353213630
6802996424113260760234742097711464752874
837588927485893375449681851646514879771
56422954294125322085576267779265394694
8916816542952836837466358098968870573047
77380569073454227229845311234825524
8546896647483225413539209820488458903238
7672153481829125493876699498384544704007
910863136631625193918026925417999395436
81317945601303444182292353123683906623
379254743086101566793315763805319371231
9970787750878208666943023273861732549313
8493932217042509742750519526436333899153
447152369352859375487816892325094391489
586327143930905844976859717454591016066
728283478720665279375776147510764495605
447132652166846239788615259985694067297
79713775866243452425485102087310808385
6299346131416274348511829301652639889032
3272617787829191778380504780231835196717
301215558047252385545641222893166608474
8324233079944182670876187917317372927624
5880992987331403865349867502255765257690
769428174548573728648263136273763163626
5597145322012315695657917920658362243639
69397186797724398917551874543878471778
33988689338525280593142179859661609458
2447929092842857961120314022801038795646
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]