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
7479779538017312184556289945363463719097
650651999861077268558041769682137501146
14001316584327786936643595214454765743
454982183944402915203568169997136283261
9311233685573303447457892878762728628
97018520789613164618345854288757285529
390540252497678733825881800895914031167
1542110731622134863450166654210853785316
65589175563825579791858793406549343604
603996639177791178786670190761953562193
53916414643692743920904201587166679231
5793631274052688640970253737527868197484
94726298637184722559800867067927440105
992531015614318447856376623165256321031
892452641815444827251962766242784274249
345729552943150597286515746654071137302
7513882823366614923621508176998434653930
908736103233858376928021461451552115325
987237113991748434048574701230977618
852368925407837432355407641402182455346
5695225920392124525031974916869758646704
698773187866912763436193802722399731922
6967689620519265755093493006431835335406
2664135320368629502682387393135761001662
397315551851848323284970410388051165174
205957436191339316643115784147933533821
6815847074574064233658328107154242727355
9754989449107424997899631729673851172047
23677141349335123013551611579119139221
4513641839361042601626786523322836886939
89562591240589785417868274701924416012
73859219309494540911991325982630494720
7899573460966785817290667377154159955671
2604398569818534843329059316370750904300
3928328580129713568216339895909040201391
9324585290696194805430706240672634503895
7660361691038384110228429416341831328484
3917483085259526229729093236334912504265
8076329733348830196443562432474229324693
502226565765902674352622315547245932575
68058060287647936882579531737951578327
1404986724568612258601499447405359311
8348937699087341243280513792935005845
540590612626406250825434794588675223444
22931967523473455239914020503142026
705756149452241285127554513844377658335
835299408262234379081959772693745449276
92588281292743779759823875798365086031
192260431544412403957056525747745131
5617610190396224585041324971795446421525
3038322557496956473925985911876892816731
909775317739983943633183283967788270
469644436176253512522702494131013216332
488379758085877972392283270141959401934
22627520580531738953630567560042462758
301376391541447820192831102887634554271
449851369392566713680049451541316085131
880558355059845489562924701564677128921
5767689572961613579950630750838384851
89067006712500723290521334365863159480
417512012868145245284712806035537910737
971192394871854065424601286911188931472
905443295564179819783262294626693774312
960148821831470632593788535114242777249
847471124118897858173599816486858586760
7539493755642309643464718306910326508468
587982789046948526332729624181572711897
753296544560451487412945481441338771670
7852783781601858103563361270687238656842
485014242349964215419425192877313374084
791897315019953669542435871716095413212
662130921511185526416241750578087499042
6262794438898724804873875609108847119923
210092877969605235488611548865759923806
59777187702971543884854664963106868497
804063026239124636882094941208254428751
3409986778329319486924386114690723529849
8436073102468965222618041829091086415
89874044794347531187170839645494171580
6820436421239748796421714303652273677
3920656054072856470730016168980013826000
118260545264891445936227666898176904225
279213259683004229563738475312138684850
141261337738176722498715673725173178181
318974643575796408970295431273879287026
31392011994115972958784802354174318161
5681544344346749775082671739855549581875
4774983281706163261424835106514590844259
8846328786667162978456602390758886549198
791449796636515926077633580521675306773
438871914633958891420573172428839979476
4978124069814141342357535828533744179087
679420838366263351912945785094972082876
1370433757002479121475171015845754358188
2635782324093996274283954808658127608841
7091462233907465384112978397998849726209
43100085299237732151184693471226803398
342137173492262065783891428150828015118
2610388550522633254124442993896753312719
9348436045473480526390288252243050866587
68136099253998560753558390464899037846
3807211783661693244562855768623330306061
338672137268613425317356004977072796114
774579854785173644585139866883646657769
278475592200326754063009736908173332204
9235418750799990658084213588236687971702
88912766755471600786684406348125913753
6014014855770735035730796538627216132
543202171749303224328954729775879694205
7259464658475315643565586177283198387204
6560772897994788794030345124431763222
617766549547293742326663969392230979898
486761233585682367616861546934416084429
135293922386753811703864299704391103598
96805827234184622756599869858647518
35344047941876047729977508256979913086
688963383853422051306220685712059526379
154796817811249253033034655341546925767
1181693872955349233373748904630213715
9606559052547261714682442518719161429780
211283238538192018137568623874635534941
843540624740451596778905963097753932675
169151051536539744954256463210944966660
977543087693579409758313066743833277041
145127541378003104363888963905879938841
5529958997012946680474359822556648388435
2952706454815339673869956104826134378365
194246266355563790661424975678282152247
94379094032378411623687341211247317023
76558197976558957228448239069939286968
101336815710986163057610407565483355648
461710583414142159097032537352037159522
387884831181217622438252594535459354909
1450797578088001302359923153486668747252
219027843805165502990901808598333861134
8143984545197588777996061977808492559020
882220577379615759868164169370186533268
6101745191566408543268358964178759482
27976345994578512633759864598117897929
568231581510929327914806510016187735261
81126652260296871094437314022389711586
689349415781165949332066082495427301810
581719783577927655746018757354956869749
18404679854885487169402982471698268965
2680900936697490616135197043992617827075
78387901519537791081670385454419863507
98248655992594546236441573092527981821
5051557662318714235634143456283941244157
1694779687896941906165371387122135054089
51229600112811905125696930122339956201
793055914613111998821603582647127078760
455124010026941618697276961865264615526
9589351159953222453747358792213472737361
6852970090387082624270897667420965896925
39369555046741260394649948161694766759
604371516317815287073569228895477526435
7972891639114248327017693093352116982052
28552659847841563551323831334036441589
1325332055957786236965729983332891655626
40569940194465995579434431736793955901
6326107836724903655462418009987637206307
722085723109144768371739935121887932359
9148542570626244906717145000437926433743
6348396010798730547881136220374583552761
244688547446898147237085269518666141701
62863822960128019296238665389402677
69169173995867933725593749698693985818
168989877976652430209980443785010931152
9755926261398192412281693372662268461
350837417308669742278882254074051121701
345218195366578481493197927142875582
784512891949556469626104895943898252133
9846631215351385874259846638239162226531
377651822435892298389210856939560968
5178958588283509743117039773154905447
535246445487525316085247345343155599457
853758552311153723975079144338819508033
14507141437466237031475844695711994322
11373220267491254460901907579711085962
4608145538738989760845943036990013463654
5203228952904523772348105862344527646744
1513183924699579873059687902929371329393
4654540121611545123838579271640971335
3688141269335591342862699183868474991424
9155522240812925605556523737291828137654
6720561742583470665189059656770529998136
5976339867518501589350974049596555682
6043906182547402776326788752152748153405
6902943362918980241492492304684082428464
666685794842529531683002575517226865978
3539353180363434565847094842954446662842
80181028780393148313287471246681219957
27706088561742832879565830880434332731
1693761555424011290306953682761798601
1401849562915764287636889699679994635
66628439358055392386750758459356362336
6694696642521055779344578621364093072740
339218241834742334428658518528066557043
5941480145056108947231411388186511126303
6348254617727619630737964635304894601648
556830777445475013355872972933678629
2065776526423698450143644817293853685213
2298815816673522346076412982562951939677
9870770333007715264763755200829575055384
24663206151745745160296976596611806762
475772513181251185518017882783984202890
8136688821889060847018766344378024323024
466081374348136118709600664987484022837
1845882933839878505827416282679969182806
46750155611472279521637981385424598465
26988825203750039423335263357227708876
775995862077913891062519859224571550
92251672285265351473126276237777094956
759234604786962789140164513510494894105
161716809152100283434758653224668164651
6591120386071729184881935936496747455
2775648871354965204392815338884196241298
569672009485927033992167299603621051664
17022293125269457769941485996193262846
8117947859428773262956645907766635917991
612349888363532750130690992585967836
9949455490322246205145399602130331237439
19166933263492051721197650062072323
854283954424960225760914386752524157342
11732896289856200848434687399282809616
57311611123119367116997350055234865574
24496992399160966851335691734127766530
67861261143885287621961331526274804843
9304965949674257267671982596239850227185
3073196478694802887341894862822991207255
3497399369763823185292186822456642797
791364082003505611154809540906198728832
4266234464238982883262815291380691823398
123845739164275914838177617164136265051
2826846029714412923719914897916626517931
105174350764984661087568942140697715284
1947103859296024190513627928705996143824
7363759385209143712520286678426826733185
5270848252726558908576426238672848732761
916483794172800513842082624893679497778
981181927154023527138593700449378126642
10636034984735421523199619479443585312
16386136282875419518521915520091904704
5780494178093942408958328497848745849717
563265953427731919164141603699950681827
49748400801275696588752935826595957146
924080853896432857139269590176148688142
3445445378889492819117982034807475976
4656896477301475767540646041292015175816
5386490750852752918262523478773279479863
398772765267779233398696526747457003428
3912625151852508618946132414951393454565
8862690465701774618454328130933718995969
936330006043243085961585560870136838181
975017173251602303086542999605273094692
586588948186737627869673007267973893981
2373116341794255723573499767936449204915
6706766587878037628945888045250563717854
780744490313614383770916285485693414882
8720968680084159429626256488240116827295
545893472997555382746053677269889395690
30467444730083475588888835115452389529
8830799859812555980011795295377578808427
5430367780696561673522443599110663069
1232571356706552365961209973662977946285
81484154624512705243911488547925915377
7349175546151925428668761091271879283757
687774268476553238988489187860571207965
83023386472728650762797682758043387113
29223907377344882026552211884987729972
86485952915557432356675725554970353754
204016728259174161356024308587454752060
1651615925202653666723563564298024143369
395996834069984356116424603281799188207
4780418633995619932691247822970357598808
560843371996925289776602553678150167772
520549626178102765369614490225619531
2564904851968337335058739933545018315
8186783262239607297966342117887020012756
97142732299371519398632254880937497426
9609915137961018564923472600937426839764
29878889052397986806322657841289393818
528857586558368681057329678800252141530
4355859691663362937243205442637211917863
1973503180635812173458126410748324299541
555490041323155552454390934685544916387
2197111029404256958564954930686450755
633158299455489457046156770264411317479
666492426474760072129292725592023394992
237284963708607502399972053485320938469
6394491691919173317558232547665518125
4807675718459029292924619072756988652843
839274526512617661595539821492888822341
134425029443258419620054200235629783065
674724659414630393889898389807698142679
16513073228775630215551551823335289632
7401674428368037725632791584155175425891
285468075944971285662502966318549917361
4142552290184701153995568587354776358288
238028191034528142248087489909755393757
745337813683421099676638872219705856274
4461308123948652694324845192515544190
141178938634945016857708580667452999566
4978516926344255702123642189663389603993
796073042922767378892577780662263136628
7701115374039277924320929874996547335
2300148524896399279494816610397255816053
676202761686926645027784671520640658644
5761155750703358924763386806943982792776
3528182736928879658669687199252858975597
145091607689510559086857361267979276845
73872275244532014293056147483392264129
639855936144580141495533768999619606881
972887438538391327736761895543030158362
345825279136214193683938950934524912764
69181146496904755608542223405519165609
3987271814088939110434677334668680785469
360177188073972356679612396710988729207
9822298168748728922071933779894362131805
7904479450928252366611404480310895074380
6346781144334519305135939516261345368
47051101383614169993952423682974414891
40315389354521322922656676356046388444
65483432256220707351373673125474633361
4556749539541637872522676052205958198497
6077998527606334904631035995873418979768
639919262801439778135601550716070236766
6170818592649507740445598167821739446037
4401468226003336137270059107462561701168
21043106408533485254675182022535771718
32389571620330352212382197566597614836
5206777036402636977039564828378884009096
3368319665621615586884807156450596974358
395210361890334086661051227646058686314
2150789643091535635290875104393873025268
69001324730561929945067208136242084557
652582147129131348382532512576085535580
97048876163356638546004207665839875203
150582741119482614259980681746933611
668378172931578370230508156998023904740
1167925354771260930936129481568947179026
67289282756459828763868853700353967093
93405401494568430327789327699724671347
8292867355377963601376462930942232975803
373623337024499911787824933412858758593
6663632718895809332352013205940083852577
925669337629590751313287580993432124429
3056483045975138850849174971830732894730
90494839965668271457098783533518913331
6662910420863181666897039903464477879248
393738955862858570751949064978024955628
4917891894883763907283881456410129152297
258643373989340426322308104334346016714
9667455484089064110522557296822891885364
644675897952903196973945951134679945802
75566609830182779667585416538486989739
320715407592033212698444406457514853642
93009870989535946048675959341937324039
1376981023106776198519951660590413922560
95543076518643951091688342260324609608
3485970853767250413417421288414548093224
7399119541913156488979911512800499138855
516360837023020737850455611609595773884
626972531481614959230093231159726284072
5757347958961363513397181987452059683767
29055614397300599031287864372929633422
178258935503617125119001172248022726386
624265289125428262093287967599532363572
138466817415689183136102831471574594854
820113184554573218813632979399087851866
4595230844918297500077724115284544496185
6346666152491173223210969911275277372124
711327576163786511349520739298929449020
2061687532442964124819592393989778845
484873727590370828925013897363873437175
42965258650831665087232103220103471038
396127949460354512926039104748348276200
449798073255675023623240951346771773
119252398781439944320401075423135923150
46117500350166311904912293493746862992
74813320559026872303334958324213378156
9581290469536029999058782549875632388900
5371622496044965869892708937607815909307
8163747364868624620242359275578571536838
951554053606754662506072203934607957156
422826894652842867245438377964727045705
991495458045912512426553516588185108839
5759611154846997455479313181725952008969
7329139682448804543329851590965061125116
5552384151962852357943683049806544143264
1078205587282304414425601476500942606821
7608157750235372259632342652769122687652
495586134789572513328889339475719261970
7107531064191654640040417320732895669159
2843594978623483852705387919826613671
632559546398345729570898253127723858484
583313837269775831601779045912413147
3326499125585700667918186062967715844255
9549878523009847428195022599878934405
27703293672314004912216986441552300852
30061149358943601616712719086866303114
800374075408346132848557041886079043638
23525973811221626196156617930335439921
2514400368197870609317464819147038412187
158131030622372727653918273563233978149
3467576520221074853455918307318528594652
965117858981116152133213465732269349366
4524415589251000603428345052181289523469
2334914074309906348635728672516569729816
805740933709921951976724395192848233182
1831770952999435371070483339937280951919
68739163834152439651071966473597884
8858654938551341514816354999352371488023
4994734678029040940285525011797282727771
53361488169095882932169264471161262848
4461126287404109694535505581723544372640
691599114606133174353895890892424325602
845976229296880954973773155997386363563
749354128156362586009123970697136959014
2130670783332716611444715164837380409507
6616999811671663336050022271522655871635
5492107158652621183483827622708544041970
868721239916840778619283650792541182187
70911218171482067668705733018548793877
9443338224492676992271431006829060915261
94889737179215731676922203116432952667
7101207314491467443324684115811823884874
856946787382355483287780600773316217607
7572622350138414646299230738145438270
7734157720167749701140381230481972071643
9541965671359236302574672929754126633
987647749620559448423431284661878374170
11609153881143322008705077339221484787
842252235665347845598552691758366079356
445096185348485707517794337832852478136
4520880132134566550999925231109842798257
5234173442069749356439681125653845755312
497231441601970988043905277184538877192
6543261694849770369224678139416998307119
34338951605722878195727590820391719908
37189636944624124441706960643726628379
355219388075306905458038250190855643248
4625771235378455925116098804905086713195
224174539688533637358606796247298089179
202830798099237967309726809182679907368
7986399521968640377772814458271757872958
6383883419355186837671632396626673969353
8231951675415800654750407208418934854968
699937839271739177433413718918428804078
928925154146409412143972831433887893405
1494278991042850843061841231442823267
3293920953358004124429077941704321946611
5294955096177860960943436923265418277096
427953599790292460434317369754433367127
4126320764731519616713707272133115102017
263651244253246523166916546432657052459
43776693485469122072418275239657876687
858224433591930911232395885921346
362883425261110969626538007497083897770
388222085465983277417716852420228661481
9373491858596934281770805697760743439685
49108743523973887976147890864559404913
429863954628197977251779158723105374189
4867658117211417359623685454935385445
49405446810631513063562796315585075659
4209957545987550767903603654926445721
7473702179312004410823994912788872116612
542232822279481333730813593107557562350
1762798496786587847871949601351444225962
57395162794657376329857172133535727483
263321034965538361851518770579125721498
354027795927707479629792879768092417303
291389479351183432136734786974568846226
677351294441311265016133115286995374944
9700303259627759759175884960336666804816
24934876505183306426158744229565289838
227838398750280420898616409830233377184
531540421986343029031994240040249232967
1500178264674220326585705809535753034552
3551833485541568848260093326646419222
8224920558055091109081963322865563519071
661078801175596792994147883871743946898
382081508429113112724517400984299477870
1436336791874818175562957140838475139627
5066918411607921122224663249293226959728
7173802817512942344338716928886056059360
7093524959428102285749804300394697083310
6885183782881698688023057296480213154600
855612871647633018301244119011668911361
382990654740293764563606037867885737298
545357892462120866243966957155295764637
49336643454758544042341050616156131035
505278851708671218674219809422971731912
6085425772146583622325393843301667002166
364235339994821740321983601493046896499
592127214736103732427743340127168292937
6248889382515413709880579762206596355169
35692197851691192137518580289537529528
26757974813050822746433904193546643346
209757321413730220501967877912845163952
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]