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
312722066470425227251446475926136763119
562738217672587761313831510837563066422
822174288149955866976388102392464476508
76275663834248995818871791015624803486
422797371048695154652503335297959351488
5729306262322612536629089425236383288953
355031625731264737471431659963573750
5171692074148308223226415230476061671571
8021408937930352805195292697166376692
6606947412338210863028991380344062176315
732031894676620640397317787561526096498
2798243110423134805464454584240960335264
950130430951859384412115414154418148338
3757944421392258265397090215431756383
3111522782782222880050755529185184879643
1881965514125645864948305692671653605708
8666211390423724538326133701318748336227
119521722004387596824765300925778096470
298230093941347740831245641871889765666
1944747923634988338688364915789725336256
4131351252201562552121087821513735134448
6591263284275900932011963458422520146785
2650907490491012528125673447552059851
691338429518404575172444983909972497605
34675930834530167551103423183748889702
9157819448396612704643677660827591136
879272378032671453863468902542569522885
157949883470920758131622621916027373721
75492040412747420774389831687015860123
591181126347837617381141285410682305771
1881928187969047648134728251201035668408
320498645926583343506095194844984541332
5047920078143293189235617534969180006775
263772773041590657341581950492596002764
740127125014438214607330787775693505
654660611496618383885155552547847567390
98885171130946985035451814871036162485
6053914840287741581748921361702280715330
49958069169225866095111847748599743292
916513002217118285703429229118382624253
9220947311188512146515133281552555934743
1454232114671358294252131168280866547586
5351138669404761930517174777499481481334
43425868876567249796307048438678998155
411330136249395794886367790903142407583
799946947317466462615804616274622835879
994412835855806711214858395563589088728
864997961875736862548377841217267614586
1382528943461145436069207457879766894
98068129969422629681235717947137083742
489553979652180032299616670148510878810
2463159574487970623463644654336559274727
817544202285551721661034083587374331020
907011176371717044373720424395701237
72785639956812046649645776523446297747
512995731048521620853431405486721232158
9210585680392720239853021987658793509551
60526373425666235782912642453189459891
54777611027042938558193147912596131619
5011382683877174386363807277850291222589
55956242701382548269104858178960529263
8539583179865404104557807267511811141030
2654583255875538991407869934466752613
31105641328696228545360866071465949204
864141047247888887026012437959450901269
70726264623855786509376298942362678287
626518940581263394343569391593225688188
9462105343248025486830104234493768947671
927914015314706370668124473421994032494
5702247343939011156522577014360556688675
5186946295247378973355172709160035034696
636631328827164264375859018440336501994
32773569383783935808340618060091444112
278750107456872711072006325493452064434
61233127585335247908681950894479521364
9287636375855026446585775747350071293857
4418110946087899169860873730631482997593
4774295798126226266313126654524783244936
6166765264522701581145258586973979232133
8929663838145626532924853769765399508860
503292865822343718939199216488826002573
5252497278594919334525195344518608151
4380419056486386651315086505276552898057
7392316493871318662774670622848172662
409255527844345296341765076478095362162
78098186406470158760966580566446631201
668580694139314075023667746960023541768
4862234780197963225399604522440519239502
2910453841128577237873734826484285215965
142369401250151248765083933420541421422
912794648629964996198955930293248458918
43017976765317094908160631582397549387
266211637410659796797957854171886287259
628024046946698422221565024542595475137
691527692920767272092613959370528078608
831565518812705825636082063544283369967
9386589681011478721173165938120177754646
29655582669106531482223392932219036845
388043948317567038588186842770675681841
370553946618113367110007226976923456937
7749611529492647985694278029367871298568
87156617873571921267134951727559087404
1117984511341434469214513331348032303389
2186067771277697633846692680688125271
341786713387557955964326615374934429162
9748521980512049653562105295261098441290
430393064356847803042556197681017999120
233784793125665091363897969256837284934
987310686548734224569136552436413475714
6530176630493873813331413625170340884713
128282904289831233053290762675853226
36883020564074455149257484219645271458
6893753444163815655191949287884345577
40083679023268145585576448955387295679
84754288505111913531901248462897224112
3449339486066969491079414544529615074010
9165784861724001859570845525454986062570
88531937396940968980293149946244886082
9542613873329089895379143683349623307450
7703801087699150765121214166822820511056
8231961992522060754265794327160536489928
8694536933301509390913159855471612207398
906314890685373692514636909596010826153
370331555973872532603358076845348534697
5057736128573712401115569176817771674062
8515357214849297825344495969457122931
54666722978498922617243200568736141178
90499654792812885823155335920737033896
867818882771402546438377120534988024484
6744955973747694860744707624603046828496
240273443906144710296699145132589504188
7697753193988263556623035046417959121127
95636150146613206184966350852838107424
7361347732604235203237333228407637594433
203146450534540747943069987898859319817
6715525356859312613329995963676435827406
536719422072304768319081954853149634517
5494121839181313247738407833197684494675
505851772873404295631492875511053531163
802855009306790272597221764687062816461
97060654063107935167565494999246408929
715983311813392573328993317904968656791
6929336880833623260992092281284452281860
959055592079479620802264685538011796548
485221469956892909226099555562528862769
43937985248281751063432581026070142308
5718929685406285379319684346740839728227
257020345114390279433769901198143565632
414957820844705122969887004158058741996
3591197311943698670359528694757469387035
86347311447220613065019748183078611563
439444391249580069203447767955916946077
50680803662757243416716512906255155995
41103175363868485381281417527680558759
327246654020976694971867301979374222767
237415444303483577369958784767278645471
3636380358292327475726926171828086417128
9415832772228935369432605998992782177161
44029077125257890459787279874314795126
3822402229729600318848497774859646807126
800087549240773071654405138699355637100
218747871266900167851158566608832051810
511141998749247049211047115138734526292
7127670348354076712284909891510757469934
51975332761762346614361353965930751891
566982835649338530228689717284924256216
75769415495454184743918820348941343881
305471658754413996528624992679379229904
488129921944483513733989354657334386575
7808524494597171954038439642450497348060
65043376820714591139273092406942021070
686326352259365548101826224745388364530
6242887618646340705418663916183631836952
29130526287687761296528280277485314866
6823965524337478144174543047699916483098
267321560555153584767973890831658028978
570463340449280319287515301908558031407
444170955149699311367021842230773335167
8395778127675729688616323897199827919028
25161428337926699583094617666497086525
402567422797266243090262441204110873371
59918044657762683869861234606973377544
3314593311454704506420968807243538038245
24840938319560650020598515803785868552
21608427662306958545643743488088436403
2962185799144287178036967736850774543020
3605298946957389234726159216242342485
534621446678210062338472964544520006422
332291603264042265310142153531183553405
388074269130602143352788595066164617456
49396813347968726058992264315631665077
15748341550100056686871348328218283106
2608788713562759805317559225389155406962
7092772654262958764887143756981711316368
883412317882950311188849484660572824965
5962806417387373321833361628846222521910
35059658819107351007017083142544488522
772367419665588497234664209864875447520
7431125389689548222449796342424697211809
8730833865295520765445747851942623539903
641142544538136155064372909489684848984
166145521530253726338276834620856395718
824871681084028576578722899631537051448
588420742191519998272467714547675584899
1546678547613637461791415641630994358376
73051320157265319176214564470526909242
23985615873317419046834444053143593889
90712763859157197146133174697621224314
3124332323035490122499293599588455235875
550822727074710643970857137207426968957
466774767414922063947949305654624452363
2733533158838186546551954831399540561114
823312922583470500681752384744454326949
97144749955638542540249479505853088863
4470454227777482522771019053197112183932
992462203038322390111323662292878077778
594435786248677715491432178422427169931
8429983490691596886817312640596917947
484426602280232390134676526539150343266
7801934471672094121962298316992134767801
104240606164595251555175304233622194969
9743231787441946201266353073604350833888
495825522374332589501907354858324047234
49817509622467698435197299056181944485
732216677484214598697550500597013568174
7938638177547975121338164618414672479380
130191521556173868041495343374697227344
373329843628390645917244583568396418713
5894348432392335173192931014289458147845
68739258137256518429391551340710937470
2212942991602543100282254648833112891247
810016099866090394350608145988770695894
7837402789995225421355715144468518626123
2961176482437026565345082356140789814019
263154069667189534762956507916261043496
515965538261732486450092373550714972939
82006315068769777906725175951399021883
40309420796805644926012158055825281280
9111655113717087349297128600186070625215
2769252063425697685423694492885571028763
6223808045449087524900114536291196915
569260362923938126139775725310212727081
1084278543458974283523171263928925683860
5437661136493555413214926610571498561652
40012143772547456826135310531486646795
942827052946210717943637276936958733115
7063772186847643417458024974848977324194
93533075222786684618786133172424769237
298183582522061451716665724511316655928
4882873197284529874227403380175898048715
799391332304349546689525211261892738813
2876640221674823678359196251737133237963
8917764586264679648272899143587536351
15079653744794788719758532750762127266
502453575407606806621292406890500244
65042485461182326175433244153121708678
3055511537172217217827968353635882665158
3764273074081617294042318771286277482196
803523251902334376092971643770289437026
423551212963440970753302695541189599777
5784956771749954126242384779209756915636
7598507911712027802893897857309684765365
4302416878675943334320538959264340737042
2695481829879860420548594053352987242114
366449447124241284027127517511274858812
300122756487144027231647883936866379020
8037629911942383560286923217195251654429
7992341528466562887653362931605977023467
338220179674111561548114447136929016761
3930346971756573643633881222832195476245
104733867518708827043377128765534308925
3270988486631836773652603073841215424558
2138659151216418375415159132791444447760
90226397248433433055114533454430239521
866974385202470845987212821704376587
323460319665151686104335285580551331762
9764784187079988366869562879958312813073
8892927876909262329745863929986690188105
9467767566878768418760351573264549239199
3435263351326031624969726610872483008428
74955192271753394412565672913853753
3270486341933886623933865021921551784714
16096207588302837595241063297491822724
2456199214511609179247916482344932224072
5090224297045654965281235113070105898
451795463224803678232777394189725238192
276625212905532051873381944621613265296
30853579502173438459468348884095676398
54316290787487044522943559495094031004
920615111124521295715013296835743109543
7698902870543960745923384241490552202445
928917857756645685084411441971654589796
367256490414867827762798116421457334007
3019412535415944417264233348669855909975
502710252657422888577424645723410601461
552583552699372456349885769806536693991
5219984591463629800117761749729689502464
7691755063893332324766682603778318691717
82739052746520796373972720491168096451
9223503533176107519214942726406521437111
874729252877840890286204340885188989248
1540665204914636277105982417068981962
8837779966275242948017183373525497351787
1065029956053808923697625928986495555
908908014903101268225805413758168111964
8615868214244549589737922086418237305042
42120417357527650022209383418596525957
946802257659505537918162050647327526199
1693180194518163112318161368710828079999
462261486189975683635116155296590552370
2993233180128635731953665502560961342253
631966526701435117628378440880891082725
5227726118079837150554571815337743734907
730560432852789339839089325985352283994
44281602490445339992789360847461028359
160955037134271493857978045865140926663
6653720216486076753784583988569244477700
825101365977467695587304487662860688537
7455780117177647344130421477107218953027
662677285515613853791718387797917067
6353294653529494186281186464620044757975
9715380433149129473381158942880156559269
7913327860572362872555341878305483858134
296585059145649802977398686244383137545
4024395586067648552226478867699227918726
28609323795811896062541613775513447744
1231727187456529923323227485200377219580
3333169699607029155772963797439834534770
624632630932255936439543652166796677415
2189413098944435720775143446355025749070
513099145595624166087913824578293448096
3776690378814361132077634654799337602384
18691165582949189062714644248291808577
97918723462364528457767830898472509470
7449295952547922611553801306283012603355
9578210025993698489456516127976823992734
5145807838585379354451462500443459502261
671595118387250384057074777852120046336
4462077313399140676554249957847787174
478042991482002694560055256753793572760
34174709474504460444967516547333755458
77669210539885660120278311775772265617
5213738094181433781130418106490859088440
9553247561463834117672012342656997748607
9619253785782640240734412927388025713829
629419982611701815130655666157045850
8875803364305305506822837686223920948679
955383532957698137456946641767431787367
9019476166913661163452622252867186995
727068887696048811744896691595552579893
1200941990258038603087585072540758677
1807102540972335951148014916266110169233
1181776693991176846192026088500773343998
7619849218663665973424539771435175170
58729605250235669298514731538767400122
539910745204296939551885230436847039074
2919635229446413566442158346253227909465
4666594490948758993921233730320649991518
7307354216782966514280969493894071893523
32138290418190311783232769271355532290
241481713769426357616629177457969238199
9234576137656972348151244110571193537570
68573054491208434051425639238370411505
4644567818641053132944807598178835254
17037752665449733273384719406676257526
2550616219508456282417972500685366016378
938163484255416926289703621217813846692
9719109819854324202764588069831846984764
881898674646351444621919753360332681234
49081871543498772623645979941171366243
5187539310164717132796868344839984022776
41755719740512482852562404669543458640
2676297040204338257782893026214420079786
8422811015477216857478662974795495534072
465866664293358212585065473834665572322
97934097518152173429161180337659882500
5392810416917319311871790567512283997
9562221115691053166940177393744468399302
4658755367395364018263882300758803409
8172168224851380164526192903319415879409
81389993564719005072502412527396082185
531319572336860994582875662553254482188
739231074653597178089893691944636489512
10671049907224024059287469878429363466
9971197889582834479945544194245352867493
7267948182695344153008027150114164958
748959167955101891790352617251363776483
4813822049551144922445870919832787425
275439558236944761543495485375784183199
36475035262146827834076003167529657520
2882843315204395199928628273464386203430
800560455586313016229895155197361461270
840359454918968144435495996620586952517
649129264433423762162078991499284098708
420736748153876096629676566832546774
785871304442412989980001149326019543514
4129908645264720771521377821689897197846
459193794203619310995288618518463803866
3009963621146129213177859862546691768630
943892582494780659865933727610030715245
1452432326056305364592182523528746651872
2646513918405770729333503711413022634895
887018078895927299504231756870523909684
5489507936122229685043167054968672595262
1465991265413941333646033314542441881797
160186338574727691867549780174156196245
952399126512738039465462424280155105140
1351487067544828161192973059268840193480
6619771539283923425869429637042563728
56124227737212188684767652395923827803
2918143167653476859166737029602469275247
2133397897683614104474647857054732811
550076371711213414617694894040531398572
269894011473814059814054221535086356005
4345308761497461803654992725147744192217
953182631171439510544928730023233623149
559025597208245046185963269353323143904
9015150298247087845727363042262516924282
2193754593179987126186231892917267708077
682755702002166652631018405843525967767
1574125565123477828951323978768528832108
95953555875267620449553309704585584627
9815311346696074140661721139417333954164
3793380531865827385765197865772773875749
8689976725791248146750139459326292084852
465838932911811445958012077911338309095
4030736475865714429020381820624877088690
189230321090974135056226880980626257925
9122438380731692784716673067155938845401
228478295143640524089441487198762237962
4525515871067913413741504781741953101354
60426719623506762307063175235519651215
4743692188018511048764210338615977669
7895984823149839203987174241152420291613
1538288182929789702352372369594198686158
772926601736249589697710745143227437590
7330811597515945364722654972762161567381
2803263349022923589821910338367435390
880635176245222167597191645153539619743
1968451758244737284808937225603348259
9164327096932714522739931414883528282874
191132307499870145026034349211223833426
98495915415733775903829397112847538000
292821286337915686688419193873488191145
490680348864775759111121691469895405539
68702229164741401834839773253231015436
659226529444217791302565806570674427474
1701450042022631158595169973595072955563
61678979618572438327504520568273124843
16060213513652751847378411448354649399
571063058985161385056222170883632456427
2891261543368373688396898669650982103407
5659982129967196624221059076384935087757
60301503601854967530609449649264548747
530732138066402465649742772349945696192
1572758711595925534625237653752824191979
17173968861219845165723639109956688499
4751832513716392601126358998421822154204
10656027368336027493546891164843448273
6998483726686747228658542873731545558781
4423450187622879828096085155981450194326
313222894644264564972795596690955398210
41887658275114939131618158473128265027
972360133995139933835798785764440673035
989765727234483693062429267742376129699
2337306555524350235592662724839424674491
322515736741767252505497613591144132523
3070262668841909857550383727525385288
171227745457832788874889105130444237530
252364355440493218284681277445688219785
3426524487207942470448014784599644963199
25103298764339532518886340486377819133
5751198650315509288188524974859569324113
40780391757932865248777457892672929688
6765866734006647431659936721728835709734
753194778848239220844777304468179749126
6261312061322699616361487974962456796545
116834275543831164033477796428540919188
8603784986171296747220152532416051672957
428197693128518987666062179155936522391
81516683798472057161913039969095692952
289139568063535012517352306484240056810
3483433838829671451792780010615066433
463522581265151413775256926540456841794
164497126962248589590769906304036125306
3160356411928316879985373788333129265952
88866522957306588245842737681015924584
1665130190014132814110387209531796818094
505735267596784825942784534794515416717
544363872134559547738635445327266839
323740735416245720302953819569810281422
93567746733465850546729136298229435947
3008532184818548450295051242657114501715
188570632159149768502761346342589888159
7544387683242582866943883937247248657114
1266650639673230374545883246337911706774
983819533589197521836799687856135224168
48477820455556864978382435958645453450
465023324950529010631975988670010839589
534357794269960557570154119868356794954
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]