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
1444513648219071451886624755591219866
6969377546949398126410463490584685833357
51824590525163594911808422483320861169
7317628268065868293318832463691899005536
569065118553485683177184472278697592955
1194539761723400994455736099344797303746
10659426563758139893638823627325256934
3097966051964636373448584962578187642
4433495115815025101423504913469329033371
97195058844347788067398451333362272343
128677809985245411029697441348958361742
9339362852044002760380502813235962164325
683810632450825433117154896277382816533
129913446175471271506816781088989517276
65131268699011420539914723330597323174
9343130845845488788029401502296938297239
5335445069713516918715574113277185422183
320248755558219776429948287690489443941
355436590857389852524085849364187094342
484993873255969761102367276986789367574
93461843544149959888623980275245565420
8834651369649358486338679895433267796
836733094073229287113748323589023903536
169728988265267320717565529529799002540
62542732461179187344888738889911985
6315870894256017559293699217256378268563
408335791833207913138117256770434309
46686573819925783775656598569811468790
7415229184768994453344033968622295824854
539461052343745373169197238943490898509
17188724459012064899131407354008340
975377663992508576444993889875141055043
9179678993885377725458957231233515751036
3225679513276836999573738622890148519022
715756877611794053326333763701984996500
43881711122378102768055975297582578742
1792633882869996916151737052183086574835
41014350915696118575863970973117919393
83572801450312325841386565115586048680
8126565478983026292932992533553559103352
2072169979617585557692134038528075718181
826342282469552566251359262656313006689
937320808885942238642642360203864759914
72047551233315765502628037182519719604
2284704433864169482737029463735582756163
696660462247524988157171030748183739478
22355953147859580695066411561957029050
396167791172664499627433520767713544635
7696955124079477716178265824226615849
20352724044102335660608543664644109785
136922085936829698315365159384440025921
2561606008802479143172963840652204367
708229015461845237158794755826762982520
2918721961793863474730144550291172261162
21634069208246362971264469368242222580
7354652625338379285528245946747666679283
4858692959055732895679925186507318917021
8613293072177174695160125493753344326674
692233296262911124971722987157273273464
6628353577154059756376015947286351417144
2376655638457828638865655670833442516112
3784787973115668349986560328302431197
1673698629593383329467527214364373169377
112072128479208535532651864370499382210
346714252257991968925784439991458513737
94854789478377251779600787338677532585
311137555286019406327536833849656801775
6102951584872199870113354334137526831204
3025814270063008987576001389501217536169
63701488899360178555082173471712379301
1346708116793586468047342318529335784063
48905266449085486883692448804985729287
272614951794301979222963362497036648839
788249437858246711572917574792772967
8373769366957082413580706318236321285499
2326997074127958129591734955441041116679
1339561066404690243884266945828842479046
5507120949534241699784384841888669805
86925294613667741772616535194550071463
1415860836512632948077131769944335910
7810985376803438962394938071341518188907
91901492702161757885174624843633415111
9769927846554384505716384029524392316668
538615387949103262393166712652877751883
867124733197255076362637374641485303450
271136081301596777184999946963435381379
14876427334854343329945522128110215936
8001903150412570996564307482958075405284
515757839204800671926738487982782355609
579944093500515062993935369978936221321
2306104263831101161493334309233997991280
5157974232673445687740347201523086371
641089241566784871557974943085298548923
108191736830645781882215997351297677710
7321948187038269941179139856973018937216
8612184766983939609738835335565124201652
5091436910627235469238417105525457683809
2022578653759242383939365523989658306295
686799931101781842028223264363927563571
3762938372302713516533939929837740296473
378898686979271251910853089431443418907
67763541608953121117608826695149368765
58403359406195234370861248796155200017
316747987328380766076831743956336421930
3541222023561150965161119282340111884969
972673705021916836799831584552985492177
3401405887019927606193525978902490274242
559972517764893653713843963540116607992
539986288051560480816521760792397897280
49217973890571729143624901285078683502
5125387510374850969552831894158177924261
5410225984845561629786599943259678423718
147519975609661426945104097743975065985
38231064356137467394747134897598121077
287065967859354466546277034892582658523
22042627656129493199875046126137434665
721626448922966278108383087867500183
2868783474369096863089179208221258833084
43516606560483343168288392431292027783
41254455629749796641165378646871786190
2842793171654800676694463660596948695223
90438455946407853073826180424384368598
9631984597637142558813453474123352444480
677067467110472633881207404339457469046
366280667267488335672974348159014023911
6312447983843838266990246421709244612450
857183347530896978643495700655741598736
367755246645792942837125557317613039548
3924896087872939508644678298997237502939
82426562630847579214877769864107273876
351956494379857768972158149487690611376
955014478965411998659869695313931683258
5752163579455046497475877105819151167636
161999925147812408661074535754929011543
8246314936957460430492792404448723253500
991470739924751266868509241779077725607
11055106538643559523903356919214124430
162739992233794344614047354226172441722
62557909662086224188977291981168867545
76669234229173965383602790356458981842
649199159706363258567188232683443995228
625643035821330766095948296353876707992
9479334181347346886160712350629536374863
624365915297946614279136645907713947058
328448818865925198703379607504317677408
60254693644160431862985722692474473290
700746562799460256862719541911689079956
1954746847513302813859519987491796545751
482597264675888810479905732859821178625
1332191734778450842255683896258126489852
91204963520853082144990727518174342169
85027211547892750056969825938688017107
6908890076136841342120285134960220296501
673371935177367362864529493561491686487
972256265833321676063873541565878708263
945938917337968751480384160128281527
797817099682232233858641824834074373391
4093406116521893329595731095913690414426
39748914115345911347870668379215944528
505798657866541551264777032519718198133
1184591940827608275128004829640786563562
531317763810725872842500586185046457435
327049922680590935843245852955645216996
730280227298586197065960169530651654964
943093797930502023448675976515472322868
23817106226542711452458384389547742822
4737256878996916281441966456165485013485
3327962783823779181493477588531254861
75057413923505662665789961640195059414
455854647385649112925667161501354746672
358531261889921985472274304659435306510
3997509861779453101530444610595183653
7217788880429919749049431633738668903
7187250640946242130156015494261715401311
777660178465821415194359188059039784
5558334373895940385343689847820725821753
27453735729119884049160856501185558827
854467771480980050943798021370125109215
994046341986126865034669893659473654347
35841550584420801317467675352019207314
745922162848149695167947527232685665131
1673283198836498832368041999368794406408
717919748075328491883585202905376064854
300892897687388056369672137560521322674
44579308323352133989810485552217611199
1618866429865286588563546515430217576027
4818942417709087460528674902306413136394
20226045860650173167670165469472678319
43026759921984533041599586843502679519
8652203220184312484428012354328546157230
2959876966625480696594781778376510155000
325911007407416653356403617637570274401
2201979862294774300018116111961618630
99822629822987388067441009935147666851
979783772769265416553918539757882125597
914863128919591670868206431938130157101
933023416257188198593752521373598418866
365414042323642152233285758403997555
7615609210695851351376341873352997707063
348957099876342263288187782618611794792
815961806798880719885523875754921151736
541617875209417945959591595562177637948
670223661695293795513518833397814547311
990772622777949412342393253933887131010
44507778679633318826019289351175463497
359793411158026815820753900227728038857
466488153932763271686270196686355397865
2716287830334380039753489290122896990
9923538740325126952615033811057533507
475429272618232520935024757475629421547
971773883116537745197932057871499294636
1036324621354655999870343122557453502
631810694670213899023167753044173296539
209988407263704457870089926963043148558
19349582969366555176183157783464431024
995392908107996516686224384222246411084
70895058834423507738284766083756424687
704259484686300878759065289243390828164
386429369882181430761571117710697963245
3270982342046866729975972570810752668781
66319428902479441777939182133865602464
75996321250224009807311473875426374669
689570857014098820798402465354237085060
7301223920597662807293601609955828062
89931014692889405418332141659123183
20744014202996333335118626289947496413
95696929285810168462295474660240611924
601989636201509443919793620158047318613
3080996165518090126372795621708696068288
552032802016979486362497074308988117780
7639486952163529590055029792881448753788
595367478549783838386154749163035061840
6959165354263785271374554769354590354300
6188463144955338166510264483654748923830
31248379037660189803892542387025828436
3624979987565035165656345846453824509902
515075275230902149962829358605492691968
226833722097706142865645737748141181448
115054711063837668187306660139717092373
819202272918838745059601358532838064873
35272330110299325431673723765792086936
90019621150233619887122607046333474012
3069685670722570919695818465736795049
6214396964853688441948315242590251715
71417796873596338191865244652951797037
11212842388173422849407562773792964006
3578828532475571652487362393413133087756
37875927346577322953585390828487991576
1111843589917592758742118265596651417277
293413414759539372504140274881599951511
809320598959054845827311041906058991816
2317989454333828920052701835678933879524
3860555057818092780625567464591161711070
8703809162195178295016897395602513516150
7241619728333061756758198871498618597802
558371209092286766921822978334942964558
47901808536044459516578750114877824269
5488458036056639523379878688771598922767
2976592930526776955525784145497838603034
977450449220896072214438613650064953550
177564764277672721833295012226314997164
2109394518748812384518512083810910086051
28712671625999674297193510523937638156
257321254411700494341802337165184195186
8003337852017562396176249728746688808252
2193493344168066238933081575336595598496
383249828520515142937501793250682536676
2220722914486791053640617130361337405
388629614535986140570639933823373173451
3964893873042107833717831256932272906542
3267991477788148476371961531422949982002
994618556991057551129717746643280367725
9795569986008474159595954972324197211
84177161294079343171289914291876039598
2451484633626267979213454179870023254435
9663345225567643241376336075696664252084
490012176743795728476058925133427991905
357298364708148699858015221198404011202
3026661180055534781331007935419022264404
1321486819170911178393020870359574233
118874087016490585926378204494749251765
233028493208348430185547154682722558663
16683626783668563567523089967456198095
919846775439453521506791796538083014738
3710603623501474237535096127145214169235
432919629416935906657615025661617188961
3069329116806671731186843282893857179635
2907360666950114404957932499713723521
612419413887405598155322598814640084367
4100480222126946631371918408602057228317
2793238046418075814266476796576067776221
7571135184434442461385449748930692439016
898837489753556798304495366933691739797
31995132416414613061387610795476808749
3457868862090641778847061196682592385
38278331802560841895489199920752215736
7999173139499017442944903917912647396710
4589340433773271551835392001931836871771
575053886757928468429249901469983466902
5307306426159297907793154353941948627205
4886536319850548778467487848122348332
5055556082167006522024704065129714817709
5566918393177875843710882783971047472184
4704803460974477165781111686211684085
58626774424691279701130504228936146874
54714672698487705996552641112056978930
311035791641761482008764566736619339435
3572726914212414505719143389745763366250
313039554959738127875378259100001709
381441695502860987328655890801739571294
179972455181936568753930614244897171914
42433675233332649336798214649436371351
76517540156236362043710321866049744574
822453797423257525115855358617556946469
969617867055617982486659961496716756930
4962689226148481913091933041297840287761
10903706741195337307631998172212803408
805788536830653064455282834454424454676
6715696369942074886538964633244137334506
621227063806127086881407070923831503343
75715431929564992571645422970792513178
7501970395308768906814499074399725324298
300483548001158945839583907244153684359
71470215878339917744616319756573875393
9708496734471125167839103095262897544979
479890701524669859424407898568766291161
8726286680107443418392518114443016216320
8354902411763924801599287736103063323283
537472542051388460354076086845598491826
2838831923635892972546493704680647123137
86957125465788518497901635316573633493
136226599939353998127828349225544299550
2597537758358360745382436905469810955931
569583788175644932616088010046819114
381268964183354831676166964739655944108
87774166678724480519098694499187839832
2390447894664162611343161317196571879548
1724413235126476152763266552793599601900
89647204996598879314152750087638111838
517140893683468305525778831798919071
340401273913399920446432967578275282
4126359884889894341555157802257731919366
74775588755571788786713582576662943257
672042188014291401135628708277783491213
436711814814652389883855287387651739484
464489115918851598863762620246013087575
5881575541412955318034295161669844815251
7559803966091047853664647398161324018
8509821182973369654543343257115373694
732599408438112264678808422442346955248
12745505879205787013687683850534831629
4503832775595581361338073677794863391328
4772127182273291827546031469557052598713
9834156192075867279989975305835841758367
529775154834272335019344434765545325232
3005389278807061754275699054914971536518
668924013039702908642998770842117422921
6225144454917357113689231795983044327911
534827622859992413739273472819825367665
411326179405077762853496337464330437545
63108778589106268144318432313173212810
422237085872319989347254586229731878194
71139333439217265837304269482590735977
7052879987910639883812327223452782269
401745852809003836315994051599341099507
7420446596229290735128197852839480493457
7255285962835910413141021737776392862656
46651365967924129829800434531909459928
2378779529649647516722712360238928354523
3056892151502779301878222079112886914775
86477569931477391477333849134906644675
3435873873604441406564063769607735957130
315736052750319984169560765366830336239
3056690822739156847656597569528254446289
745383621366048817632263754329370694200
48779573930685411234824398317540156625
7484557090218409564915238524427974562874
776431884045186894631004129627888833579
1245321983526808978371606711864411741823
366443377566299898029744443538325589199
3935187798342801597697014839461833486
3890177514618321353058095585288138685752
4767278586046901111534205651557723881034
3600573270084154455278763079405651865977
9776696037342392608055132950509767805051
3488422255432844414458779191294819954669
460076096740745391746074828949506978075
87463882893990292675216945107425362343
1593870279315331893259967712735571395273
664079468013244922139224501928071835105
360234692913515851279806048244591038
1062440473617744671848429573598415449295
3847756786498020143354417216618858952336
48495988609897684286620884745281468835
7551659624496566562487433156680956237007
1981447396188547719576752904461821265253
1000374366378897376638445952649584912704
49752459178379081168314326290583568335
70129712542536032498705345564717229443
3842913095822981385947227869688570918901
282467587037303734094728423845637233
63042474906469485598309691203193547410
789083966768479639319585824826065334605
316494604659015359314701611941111936255
1160715880547726806880857172866538137170
892528689291640943258642874889224057317
7782795035676839616172981487390282709269
56868028611150824871022835594902387840
189060289578682249731376545994348282132
221041126130996564522928614935264007593
849440125249978390804347149729621029914
98626638189026392814599830349826616009
8453709363564378469523978646665416714923
2759929064147983202721248159217581247189
1887102293689463263049580870673315855
1878419523045549628165235808900615774921
8906732409741025763665270481683809268
4159385010177888919851668988557996847512
8092626892165382704130866817684544109079
59754977194446820382007395534205569696
153614772595987653401230447767133779884
7309906195158850249126182302392357368863
9200246695049973859333155293539965437263
18551151180143201367448844990893832521
977190731607720327943177132485467271177
613796756058141947037766330675383843
8822749112889667792433779155685249871717
5814223568527988115079498275735372202934
720801042129631468115316361313422649765
77096576280741421197568439593172493378
5540551172525002301223407589497129095148
54936669532723961432790273250276053191
859091855766868636634128835768396597096
6718737946018791489725526563585682394241
8592255636316533861967439715618258116074
869785484160980811178776958353789614573
5995538373792828933286766665581962771419
2237466866764260367762746501849436478978
580283172398878109276812730383381111
66552414951255668062786544514885566478
6554159726766389642673055120829735728027
4994340799626456510992772809815679293558
7073106449143593886966146835588351908154
2852316631006800868732065947174496788649
1623832049906692395368846384660432112029
3466530827064363633598382987267692515233
264171125029966511984869224323363238280
184588884514508303527792016127813938516
348982877879459829422203642829879981698
31269611763287257550811351812454128057
3606683178173254625699235772932079771016
3306711699983099627248506447735748699385
4178150085558437446758879758419184118139
8526788031174196347090166214509274888567
9748338128898088424773144848406860023712
851619914446713391987437049642575855318
976821234402282184451052791582310703393
6686983041423983130841389532553210067640
980112529236775536054547117699275538456
740723085492567042458419480943997352923
881361016091443097363645749823159724641
59841609105151911521655791662643311986
1168769557014938938461282279828842758
32351507362373602964054423153695785068
2771448498074712743535624994754251196254
1002267898877575187792399753397897257731
553123126910737470217528621377570825088
65987405524147690674391307721387889950
684546104766224214308231770793032517830
8997154843164854146537374863491554939
5078865140643821100442703406246750689462
133310104427668147143453173192793055291
6132797672571932354985243816276110964659
761474511247975316059943727384817757444
5637759721318046172129973011111837667083
44627473745628892753294410943015650995
247414429552160315055872372834712135554
75646303838612993469602669857836022740
36138567410361764808931608281129165599
3834790540571121965329133829400450622062
7061357964128073776529496959249691224437
1630483649679824143698271528319458713863
441692781316126246838027461951545358791
61572993494994853363068838694547359683
5727697290941471209775668136371352248979
55375566650151655363422477698463863956
4175145295907933706786031228035657781
225650886888677823791606778713941559655
8784260985318508419919666524400822428701
1862807749614505986151927190140991843538
65892459109361748592033663619475605176
2348581698935364344377685642110861404328
7265651441877933740325414638555375445452
3036707718048916658512419931420546606430
749127570244939572438931035573779389347
589951541314257426741908569564513117240
4423871692476064727766843423678858976672
107636344995370693485897693946845832
512020669588886863598546761680039548721
620495204688353513116147182655344063128
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]