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
71779346360154228104458875380836499516
4590242399311183553835737338222441389551
561444940655231411358561870862779864
576559345036812676249690040525974713
9875965266531960701638547509622528824560
30275674889813899996175276973453105181
6986363275929425141175605445757429778813
3715915749772589789528832008588479143750
15106602555641543842457987107779657683
590621671541246537014502451181169864737
84714324109627518945561931836986547227
7330196641443474864256191799397568483310
810760435312204451028742842155976846542
7362268979666947511976457267945577713906
8924754776078687392249981570486632587950
97064246215856225736947937906131258915
46212006254933179757541569232194603638
499450749276329044809461666591613252100
5265736827014096851146565895744631685706
4087929368791817250849111103826997773
14554963688476945366582265777622571275
958350745897644640021068204268746505965
98989915856650179541005424152637999747
7417290286044228728286914715208175792712
2191354991545646875883084603671866469725
7975192148045109124041981755383314072863
378149570227589333991208764461769914576
511252339407173689031361991026970317675
950917795387314391426595645545008623
33721897923757613066161243555732261880
9689711051472995888958866952917353354872
9674478453039859511422784426580553145457
915972762910463834424281279065959941618
7637228931006914750822905322310939195016
3951123230263623753756843990903532151560
293785886456856943227043794705628757314
157121431280209351995036364024704269326
1424334121638271235613258662390768376030
86014307676917483022359304938231342133
1332481625496755380606985015436703987
3459256258246433336964381195685120296519
5047483437165736325414336423044647721
8800745515615733459282107545766876941707
243694125148152256327028750603486764481
7409860359021051843526548493799887883806
6482354176699990564196817544409531893678
6673139345356737615134924191721931142875
631662873859573549122451547369445959
750150408490370865838906348638561398322
2748295956676660740689191586522968337366
694741585675417841909601280415376898766
119816419563435091734064040450556503084
4140226471161204421289661641910578452660
206647774913124881546837344883486299660
7983932062852782587958387468942473186434
4146640144168487763252603303566120089845
4581282050365565124772726917842099528
239614008260575949924232218491420647234
8527758386455890525852959393385636665090
51089342882211931745105743367913245794
9291436385539036521540116205304878895269
1810806334197575136398678415325583226981
8826887027223177758843827734628768059260
959755491958456382339391712623112583380
92281927151082283349211951362313917169
5149706957778402139330727713881701553
1633724838462551997685751637610189320
3573191776456880379399819243732199831855
215091461102709453754298010419360358271
18604506147837818759575474203248361927
70388158993987462531379847963520461805
7553681080954706910980928687269992968872
540983143397929049236299635102691926527
7635100291154991362132766117836017877737
376575999402588213486194438585615013399
22589899618926511406596445553548629287
4570691812604482905077485174477818616068
80415515199008764694395623895264446514
8191149242854446956228762175826631248
3734574228991467187414865948878873378
739873709811700324603426364204453995969
3693573612871681892855035100112389215799
89933227353316225077679844890574661442
6158988837719012206278472517372222055517
39368969776888465998463433342442728873
8022603138711152622334261892789580912129
71326154603814958718294094974465457054
392922534522827949791148942102828863433
429844271856705417330203595829437251622
158072863251145851029992908616899555897
7146951648602668675774384913154254825310
501145278506249373825425082546077683911
1743121499999767895548957648845894027139
157732607069874425722298761689573791364
670520317300881928053950318941376284523
6136911190907066555593239101576220253546
7320472886761827996735472451543547769663
3563163757436328969364454086521759958828
85567881432597556841433321246995289823
96473932859060927121446162904249324731
8437707126205375283353479916765987953477
19845164786808868098775930789191806091
1946870014354815375910931536195234633198
912131981938365399184532912752863733432
5316952665892751840292349164413416
590875998102413254421348613746624719625
62605439380545567015563884183682686553
7887271736336110237516528144607188646526
324274216665216793938986715850471289241
635015902612523193871147378944810586871
10605853912284575515662880123958948752
8485167980201470150763997949335797627920
435082675682114056873939457918481565407
37731829189781212841817535317461158263
26078347515350262352884528495827963437
9337914126511506618878269341585687269884
656605059176512161629632849632251352304
66330422871132457828418401854545339315
84249823119388355928350195705107912261
67224576959139665311173693361913131856
3307761049644241122208637298110992588
5996419913466815929132976937734565715409
31350886358230470026392519803182295661
594937084764131214093637561358186854567
985788997338388917744541017524225005574
18756731597195752224075270235057777530
455515861175683493855481189833014068247
4319456770097113809473642547717884314662
69638461216895168586891188115654877820
6802738838812939631455665687682021022950
308441362924815542521855153937570332695
7112138150172445627249488133555677563840
820690011950532177214680262664078774254
103023266762944531799919668233810806965
7632921196997961397456871456300193177537
518988509524733586406642394450268888107
326325152653513863069248027247158359150
7877355960418138372840983524922483329
246937927363184591836316168116929736
4098358373851289789265912136968132133389
948765727157188415498820763295847179938
180825428862809943444375550059843849878
9877833394356220781886173856210533651758
692792776497677840822499455224744235426
784857053482412153087712599342221939300
6588659462311922342891115716702788117127
381038224273826416853161646575506378777
8003195130422738111535818889125317209812
4837469668243093307622492355608199773772
4193827998502550373218547977620584739558
495514769298433555603981465626419431522
9461340439358496126242465355499138147816
471844487972815097764607605934194579400
93806365225218919244487350932712453123
2306897892391115726743438419284087266561
81446689890359627299064467850533841872
247186513735580653347595173682131396228
671334673813890954889907780835122187629
939631046645457225105404872805058096386
348537134041497428987677639574467071308
768841879331346545649384208228743724939
279904629604481577514154342202211925164
384649248104750446636867567663396688728
42128369602463947495905330416644039144
199426927399791662287246509509957723917
9625820352081508743361049547152526457830
1310545675666627133755697174527033277
8195729578858907857610821466574983872424
3444416337334946888042572980612410697542
922995484417232623645786973574284519540
2595943975913232391487582971610066329052
317230478744810745240381943937088578397
5707863143702357811784643318354196472234
3290321428821962577232339138580564721591
76726880654495441450627492766188757235
6304511889912693246284392419598358093209
5377238150535869224973014929120569092
5983868747217447487438186377700778473723
7092189488746138938859847984540581151
84217419204030995693467126425871034287
3431945945806905121463092946723747923268
752898683049571743904864905581835167504
1528611592772460595531038953165928007
5163686619071493682129322145272575326384
806993949760387356658435556930914275754
50974448569313106167632260885292733483
22991603497420335759144768594951393846
7790131939456481029766960527280463155
724281468443436371736234927203684363508
2080614922626657891728924786665202451
275179584317154621349085969213801533740
3096801196849392831369433072251811389100
7517510680926640910024581386719197677126
5953394455962134497672044362178051042680
277821418237484566517278217786577674969
973771857063556689114692777115834077527
484625039837234832211993552548554614845
16971059582066592975139388277774261584
9170476072831924919761366015643444158760
5312213498113855993375375696182749723716
4769350574154025943781842962546994199476
718973380767705978831287381467919689
1165556573041773606519337382853556441
81113287342734665189766140718936964183
5982024852455431009829988327399855882
7558567779878041590989887813938223436194
812672881802909185137612850607493217906
446855967606343305861807951549723585558
97704606807135664465568236861109349127
67313697809335908596228984854676167393
17378875491236915415904975737133265657
4149996590996908617311978952529728346
280026436833954596751812522703813997421
244612593191013348995232157994523600
8561728616915866257872659488867340463204
3720609773064240582585394815374580957235
28092086338419557065547472809510644524
9406186336936848292518005904226559518174
7187441029357350113951861099885418794420
315782507932555879067002365548302507838
2331995268985874583085673839144002278
30911946546322974440705285732512125719
391048218983868281489395526350739479423
4486714637266361685678218058475894617
428628666926324195379823620639795996682
2729935281375903297364283568818797042996
357994094732105054817350183152250862154
7336144336335421700996703251977660976675
3087909069856977790467107141650138728745
1936259426251653509546323125851881173587
9997287989017990150345677435456045023535
798181507445892697868823013803230807889
2085354736677521861292964749797631875376
44824155510965629534855951213067493439
70768251412842103636115570775627368251
294130898021231580244464384162990512007
9882673744788249567538645193731425065278
3607240389992995184095801751442228299680
753336454641910669439416284810714083938
19357701955375512116503200269689719859
4959206017803293841552278789176945074446
2804127329332659140541281987749180775958
826384437200895489004853949583802347521
69007378527994286597274298628141997756
391369518926265889135821351345367257623
2207593316523415549145069745851630332660
2915685844827417682063123284677791755982
5705936433694931807460853483353373226083
607223898837460556993925553489909447
8515129298694196771822981758625141288889
8391327445187571714010754924793922096309
777530003230536441005898452748136915635
8319556094433438843593013989373657826264
420344465382517985617811776924523998259
9892135690923052680050731907721586404404
39440211506308653971241636923997208315
4369257949888346546850201393605852039063
379546726006390806820350308071235754
30369908055851475662181178993065813230
9811977673629889873738105909894145926304
287332868057648046078707762652628555284
85353490529746988076694559940258366542
711847879633917742307538612636517762765
696248112073072610950062349776822973970
52601571247122830765019563185995582311
200942012509995222113405040629941042414
703576729072646841589407605217836774533
576238548347176777286177316850317187
14088423487725636418174846916382448590
8296666245195911422169453121885337571056
4522752743193284873671135153474473158834
196881033117285147248391483665819282224
1258982737691187352762232315475199434611
642488413988930060866836966579578165493
3211247431527634270729081035707275839240
6000401745428555330897391600206292625054
1645732222939033615140637407651599527617
779224671681828206983019884623038597776
96130964644053143953147290819224927681
6814675931179649582954919137155560746474
5635253129845331803931774252794017122547
56091150637393134723593752957289333660
22358081536999318545494249886656604787
293595630396892370223318725815646178294
138997311374369513171719342557238823136
5896064714743211589429759813573015340
738011445267246365947519650755516789866
1295263063987950880996193321222588186260
86506427697563462794366648570753462832
895757161255924980719549066648195638149
9977982224156923818952390926756113223
7501425825825685556996791081539097412849
643290361812672990355802840373972432865
5211985931456937570665708162145448983096
5851813585063886548938982831425582872369
577580043726444194255026687370431408524
250977120809641863685692986409887125370
540933999967863141971162487310010684111
269488935322630596988692739191637927947
737312213368759981141776856901033173478
1639346372377958131446224639259842352340
631240006016371379937390934649116327579
9320281412227239122016887969343761732492
8436778821842102541576792641939213601338
16776947613770219102473514247317596729
1937563327956311262486827929464745477385
6009947994155643404740487555419359571578
858597458337161985734298517346349867633
4733705019823321371815982332747110248441
189850243164172173652641862668085001817
9786262307775297393198670243179238049
808686854854603860333898285979066742704
4614574436417051118093657243948237519212
7631916225202675127112986417915022063087
5537372585751387329685306874651724416
4313421073651286511349123347654781473481
527606982732859302275684915982185765515
99194420764501480553606100769654766559
5913366058363127570737066632213872002
9571361939552040106720848450807787389901
2744142678687115539763541251728435995199
530086129902740561360388343585180769035
600611792733014761921714154677142341982
6354627969286797753331187430184086922647
7383785481732610893875147067487528426412
3915983390244869556565793874959709686
885367012985828744823367306926990097840
70335306675890102643701031291871404716
654188608286721794243728126981191167494
14677774587640921948303822387895019966
773738491588162409185459741308059208494
3319772674142308490462923342181511195898
6244290720176312638170012149844437495
458988677830947420922225489483382882285
2290651752631942224799764958780297844234
816821087029611019311652524938487147347
5048684170456751365969609884911592643483
1013968246332602324324469288575643801773
716815286892277110733561741476665898699
60408145340313447013181835880827546668
40406355174334651254392718648142305253
6165635948203828459218822841422309090
943331914261849720610094196593653209858
2782265420159326627783907437789155248189
6133386799596941359640701435697394776790
887416499699110566953156909661938968692
8393444631383433607873841999376146023891
584011484596819757426449582913345086
651948569037702908480917655195534615957
914250258585942456746276128879412873415
1953114865195508356926323609234198384270
36377789563161129674555228753982595462
392423557828858534149195275993163858607
5819550776124766261927156887177887285494
7097106211868714421268512853367766619584
427681708812356934140401581982197031236
130530208051756733615589235944429157693
687595693990671222733233739481163945662
762919616493600961195768286397082177809
3862848518399614998667867885310236839064
834960165907382245092797401039411014802
8143168796736806339495842283922145741210
6592244078144732455425531355123796908829
426219279031209414338158484505942818138
6621947827605433124397585765454184229332
858559041825002197174301155270132938133
7468281412871525248562229376953285352494
5336935610206615955177683467473442201
3800102563793377212366244177757324855433
8063785817285390125750808508516784371790
126628824492736782619131621515464214281
5550920933072878481443363911320094254925
119542602990807732066143964235712884685
84383913553223842555142441132120273056
163358659990361541799682985510629283488
477586561217118561592504589028708870
3099794929330539371240549043232251561
9341141268408437243513409372279245987555
798294043848609963295507733646460228397
8895859183764549483495689822887419079
335554336262814623955250602493494852715
428370709271819818956443950100730782042
6126163630735280571081617206667719166906
6980805064801204493369168723175622584
4280761898987242959407364169143756922
623284451134223635962495486734277245395
728565828831691533862736376360364757883
9562657887943675698125002286925827135364
4534756075163092232619657391904837417585
161115836878783856176265534521740227996
654738326668427891824728906313863015215
843328961087155021549423336399068431171
1702457772331195518680640503277262939
630245573287049622264145530742418358805
3722151368412733028770891195581487450
694866817359357331822167423185954487589
74146435967561730175504301705332884864
6663251396062511834869567955109878193
134686666313095662760061625621636628728
5544295186877083598141081184651456353509
2317447642684312456864331525421962732955
852148591461638670328232642183816412882
6151328178357168174744761805365654219537
18248940338727357253262398712116830944
377976684373320129386872200773010298603
61231359374093858142537255141925001810
8955753786072034212781855104731150766572
1201538321345513137896526054763535195989
3210938920013798291946204006980420821570
28926293761165986026376725699050372811
213580775926320454074262385454236051794
1643953689328086513818917299822741978810
195092117966173615153096892180567422232
943516611625500596187403567944632437657
633914378160471670804429653299352718755
8835250142817022130061258519229516915955
582494127317586834146510981678312227561
992851194438304714411723887849679409222
5667193158819785798584229090237326238656
82581056164808269227138971558036424853
2987302633853400990865119058151943751732
1606119684042393375627481076112945822568
5464415374819935609249438358990725457005
44436084625111110809033994470854051341
38752898716394614590870385935482251867
2401473091991626756069598508420698886942
224424704374401687569345510789974514957
458941828531511441395433116326260859666
2933926595074312923673373908813137180
53795269965836113916471143106637028267
26687879071862040755874773349976363774
16987808236161516768370897836911069098
837893235873493637489287718758106569378
8201436771285422157154382591181473357279
133483666760431190782659215982093971814
56364492803323431628078781743565715017
9277767983612027012265335897593229747
152352609030684087618607652682685623090
997567207200225531826951797214910679636
295859262931461367879652140951752320
269890942616473658167768921704140615131
86795854262358276317083955813036745901
31494118148871872881710989790946456563
8521615642766872832979063994621536373929
11496056016553396982389298374834065245
47537776401744178855169094705158211090
59217510432930756330362959214796816049
42916930357492714193480354041644335488
235674111886103761575903809520495503981
973457322844336890231719265820233944247
8804120355798623516054043675785510546985
42992373145839645376627860822684641001
2855678151001095670922582783272418525825
522833942752826918323857233612784005543
1816238962558280514924247238198797738891
53165918953475223927946519607148837758
857330428470406278374444727609668853889
208971764310260666892563724779077612657
538484023298799239818056156475310542842
904777414847124973203568424935956047489
2794594116176198877038125867139419977921
1553861772657360863131109201482620034873
741355775541843939906917798887496207
441080598587694805042612514583184036065
6239214891311755153057788109494558032841
3339646527378146579037476913978412135775
4706877147555944725741044497636419495275
9824763957248831837375655329692478319043
4302614246299375633585772436376084617008
35836938837955304742815127372291015313
608899538693563795901784154529286302827
6759618955303954943233933314803642814659
3882120145424706244663735300900438736580
7897207183517494625537076169659858789980
92846564754950195989241329691946138896
71859224457255116893562898817280358600
34569522765215331025346280309794238641
338914443936531168357912821773005090381
599661585683731396882872490327799655597
2994774491654415142887669665715803701
1280547063855859293454503317218139961158
33049818188166748799418933928471238919
45642796950119020742152613796931546615
2819276571918311274941998334733374333174
7241291852764513936920432642343829621
79815474480269584801685320013565027511
85583367389198062520818619059908385684
15438152935720198973805073899511315959
24457381516057234614797930279595391678
674376944876481527626479355813220046112
354188531012526695779946215786045473603
31799321422976783147655185135864744530
83135294190401347423064923384513357831
6538803925846399579057932646670024454666
546341325112405775070398141288210276050
7539540547783532824463439223781371442016
4054263772669976476956305922556610604870
506247636437565096462161078588178499580
38049232690914508070373118873436033513
530016968630532288434843336549413637921
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]