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
81167434846982486303772469361937838698
1612512416918760528354661516206178426171
87685514887425019652885186717170274127
376679042337657478088686141420535037062
729996765711702052048513870531868291448
5208244624835365212223541799700724172761
712441877968941547961086422764260904642
609167273474875893689801786475614347754
34303857592881602264785970814476462648
80514524393321165884038625010219764374
5740910044546255801979803422165985285296
26918612480997948224497234834359367629
821058163383828455415223639602996282714
32076617602287801043959280989599472623
513643967290191640578305736594922087539
338082402497139146728054458883676526958
6228885429632283649783387480161235727030
180644531664124672441478838306560105866
25956145146256543624742361372715528857
761322231670205864385672344558880393174
977244843987647469246776955734543119735
77103645838885049586098640972585352182
9493826025437506907523655939354877537233
478322637038188481643598237469127384623
847469331612074545817485591943159027732
69304382306860826905633739292837491215
690696044203232679493944610171129875203
458761281500255194946188283321989033205
3578225514898594703283785909751196953220
408144202285966925582836367287806941789
579369276938871058204163936856992723294
51035524279745942732285257352894071
1638796668812858109347012031578264839945
180449791653096676562501992571420881511
8619435885864956358397536403531922402
6249163521898550469333735915655310177164
594491546987473202397854746247961667244
571544046758756131397573145830974778374
5725643551394037451039593699493429886165
3200819298616601448461647480101681664075
96859606446978628553628272287423356175
1405604164518517179815351630699242658367
639997604623155139003769943789547278106
166476114232423848372862681880561167108
629250765117059252010082958212619308039
88433164996588683402038419597891683903
5239295366256594862816241324724822254327
564573207674342499386219896845435318712
7762947235441951531630698155990130692812
100623829694250309086891140954599089117
475875967595961478516247536939888553999
914593262054773203249195490580430889237
5624514720208382845366281173358734667999
725663666787729517444636680389142529739
917970515294461111679034231192639303682
9678801800441601388901216542276907382
9213576728439889714581933430850052267459
370370927682148884421516105128726441163
1476989763874386308814061524634792024716
506738761026837134836050491018818111452
14242228570888098948401592429247308432
7718368094023102143618931129130675235
439180401368942332695340105164969533607
2468813016504734732463484505488767673398
302964817520807253854188256522009186800
874589784094587724433213394932153464276
9635126827186946229875495399662556848773
5815176688821007036992213972852104422
88398652441228202584671516797387997266
11085896233899582342051442480265254558
691270236190728570821688919211558752990
421482917800622022039189783430943718608
82123120940771298329961655546267336542
1409382156417076290652255118613867974596
783419691799283078141922326455514791455
533743787161197932358560588071502145
2054234596429930497889636917489589295358
8138986673133546373359794674763040952977
844130273624847118709794889666273384343
625338557651326816482039543515873913645
4301791670857347533963284756308675564884
396633004771724497594770501977252724320
924838417067984364147686224378918926570
4626492090863910615525072231930514761759
9983692443277135483768099454192385675485
222562890504924629288035331866202741
5366154716544818601933834779916719024988
921968040311135869185397709133997722916
544319435891318696377108025971111515
9708843568845164480719161945822819291408
2670917316225122236587446505602846771316
9748745497451005125317916646340616484856
902168913830718012587213868319218986601
444125493358124087343179605383141361245
3773309248131098992520046658645683247670
7368499444617793942473593756498396567085
5811647254519855714525785678564288360
1081475022633947127425364185296476335043
9513971134505959262940356851696975444170
470211875743609125969576338511133468036
538951801497133152811319354361054522901
4039136633284374600145449744919188537231
175506952515426662420763515894643205
607122441232664698542601229935377264595
951749885819525554031743453436769942555
538074006093347720735573087314077196820
3025962654577480721894523832162348971460
766310408457823655616919501492969034608
9386864021302160287645017385105481464320
18588325303522846553026367371353046032
9991457719183289730997642226117435401736
4961643138713500849889978783853362534479
890772883361816962875259277735376273484
29023648083221572650948236307789287156
2623547119059298188360342257530382124508
3329743374228653383526995642231539555432
868760675491901079264897952389972282
767245079635589395887003462542229318577
904678096873541096444437112924720519058
8976588491075548969511649736784831136497
5091872541356027849639862703587526644092
9478028592155683411665631542824784006
442917477393443364116445879907133087557
47244874944730917697368536132216234606
370833387827037438722809051197848566804
8387931512103069661033284239286386399801
568159779417431670358036927477683189875
1449141644563852206872093973660595763249
8895103839007257599718941737797883659835
241462801096641658883796299921281624443
8586144723301479121867534267777634174578
691689466216410354168802571812725983778
434630177626915817729896062797071105
88981846355218821121357422111188975655
1410181851860377794758657455635004845
9159217470709467113340526953452182217017
382375159699440729691036867967943797780
442070848889540850592262449439046809048
13208710443535509849708287782942947877
422932646391806954079671741177388207446
640973267795000351215409574215031295411
1076653146769218258627917029702855134988
35519118643335128984173864591474569894
78417121499883629239634291436746557547
9637364960493685245439201235934399227
6124194015128648336547066208149688124211
5948376363023528544522149934163668488
8077642846481199305649682762411225176497
9354310321095834133060887420955986499139
160684989738923122646283682112425479300
972865426569661243671650186190036384351
95147959303984176559798221430169986658
5439941611643438239932762749998250833237
656357379925302314276147166789555658369
5966460794815949431425143449360871086482
6182417232079705342139430653467255137
7796650895697792577472381197812786325536
871589620281126827264234466521153498897
7182916855603929127324589010650368339458
7628844050885225429649868748230717047146
395525818572124959923012348134616914302
281143029909996829537889206342315561441
3568580010277423960853328150231597290
7931690471723746847263454590656476444263
4728865299192590258369858470932429613571
4786895029421880216748802738653476369534
1361609516847037386137215450742954372374
899848721557935857458803205485390054905
508163116846896917390738633999151446785
771970715584611761640421801729497215158
4403276819937887616354236664537445177
1343786617382367868326348112560467641500
529351142388230196127085784349657849290
73651325674343879958915741179475993431
1323402254243155604391162931880376369086
6095348515729037655644417844574539468575
507349233169612056533598451431307863461
319418239846679442582236606850163167142
6522291424114091764739332965628189789575
1412198922733383652535501249882462643205
9187874858422021166657632032387091206619
824476275427432438407455270089404908508
1321262247996509450591553995388246266812
5819346967736355988697479995476976447670
9068307315321072147817075532746777166624
2192102012885285733740265680392483158280
9928566171896112610971400137014662551
853535143523573904257153362055069376
72993151760741730502916539754094236756
45991095494320095562315611884305346397
9823171545086752648237743890418275457798
334582648067215482025752130576569739711
1394979573091472806617037220665148199202
742911371054593825339486098909636951961
7390899619853090861968286846662005471
2140656989508775884190547196638873772870
727927611367422236856498781272083393
15369187508056291045871457444880545995
4466403321446034226918163719609212498770
1869225199462096649978498850579344105437
275452695128366329344185912295347903570
6937120957561373760374201191960231443254
6986930289466378345195732577937155301781
806286405307319069741579746422936888150
865514678105483467501287627414534431120
78629387925843594052478939611049416352
1552655879137264877635289996810191701252
432462271828695791464737916568196411846
796185021872207863679459543400630379416
69722161474305153563418709578594726356
273620253254329506693069395554621759
50546332511947528221672659720065532344
8255349444774623537093016234490018899860
58494697247258824309186942329189585
1127873615443180940818442270459180167426
194798021336213580112678811188989204496
523984454861893142924486892161315006602
757172646285288932832484876940796755894
3265889875355663313942191003758356117584
663145033253739933844748659404696462271
30382691964860057376255746493273471178
2859448682706037341090137464569295419279
254879146137644755141313845648754337341
733843208996649742071945252778382106780
429548298568513534415401252402559179226
52770530272017513462934257594119457303
6094631523514153794567228579758777506
636230482918390056346267296183668326017
42258388227911561072516752333174707108
91635590444267519071440570163761326627
694091775908600194428025157275624339928
805169953057761158121444728668175008516
231761081480964752599029715383559436060
144612112186745621392164629641417319432
213779223401693945364483440458862645146
115831351304971146165379642217628888
153111632376985556554961870335489399462
6535930987026336341321568871441857558
6419972028401359899686875262707732183682
5417987534946203133919118809958747615533
684099562228555633381308854233784368507
795378734368914456649598379277116755015
76962991848899199062429532385482234122
4251885442382700401839942018143483248711
850870029348322245735948604105197617321
50238494821614116384641122213359629565
2476939753693023854870966243291250049167
8991577267467853981074644953159216315393
7651463494177995638567310220563434132
2837398319195134689368839375190889874639
35935015735032578881888359856245765335
711325479340292869757330537549599673726
7836173428963508479617601649140663053867
5790857486544469946919141091613384993011
645179074662833632319669868781875380161
9180433521948845854034498317928469046228
9280812385921837509834426853876380464186
3954991584242032423790024735656555517413
361927585358526536046628247713262601435
5541545046763620460196672003513623786484
3982885863489947962433135582960269219669
264398591323625354874813680567586612781
775922765584024886495164503742582278468
88906553489213221600631591185714972433
4551767852981180793366383377410926443
719288413906967906350694177767435617520
5409559974839641674485232346245340955
5332304453052679506320924775488167019487
11681780638927633494956898648816969136
457347843502196294349150339649462605075
874564884500713633233733853536146068472
8580529688953751611794509860811563809329
944635506191539438552111152076254157851
1534696910363992797671672210118346283240
51995632688121521792943128719491536307
840042496147871657382926261682585489645
77802443298802272463465076686229638049
4766779010991301995666448163217233234
429997169009501124025969880346619145138
5199930112954479325791027129818424122943
9877263615666285969427055656294873373932
569610226720359437549964733656449818895
974370474678615129093331621763436157164
2032670077175875206798844829333985514255
5061323462698974704099377152320120997766
85019536526969027198439216674995507438
596979426198842335519275851736437365152
33994502617774971465686918835977799885
561850279548827281087043902923651047909
5542677160803285846056764865804373821370
6455738940595909947379304826841074727159
886993811197941555731887145922173627
46448167606133278659194353269486388863
7836902567939873890537912208109657781015
7511446530994049851842215297290869835288
342964942275774514861648279095552267146
99894059788771912727295447786828802712
485566441148939828513945589780393179655
5053406423825225025946104192388325535
78848954185956622974178825346381471132
258183265617847607232667478927303986
9561856316156336477350895677426675392
8363851728636432872860229459841652693738
728390398959478555979189166505989125556
85724430855642898541872031522311642847
3730679780081249974879074818975140764117
75799628436080285051973311132997346456
8053428614811241500917794485547594477100
113012813484507492356518092627526996
6637146569196117113990537829557728152326
1195771059533564767873171090142887566973
24753927976348435963760572972646231489
4860927333631080775056825923200391367582
2954776566905921297984955270282670276878
9577621641866452990098742077827648305877
9142400988376713733371697785964718171623
7587720221369717142240882477126129517215
42089045286427380745134548728267076581
7228175388488709352685656086324962543369
426440584058660797769717290081313137107
295783263844306381329017853475180866391
472766117890309211871281900157479013753
1535187667901738364636911960306721844960
16823439305318947591907440259676636976
7020174477375402689634911574306923501477
3281172372593897579351159885142631467043
93292585497711652987430952940075837818
20484662724694308107722840654936923192
9966162136568345577780216011186019974796
54855448379838239068567053863214092553
92915524841204033757808839342003897401
7831565429738591758671141178457711047297
448026713276972598493649175325384261
4213136210603165673882842413611774983
98009833293593134186661463971074276348
4542449629049476568866955992766720262458
2484918836313623259413686594942515746517
1870930095629128309451535456146215324790
2496986635528480552585907037364298617031
8115961878699819194512663455118572632646
21548016468764915912306732490543324576
9053339616928347181813527625769141598
896320214036616894575712850261168903114
9095776943751511532419165582963654818362
623387165561649287919632772353728185165
14309364166881967083668755411926845130
872278785034455894967176619169168671186
327999898736082463777197941251030384
7323382937515618787117725168394773812398
3238971036397979267251722874753525673
77789649138193713697612337585013322955
216665487085410750103369548219276121827
4460834731339221429031367975192364953634
6847466882233407851617035283909946861686
451243927417435810025486077551410462717
5675137431513914201346792319851209871
502786743299533450239272532636435205144
448934117248507661694368836527893548324
7647827025421580514635102092047548348
1000097672911509876319882184753942343967
43928519426337881170764192660888811719
500347280901686869539513580169094363896
1360867754843820158996197387304893269383
29506165497433759324824458865217851700
461651649166256477117828640359334387548
384443521594732674172184029183392221015
985240919653141815786238841040399415250
7101806923196601434713385288139743400
7571892115775999527658542484245835904789
3176141167089944400329013520599222369585
251758753567685123927614010942635367019
248249129346482411347018627859926758
3383788143576248858592604658628449948312
1478280956458198001296867680257661217
1046452362605633508614246440713314768446
9418496418374766263189818185483692458929
4276417362144073361911780761134989267
37532686873910474491627793752353360290
1022954372895512786715127334524720541648
6554874488892272466075852767757995789918
510521306128713010876427238357678696901
4301140894141622601651628966147345661176
70202587836658205190435799010699669979
782668254130109799975884690628687488269
42443199482284683225399288777519207480
1859719092466109804094804046120423346550
5750371775859866174578743034939297715053
5732951514059291861655489446243014089860
80761708291640459892673554789294621944
9896836747399713882290861597710755994749
281184787551346390136807446578626294528
793708536409660530516782097919460029876
528040249542186945939589220104883747611
9238452431903088720083568092590551246310
827118673468394636361964141857844478353
2986221141316358818094585493911696328750
83168468313395533219681728091554213127
688129222889906262226367798412638978189
476621372240771118438711881876097097645
848153117772555116766872705845947613998
108667964194624711868747021715581613812
5201804411446419395457321766911274911640
8112941925612129269139264199895387078881
97359050245329296846905963136928843972
378822622634064815828944668747706841
841914649256658739952525522037135257130
2138858397435770113852263921828127284090
8332565457065086651941427465248671326241
5244297551999243103468083662228555371829
6722193946465029202189154076765216058960
18664006830538572685985075521847818682
2789332724108689886325943332254597947071
7945106515791730715785629183910496918157
7807560137461375829316367543364237548991
3618716161613470210379218739367894372766
5371414358577629218297561601252258571337
165796356169526436433911628385443424623
7911304865027321639636717935733865908468
665594218228475937744462218327543655441
1272185762476713591977921124741917714
732526076865834522122307549639537215121
445567108061559849553638491418475769350
391054495545754882864046369165690647722
27705330823842092909614383884531732474
63011841779909673831067426377829726903
5172180642925666282141457268389410775705
849529225236381335093818758387679491477
2276588917176571990675697025325925199657
7660790452187635724562347552123494286740
742454139174950551833956294348392189834
53194121294050338191792349972562105079
456576139592989986786224117739855752752
67846602738315284904274442672743835578
459176115244834221539938643289374635178
264133985104200884684107842696263852640
6693308129469736504547489394332291029912
660719079787239084831942869577735769299
213880936071671939662151356842626616755
4067466412968273781443389150106535379655
306412961473853262768040487635516795134
187232882798442077052676644033378064580
2669660259837552495828128918769485082851
700436124142382792287561485992405954186
48282178766499815456318817900388142976
9621323747086992814073371605674247168965
2743500532643492580473323106284879249125
3326802943956064960359983007706625704360
6866574036242182694285554655942577394472
9055952364038587416962941618749450677437
467237966104466130441799963652055211778
5041418793748963870265566129410089167682
794268658694102127335962942788085751598
292222776543847670038202206759854352051
723083046143620167089929329568194977444
992250637239350844792444770304121045670
438138293090853543599185853921271885606
893765669783395350010993948723227903484
7302696137198552883148682614145757468112
367456212759277743646734092285374118200
5235125242962223870894670480897377471
987124144497920262090408395362295268687
52219786194838174823913649483347796582
5897445631737294444233686765604124439084
712728144428784034189500277719396752583
7199310271642202355572417002231411144853
286826009633916783840694561343432273967
921145764955701422831531462119477475263
7798622163612547653049999079926042248843
4131818741922728423760836862585715101860
4854985854761020846218948392726423289533
443453219318810774988808972141373226853
874334281812433261138504864844259669475
317214151676689774959554830488018342645
291773828925111688018585722753695406465
4786508623367278846221159374928676428147
7792181986632233204342006104853639182099
628035661986246282952895381945384281159
979543818485530061488117262634429029712
81533330468552158649886923281214653046
4288853578392484592943923392663832286272
3227731969636887192506942997393633189
1045270898425746404825355924265792476
5817974660162849388696158323587481252415
8354691541114271742014850241451435925
468320948130635512427755389162406444712
2427434986609767784437218521858249901231
60998298125498220816582468177633175675
312756366330785543779578131189355479515
95297548214180242723256557443704573189
650828578145257044402793728332222239583
4553684045376347834229813829829962229354
672227923488773235146250832850006903697
3687527615183205749254177046134347876982
708472190781749474686179468536229579684
519431231607778796422299208619112876332
2319907753578919987835757759184577521552
93587225973431415474420285494337162008
89438167249611568456043323667495651536
8688709876686125520572128973711849907306
33312649145989933236781422712119747389
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]