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
2344809636834083138020644406879233415032
5097692774171042292635118496998041691612
2627664589885571429780255269989795693070
5535334630973599241018412089535855487390
4497365546295736661364599669924943312266
50609866721697898913168320493966321249
983816823378143829854033860174879859577
860422552128176126684275592225143447335
408902917583025670542611941364549892327
25132750973127872833853499791780419137
873176565047305676868412805815854182054
489020317330565655769327549082818851587
7807129033457296432223154547321239881246
821312217883749665641463664624938355658
1150110042615132628441208977815164067870
10473518536313736539914811297389069425
10943469780320010825178553597094909745
7304359285075222486683279256919372543171
58475987150110309309971912231655448322
5744158668962528195720323232174396132781
3915446357455172306477736819586220547139
40719310995514277060617354997067963622
771999161364706430746212877681119735010
270155358697327824995389020839778668085
8609619994129437799338999794231855387
25004549150789073774023985201754645785
5035573338581732440090714184964359022400
1576245817012125120817039815517462608652
2897494387765408228889109115654736515447
734615685385379451836844391760251731508
37188235732863286607323650431699613336
599978982889813254346302364492839889694
9345499469955664689629114058296512909
8122444557918684903923649150960132016545
967080619446078211445009663979553699
45428657118255537326376695485495753
8316130428908426257918938150397793855646
9753431372084081246364852432986945848602
1256592372552359667770581830333211189399
564725001558089483715516532184523342716
983203247599628948477615906214738366137
7469443648414522594499339663137819424
9529382073835558555278912900299558392736
146251761612142425778307799522153189633
2862237762496582315978521985540110717438
34881963837228699851138970063647779221
2356213896697561225625587937279167718
26918200176161939633435017124116479498
6920140888783182538073387790566029101112
3858969356905340784470033265735163934615
371633404323973418931592046845729728892
594623294186106704464985428290124265066
442190959624391486009679491642988245436
980583465440440710312314599748263396989
788591776786191990922241222196393356373
387373124020199756523752033588420669265
3305337555929888982781983430645172065
331089225744727655394898278777398632790
9161327445688701194826727622648524933995
63479373764484561379552786519032139148
9891303268209951520019105877303072643948
1107388299053617967882506642287943619095
5204984767466345174990832423686271574810
9961368847152613310338349720826078543429
590836649041126622327152869382888116978
827375054673290263954498931465145432165
35821116011104660592406757691895382538
145182139155696295552753119466529062541
7374415332615729397491043904730536222978
397055062756749224714246019373523159706
30036555988645991377026921640608368509
2705843758849786459826657989897423042
36731265390582072552847875307933807338
369159297782878310244653995449557586626
741532914710856769035171068789037993079
34425418539670411152634985481413775404
4921759428343674361954640252243439502
27063672526257772856624855599245878779
725166190697102496757458892704681143302
7860356933898074267819503995283020397859
4576574488713023190826766663410444967752
38488738123460221047454861444718735132
603791935489779873885148974923114071589
813427040448909267898459394692064023915
7685478950276924420956079976139626379827
33054847884680421919725447393498975350
1777635671909934494530671679886547543
250122671468174955878099288986692563309
41619764441979576582959331469157541538
7285270660639003486638388962907974746287
304693985647914161997451596191572753503
4323284745084720137766742977314845706181
9339140778891803469337529977137934007563
4039531846832264974329077238538151351352
408251161784580637136578975446945241927
1464908618989641472403917508304337257
169380294805154955748347193665361905779
2915972393925406414274361267589078028280
7612701392379351433270275762523184493
534335877398574341952785162123227376153
6045282659780345820944864124445703104
28134890693136428264614332819557595878
544654458747598114388579588746188736433
8589589468602230525418635890454780845912
4866859352413063139033008826745875971658
3020387226759855741837529071702685271092
64892900344319685503113526194343773735
304218896651067144758224016520794463750
9768124926558005574532438283237414113383
3455831189853472744499867623639110779735
704873438338471356544413544543710675389
44855035544723112728317372271495028415
5976332635493975951870358752317459155125
924933064914181820993652532706865525051
10128235362831466784224968752740996725
48516484857152426393114147829434944338
5568557685525751244180881631132786847
788413245725859350921640779683089413577
2119215866148963667066171381247381284556
3270321953777269819887911236810523411
327255569011206853318307853655594729467
770165205385269886755006644109935168937
195860521037806169694414539814813581042
319215725045906867392435214438957955593
9089992624746515235398556487293091356
4440831852805326644139675128339107027
706395911710483820104165477611215322100
828225083717149021733180127562816623115
21433727164626823035116676485303118921
16927707923487709457181521496497926224
560627487243659329837334507248766899747
9385191037735366654837355115643621005904
524210963247300481228214350476633327091
1780494731026435842968301760211884038716
8462271298808603608987239091509872327420
4151434622738758191931836395466225435071
5224601753444701282468146685222572874779
86786331779347134808740882016229315181
8489366159637886848887769567713955763322
3650479896102148297883364105579966918988
694334713764447688342184364992914604883
462257858494911685274849704547618957872
7137931426865342569450834114412561397758
8965571344125792395576705427469489055535
1726156096945668428029694915451213204591
171622325076595657010807190378196517013
86171221255303270668156814525815841777
3963763273126386647831834744722770046906
900228087561713320216327496147895077834
259150791094441303589641273461269745
4550881563611518263774434855403048196712
198842691453372385157977719220875141
8830629343496287795577911080609657658045
99063346833849404044777580780011659051
33973211033288716738774648604066557511
86144688214189009483619432879494015377
470532044137288344602148358167796622218
13199314762886866433556763610372815610
507549452935881192943860641887349149880
720417147357578201062841683971873851592
87671280418651235293445200192650314207
126111558849533943296172536703919818106
4742990543053814774425495697744776791
38422736985624605897204352717964842871
8714768958193015645586878174524617989908
8317425960058335973150951132673098059519
1853204735731766366629821720509691508862
7674820435431170875668821182649790695
634663323298315691744803902194336378184
455813372678303027321068960999162689505
2471222150122156374948676773168552993700
773696744867849447151268203437894985399
499646232305917462042889454317546115602
1478369212685175554070363481297339995803
2053117953701956590428483358190837614788
677862377334542333321193442318118415779
6941395854489289505694647940148089
437743048972192443373811503794613493350
1615407148708877487752573457380371107434
557145656419012558908166116325473446
605249169486641885668772617270154871401
72505400897245959357885684022092131021
6221454921846058984065071457256481729393
763947566197651515743435166296699323540
178956604187117592818553940213260691054
686112754465163941885837808619225796238
89891655922237991512243932437862484
3771275585621827969530063644862555421
2302459164682741577367479488917209184
4189974754503679930667984797686233259369
960478629398510923662925765795017689553
23248032944122757696485391791448227828
982458189474587327263384529211597407574
7141445357851396631315455822737226129522
9843372743564419273096217915811425771984
6449611026875118733047875916874583946506
487683464195185391213676663177310095833
891496233512767096939041247439595837932
3743572356778213637596291698449325435826
311663966199707102844549319295847484
70397793387740788711817916243414165180
6941220365773424479024115370772249676943
185210285626723236382746763368579269003
3057923137557916529146944634522950679325
7669134272175981917750815005922189261883
2611726650379333831952024494980173949340
465038831026873575993212821210078537444
2871924943328199968288826571503231907802
677601230803986927278898583575792239900
829714855673981140870598126502753938864
834032371963743549569646461500764685025
637023382948747067574560823658857224520
704406131856443486238808454506912751013
7345305897197498831402221115263295162
59384444404381727921723183381758644196
5874481868347556363597375414188850185735
293659572560879482446646258898438014533
802488124064933354717405401752556420233
43225341780663993774225657319643968485
268861294372108242234735086412622502664
769310293775006733166472648803664707168
1230443030923893149779803570380710313623
1810859657721289845310576519690777402037
741797026472443882212019378117594996157
736258729171878031648014005486410536798
5859920169673206852099685670187035886930
453177575695579612624111857849768011
74326834772777148408395566413736251232
9292965321551976295220919912199761101824
576288718740832280964588920678686313083
8943480452954751786613781721367863214048
8153608454857756615496129950459856276444
56076672412642924872346675567309897
455020883795732504366037203917477531938
1603366126396672746861683680914931970
444741612574210143630103115837417546920
46163383671317294646830604944058764178
3565310970177517596499151647129114373371
2430686725612164889799053371712992403028
96141629403361839366771419565764363538
5463641337784964475096616170200333339816
4675930695921549665586585183254423469142
242680042737991333039446933584213144898
85949863124655832433827824374118771712
6374485082558917294393410269356469634
4901215687286384190659079401654050501439
2705128816731699543718831765506174645660
578136018597793585922671685139520686012
24957697569459537371873665820444225266
6830831724255636152227932352516331856
55655712146738927746651415028142017728
707630288242080226788583951222624183584
9653563768645328564774078984963532822707
7434830571827890648356555462783030544841
7375963579041217034202144647833507626
1973856226175008774361134891547751929476
13999072951333662583824880186324334602
50087601929865828653566147392116957850
8433725680542046747594471630371679742
19881355856912552742487299165958992504
6978506114324084748216307096607729275148
7876391865139819913849002439456553184192
7665572793564146678816141290861245804188
2716107166841991403660973410963296634401
3146948415292615022269684181641335960
78212338805862808751891771436839341538
10254255575714955557233264924151193681
521776856135900610635586810182878364444
53142711886336863167781273331732023442
2314762991249991234154705989808787569864
451785792206782399491986330141204567290
222695217587345171644109462723672531872
876829637574028996981545441947571193376
5418877414124234101845588468288091521
121803110484562928244219942391250349041
8838889351563959435151813571499676411625
72479767370344031859784559668319918815
471173447559977741743661452927736844369
16575448906530857109826764662022892086
97121493726362758897424802114578294827
865388356454896927031754176030483731736
9475178815708274725569195850300871624406
106530971911470314288323961186135504014
523345239530191593798234046302243275398
8334751891024117689967145420224729523222
7082516589016265484154279561635171069260
8485312625986360224292588580821273241349
72968718522757586445067708835994419805
7447440449787481844854607554336439212
987688898752925353467958625617852285434
2103806972493135270553593291534640029266
6122106412609758544514174946421831814178
768888364986750588138652330425974036289
28966405477997143649058285953178077620
5139466325297329124295529619727047309457
365195042100120759928458491943529418433
5776143439251930686549739182222613575544
907711913265941896250852422795565144009
1356341036225687615053581899822799358493
131595966573120699255785612612811783936
327231191098958880638845570293925322635
7635755631193452271066259851613853406922
685439284333933983576445498307112642144
965143815699824166159836101945285074034
58766213279159036528950519963547224147
5389260333966390660423031029367251489690
548145171244841571566586442888244
1466613655978110988566705067343544541072
35425891733740556513562411390914807064
4456734545866010153258194650357282162711
4439407058714785668968667644603213732
86649003392895319829020120414827547832
6473255988524865686281564779538613116618
4846108646513679642186426212562611337799
4196830413016290483142139410334124109093
402586819873228880243042207499388386806
1185680090093605479070498958559247396917
61215529720154725645508351681491179669
427410645166701947287768460981512285615
343622304846390079946834894413859522
6638679327754491936465191726667791869
575316104195371995260237306789647791403
79843525167199952429661523611151096236
75998462746615031032616145567945908111
758778943507935314855785759106497701870
607214469525419277672769473741418651270
473711202632964701790982586282593063
77323433168714518946724954232129836228
6493519761692440330099537558929783384736
7036737166857352194985102114689041301
555847547876222752384371361755030305261
3086388879966676838715383739518518332553
4703260665287177511892841734635162107064
580366529635776438470665246420384951309
183640915438499350628483147661188032303
8171610620233385478294874786349694579357
27798776302670215960984089376788465244
107093484240796846091014441386556129623
83927664374262179283573611666636308604
116456435605422095127329233620193813
5535239781787778484171925436795016757310
7992819229519709362544755778663954353652
6096393676331412511246662695716224186
58025062844833144725635202752417186321
1528279479362178236092781298220080004094
6132445573159825568623049452837889597505
50024358166179253038482281291077969684
428453440803616745939231457436446719424
212556945375554714298421403190465896192
1908792674971317697521645533061727663
576032291704739572908117801470715155608
9561705824554267463394775010724255839697
799497781894564188793593232245980208918
9362966275737373610385054068751015951644
7968837786679980101793128522486839684069
7454961324956455115845519661379091459711
78348351883022774398357948378194388071
832841349892697422796802461438249694378
3990905743368183572084667531991613599034
8792425593582804800587262532352610489450
29661030249723849599706882796397681393
3848207373756211745625494549994260439926
545518567436526899132517364506441174016
4364813995961493069566864219895783916
6521490694586742263028081871711292575319
5264520962337438700146587938434729223937
6997713838758449209525646479619953955657
838126274141726511740729453217771059551
4754145873481183533797792986978741483
688113122813248162612891714610374507571
990839374594861784427463786498732072674
9728826210512853225886262390683343927363
1804156285422338778371533821263294632969
9244183289948630824772223035840644791908
166811223364819736428577064841959108241
1113988340910997463547227818481159050
34084789708810247279654520137421393637
13253826387588793229262060218519642559
536848342503256345850783261214051541424
843229855769050958232111986546563994548
2948414873881385140393175679805556236441
3928891559505937621512127123537240336256
8598471322717894961029615554799662058483
79137276483325886918035855118328047901
748541966511670285286581854986437062054
8510269584277088736933329280457595727598
53687413628824710344557749551747443553
8480585691339512382086469455901497234609
72911555445197629003149341528083649513
96982196324653046112893701983874604989
927623331488007985437545767118175353454
2066433755359538608619092676271063995859
96201008354295173478695760603348529013
929074743553436860004412599909779647238
199674206043538835232941461558793247065
41058593923427552003024845620612081359
18673837650960688908876271846812806619
20011195613594155286795513292580971111
97259294994432540908840439358998613297
6736537087561840669898697683457872148
8897532140858610577026353751158595146009
9982498334106088828747083789952030931343
66922930158498388106936466241242775389
512755919632301185587977519410571124246
9925740556713486100154244890075428907
5552558046822859820679885096760369016483
7385475719593364335734699290136634682329
87257735343832426989737733113657147519
774968329717530635659884591796390238011
1146905413692704192840136922901739873813
692451394257247039024649182793380232398
828663758245115813671320302220898798427
65401594615797358971236611834814114684
7028340456394110399895415285784596712377
4503521585773227578156118705891043855646
933462449814594773740489566290779083742
887622475262554942294972211425069527965
56232405254456751117645271160789491511
908640211537315197776364720871892904866
2802472856352222310069735005600082533606
54793959989227436505815860028286955500
931889086536458472263428303912513181504
5596965176237031231268597750820574432140
9554548496397155908410369875383030875944
51875437935164641198088010547231225090
3554976358544252497198671191831579529629
9127656480566224797598966099353896664133
3078533986158290615166943033237066163760
7140496950869434542838381092990895056857
2393653848087477123458904366929169276640
2083474019973690544369705151410330116135
295654909828539532676643430973615387311
90976660222670399840841637647088792326
7492881649843695367923485442408847811997
6803181416965361868413054918411188884251
402713074985919243257267574511240906298
8155358916114630344076972955706497446383
58314056953998959902673724535390426200
3948609032267491363054904357625313892859
6795539062007051788541419375708737945502
9040656896092299139897042533551776214023
285935709348112426645668664034204315
5949260394191774119151065690450921467392
80025607393573918305042571696370228503
3195786558186043705860912283372040777341
46296737950424919479985936650036434599
973210000761060916501804585321871046516
471469765827840715839585186889547424495
9243471978605830170422447636807720244064
3755450773782246862014838610768193208478
402235888364222528121465084285255296856
733746025941927497949005845875782469837
958311493965767309223389680402340457574
7519828881793354344625002208183597612699
7218114115614919283593232653842130265370
74015912534280064958121302935018666234
6591783883643780591098631976580233507915
1610556536609273506568911422229618548226
3493997424864539265485487511513622086527
9657015435379694495652228500118059056
8723447827101420638388325830299854602581
26291443202697298377129536981572264396
8313610867615903376377897484834566901644
1489271847195013799096273018413576194286
156353036092713202772655989288663208993
346762999611287563499586977025146720
906582281430982949081984616963466191325
712174597615855192808446339741563486617
81742541226930491694004351455641827509
1807342753954538697893435064388181854681
962985611332371675403566795673673729
22636882906938631197665157823966306855
630193946155891479013131629697714048974
636888142369057794033272599290095558783
452246722806539362663619478821894539020
551986179189336235693659551797517565262
875211078943455700589759624336129614245
8646812781111294355744589395983757175
1161982817537714212585841695196427723049
818117525573194867628429319900370494379
44492408415559279609088517144167095801
391831609453474196223987310020175684068
2040360769847620223850664992121673077496
74879139901733712469005642229803367748
7069832012833922559074919899522778714622
2278737634022213713279174351171527319246
6364631587365842748470798276395440366573
553678430898976689768187528210223281132
48057093926538212936215695453944827249
3817137583323649585942181474206259257551
42689813506532824463069677951594742646
5944749485089313910676389446909596846972
482498313958133732314927217434690585898
43291109685176648778206480462358759836
343094181774142126073383658879767602040
6361926641873117600456618199450836387996
5056763829483494741286441530547484716993
814465299994109152632625518790654918380
220041777006727363060785274171632623474
496543128264370664002272962026796742187
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]