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
554542053407825368291758592631587697
967955812649545679016942971663557531716
793743695904489458059431954388304289766
9812815534524892307462444145643559134200
5451572353746556189752642925706172476338
345895211931648387852148504975922712675
692796028985537736378683151340616361862
752278273007424468915616178904141498279
8382859476021308572355462376221035396126
1023109513091688492694949692205994326265
7348441419766654479414814212520243191
7171884087081216580080939268491745831154
709270553339999750026837995818138587169
173165849405594104456723490407232456331
5413231478334728433294661157314913871967
4780570797284764265190837764420395829607
618964889296642961371547295569931088908
7844212913545678380156911502211128656632
1238138682391675865917125573395787767273
984167112931445117595783436292756224220
8402272030323242109026055907657975562937
25076797942911795873764911219692546419
4226194694037731542623172441721416486983
957678918392361259569106839236842888285
9855824336398016454813614251761336723646
9296486756609674984345926925534261591
589230937598221392009763962483650662155
66629377845738384226193567131585007931
639913334964666735590565574773952264521
850848464444517878687244331693982208709
274294993477670244454593981454675526493
252182059321727379791395905757236356002
2328448923626394671864590465171144535
7209448872383342469229604569410349777438
631944292405816567246991539281540491834
9493576163417834478797382675390790574111
664727176356311252829766422256470901047
690414897423966684712338675141969859206
27961606068841993905798116832495717281
1243403825409846528275797990487505236
6619213865628068670638396955233824436142
681573913269699296544081715137598475201
835947397383923879044073561412088795508
2606798584332221195337414422681961055
9646718511571169738332052913932969928242
95965695128882353636768629950219974623
249880955018778379639937853727874448110
306622227999595251496056323818314181928
883618362329594328382178945605061265831
110491012953531911995756795114904220
95232010177837319720175512227515184002
985536062906736585433177904275627526125
5970453777222082452763085621574761171021
7552150123884747132754635348303756428563
4879350434816827313036036410524081921545
8804128952982388670198698025729140831402
180793662951279131906747776908063529337
953311986593827421885182594613961461620
3097623875061606460991034605832162151793
2835561313006088845985428994878396398007
4018810515533333454796207279563029768154
615822626681926678497295626386840591511
3777412420429312515596846938760065209114
701191057812464337393878681630854125372
246998317177713689575945490702039044980
5267637910333268654350459654924910114431
8850316029439663142543307372722682851847
6722639539365979304211388085935867311586
456983292832584719216076276889172866417
4084950393381931277015062513401693035216
5019475423973197488459434672905179125102
14359122111513641952382970256106802963
6760339017854950652855394833349265489914
9259565716982271493242094144468782759980
5059652188193413242048702802582471918608
527884602661153361059193686189514865931
495370143107351810707836603693062281470
4856765958778243058306831428241045630
5363928875548971230091269699453747706445
8848325016991534524025776941542123537492
9299705222578679687078818118914482262118
8172876442848807696119247209293345906328
998073857051588230057911595138766990290
639878779264527820227227974988675645091
8236417609667237631396533407919171001
508175908474783132680335553314470061627
75541822299996606367251695516086877368
661857136318614697152312791450336311493
3313995177486857386343318003581192861218
201241018162510357831561394343581662278
31965072473661909052948727826539551815
3755734838207283342057666231831449482493
512347675984723104899299186527270928555
458725389554444559865432129953049753910
47658298306769274395705960402129314471
72026067485768876713094539649944244974
500698394561741119716897884465806465
8795558781423633377237564003554733567765
13827322992662989387379272021065206151
2894614044135640608850251937524162523072
678733367034725806467012282565460223012
171179116265214784104526633189234272284
55165739790475073231757933415209567932
26442804841937583688209673741705878953
5456397453793471153620412949212877485868
939482862752975617164295386533557476132
8921637564309413146212103445148998359
92136269233460512352875237878294312374
53277552666325777341383111937138683479
2471957963636950880413796227733138169208
7673286149758635826363713543550861704861
7492281024236206835593323011130114019134
83582708956445381735543110777121346865
522694539560463595491555805761236125689
8593547699035211217871065609350629262439
702963295463696288184557786308049249958
721425763403135265112956199085272122657
2326607979275733893432032454234820466018
413749489938273576129511444555642263089
32017565852328849063462172082848525389
24927889565839086865559846615355823628
161431191242558752322649263901674461545
5450597026876935145471864286856856341793
9839825011585408642090927904959372043372
30676358584824234437785476522234018500
374310545060940547281978442644130858765
9062433818429011454083955807316340623519
8819391069182638690895119836480820359308
3599698150689412489372395546365383387396
6792396363823700589445403595930620629187
24015122100442417038905342324864977409
3430959015326459833551128974433019857848
7279139562327253957121922874754154671023
1145855745587072267054859749754728421107
998255264359809106786276111467734902
779330122066187839911815738483655459646
219634587743155549282167026365542459971
909821566906842698039313421984247098206
4454248840766068539816755127622894437130
50926032014206695176523591467172285208
254278951502923522069122040180427535335
5076338290834366745062883456108954381831
452938432124112576180688787797922862611
5431786316334862771935601054664438922300
7202564327857809926880281282203786062141
87223281006321319294494207883032979002
752847881009760188528535260661338948535
3440681153736612217414456332402878253419
390281341048427361727850679256123274561
97374313617562742326078620619129194277
6913132878929701498199994052331718877
4495657054819492818427475594868965683736
665918496525178935250227277556289837179
323328844993400648284488158897057634715
621138987983636102715685254546850359100
71595878591466412478915093118172475889
443557733178976361266745444445469268640
782042773270712998551117061435060997433
73477756492589673903484949118196878
436450974368973804865668651068404236
8602202831607229484766091559310136929
82364248811852956090915644235284578253
8879883689121300938114898963353537942307
629755235313482902688155841577443613317
86951344958280326881249226991174834193
1554521660407023646943843383525220575430
3867590999018119584293021242640871009562
57041891676485385284931746910169592740
6844818122514761784747627717934688789838
309132969284206316783432520695336981
2039805455274116336048703959437418629788
54680940719059604134007377910617358494
6035337155951681421814595822877026937964
6472382141552249460546873497391850878855
549299617545582550025268193457163799184
21376449081646791160126365417579035301
35384644721578343983788106205734216486
4020107190991095663034447336916834371908
9515604769766839837181057890569960452281
12632428364588991107694866168223934896
3726109451585110759383104781800756381654
2830891626608636612882407338141817348686
616255177425217276389336743380443989286
2325901717919966775559877096433766109278
7003538349899751386845996210177173676360
5110269265532207424432413232085072797
6731641391351023550324846665870432342647
154559948186682414558495339100651166863
72648804506686624877436403347730069657
86048625992142856932393672903032464705
773551832503259973225928510220923075475
5539990174822368110090754061835823096190
2248964768251307445605331246769132247
707650861493964381453747104224857612169
1930579612622311829324787923583544021232
539883345421090596474846767139492086757
30703942302677377183513784958006815597
5138337661829737324496539622688750551283
3629113134639629146563939098343435699170
208660204366228753462073133708166813257
857688897414909910587053123423283602874
800015323221156468475490374926875468681
8716508743193627165031435518109133075801
2434975446107361894025511718758355317512
4181497128285714351766666506261765789256
896272081853509300754612766768553398335
23579350713045359641567790920338863672
3773191425398583137954336002631335632657
77781548759241382050782268015891324871
105956721258865709845897044230183442013
484053115095289517517324653610608531169
8031850650763807393464185759868129696153
3499814718944312030497887123379812487
5996835943613390933989268186637522941274
6141578662774513405589507437701977306677
1781510895399154739576944510964215702140
4168869770747760119994042517962024255523
67507654542866864605149035484372379076
128481293153582071104249591953684044436
5441279786736179182246901521876760843858
29374898984605901230755285144510959064
80462379552185710366465943796628646393
1777156555116391694124357045873850333064
574899918835269405740722375727495016695
5068710629447622223452738680688719799219
44977359371685136571191126584052515253
7938441229169929849640908193805649862042
753316590711657951997113643148613413085
6830874454579780603152462325269146089049
313528993921821235791154318510326288754
132994585776510768359378208653929524472
583683461928880958596174306385389084170
86312732929738333612073299725633688045
5762284125059823414363268537875513503196
565423899700617682228077976432883377158
228389465777565177215882963181874264440
962185983127457923668857700727475116466
923979364189341447518836648195495265819
3687496892944453267549345430928149865402
5313489834158195696725751837718681779989
30895526885836479495225667500344197579
939937705213352868616757748128528886812
935939411388293910226736400920039228214
6700515581764729879324975409933147818358
80747613901770501190249134410494195180
163388092417636661707713842066889923750
106174038418617087007735608497358316867
2110251550927962654174820446725282799
475683893678797897676485334157592109469
990631246649498673784497688979430974
4494572340089987568296634273827475584
48095192552438330142429104612592066017
5202473170941982543754238626661976478881
1986922014794979167672914106935215123955
474781741626114722864428269846985015050
8498519877745699204335729423614326853707
5937614724232626947736933943170052351186
2726574912167153465095282091548654419069
49576850961767451918578629213279736
849500208129186765494779065625628223
803624824886062879128636365262721506737
237694621922558126675476494275311231401
8148844244096068761643416664100076915114
244371369393946992456574756856025447140
360925624809462246698299574889095762955
2508846590917789879077021463236932208
12474686779148198643316219546036503815
7741636520868423920317556291803084071944
5601966041335614976866057222497318165388
653132118672512983396931241798957374417
8001373755624069834230959374963010939573
9764968280834932517975496291141108302
245863054911880291795196019185765854321
5725133372177546973254749341214774962478
666862269957351281341464681264390304236
366284156208115233324635225206514865553
5018914550478942727853705231664742354380
14037838017992912399494649757715145974
3802748554749441164733787090875770177534
62769630736593478116139574940065593664
6671491860398149498647556788859588687
779540477235387950997159220182021243973
2869583551552578706314363579041079508
785990779336473612468050800658547318578
3466491214251501741138227554594866001078
7821832390865886179369239080567053601251
4705938022517973118572547660184439159724
79541272415210084548623673159387366752
1272226639633628585279452529443587396530
1094817885623930662982712053156833379925
5007715488429835131298554857958982958280
998698583448997552527104490440947378965
79633819478946155880309920241091122961
814891166385906917776024474165523211807
6852955977512919429292248556561758352
408326391330512984882865582364639974304
2153254244888104883544844275448924885293
3972271513605190433277783041485812562600
5498841616832632822731336747117161268128
48049786284216626822756081497772765649
73992575271945995952259545316936687575
1046888781314573208293022870908547841918
99672049351462644313799719217012558676
217257082502322962289651968237477181107
85174408287292777081691329193236679165
5494327119412985061275238516919485712
892328743528901098231228355379557784968
55704130437988214925976162932555962913
455182898681247931212900665616191213437
8388486220748757956148512118370744035103
3443122198063375139782464716207589148099
3630548376335161165681636603964615948827
40383585859727988459422476553689679345
928724272916412087518545953491593353465
9108812712907840307172646603321549082559
225039815672653634398657881942581829266
5455938739567478523590465882383922652312
3494142518919288870826013465778777461616
272830457568648612286161205158481496904
324172912881638391427301937476821646603
553446893122647520480168751449413984691
90556224772761586123868593261955606274
2055637884929048298679357059814431677424
5818854477782063637395222215200873814607
285399571062575525585537836770012171
9412656532714338654543459175387677632934
7422760811915968494792684196237056084361
8242465437546584394223672833320757637905
3436872319661201145013481204664926065523
39885104112678724995719923022064248401
148737227686862683610944728904969421981
8871383154047137558420021516878651358
6963317358519279965882605336199996432495
9508922240567467787970466731879377321818
580130664273721596301536368478763695587
7240146415768855719439062627681380909667
919217453754541347484037795905454519268
3883515270254389705865739617415736458261
2023123385901842661595874590291718631671
5643553025025830495665451789986642223270
67786092033467253545219480427942307953
9706713417445020805735937246202666297547
9588988887933185834872401842804247515046
756965084673067105391431575599526615
6734796311505233689566122247877717591893
785067097873383741998920505565323626057
907362262391783262067996761304329235
205052647000135651928478412677010385457
778826213423067983445255414466656695339
141842312235165414991234213995587784434
215985866782483573438816027605126362267
50367451678093970838966850321277464511
9682066653911066480975322626988287410
69362684264776957397567594784413454072
3508336898037514894083302069249796412540
7877775911876542850545269298773920384112
3728235293826964749689303189542699354
1012450713883037338647579968072895040
456485385448570821304759860836653561541
3891479138937841358186731677147187872478
7684874194623760468128161759841714949320
3857659448369161322656978303435848659693
108617687297457431747316394950924663614
5934149098686895212390668657303882526218
1396285389112817696953907566741135775242
8882515379053894167431082203805374556845
4943173477867888771727318973456240333833
943273763890633855690211956154314759845
255656094057233271745721209545184797202
6133972258342456593196066749175746811836
63287664616389754127839289644151975249
2864521628613796592816833751402075011928
1415176733757792137191233269357384451666
7004440441857885265525422893235241083830
1688460717585081949882786484115238919672
106562066142608344579995400112182994656
36973583783896563723232324126207276886
468150767531705845639619813450662263137
158314841543061315609698719851308472
4800846391562693843160822590257347617126
5022412085157359521898722403351333234122
3001327810719401138117111134312159455247
486230910656446306770233876602547523778
245732407735656577018075605164736527349
150447495705741735337188256074461359717
314249525409529986714489488378782448696
7464556925356639861848143661609147221708
4798231080299618565867612765944712831954
616145311764089200272308116571787182193
2262702838834559642919079198646858469170
6366476244578394936137477464109590618801
38771464969221182536055842344019329943
6897726488433435627190726636618993818211
49093582899188118264357734590339126389
8627716745554338556997632981240189604246
147297868089881740364245504221516918554
745869113042895369911205186235441589587
6458302410797301330475964375855861556657
215724522939225397576478726456518962595
1960973786696506681923544225237391994
4213302624668057453132446904117276387496
63256168897323969345739573424727544598
4304165241744848943393025992655529106649
5602715714112283348379036185318795548311
1610491811807372217567534895180679287992
12095165239739414729559230172812598
210771589425308922009245632270189255866
6208287981623121293078525988661923685615
5846172390896626141214781895752607349
5987866147181496927193092684930323734
3872148570242555632628033648776573545804
394538391122768468416113117857292833228
59311824789052108551844848827355164268
844472355937946695417863493417754591138
4485699529904510498893668192132818448
1436222263587063293082038219315628091109
3512453075623348393410936278818675162604
813821175786956820797430454160268742199
3008883092232976168522081266764381139
773930974064435697199317061605016221720
177867363342772270360962714467142689241
2145232596254629679261156044727147186419
1438540073015883531264532070121527508762
11571305703267361647538348031366237408
619058238827511273985763878326350088758
767776436169900381737578800891676367566
7403989772487452912550582259136290854703
64494220413514174386939408214028699074
4604611234187897930467452785926063032891
4869589957797046986796541488391813635085
88384631305937731593279287627649441460
20669038644927029088113794298218768859
9586516532853242971365571497450822046718
361255759644655290587496728470774931384
701142291673033369832935685901253192887
105161522416360255345085527675331758741
2645275145884356520279647521797479886681
8580898284191552843869174552707857755891
2604295552757078514232673684580915248474
5256387831264508261251036825220657827
780891081705287436665722560530785655004
942843513701816798182134249654858335186
188993495208933506240222264389529881480
598227503465954138164340399512721507760
3229722552555327427145937387879197325670
137624754746598017537649686813129173047
1559438298991969441953553148182351751230
5672523660335115310282807924112965186106
190756168039538490346698174956852991311
8731842984137555678093039798154486761675
439430344276998037252281706222462496121
4206798455583086447782746316417232454789
488569885559674713209764192720531916222
4443108678057395841073209223380219942276
1099305276802268851795243455573576044395
6841104640531116700891577317218014167709
86457193767529876417896511272152761880
197156060005333468151818655804043943733
161812815523262823224698349266014552318
647146024060252118714583785998169930429
3629937025652756608333564458792686107116
397029202784599440072622905999629176355
7672410461941409792954568724844594572
6267419264618628193926081925753768897200
2876534877077465549639921769946231983260
72162438802899921179774673448113533495
88674621404616671111637365367684756766
251236113209141423096766151720061014119
6025937934517047603529009961271629151651
4134990248479040462827356492184976883685
978153952411403975010687887685974419998
6717590193663780991235833677382812646168
6500714510745019522562072175461373219746
4681363141067195435923542253997168567976
2966426759289148330215032239960493076101
832531572141437440128004946912883231467
23853108245686498513259198387749914119
8181114367442613648079057141665099871909
3789588492928543878164539764667437255044
401812611483749408312663105571855328379
3499292834568096574828649299288287398851
114673369624327545378906784299835037038
672460748233629779783713760824684967444
442329444436501313706746571230506074331
2509329031626411546448209782726129179159
612847528763338366919695562146022291528
3066615258682367835383188085964798839665
30088965439248822819364825125354998959
927490009621580886357125871134338482476
10344752449468530214724284346765316078
583621649943311223756230555590985104030
1240388744527873727953104906232997877071
8031825955937880979432635834711417773
6115607808712778286874068367699486931
4956851640394179941824222002498428511366
560242332530979205565572994016222886
7881160764621149759947292361432286518122
787648363896187852087591852285475092591
359857925227912982183719762380179800604
9400419997344588822675336203506141519679
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]