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
39896990841326735922469611255844921423
552921986216178626094980550918221986236
51271505471547338681575323131466780626
9022954248544303302616389109346558782
349430905531217079846317844372712494622
2736584329913655503624042675752277924034
351989897682063630785636997233010115925
7590462857125099994281502027422291257641
790987738820505699328812326806926827221
908745885582284645750518017757972221477
1941165769168869310833808978267206932
6640683131865695505070108842753947143357
99431608708824043787724687075533564857
593060791584999187298938539387249143455
658713050612058777812721820627810963900
627348819348258750851992366871976483319
132397027555551424551613355337545251865
662153247616877369559745961876045763
2742176160549974501484043643362599084422
700124955739744460715747904374687703911
15699920115021912867694286133691479243
55745947837387081863247644460865354240
2598857733768500106552952405514093627012
77754319372353918849427133274327273888
77691671244601635829552518886527847903
647874177913358929984715116322731431932
7819488880692692278522874281143863924364
3807434571687687771370631131763642261052
3183740369037576773816462009861340882107
335927314839285733511052767893839825009
776397893583286866539296040645385069014
8407331641418745483635921598455532811471
163024394033526785368231951620514603397
9545639712507257443750537791190040559671
3770117929695843737790232198188824238550
238525801848783663425312385478775416157
897585575413821389135957619746741776570
337949519931198285327142223188505620
46148481151470615113829864133927394692
1101374748046549957472668451646353205480
3912232164666254589064961804120527633953
206182141221506942437154295913762286084
3811469962199110290247333583499554439
7074824769015913237719882090783640109286
932458918856686139361422361580425640359
465967458084966801875962092818685263633
8851535493763842336965524148386045734727
85584870432541164212812919128483803409
3467417673281685381727141686549237299313
1226642653666626615073274707897134108565
4911613136079601648488319364601826796751
3045725947223518580093713596158815941899
83766471553453991037582620798358767877
9511896888179056778022165347214918703898
56745011277777149907009965700160052188
908859876572575313543795028573744879849
5123101865845705140895913902267426103537
7889212826253448361759163293928028218593
2921376798128598990561521859627268165457
4148952573135482780483218763485789063983
104786954558189163081195611817819805891
525418932916312977759044498708967079570
296838241761693226226065408971927028925
743932494714802948587348900533441084121
60345442230173303424886162299096079022
79569881576298116830858327409425676068
676790649956049359776137110361360484893
359705103156512091891121999889049868
6125429593875097486255586972978679792757
572323864752384177917841203745465362793
9555879526185241306466106923448033124404
32669095486871156617376283750147055960
849185644511464892226153070559398
8312292387252110669991025051981619405407
973723461985024507590806374791661046229
9994831275244298366115767946241578178247
34252993578814796483434085449314862490
843926234519694883573065943779083221569
5483817229574851639681222660696653232706
3369541612494687805514471852791307815
298836908363256284258896021534079956758
62752396419567467207430871845896888313
4647922072743299457612351309103065192999
2780296033074261519537847232978844458441
6813397035794725944358205629448272502039
6891633620293585988612449472453227063063
4724275873338378777441396072423775929855
388077813886215958694955227466566155
1697667616231000953717626009349898997588
401919139080681233867831744428786173191
92603795499769815059286560170530442670
4151939620157336486234078132986626038384
4115212120693765529151279630178654594348
41641170402372615705587500683457576028
4418268192857982103078191876556669776936
963576521895556041569460354605379538
296686352040375599467253877767759836037
8350814316269037724867141603682827057795
505820215083636809548707140778373675104
2639368594883778157677818394397770803398
2448539354938735337585316422684244745308
17646094225398236079708670944973369639
103272164012358778828063826661882282377
43284179415549569983965448135270307098
1091511514336798558299055067746317032566
43989927506216094889968242686949646563
333231032057385849638371409498713543752
1035951631962595661341785311569582848516
370439341992250550865745001819716736719
2458274135557107778523989460587231851799
475483535943386709337243244147459306061
4863360238538564502845292708707628035
22945366702455901012003180355379182077
794444368088947815571792315975525411992
6913832918311463272631453639834366989086
7162308828969463367091454130339766839
36589445842475071153975925544723539027
293945832671434372099084027151026426274
597518262167420764154711753273810917824
1545838595228425211893912626198741178522
872066173998221878792655368802393151351
5849217711013807991126644924885021326148
933196912464650324071621023886529429893
90617298553579306145616316861993792974
4726770713936507460433486937350747808159
76634013787754838745038919490642182806
7052878774935032317854164633729018989438
8194989046292936571828522636454264245455
8919424585856762612688059456578891707962
418188639844155723013458034968022206
377812857504322658816876942525429539501
658183585203284365041715559845715839
2319404834155120532264573212102770598890
42464672466282884235967501922151268239
7230112141192310238610603812794052209563
769478837059999784885463634740797175586
93885213017974570886681221529780552505
8992291187782329623829454839765773941
804787899115133759215374670365063577173
67684646700777377718280748415081864456
709385813974810752013829223842326683713
4994912206199115819112594712360072573
9494132946962444791242276077512751922309
9491867911025436888680189383512484391328
9508502414764229750329507851341028298626
400984597122134142843934286741480354851
247283729036079714415221253116424553628
1291708986136780949741105608628434321
4288856959061314171446918700388558186081
238379261801540335539346966447555401184
3339786718336067970468862791142146097704
210873148727440121468843155994416584913
8505602880889425971459803827556723325
361668731419384589705489955884463503594
71325512283632153421596016610495997634
9650944161511065605688526850917249155901
6453468317954280856308978756765429408
197545577395317977130806584193328473046
847218540402718332569774831821925104635
248482349420103483005794678111239638566
89862705539567950685271761751475963572
655694497324430136257817923258193771738
60117961128772262859761896675193784557
424056574647579529515918945870767127272
2341547481182652800765704549311628959315
205231686041626896298345032351457648889
73124583927186884318196082813692660791
6741147694325056708758258450394654885973
122870911352150658054591537646863335449
5739995632341192857829814513358540902468
859572444863692457843676534218740187600
117890058494598374706860968809849092
7923835916392906889522834791823052007933
79667492678051029671308640449869574395
401277756193991030643271158526556499520
74672046754226820028867322586468754835
680453969342525041883576424798968741958
1860401240899509269923582954837134921429
643844744008841989199583978269739666504
736775689418860792913279651148812721130
31723687638850408699363441659445699112
533845006685547832868810623508914799
723716934837382575541215795042226083848
6887484135960152415491543638285613491
9278398825035193258344995751139923792054
6311221647313314068637498778145591925
7585343472528874843770216640648546264554
151492097542569429127913075467162315700
10424574663192746618504113218872801876
1432406734538762172376613348179774461978
7360310654435664599766254571589823
79453545372282472603946134561522114641
8088512461332816999754047970850597764772
578614835585567871989614308542364617
616011656783746619745882901272184602874
1230419113508135695294164723336630412288
524440824080968651076094214383588676
5017782435484967458126059913322150381463
195399314483061377250447916538138253732
589081605832467285968124383690912193679
283825094529283428283395824005912285
686783481151751247792762228320252954038
56856243737213831745628022311295444120
6410555693523488222276195292967681159
82116578282632554906266808013426577361
3957411413493696679257383851501765806819
3012766836632495120156496625450225458002
7844571055908581386521645506926630438735
8852167279227260115795255311732811735349
8374625170902064409285384023846192158207
1004832721057927985423498668467210154500
281980521167630139017267344139355525260
7482866882502724628214038744602784713
4320970564353519691262838790884913162786
699286959984601167075183411249146437027
19604985704530227894672167898669477251
9082141695686706534040075731154113049404
224919822397801372982522119885234267892
2012490924811729171341954005462773876946
76272745976927962374350318257293699319
7334103195179123191747243032807550464
4301636724651113852811781368663314156851
637576058960336450018381670655271938691
5668204435259877561661747775586581742774
299139542777725997045749608914549198313
180210973759669902811191454434248803056
260381612796139672389865576252768791957
885277833501434823323349130665034534182
4226756351717945184251314912430134125898
336892004979806137191457483124317394823
65406101195860073636164555695709637447
1823934916575097774325853335775070108553
8000357127628652312261637342598199849174
2240672152758310448335956350461384
27894291746885967024866364696981738811
8063397001440111765802287882274679508
57758967384681379085714604884788129441
406067621835809397347908262936184841805
9888297214278639780824705457283063645788
5936995091669566384946997765292991699603
6878240161269192796714971910802194656433
796263345192880179153083726929371995017
36661791598439206644494166761261795847
2027233688576083115213682925356278996287
382893893519348121359748876113154371890
761034365740533195356528973330884718412
30387095680779335923952158534921615378
7547546386813423156240877938149150432950
9037198276283101690376711081292097255432
3155842900545140352492774375936427723
4441854281612651942186426142714932862946
3843608874021599118214176751615277884487
7371308062499014810772928553950421607577
8865662632488749360395493388231315681926
62235209346562583613990341941733486412
2054576559924079526647755857290982001763
538148139329707989033271751106352321904
458374107440105315160547006561080105009
74165401720162337325219669595283034755
520770149058941101510127472490999344943
5502302498248257178689853656885093314888
236460257863617418467037105730518877390
97189161312889110644052396934938774470
19456168490892860273477650558660068298
454429761021979553215796645638115271998
6549691738244986571913214376666080773422
6126550753881967604576523303850833739552
3564139597424213470181512722123876822129
2013350184191196855627632615187610803704
8079789827739680338967339028248329911753
625030318336601748179340766177099793358
65192826488769705686087581391446149400
14557440115637677823264526536271331498
7771937239573292517446342084279863447
989783611951447155453593199919811362274
109815858586173073075266111211568096062
128796531096132327579139742752530451041
2650582680333054573685284728712174643888
784033722699903218292765739587760661487
9362330250678318205255867707321793978020
831087283008801829154821530769893114072
8965656888498328893128096703329871034675
688663602985763337718213128901571571655
8042431042505715571048797409134454294334
699822386086953854958695707725473176
1633589634916373411878655562471718534779
4489366357129360718299317913677476402163
594698244350103949141061744614285266043
5893184198628449280648683551486617275846
7109311322536250285630349244899378924678
8720154754245102408688667893728395569897
6611676319876261731551366693470167982848
6883709698645371992361976584121890702685
1515211358546566181929521733355878708194
928864375163903732431862490480439047290
368594515698940096465730224741459674561
5438122018046803290376817209778956577542
26676176444874600062475201597344465134
3047794758674831775661207636231055461043
573566205359877392445923976452255506527
7016933859252682158746451133578863329083
8731587160577871821545635365625345773029
9629481689703839775766735340834766686123
954312812650713064976899711459072935721
54196503537115554638846360658098947888
1854208426108368543854195814569251641138
3909919747221352212671427600967955117980
14858496145035017077780805210439271350
9255836617467973767987575872689543837559
72874525516831739879960928393179151197
1071609243271214335171009576456972646116
32052282792135593259223890669108971740
143980571286248237978363324359199853
3747173736631434475499919986164947401
798915071532804979195589889445650809347
5770265126121897615842008353321461237527
9144101089159433831930242326514415814510
535225644570961982574006420507129618141
9281833057971868411086781992626743348176
971650671748989555569259639410986953463
8279730240951529934736056522385892108347
206634352385610536254097151797661893619
359420921002013159083761394696191206333
17678126891334880858608186684385062919
1822577428861925868653482111364232814859
39684901944520254722147463650956143164
703630761408247784548209544578308117575
739285854655449626324127294705984351737
254977435044603496789372639146660098163
83139340522195441898819674125791598954
42977129752173218957694779344689225400
6445158019463128861622138204282010585028
76436389985553945578935680530063036418
557299256474579832007934353566574315230
708997483223782212533867717946667258609
8414326569356100960863998923598745492338
45848451687168563165171907598658951284
4728294668137293597218241469537691334094
90397304548969910221114111256480278822
3366499375498634908131121724847430345801
9334678527674461112870704518626988614
5408448834449147732899262478410442022209
930658762548281953829990755885528991027
1834461732778665861287074700604168217607
406790502508355772242718503351583737112
3944395727678128681247468618788857119607
1000077989138811715875100473436889705728
680478207789686317215879898945482292341
506254376458876678418567252397572749483
53815053613098114776567853963421911321
7692963971895341988415752993823931129771
227669826996874552533656810292176827266
472216199153964232474604905539956027125
6927718591105613907947327524175227363907
5036410634655194454223169646915244953448
717038415209277706674964272515498603
1026854667185495108746592657203264417345
2905911192268030792796554991195639352856
235531999090213414851916862920149884794
223028465529356498369022183062132096168
47455493838349581839941181933013545550
5679549980781320379396831825409493733649
5211923275421034868780969127997957221674
1422806550777057894613323104218175793288
606361401682469916478112218316490028813
7201327292453285234253453713958713439730
31663658020125359664771925979592297627
2782101494384127148236569967523621892360
321374087226131724974432509915516709904
9244990081948218283118953204585627776074
302867924712672537391285325663807225161
48109384904456591176187072458315732877
964293404379117053897297984315064795840
903354458732193977720169402215583117330
2633253930035427285984606364409753153157
9304374093996079641824722479630093741419
5340486346069247896088683680859059206862
8174517284072335294078288355369453853126
6620407035555068940725862559594919576059
6828244419595399549966672094677389703645
3629801348747906833262144792413168719389
4816666942161863399218741382553110389506
87954956236303712994954210157260967984
255104195799436439706949218892452932
2880636318968715301388286013430027027554
5377235860586824610016455694692260391468
7809317088349454559182707390732290037434
507262905068737524312378564821553473447
526137763899357611627628732641474276193
31932802600474685991589485358386362033
5239895021888079253884457365373876144230
56484324203243823261565707992238892010
8881101775024253271363158182577114996412
8242235089559322344923966881731848386351
1878843839685452137229714527718566223186
3567632255714083145088165962401143079491
368572201031936488368091328217689649927
59101221554461578517110425086790844417
3708881273258047414346503425336674024594
3482253538038998652390481220386826796598
428474606747271146691193782571287117985
96134581977459011071552948992185137387
99731042126514438790844845555396117596
2542850682466420895138996932621916014831
95210732028203146285654541475020146677
2119767686585363149126014767477668725120
8815720482236620652245175328419064022259
472850911636866016163034447352391211386
3100862964796362725970933852383329192414
55856226437673974472513100432130469297
63464379516865319909924864738118572727
466320094187429594421326292934338862409
380040165824283898632689848729720748746
9833144514055965187234974680226027403341
494883365435346619802121667867797504446
255480485008973176238124025107992376978
8631829028715999932645621279629878547999
6082231359376921924576611641273681945691
832046899441638399076919924780831067351
5235153270284210815164412351962894234613
530113270929776652371226401684477308627
6029554919961615684654191228671674167975
114112131971186886886215987422759239924
5295969521959407904256816955825382266
2320247317034142470957374427770265762
77908281235538322457182528951647127804
2104383025816837915933759806569751057987
442549143630522860766701550755410676284
6267471248114882665188751784776608785
99991720781660731940441473021411972194
21242724412801178755081958527792079130
870670538854645367334157954159140282416
7766117229873425295023942159215764855769
1024857888664428480799782064215921557761
5675849027422411835836764251182822234424
796993012280670994473094958492950874030
838262845796914464413084372318149218622
1514381396947277796339621742953409497
1245307311591284701613838015542542077754
980529211140197606357466599134163519494
5293380737802069541915028921362851701736
17851788103510676608437377489530085285
8519291776337596966697153526676748284
2967209741887297971757556916180276830
18294528841186239305488643106434687883
73996183388155763777394324610433327653
365943929661992480329658199181604840621
9063790579254046428973384830810970284877
1367505610465699565999978902143676432868
8624752227665875951059321500847095373245
946755942345523757502735822713642098355
269937413857454813928323953510569017195
619782795392161355752014373738346147129
263882474632688538442793889367564032635
611182532157211210339721690756383679261
93401399125838915443749830072410192243
48734856939691825819356228479387902448
447610445547592945522476176041118886
4911670899969203258657786191574313773095
554326028229621059041562613696528079784
87567296977325055501721245311121081503
690106516193941603953364776457718465065
339653486803835810945139467132594852000
4299915171412504594830942975602385209963
3922410034707460630962346796637970513712
855930872343694648053051257210973228548
29627438787440493219885524191795189668
4381354788427396941443486886653507938
39698184866070745029328851602552022510
2517592390268229521115253896806637933326
902742734618681552274406769865755559781
1809944130672263791634434429656444561667
386147918238255413132484657794108977971
3979746994622003402351356393903528803547
93735646209483171012908185464365873423
2376565762007259851244923237185919825
979676445729938637757834138769717826491
3999595835973762242252058505108693368943
409698704921756625541524988960722665
2600477388463630335599203333305100
646829317531912991155887182466188798259
2197626837494896302924239280573027539658
988691694788429114194935751428222777630
64678459701710574198781046189475149807
946218125727894548185878201151983458272
190532565978313963946358843587863521416
3613075537095829214942851593048479481
3669933922881335131832204836646517351193
831168399100362329857307594545646995248
486467325684108766707802673923852641242
4986907045842050943491092377756738134686
7034756749889772024829193949579238868
48432404042741920641285501740650466081
124166411916247217048882081128121411411
61738274887635702993880746347493447477
846245665023900468037286611673557151614
3454122214188256449879093338329778472256
7178523130084980716471424964471088769917
734578578768650180414374530562049528128
5040279665319324254549454885379706
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]