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
92004519633355701559631075830449062909
6240769714641021585079112312587493843845
3763216299625344811395251954972271238722
7816351045116159854818618742539068211893
4665437046361825655625982549775474298492
1855536287738919758863192579971219382832
959161014825880127235738604258935856888
7631658766251493852031892893174167789860
82527952410556385348847604325175791894
482811732477633424341083104287990239542
2966950897225786689812521647878898285714
94403071916872294318677667825961960391
78856991890518997205268595665322243232
36328194239534149427751690642184124529
66765386383193760075157199412847595586
617996178636977419524359716805853772739
5260925937672058351310168972191248966742
892592429505595169437866372038823214171
4260312171247715919225489312694787826975
3059484575452335685073439895935393803380
3517797812742592229486673190476986640
8087317327986230320847418830307527444364
55252076870660268155063278395095267137
57787100754261543572138399627325951610
9388914164756953791663267021161911406535
654372491702075233612737148370958084
29189584701659269269696074233681190459
40956597043782553261927389367382929
74228597315119013746956505124691477879
224872134467679660016054938421232096602
598021426438260742254643349545527310698
858888289476615491152215751111699981267
907766023231855844070794718854449529571
3359344122267528510267029350427250926284
951371854289023660738456593438298901400
3654351027535763681507554583198512991
46905427651478501101809313185107858219
5892891738693664102874815407369425276410
2979579525941015625987467923270797052124
13185099738755736994466858013867459931
3164373697344407388130634428668616779280
292948267382296615074423853443503988568
7612988563666049943655584738710887364690
98388602358897814773019988853336602977
4137154275695394193649844263340660482714
844744859860349680491897738374843554022
980876567170387649559212448334622929777
168354249431613617438356737850729359503
128677527574884370700622067329683203
562062595918893616714917007445753889525
464283909284232430013581654840626097108
78109988804604919903195893195812512273
5304385428082100447567973485235350731167
209863914563007147463238877886248998
170789358208087251685496436248156204541
980954831657489797089659220674272834099
5260797058492528747350993428842381219584
21101105493317583163350744904071254462
4413233966053864594921572849175386091
261824954509274032039821827772744486841
63521156871942288340686997548744653578
5126684571668046037484207648015536426
201541385059554076632561938911006589330
2989574644268531503920489307952056689131
46333573606471228523467418039994494508
9833370575111394372718119547774689114954
4284675311375479841033722252367486909092
4903973429876311476743372349500351728012
644143188594452488111576502322562307391
4151985038615886205141157864820771557682
23781624929330246663073835560225035137
344279336065083346075708322693727118102
9957229449709441395746658829305748048718
7851472265453061756762259413389783592787
1234496624509038119770822008302988949526
1267662388871102678148488692734132539
32543924594770996950741228251249632605
8585261797724354345823763429639385681475
23816075209318074626826515110095888795
8039459861998040118891237344154731698877
666982305874329474113630446266255518073
3498420444642498194225432973600110764057
138591503926907247055168781883922671321
36496044739833470103495040580966348137
3015707041459566538644087354500631458137
504230304765613016387670610821285073471
210235583929746574042866583181871771370
574179844026979867171534489002459868
97388077801723369336204429270537078591
6299831897213533859081042977333716443599
55717001421442119274713757015218450082
843755589038977366544488962107111885650
1691665761486271942523663204810834577562
310613759559158137458249351496752239850
36486794237018099356145261981932026
902199851303691818456006245759511028032
890663798686628939539110364751736776791
787932347976140752187986207661036599402
5788836878382359191559201785569112662172
877614630613137320343675595389457655
6358726683104089567257097407712394182166
3627652582978035885737241461810414492613
17496975217706564007919527809264084
734333983897426379703842484952598241119
3529443485024994326918236309809386128216
6932679282952011463819851913613911744642
136254016964522842567953319612398564889
273421537480789054399154343944287268162
827854478245776556169132952358874299490
3508404429779925377318756330102030148719
1445637893175669558227918154299921800
119359222045483931434364798859453392307
63812019634131336979015728822991156809
80185367294120714538733851539741692267
69827080962259521018873996873246051915
7326203356964302629324361199938756486174
707837261545485223444222882670773427113
2713819107686775081886572228475716725
71469096681927468696146554536024862903
834516208592912920226012285267918571673
6283439317284258540779622550161824961721
346864974852618616252950680697001448882
6132204715059174501920852617899824032842
4588676294263811714650163942549655529901
417995270826437842278724292384659109194
2445700711517100851894171987790290123024
484416559188876714043428522122701906
6652450982713414627798681469588282612603
1878319276043993985161066971728769572317
554039324518960346896354962402756789157
15492688919731445168637401124275514340
1381706289758260672459124939318247686369
5538621164862249771799368955498255898298
5912987023565868505795803900740951013668
905441982348323754688197778966289762
335579729090825982285681644468680237134
913614519057712806815393696360913062123
648778160245373518957347467736099379367
70537762675329129102979545131851924117
9684393254689258585274314129896595953389
40835448579082103721958627221021787556
89924268883217861367989989153334185254
723743075565989997023076934127381201340
986294756298786910793104279569911838043
7258930847516273637189238212698580536401
446424238920455446011922147080076065199
5408160597848737380951537295524281908607
368957373678276317293707265071665823153
581848201188631457466828476204817056288
415388475456948523824497182696746036559
2545883441723752181131602764663769996150
333463555415335847870632955533138459486
70438899630381573376233756129503180602
878854389596968638027589381231515121493
942842269001783021599170728815532881205
34953690395965596116074110370831907437
369327922784753609110375022272954038054
535045205771294787797705874920171913691
89829692485146545948210958286911514638
2088647813164711176182565765120967537547
224412711958122558969277686683982218521
955786206698316368586630987995541178681
802119028898361418764040943468376808972
473684785694955669164629538761359259265
3819421726509139989143745682477113521228
1487669429101771679651921953831045237829
1082167892827977780254203416103560675398
9102564337832771201157414077158538684548
195031783264550472606454155372339772273
647183884891496411342269097692897548
256327909547500769720455802214479734092
949717274123563882083787512857324773933
99555108701491554631398440967317942092
79246875857032641330817203272479836345
3546280739245169862098786396804787966514
5670986883991040930413548268590931653794
46244919405490092809025891820658593876
8187252348032261752677041198964188359015
3356970469367485512438258956042429968
337496297296031136973414162349655578701
4545107868971449711477002336931111975825
55735301982690087572280129416856352093
164485918746040776374963620285230371273
9746664764029886303745003804244398722374
29857126845494762494257896218986168747
282264364334159087440383683767951459267
944740654289498879119699181685774417343
823944747019483449075115811387612973030
1022795132591108884012793063822213041393
855051197396204020149091659810111626842
3928763058954824105375347671216660183271
127058742633503825871366742725688132
67949670880875429087615266092941228684
98875087161866564668055507211895948756
939035684860163667614754131851521649542
719310923588543353538388905072772555715
855042008156330549648339956840344137500
1986577136459741117071381085457242232666
587874376525320039937142223199061068122
58246925180352236712468954410614398392
446947582365480325776603429705397967749
76443126104815934765797679679854419312
65992928627276751438842956136480419815
78696428205462296383153756342887792266
110847476060192355917375705811609406593
774531741158393317925294223664710434051
487925445595417499189832537729604206479
525971812925706617737759760434088012315
224473034278223075814978854353964308897
7560704543273819174697815816833146164782
624533476950871110846882970630933182810
35943724207147182125765309638307033911
667227845717330126416675981669823902
5551197380022474248148593622288485256788
873030923475536087713242012324851875795
6031239344814732427337044182604119248391
607086388277546643073575938525336585055
215385981430495912433843918264486414346
345683139985177408291042593495289409
85319039762384560239095696368238954229
750428047378626957307031210114571551257
112013766088491193775249289768224431566
969560929000491461854478743586178483944
9980900194814885511357193066463912263701
81157944486711439280942416663082855914
562037341020864351023084336093116757035
74782210247104535927441104933631730230
3852870085375858851445741341244768964216
368814373634455501374625118804186225011
2731575999523803194552256764917115807
971535381167798802772907329655171888921
98612646496796683036623872746643129707
16312096425616256179772351363714325120
733142237709269299793715608442225299323
60963819467865585361620766065844459565
5421736472482695532483224297939089369236
307378631542182652417574227124607123
621298895560742859214172635952882287671
26598718196738350932238355299058102184
9237280859564327345469199361805662276797
50412262953886498407992282445553792989
7794784750141352337762451177914418808068
635954372728197056016596515395928405388
353461185057900944118498875199241299349
9835432883948096602531981914187237191570
5858836442951228629443053156907430885222
793555333609902142998478273802658865811
684118319816189019825297617193288105810
1765568897666004521776365880886318815
5918973860855516904911018471577974765276
47754185158276144565090523072162558453
70617197465342710084781770212793565638
9867304117715739907999561036677192266464
544986844692187616026766248954197425125
6222957761514534793687425109923560648683
153864236087155788563319886528266648700
255145196422412621635646893654079674038
726048052418864626598811578650681469068
3293707884916248580268934295940997684391
413493045268342184752522078865622991480
570895503859195831246941545884551748616
860069112816821734073907532347452574889
66955194306417469002279619063278384518
34512325274513294509064124979476647259
330653487951860560455803154156555601323
457826762491987531331128489590371681324
4167769314603355207486929760923798737482
6154460250442091695518021797816418922
2152861360649453798342643112952920563946
30939312946881796831082334479336617150
955090622643658272761260368072224770479
841681054628654088953360213445356713945
466338861911373853214938204933874443419
35434936412425799736255371898480675882
34703674435656133151053941256080986463
465447807209188256565733755590957697252
6147631465671391309626428472697530509115
3163580643125227106232176205274240348142
5088604517683019987948925341267487783
66829375692164741111858591131227471755
86131161130553335184757534929089215356
446320861124543360519023772263376336062
1980834361637724511120739027614980844177
439485843137593451494762522259598436058
7759899873021819883182699570023083591
5352365337729663230297467558639236253049
637547835372722466880159177105996458234
6768063491584743005832689286694918092
8208858947936320261131143882184155563
16701743741114269681663310834836905569
78224326335541029951896993285497104138
8725988329341475813863887983905034846306
6782811791727645215894748434583847783140
406558357450867567265094695448932416771
842588696477956889317494530560963715930
2549981954919413550261391855600616934532
8721685287682695384265985123967999842250
3348409850844572239179134436770833149467
90442148887934550005238741857596872956
8842248837921213304315933194610250676579
48239055008492421255819306395918925422
879994157971013301698482395694490243876
6272565874894081340579914662385682633362
458550771344926630563351934913571762764
2532527065288894324741905681661831032101
61911684609429898515507139064991844817
977968833339250861745684656892494693704
5435496485293002458176818273378236014870
76445848707717484887608037789790773488
7122776454302254202697332943546467913419
57215460894380146815568350181819221128
3312892412363159251198799127432255874386
14232533481734257667093383740143947396
49271513374752284144585162410131629252
777715197197436838735411933925029037959
3793248487950768585038952796038927975
319092852080104276801058461293050107921
56638757408151072831415386215313193271
8864353299105986589627054928275958052380
45132073042507745123263913583853050935
192285868392666612033236478329765103996
549667962197470751942179166144912498676
86221840108213238074826414488997128100
44258789242594740305723235460838264388
93146711991751131460541904691197681241
403753957220762085063926907853377371826
8493840829277504894770518651921745896755
6735909996908981358392431972881815194613
7444570126345947882437864910766533292
8007300964414386789915548659583443322975
8995430597558933568766162967253811083170
632495368102532889988367224264961615500
7767642585909235410139769119641791167927
11471772860243684994809818862973872259
865346974804808853122314981985388241005
635738951790826925425859947203680425489
55187368117950523464689543240614431702
84235754081637876882939923894771042195
70278699501729930633608477026949362642
757793097765537350831217139606822749282
7689809693068046578362136110411792576420
4498745182892364211742987933473320129249
65837099724784099493598031066166322791
198110363783412055569813454389686209519
614261185883527522681578140726970832923
639245245473539226314508424418387746792
594863002907363183862346822199659305765
3873225881631957968756683210326412182504
196711401894919625271163364909397341539
956133329237346928954935318296419632562
7431536478854977864496953352723337717543
95300952881842410410275698780486612158
360580970463022427121076439659634736331
5329895730749678311774056982127349097239
830143154411158211290992117489235523928
76189417385041731020482615894985222769
8708426068319729616718689894499237776308
86923398382376816394200893066995019818
69069711051208477398453735719264309733
786739958751645864146023866929811493573
323337341898991214958431821223238027649
893167004179311944475764489804222614541
2800483573694875723496833245760581845362
48505316550116993037444168892484196702
68622809882330065619862638798231508633
2531758322567659331353744408904531793649
95738176729275510625894857791326844712
95641715714874351456731367841565434308
8227946936018477120375245419358468664873
1111122595256384159971252944113982522840
38149824773623134507198876375543706308
915865134171409746977215180975149907039
199234575583859988839522301214484566923
2172192197748380895978347828364157518929
2386350696776647235479681400942812313727
17253671535887377869347543934301467107
3031796066684190251520415550554936868315
70247498755360107557182726384772392561
3871315562874658710279494725715094235752
3230748446559734351572211215046326637
167611434195607916002194789887710461168
691095764118792586558104129313653499958
17811397941564523976370377556261297203
45509366259240070008333242279234614902
65379497010933180013096263194928369749
378594723707284724317047716483065977969
87172686237541856945617302679976597
3725868165488798333761673618932328574327
7355752962063631225921458228321472727
292855513611403172773328296729815366863
1736740742655176747065397843381442358
6112266252291638668173062094413389295300
7199185447526653293655709638408126103937
1952169576248435497090704061293464152952
8051190526037543822876974394944156865
2587919480784441125482236328438201825
474935063984496301476136417950672482410
4690654919934044178833983876678087817
6280111150732262646513737111321617535269
1533502845601146853832873634999218631
4806533894273579201277733149246242541974
576673145118996750249652872704880243779
878777679366284298794302813348487111975
6572698121382903295450181728599231932711
372348777095385962845717788253322990
3786484223199492513176913628692929781628
58241465967132798449047792649348435981
93787680585867195668163910418485364720
782779566739584984045045640561445687269
8067711476204563347910095436144480904943
84327086810747326474817256526123270370
744567222318820249059048285436857235346
1105578743553179103971168933381528386594
49044427684132877426246771479522983776
342786212347789717761052096524642449
507323665282704690193036089351819744613
514018143990484866944151525269072313672
5739127214746190307334767835241198313798
5240168220684639960480358902122880173055
227151085735935932546647700669716697318
1722238656011964514274161324344020439841
7671294658441801455468524447345519654569
332041845258769557082591330708056631413
109463539909201637363705030755427689924
78401154934377699941465773446934227536
998414496576721838839311622910783055810
6184837275342215676223017047599437105461
7571736116225497434208323306030045993
464074440604643620581396560373431638249
632951908633142367761813739366564612540
960011462024112784838332492195013678401
49652651326692495398241838849156961318
5953302254508096159163804894452246199560
52247335609761227639774455309219202570
9567950210183781353128302883194998623403
206722463676986374123906855324941662971
304528789348542328096433803618980571586
43953519608565580663100529931383004769
733714433999244543875616597435258433022
47899852871071873836196826666193822405
5464672488111164133224558022712170892353
775153596926538877446376847578353566672
70309225650661452971317380230669179130
904835458467748517818269871674580888745
1202448751088661513554416086915780346540
211125158155935472422932741138559910
7064744632249956351324494243611879186564
418187976602499166028161956559475097428
758948132826499163210223616985968655266
6360101267047203444387163458625245065328
2425116272653718830257155318836083972432
95795568834386288318442481773168724402
5791275734543756396106830909793536481
926355817091306811214931107158031652959
468597152250300392359648803468644968119
2999704344744970471048444235959093888209
32192163745267226419698646473941428595
8225116291318308531543745896307395116777
5861745958054342281465448528251687955352
94792132131148937889149502756498286871
152395657392667378643946739295934112789
15246378139993390592902380172127074735
1305918780285647201279254653354449302690
3772212861133488433460497169352378407632
154713058332973755598088331346557595952
1716486318636968943827669925662531689584
5513463473243812471955068428779080791662
7216281443076856123577362760515519877011
24723760704795518240593186144092219467
6572929872105717482732707789477983733862
736719636569642711718779666347770797
623024548420660646493719211357399004707
7952065044959464869759123461924016262
7533247230236401889427006501900766878385
807983591976341653853915793988171919628
983650695504544522390191649106844928191
6270881937467290485920639746986896792755
2942772595766553210424512474396535958468
43618712318050003992265361889364469164
221351678548481552524148901526863688525
48902028769428643776148053555189716783
7688437967166157748910143892917427615738
735832473276450759212868696640482265993
9111839753698626488918257103844173482574
160366894704801663797861476175320083424
969650645700553747395024345174486473
9612494836402546589912543674421474495752
59984788436429569492000800980551776621
932076483169307122814919258738378287531
19441817191266531803439182001039286343
76534253119319278550735889831262863443
310295385557147862936229920241109277500
8047333010417689804999046904877033676296
604811719601745703051943224245725335433
4852864572276735437744157192257440154140
58067571909277281000095873571776172199485
627963577459129517856867204662427625018
811784989074608034595093608237090288180
7301813688723194475305430682510665019
173654757906803956662120563033196661
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]