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
310621820768945323725521027208688824773
7533109053262884534572128781676022466308
5935503438332291864328167028813534344898
323914332241320288570101869950376025726
5471658211419709195292729469566510407
492029034562682322736758645597377471515
7430264167024259457279337048673471214917
8398341223438515537220683083135993086710
694273304104960341239318321475652222185
7978539936352401420335518831302755407474
1450702571242519635669944867494842917069
72989376237885223933794921619621287968
711483492826756522344925199324273132773
7950285775492537789560724222976278097493
254159768287599091917462451443769776797
139848879768454494899159286150593830361
6625701470929967829296081859821463965179
104869468256562933725362814959554234959
92316599245361730458191386990437274254
460163857287623252535176908187927812925
647353123999730782123712873971207168804
924162256585650896697641999489937304454
4631278147115318764039137959907926922527
7749245266704665911674384164382152955399
5593557635667571735539209576195678599
335748841214310585033004307540834302313
4803963285978488996165632673559282962001
7060727071265246236521436344734925857039
64716563642706130402123554603342893356
30478095882323781722525871815162872808
73334076860873666642495658907077389901
66246503575974260044851644895278624186
4419391358169985681360966925193843336173
776774077148258167511478233899904220
5528670458625057193680834822650891359162
3645448379354575846624724795371458419314
4717883089852622719260399096586078791178
5796366982529929577535071735761776311541
1155269615086443734049124040168145426252
3209164860231400951965968475680057391262
6563573566802746386359662339185916575369
909860466691260712616327986285178952707
433336629687616197278594731396147878990
3678398529157318959753512006921727228844
3432193350672575628795906478209593329424
54556784401784271363352813577851471131
873697671546709071508169124095039189270
7564725899906800951240369254607857845804
7797106090417858956270891637399689211420
756847008325760887243062979626926799238
5335686459922094225675855661814337968
493625282200650249415011522948268256486
8544817588241856403447886530164174618034
6257398776497007553534589857639326392375
7237425174823577584586403337189151125
713375009743757752972678797086734584291
117812496298676835158848382189173788014
8630834655819481849695967925479271055064
74621034722168522803880915635003956736
7050523743098202235728997531169437422596
3206207167271648540086224772940272437772
6597347972416063883295311650177685053721
10852792596753592577456427412586833104
1727709478188888389242459504859875562232
209730971176185963679056093636867214929
353943739281223776944098094185880855474
78408760128762284082729579588691753471
805735455489973980518826482650897161541
3018805559560025207006563946742814760
643860008270298198133350342661591449577
53415300486172534007993695805488094218
27502499496356101447704231769775151498
98281966722606301780244754104150401489
234840499366346409347168401444211498738
886876669629211294109216825737814132201
18061546239934807361592509789643164114
2906816566173734379383154647639030512716
711653552993307825455737536878973575
19872907151924779938920876514254215114
434926969935598797558784151918683674186
978274047936487569948515364540668832638
857412937771287157478488602130685924902
9711490785298026681019282561685132026
920219808373828533825761817118026892509
119045109604996640562084854796431573622
487453232667523369078761230178483465304
58807451340218350111656822011439024112
5580659992256194839204981090074499715
8202181771461029778096828996253928587
9209868320828722879372245518027663010
3961155426766634985770286811349121174004
4064720027332903873690889748214385087834
7525970352755841318668786958428255463072
9798856990755898866118321918300612249347
813410863823606130828375225715394123792
6012338313347544575431545618746721655419
478253155691518912025946208698525965177
605818505388451630575393770844037643729
942883432249144603887081280835585324049
5540247816569430347961212585860959481978
9422150869558308551628117149054343646
834839431964309141358818780396463076517
614253696209613384332634994444442317
239362875276108358331862598751285047231
87489846241632348299454375972661139105
93557431932521092563792686594164727248
4811135431328331844710261697426674991106
8605643785951726723582859623567848334875
7785671325992291695858102386308779821
6472316797186859652628656096165494556291
9520859317781297238396203854656676484689
66586165545887916898318618525539735983
92127212832683239773047883550593232630
5382951440618698844484496296667888226
873415796380104272499671893052057883346
5015309857885630508391148894130625018424
37044389671838934167214321750028384497
1794104760801470433385361649800980738036
2922433222405468369861916167164813446838
89268784194520651395755955447827806548
6208629751893375458933056329283657265261
1775363765042434201462594393875936252379
518599748478215352358997351029267489502
59944313826288844114145348359072525148
453733815324860875598776946755229426136
645720691534445120938209463956419882029
7075434792694856578735398741531170074600
370448791213513687519883251154422425560
29011998464704776248695823423750196747
1128274310899611913013561495444159016281
39665855354597644282027731466969805379
914598916411101877581755621534251812226
6138742471369165833565475920175471217
4414606365301702489825811395975679643787
149295043088304933611778332589781666887
719459986468142490042073709812851419961
981441943730617605213123105168251109341
8580683448995879942580693287899415559279
204466554327426875944104558189694702845
2809276423911125353197453022589621722111
1194769812116990848479275914824823976959
11416405986635939358224265424769459741
997441958211295572295903523541722943541
7392687155533904417335823699332740675656
9341334988781961228294088769605330102454
616018414874754698428149102982326736980
926415627074141216038952962175037038322
9758666464335738428848856039905022544297
3467770394811969250423997133890619066952
408188314748731917969117467291112129054
587179362857530258408524707260284872131
88742531634168178689796766052756174366
8480108637388316668678428307563563399225
638892375045709743272108918294418968437
729240439590115058726461154637211196370
203228431071662267990143954585824343303
31932376433969754865316241184622271839
71055235292764636959929158113082144657
2251443221554263715522768740268079438496
624638015047897672897914753993568437406
9182492153228013181690763851305810781069
310243875019216626216941574182259138198
6586654632521612482648817406650130853280
774951808054239861398629637319771653046
104196268555624393678024103889738241791
4224777268675687932158038914391903125
50026030986340916594632294032719989908
989430477177539284899443820187197644105
6384462558815507850143226438517740362100
41203333386831534433814587685095043404
263765938842995583920324450732392591562
87094423871710116337278646516976186772
428214521024498369877821716126182167712
7742244917701053808447859244311117382419
546026857858960182399371122815304971790
164378187005301615796301460606266749801
734598854242505482089481748284066151554
6816508086439425471381422931852338957813
8780651749413777931446269885507635528899
2712373844124583674411680678439084112
6870248255398181350993616606813520417931
77694193717439235892911411853275996036
6069172076391100892525792130383054039188
38307323725115247413679653388190888290
283788438865688921748760696453732331724
628831588560296939233472354586679593551
7208105573233225374146686814591675327965
7057453372886284727433314154694205027
34067965756853443096094265854249238257
529724409549698752731501027733865604275
88326472137671235334529635101242447570
10329297057910819704101149527635281432
728878969300881934234020330196520961123
921379041353792377623276946123311932717
6638147010018223915363066164114358718503
12991839157266730877292757350645979802
6274584154538865655877514077753481578174
61356244435252893617449931365152744596
7563135782610725126131903294702402875
5290687190287281891999632288529472417708
6238457633334003235412341986237727155564
40174011433867064534850756096852017843
361959502037224880831439179192826387921
74468791171803461146436289888687375235
1307466982423606975489863796777420262176
6392454389579668109964079426357438419311
5546303287349344396796003124197846187676
19188915187185943956104945554853310187
23783795501768636363502667529105819167
338981463197104742411328462776396821
7493316613377384456830206711255392315329
97015554094915689232330482373116779239
66757576869538488369216561533992259067
697593994716593042401328722751217644897
6361594981479373937118886778311620807555
7658388572884875483975754932515155877906
1812671435815852898624337445151118452585
35737096850026319668930838892010741926
5732286523527007754431929656178294556897
321276153486692359221800533555125836821
6290862536991861110494994873133736737286
279287867366871247485262340094237016740
25364743625932025465595471790550011111
37663359343353829426998531464156372316
65599198331685443855202446347245109759
5867949228977541487342377406795158516269
818695753974853412483587870430658441327
92901912423255969507558702189682682409
6499790324883269430677537465597749616994
9254738656627404278679274195580874943375
24801622466901053121171513292137395439
8707238796297004626701256656061574107
398170322280797845069852701288675268599
314868722506254115806891903449094675514
7267961675544509896729905109180247409944
7514926071141079801689346589642777721621
171484132007369355737269548491844245698
4950176897617115198854876984816727267737
6716556352268899213096986630190989669025
72115105818512457506153129594212366820
1111084154783228851763999251784634848
5365593876004494488125056467972470583817
1289471718082341261362687927729487034062
3573880399697196530128818029674417917236
1147437789133976184412337247119666413983
49147077831705522692551570299357356766
92096260054694234867749717712674547
4594692746012427214345883332448786969379
990446494642163643312465882103319168403
6248520075377592371431965996224242211633
553459299916946323245556110921198394218
542592943133149123799134370971610308751
588813174771618657354822313952183206867
254547595008194225981989994732169796031
4424583080459047372458172035624040217698
5329119871166655221263338129820451432815
99871047576255985993286718086041676
2412477386709669396166445132394299045875
731042443518667425542437650665699975479
44960216164536402313272733949528698430
246893956894958674967145172877464074
1022103450498450332915203419713570143188
174657505359230578274763672279226212180
1959815935347634889835684198660911311683
4822275867455153950651931868622796339474
6505527486493836562279736863815514743
524685121075281969139632897707251334538
627711165317232418893933934834720813260
976955397899860450475841462594204236204
815035074987967727422009170344199517292
962071261323281713960904964848733434394
2960922571238618937684989819883788423329
955473942053137303570974091630781984073
4748624333127554921092009429516618866192
4888250374851685201716659498550383748
2301357414424774478148989431890262278261
7468259360595452717014037378420134421639
813626358608252492533848620727337197048
6606790658269333219839498332830110398552
2893413774856166318687473946357895356833
3615908927143187118184809610837882423
49160929464906430921526448926738191806
880574544558179191651489581574489344128
76128484613506153442284061229584038
56552529377361289235741881305350123465
77777107436064899430401900191347526992
15982758576410179411218737314042905352
7366184697988067275742236561250713806
8514346830786054287984428385553150366197
2147927989879383514030533353919530395875
4038376525045556542211159493110179431525
880486364931321678295276634513610325284
1742442178676991148215713345893231553441
8917335375246911428316798622498676848
250526827510641365091118293505523074268
9324250016369097536168381348785874965481
6237537121071933738986726580647496072299
225696798066579323327744015158443998384
321450106257936041794178862358747281324
2927760948378888463949576664138981327943
993164177095185454117724231815152049176
7825546465163042195249123696383130504117
7077227317291329361417446726138661135229
259797432933146646133368652298651584288
762336626589163187147533775459951823003
5848329486609335867372388165329876788594
59370774354762726057084744640672332234
69176026816530512479231516124416372434
947463642576842628069909701822485151967
89286206182017686149748063507008751341
486746151322284995958390920652273857160
9999441385569288284185564257840858007
268761394044286464455089846485368507047
41705460696691813566192528638690287925
900756768494711066474116301998739564139
746080273281667254382912767951975515969
321327781080585387777575852721341293503
63967033551825881112370924574563635285
1118659884764475191995997850599935842302
8355754145459606675472686153471520502005
35296046853344738052966663526797599249
60214575945347081869482387553907066712
421620829219547120624742074970182665752
80806689929679847856724235089794627849
735765601409400452156209681107396041171
207740262194877557772575834096812534888
983346898198656663591123769533332649523
589668573905795761877975122634786464041
405377748968776099511665430825550546129
94824819323432844789242961917476276562
819362887418221862551869889933306830464
966843469826667211550157382132444184918
7486977165558487848865055004924928306487
219493847551101559413807567994691583003
1146698543063334944434298570895063306020
4768184952719511474366222350744038991486
254964843676595770502262225929239926200
6605719012847134203892526132711995149981
9127321238503261336724052432915153665689
63899626540505422646990317344861273990
532348139726708134048151046383128164001
9633168237378054227017264999660919444002
695041573188210229143419953442052392819
3666813367764785324619998187863477523580
492297692957238309525282987197741879831
7272581587502284892623284482323451983743
59144031677212134545551793873359015295
1394947750026402700292866794691238244261
9044363427057167100592254366179011102915
759262072979786974526239681862302956404
29394705181965193913076999910699314577
708666913850319437708425295407035227103
1989884296447361965074582191438037949854
709910613338023695434948795197153006711
7323525359284313275064222956140355339110
433793133515462368412032128889823879703
74744367914837392542122965745082403078
4123830548784568760143981403390280213118
362874865114034770858173912768489246
257783425458384144538226466375880457965
927542776062838554765101219859468049263
953130178511027753066993923689310645173
63453421342143447248239308555261398821
906210876484553976876557423356820632814
417822605242618649759099949646342784660
62084509599468115652512184241741531275
193597968673809158613374961514166196343
26616849834791089912224489925346984911
759983432659305513973621195342993172301
118234367356103555891004382263556318147
42963952395493422604631594357623468838
4908854275467045959150034834294460804898
214239345518953555453427636768979239641
727827871524469021659144594711463035553
2680389444511026488182242383684744842847
346434526813005450265081654394936811687
6628702553431676120388023170172513134896
7624442235723304473989711179705221817009
54073800420581861088432706543191966046
8487911454027258897390858492429989545946
30245082280324621949116978376716993800
49002426261375573763721831930045337257
1053116225423396993738325285894812048483
290276418133187577945485758372196609219
7540775259725495182566848408294486421925
4887297062677249501369773045438351015986
72605569172918911406922172257881761179
74343166530316799688748994622807230536
1111388687738537252955632749723932202
2928391134621167382536027167639122319
7644561156306152611260743875512123646074
9360630114009578428164942088424267645
428025689869299269837311368844035262
277698644832036150221042902620136093449
8285711961768513902758173021467441304927
40293102366421398329716986338669380676
8117462610103391870031886653907078761962
177454807040492744948428759458166571022
550810127519599653910233977666990746332
6080851691885392875828649371252746596077
792668574235845296587219803789654029022
6072351736327160363779767851634532616415
908413872923461225630161724543217722739
44155082932788314496786372979924937542
9358946071891921854534529288619418136575
19062114395946592966412626538276973834
388081219479534356281807567411419582091
7919558412936403599581255697986448952565
2085212958208679477575422305013057280
205411615179572910453025997652591346691
2982604861369701897963778530585998874024
8799443178917245583895431662139160484417
368185426648097849393649065730053295234
1827696328732319854540812529523111533517
724469776507398559185678830624792625991
846991111339190884995224414964569502651
6365484948012792524142413098897058075825
519474354746689343838446849703066188506
71492160731652962781957305115929861202
471437472119251866988233410247779896004
1508134027996822347097939336952183032869
1631794075195341444178996240867624833716
940264709856646363181475401575317596
6372145741075846771385861512645233886092
842194054298584195948940393583125998859
616429031422939926051307530842376747847
92701447475293611372763520256024295254
814711574141972334107685646389899853
811788962847646625737656667825692247972
39623775981927373147088595826085045759
420334715261899312760947101432494567612
25854595035715251215055172463457471685
975726904137110337434135333497156461193
3174471277943967539727835666522729139293
930813767297911187441291496493278523786
2191290149113401875893793811781088683344
617040671239833264553328245667419911611
43146873755118011114517756637539134492
6515247766183168944057028317778579268746
5885813391416214339717868021874821815702
14508316871029244544289941399516257511
97802261639330801544804332984638212071
733410239904439887717546557885967046287
250270924552739038484393329567971530822
6793525319828751748518712133825847952398
281273103361831516602851903119596686430
7821363607532768810224576305354704371
6172712856379983808060302335818967742
353639898897122949259049431528845738
568046431256195139383978405327147406204
24549533846078358242522778516768218952
769813351752408353590131563719116236654
7065288254508965565272297083492882202794
38783441166778349590334821138643991
1376239841735234761525875248655193245546
788760912709744439781658194787181626117
86023293421922464928241124793088982767
840474657145529192934928296936132997224
119831311201836928435951203281751399981
4145335247646930430660363282706241713014
38624656197513720990899357276563499992
587814852415974538664316439870389776240
1739680116591176624253931765336708114
924636634252235255355886466897220849312
2737519316053068818995214528802970449214
8234509540652649234537472507731080428754
7160872388368421605440093086522010958067
4668990317481580824811035968303589925
79908833476468823088697389234095591584
4104506738069834661361877352718126304225
37172417333465992696166268480051679401
381851981915347566394393377756981146385
639828545943834789548353560490903051639
7855162812727344903561134725627175776738
8703258187843768273779076938569651243644
6919484766341255518331126110253232838
589776278032451120435591681483023387404
1711550815801700184998522222178317832505
5468317031374579383084487996725287998493
652554316156327301461328789228857341526
697630761748687541575912902891269867776
9940580996667583187662002582806938725557
8958333831457959993051469938705782185572
5627707223156324949305687858088473403
749110528535191751050119447241731559684
1015395123276309212483573011817242784146
851087572506243931967758827547181344115
785786873345779896699208222137732526734
3158758589184210836784834921319926229511
450398412717782980288179207553526962331
83969159091149469755658379922866659864
361810326844798496076768464905820555117
23024546568525240574739216630630847471
29323572559756913450534410561947779181
300546834527115395438983262144777256530
52953081358226695592821426057359154472
999514693917447897824648473800556066171
5440174286034757643127349732739069407181
64651183791144423356472868635419770253
992824437338147124919078689757964568775
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]