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
3761546025432399442313931190503989548318
387239345263981272998635595851323144936
2671220986212184064443751516906325876
23691346674220858975271570308426238489
34754868393733071987897290985852902735
5988859862477857556317785982520325188973
394660508592628969216513972765115866830
9560321464269315313889016567100720375
316477836695924958636834448261958519408
202684944035608501019183550108011117549
6006375560073497755079095829862154458
353337592403472325229037229770499805844
526753319762934873821638887309371501601
4918952285273353471564678015707141305576
981592803964922580326643545987033183228
73680103726363775864812340164016817702
8729600573997077741695612733120061378741
1905174408759245854979774075622288541
89235792851076815404690662244344132809
18793633326794481043394133643171758858
21550642092527911831687493480384147470
639484743078225354834665322037748061236
814633875491196739094444263340923487135
647775323435426363129204286649792065793
3619783934054039924879551293546674399073
93389507682455679028900829653984268701
251850402131475518663525819885239254751
1623444068595461549812882342140644639825
5869510050245875728010948654999671627564
4678161690535814934164738383409876084782
6128614314848693134014802820553746851170
4908911448701260615565868693329667302
32888117361743037903517721119119806492
5105293849606953118899071209571574328499
5252132344658461776520819212930332981147
1264827347688826810813801384730940412756
309417159955143125216944326609537625
2144513013665834612947855213725768138656
6150294069858536320697928976770872885977
7523496334189004945702598377598146109
2600741043779586586681108940801473149698
796415515534993563118844592111516998506
857198221926826386326259389110933784995
1139993313279198780714812026535472246
354771157552398238265190353678381363546
500893898294809051549515878926117592651
8400999944604846517980135585988324082982
497273487188944611696200221507179
4502664353873073852886259473507775
568482939721531075996666832893618641965
67483916822558769745205412778376231601
4051980416846955157327639779369231695481
30555550850670365475898607369787078306
749011708869465877944775057713895151930
65917451129796721257322920927815112674
7733850126634707099401845905438026660
878481802791216729435467665453818042146
4931292140467435971762766988158262744277
25396041892547591991746770380642001006
7538155640864717520885046494434937812
8384275460407986946966391315992597898712
2203902253903586659739074418577456714385
3570730054831649971387904337942746704581
1710101710586667516728847205114851238194
42212316475492491641660930953713636928
63602262998518866512459083996829578226
6144126366241255123316317313252462584709
3024412174749715323460134072503315824906
63099082803195014029921466374551125991
26556849620285503813692574487995229675
6497860073258744103658091300636159456191
867295555076694420485861955023694891644
585585894694641918384049302269390683610
5027713592003209781297154460886050396459
14741539536262275661584807201247475629
1643685298413713704453545203303722024713
3350231216985027217396154402210890901765
539973164421643642194532273879087148039
7307111974436671618421623497352187275478
340449535128797741158985600640902525985
947764381696260573519073387121311463624
7391995777602446800234111299509374756099
37831802199034086509983785832258197713
5143700367665737574774358292384682527182
14868433024335520021012527637254041234
35237072915630949671795180181488383315
32281468465059979064683850934144143668
732988146027175291338922443934082964928
165632233907366456975600535825486614536
155940531037993842582812033217523535491
739479015247408738146856776514811946874
4228189726057073914210436165381116139
322930851114727856857398889249544187486
7989819112159017405315728086295295488955
341264071407113709891595445736184239998
9447536021209434774673793595142926498589
689615152743596882568633769537191979074
395171674358706967729943419839658139058
385510774541091477237508619680235689671
8490560018673873229767033708513640208743
6213985878617128371954323023280345236713
1188476068911724805191564538382709561
64084574319104391374799283654330459733
828773782587883893860456780614269808907
8506480341124774304867971747930599016378
859278132939808598631442566364236162957
45497801246187629711939515283629819195
152864628208469532188238949181768788965
43689963195490096260141731784352349683
195430796932507723529026681596623291674
921352638353128128238143694889915616948
49011470481919988081972187125478051457
889365735755683292114186596108141322441
2838455755922647544493649784414852123
1397194896296249638293362014763720908483
88957541643814625986792897764692972097
394351347093821922071168601978860141960
707370971539019670534534016829725180
69763533257506424534079138248617769318
229666165424326447148622822176074279421
8924888545082373962616783190100462955542
211842206394520181322123256074587814905
6893197387816956520632032857502129764508
540049655663582336665828176575338986894
76249801641439086896312160466796808961
255226159869980500170156178240882459707
701727302417469592075399915226680023912
9926206644242309834538587744821155173718
79286093644896819777239388621436223336
971259373316038555247658318837135262463
5638494961603211419398822581621325627803
771661635189380795480918878434369534560
7761409011534287345466039806632244234439
9922497061490649647298102525499944
872882575295949639662523603767571993118
2173950733319901518584667705761975325503
5701852679568302453528227642705021487235
2959339531191861536198924469944232817838
40537476706045124931513617657115241022
413422978438033542229764504822370568793
4016507699394547609350475290515971227605
431696521789969165519899647963925636224
1053444883710469945512666517595275479
3226796831069896133347991408921881697
316872299841358631871081421332860692950
4786827891403693676817772623239448186831
7797602081062997218571884786292417787060
703339251224806635677932300118741568602
3412246372738009985210731427175586386330
999618215593579376440123101735961418859
75719679928243834538123283405333367489
68194674623483640188999435394663600838
100618229057028753647811508992478004619
2984257197538258992788204368892086961734
8596524099943371597253088530187464872902
121210121434263535142079950794961041062
469925949624544886227421670249588881950
855800312749446359967501542645082686101
71568954283356625173024021721797099
564322570706139452653839788613053176007
97944713257633172651909314557327996739
23033611245711580436671778277277986598
54678640180412946911195124965627769077
447020605001652025759708966145954815948
88968822631472294053111270391563554232
719845295464875688744926749213414496711
99681852869035288191969778818429809533
54416538189919845317645621388658953438
5203175159327819289690196134367315771265
1028691529181334239879235343952078912168
942522551795268142026483902857294595508
555254594028426685841724122675824466187
62279168903599298082599566541157794039
5288725326341924125082116932659416206491
6748240035834407261658639278109495525838
2853913840177278584332251228847179633
1136590782861416978162445399683054945798
130653323031600148776708686773039898675
180737088540817263938811213138078324168
997980005715676523353253624502375178522
9085223593032214935867116209978618036904
731241145169157517589945676444665649921
967830195762124145912755173669018909150
674818094435413067021096302510591514314
9270638174071640952996613886380643562853
682716365999781641413511734347530481862
744069682514475441393656503077323466040
710231979685541415585135347751262265
2343245632602616482979878566894861688669
4759440053448139806610975018479649147937
6009993531366278613916708151238223758465
600706168386873451017332423727448631525
167983149602198571937243688986191764449
172864333293185390626258459876963227209
568292771520227185672057126224963154507
23683929289366683326847219520792331619
3676739380401792610498526877288537888892
604527795760341833859897659641659166624
3173204196743668711434692155951830163358
4813555310166766121092397950483367916736
39239889297913954177182704175633548825
905349811015914714638005354631392692
7348162823107233729660391388942468766826
20511828291469007506222365190451478696
67062781617581456764136390552283467322
7830974264644644769862859911224649255379
234384158796156129109318334031402459412
255847106759649508470012556209359115211
3031603760491429381759895884639528970
8509967083606446361782131460542883284667
996958826535751280013417420401076206104
335469958641322604211993678374632434
7877246949563392171319011771516025648895
7799872322476323894696876909170997934657
2708735851554176335380524901530876315466
8371426183651808280489113483424821681120
71979984703838164999647381892941212041
316815225802223025153914210821144858399
997653488654347667756010366145331648306
441080834169294019906314671061475043243
41818656549441122479609458296563769452
717823597160232050368390680262446451172
7212457793828440319736943087557718389092
799559435115500233643869335859316266419
9411348820703147292610827956386253403280
4190876992637446868959245219272466154710
86598421863086874943508093245454822151
42297585463696527579402400318054824033
26117821111420209075472316380025588785
2828914945600095366109389547826683965
1954285211611728714699702280232297767710
4364350615411316531933973689334201590
9061724066089833792537405011107424597028
8982178051342093993029842517687463577281
113464244046382602515351421234136022973
63572549143393288802825541349104354557
363669791242753876632377301648799712723
4380389682912400574252539689669578065445
927353354602336037371911897317861811090
433595527224606596740171915993857655258
40827906502636121333633876441337361641
16347505213526497726187461710613442664
1186321134835873348414875163345472191364
2488766759439096341478368970840410295387
8817932838447234379323931041432224759287
113750018520243751913243362499344849837
3846303618530572888429485774834525764
4288824093633220748364771936376047585850
1122553147288742398213908041857879496
857965474623682792851998403829972367303
80733612475427275972500655597324583950
578685659555132737489188377266248297401
593777019841523088855167249582268313641
812012401380805846176138431419523058262
5201399437776780902074246890295923715142
9777923337962030455347884996110356766107
925286173228094266315514276572055186658
4918657286214163502472318426137151227685
427363039796144986821876673183981833535
1947366762323423431462659250601717385657
7898327303384877980956317036312431816
6480963975026949450999319077443611225980
844111096659374698125990786023582914955
8588790482076333847754753169637458166
530760823960428476986146772396968828252
885581179158244984999551142118241324582
377369421991632171282895277766523851312
2009419379374698625589704195161328083977
2507748831465347198260059785730479242179
28079411978566297229079994492354142195
779196201191573758903975935761753643938
10573449460169897113039858173748512726
7677108792647616543224088472334487461
601525266125159496585769317679521737457
486016178634578385598329168120183128194
4738639483707842335793308793166047898464
8480648324707660370093605721313994626645
944477998854283990569157015449193269771
825473023167280874159342306196075839622
498042928421132984079779009161316581974
1352667137568842411237565877326461549696
6884649419027278917734959688353765085434
61748807710820422730792014247646766795
1690836938021578757666626296531602555
5693722276416285625747117237409140195350
475644834921561358171430356968895683050
18418178931457804241824395542264349462
3887770990629222734535695900563002950
1342434039701774598873507259537749542923
920832311524535335660829942566198098025
2613735392552607535123015800484245742949
594979444754962468632458316714427789525
315693855483744389674877823215021658523
172110597925771693277556918266694423218
9430396663217072971392745652238692819
2068471769803318379237009766100757527528
864435397242480798049216009996614132275
7248573468395761135760113813897406832
9806259180328949539072168199902676324541
13327218607559579613934678763547994680
1072703839489394147367202739535144232574
9455450752315586130851864853821717343
272963799868508030347748397598647764071
575329062152840190847002525970155534999
6124670973350838318244188275951262889
494989908829959966752635457491257397923
237469281228481948379685630569652201806
8455117171556704661389037451562674574413
4937994211991792226753377950912525394047
1342720770308959502328392650922860085576
34532762693129831170552106187739466650
75424775831130838241282299455390997157
177266411810817549273555335193836636447
828416397207825928878565290260295254438
940071409850129963585962009409321331746
7682916138965873253232748899265354919942
819972737025809439977705083589345637190
811238838032899540874054762310847831001
509937627611861113583820360692642425678
1113336386151491254491801403787645634391
331244277564742772142754070487053055819
4867623110331959383996653550614344807833
7545486668814407294560823789248748336725
8998315015137343905910203940392887472012
167773119444367168098292349516999399913
515324359392472691460141455928746426334
9222346274855279197733718206146958535
8188551023798379765710434938667765297
842995147933195718424772811907325392487
8168288534005505672119486976522777312849
884915295732564289322564878432130458247
3726305588886291216312254104614150731941
816076865362245483432690219184151035806
385128256625216168313273660367532213072
69787953862417990101356807846036
765769099979139469563148295713162503114
7339269980054540505141978717437334244384
2520956716845417825928635835519423846795
9555794737163918156980794493803834564427
983150981499735365006357246715447179313
5706358172825768654069583983457857223339
999019805263593182712713813957163459138
94233923154484204486808679098567844511
300917273216153593333395214425660717529
59812197957134727300682850599729980646
5479327128887840709514706842354521333933
44338758411289496569114611227137798765
34878905154310000711739237647206069534747
5759814492046448439698455724128682666707
757715522204116812545442973638423702417
463887309262436074370901312328877649010
822696749577729251340378214641546709
636216081793618715129317766427542109586
1818557253746359891771114658337216679
4604225732249629766888115905444292047313
855765278581862843103133543797228836089
41048174741662161845382436124622921075
83124839643662318883529203341787927246
243620528921285392726013635732854613838
6582658828531026338514803110896844247
68533889389757492363579507100331742639
17534078574878486587108744440886553941
7520633564698822585451251604821065428664
519997775703443287805187758339023202991
53342666244699028722367671277617939186
155228984058796363319366888714815019079
45331802452917251806811658068666945008
6003455995547666663333833318769899092711
1768743267487816205771183332982358001676
8710516215278850991517923399275010158973
41174826178813649053310278679557336041
8692642462541926965257407164182441249
98714101979616464619253685105425668823
74583886259575676265853587674284778170
478294578953726986517517382825846243445
4448307123816778730914233281816037249351
2887860594546933921342428836267929684534
1038626432622431906974507630513940541573
4861356789749801022578036454767683799
4892455626258763914145812679808850653
1525292781189252940234443823191723234
743963951393802980815564119563552937823
238671491541795579688262197923931091030
4387128481872736976875689985216392425341
60862064474834935728373489610360635536
316869629168813429365682667789276185328
1884583413395744922318739047653724527240
9708461782252123932573338750993074217
29814684144772674387172449116375100823
7230737492104471133883268274427563755452
7104513156409442154155171473178616952256
925698632563865668179681658028716638470
536960492641485821233857469645460513323
552652452458300987423225426885760475887
7504699793705976551821984263900533602773
6024881293684064655830945022356031729711
463366982683908416190505645706693463600
3773967643239384765664638149811857002228
666217924250281665889276614680930993077
836580291025748551057565665476553222005
79388253628488995448324117723030404479
48829508941818150245029399990419663886
5628364353415251474136417632508479363399
60830957906394450653561366329096415647
914170192853302170867612293016014293203
415155222463469367053536745247289463215
42317859834540841201305312921988472136
4169222326914933834452496008788052754067
6558397238644944642110555298774625941657
4040522878372133967070297546513624855055
21752583675954082322918371100317591743
885563274522924109078265817864688443101
904775436718816474995551255150014027817
887965442485974697072729516796891462199
821072943147757131865431770838869189663
5629557148409892624969481954909730242043
71622722789203370295151310545236787019
170514839093322630789662148633793745738
36225036724925427395109199136432217977
200044542342920587414005790321581951357
3908656529405436895992418152633635853187
199259732122883741746855419911481128073
1202191333514938127158785671025662460
88452501875532480252526075881349973022
186760778928816736787103909621450586145
71019652138512427398993007155380311157
6066579011459524874069976209948258032982
754640287843974831895632501170878051
77490218633196937455784490355499301620
4186920530566529420664641717485991939505
6692375017065252788251841124347963365934
784468557430698735208323929623453306173
146959183512913440713125224487086343021
163886512668432568619944653058282645063
9216829469347465841948336388102276567991
4597881322635997830154611079179074658
629728263206638759235194626275160008495
951823548231991828055463294739752148321
312171833954246569919835502765221114327
7308636149986724827248811897502168646501
93825337308691293059328692287234725500
2968477836889735898861299150133789536
924153861976414556786818982181330485345
6868598277731863750785555170203263134233
40619126166829248623656573836435076534
5382727862163987509396237950411518538750
238142965048881409570629709560355731637
308542346117236913951485710451264456644
2664997766171804925942251133545391024751
5014787616935122355120697469256253674437
35049278407377414739274728539011628152
1512826549305703471647131777488757296193
511559852958795472094926800495484774437
9667890336267312766738424222166991232410
428881484912168354178499299626551495185
959985076397459124876353368121760722758
3701977625997457350216969212728673188675
928135916309290344099327530927794466383
9742565763191147428286287932315732630
536363913493387679021199265905761701634
805613015539608636169818799858645532213
9716940147543098717443344371564011546
1057601457607044371968744997309850083184
7699684911345801897707516375899801509
800953926287650509426723027281485033600
63145872917612043961211911704684855
3308995499768331456280373974756020831769
8040113151537823928412428793828220732625
1442393056782684516511171860572951247
1447712163550263688044377374913949140
4416960497357624981392086331230153417179
992695504848409890079942948161781791838
81647070930677289543579447911770879057
32793133886644600099951663433340311948
87344393821153636676724058656934253630
6085374474503225570599009808707199822189
711561263686818375428795794377853147807
30232106753143768819058780948496967095
5792558691643114318626995511956560248742
909625157245483552671049116543923479200
234044717792150082326587716216277514127
5531125236592612266058753396655884144635
537193774387898779344896864587788404682
69129571614552068128771469276152039773
41203856740496632096120799696457013128
238610924731518261784914545570517030
90527410699233015828110769638714547431
486223843977224328632236208184418967154
3750103748121292815276981460628982380
5848698897034028578297447418371418423062
7121545446596828673890703637885060837020
8299816788316169366073554895659757556557
8155738877643250139345889210854682111861
8837244469914082603974567648729924690
528153769292878250837978147531416903996
366383489386035101124618517846211632
113812836672985825791748367863637798552
9655572567119704535491019978828030849498
2054429615821850640858947666155841087618
3011769313637828669445386354479976839041
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]