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
67387089923690497567218724960203107
2431934291905995324947091022473313365338
58219820684240392381402290800683859398
45733267980635928616875305335936021744
36543703147944288719376821164841479456
497014832651376521437021936394092152600
9788925817316030843822394420835613165513
869724688419542688124257405818598689979
7749228533397349531529387422953591852141
7919781135356454130119957863710981512700
10559453407578829771322913118093297789
558296727842644381311680239131149203946
900237191379701050565783261978199355618
724877315169363338694359360571399677622
47951413012386215105783610429089203319
8402795471715703123182426346651575561058
332738415481971664530184577864086613681
9325331256712921811682957409303112185130
927937864813766747864385078983536829555
83856478901636657528881671777437519217
914152993479671217801207658812935683690
5114203541929295712808643758491089428
381919595919669786260888914213682168
1757554999024127685375473605693294605211
3377121525929330864249559702820927801719
91048226095827958960934653548122991010
44059992531777314873158738395772845008
343550567986451326145109256592115359200
2788641748727152199317278582860699197766
6549626645023014952917536389849468826037
480974379640117238764900584629448718480
656188759572796679360545378229695575135
25709507526059451958479827440789697110
240894308094584817827708367887748821233
688526207359115371869469746489044169860
87858024675422066639091749816236839473
5282251410363570101384098128314418305935
503475356738316576829786030880599168927
7764308471126280244361111049271777649210
221759336867943265414198956226360693453
1641626755458306780967039388109555513428
159573422862863719564821192826234657
8208737271969739327737736792319425988866
609853052650206014843651228863592853576
2347865157791432814440678533171455488761
236523051264235481729756865130192088620
79837392298246878928638503840913638473
482647653742801254395795527187739071116
451763867826607588236187932484151073785
402865684668306565244131014375228272738
80211148452095367731313432048812057514
15232465574489883772909496283918743637
108726234841915535229118680698012875658
960544926426181399921033423837919894980
52886703828873430551751513120239112654
812214547516051348782578845753689682323
79319418919414679689918014184682637760
11113759062159592358682738595889823447
7404434576494123765334064262188617919256
2064730971567993980072752732023285623
9753867269485336424471454488146817392047
1757508845476278709862655637397981186
68694286980168919806414589980014825216
6246160811292612704884019755474883646779
260799086079207359444404602397481796810
639272203826303266264192650940931139785
5777250892607564453514317425869717285791
26681107196991333178981656583732586506
556929117637324327001295830862318602
4114741046418017147326557609917455693438
8205508657188288578561322255827657939
9334444252614238967521126675113725714
303199133771274150027483151436526595918
6641821743887473781096094048399435152779
7985377637202668981516729143185441418704
463380365468228597595331664957160096556
73777392212800466536041920754491023932
6825298068619402349882822444970871906897
5088828395505794439799487973387421593197
9238565218087472501452897920013515433
30695301627646818362237761194436916045
388311409104799161355896261398528429248
430422195719112208430731116221821874755
111480511958576899515473267987993417788
424884725164582077475359738394587459146
18401783819518568744834816121749021005
47939068320866884608076218157522453329
89789133911732800141855531652398901828
471249378801814854927181416578665472155
346770236318254708299804040109437024472
2929141414064842472513671928328524978369
147845079164167835843705946173067903584
140056284456202140337819946261364618006
3754974496332665358258664783987550826274
68498909190014909718100291923849565774
2381158824435360229184631278499837063360
4147919955114455501388628185556320102804
281312717439929278256907215503512282450
35885920537644206629272149822886968020
6126812077105428945375786010163086055930
730255334718450184324276953093446759
639930251602456994216504430164753628836
5695110570014717574911553536577927856
81794124123799542495433838318197722909
232253688219857065022080553866863628780
9070280254553572501510994765396847095043
815099253723489864654697813680039365164
663863736295777368481097424885073741730
338940649380120138315778589696167825362
2471230838061215646503721631073022512
36525781336871989088534199644188789
696528693437329830934633537764844456449
3161476480587186544315386148672050472951
8655324649105317751620548294755758321902
237744871013325405304600933095796457
742656311502463973080698001179911451128
3224347321213694345192749987551946228685
160548558588797842666663759824647624216
92516493349340363499625039671778052075
5856229683454961951018381629857425742189
477361855000766985375431960739636501
9531483515003892616788929248315543591110
9568548553821987644587281291497695493343
521831718022624793576052837991039052501
48311072431142716969977293492838282291
2477335357873684625609166355521148848
2941647880026727666137895520435029403770
10007283795736875976026709999623173095
8131547491483748277030545935226247838164
5933269489265120490765828568704539768601
4289389041894384819240885565408033043585
3559835924096591857395081786480166169974
802030869959671048639722657036344269050
340853405434711947558017138559759478382
932835155508481754219987141762310659015
2895761761033589467666587588118328262817
169111458241306058589709536815985644
4221597072698357268058144472678452122019
416653751302800512703921805713592570338
512678254810472328651842244919544677495
1097655925324934236915105396925778774384
48048950244611032173161196988662959096
4220616376202793298627104129392775061092
9958614785104292132872372848051049211
8930151730993678287623632204866280991482
25815105566959802724421412422018893739
540119827853100371118438172868838553916
6504711318866999133996375541663416322076
194144081215451336516373373543893815863
7088643750092600970351408476867853147228
735122423950996633262844550544073357
746470629301017638317664876758092203665
208234629694704951473335716776330212549
7641994789174292527074818345108587102081
406318756995386973101689409897468025932
813625676116316561339625758116626207843
465552454111902362272284971229477284028
769140145927349537538991060450128059376
661488264187425587429656285809140143178
2904227355705586438759575679622326307909
11460029696468115557102436983398974
6074459543182328874844049105438674739651
669982556073507720938161172578619820323
7649455388567639751411673037484142256205
3759268397547811148828421252842473842830
9561338259152659179448875290858418185342
3753205833571522454841673658975465791690
686871069668282588482704835419264433950
4054891052219819244826379642048177477
5167996223772084850343832773642280114159
18616663080490735145298804433664176726
628953072035598112712468613921346269
13744554542938462481674215832468008800
3601833475932603866724563783316573009741
456685668335791854859477473639842192257
55472094911936534534260539472879253838
1639977525718998335635948304386382937663
560768111754446784064324542913563687683
4767947467198101772345353512566328705
333965588439566831893672687813740411455
150928741829716693468414510445319482294
963141429323561791371784134783185051807
5560403485999639177297928284929345125305
2545406913583393460465143053871814867214
335510729612275010324543836669756204
1365144883457006157361507180460385865501
19158682022795172387367647317537762734
6351611360920503135862708349777526904
3703106617581302272069984355923434606
70154781579336196819169145261842652872
8287345237059132361892062269417825557182
840017533295474585497476322901331621910
79852555675610571058404297656438055580
8268847638806091923251543463894181356
6848776928803819209335555816709065621950
605479326998843648437684042127048031744
2498627158177838626344674627429732195077
57723695768233846744425911169674302911
937813729331298217292014912602260962664
576743996986194939894805596178410457098
758983559559817425632546804661050572701
113972978708323979091426633382569375140
2134645063571854620463428880279434321851
211184417282952010335773510845238362286
179027228048626212599495613947447908876
53434748176686122617340048434022525128
657632225073992343465609342113058959190
4356211842995806741762221326574497675041
468283798231456469813143323435099123926
380830586808735733121857212664752593700
9212474359977790289753266368967182237174
74055672854835122983166817239736627277
4422562228883721420135074585773141673411
41556976285430492431938652417498013544
229701496054095689174586418455353425047
449420107775131058433704858033622354768
783363916508596338691529669490983415783
2257963792825817144288553689426656725978
73764485089498946293376171317148916590
9745870375466033905689113717692242263587
470545497503241345838287880148149092722
5270315580967610574842805960980395332737
142293955673566669581293091557117331003
91761524749288392879163017193260186880
5698992999372426980850715092647955442663
49661784754651432822155768588416641096
7666428012257755345223272142202160184507
24755190245113706112548227215089239182
5304913017701040487284766618427632739981
70808133210639442926191725683624812601
5849312783782719655168965708241671607793
856660425386491021652467904778016159709
534888722788699198211585131605336494097
78506431564701055166451755150567588422
404932684792411978718547837786754399730
912742371594637784427644643734945835409
7961182986682801955456006024703549364232
4464127670392136882228222166854428687
579594542685845554722753259881871946345
60184523717275531582773152638173228209
607228253226028602337085355777977575863
574663054704345359191733406568071798535
803053732649679020762685462334371437752
5578658016736754974282445425195450449949
6805521525346361110785392111643524371951
9251533097471016401342729818339391139
7037368018391657431413178451447577564
6066158597594050606883244071218530121335
1347033480247679655227911164691661516
3498553291496598177745004621871981537480
7038602523312025602530193039144817921663
56176433855882329399311254343415688687
507255931267586826625648423161594142740
9520654584101720662433304048449323716814
350996414611421477227497505589249222
625563366802171489340674319936554586776
46068046230118353005877329788433141497
8962508059974229650426056302760897442250
8219666399434613676989091045334143343071
8986209749206495667477344637255862372
6416592473208012517479465217975953831265
3916217558164867786698124847990833225156
9378116596934351778443782905605099889218
566324344893152240848391561771590268951
8214390866784136922871511135901074228858
230566791733166282025914758068257526894
5008180428533913962961747147374065398317
52576217544293452311217778918588114486
312432927287352966575457374130779
2727274526607388811291469886260494969917
5393719469336046346852512360731068026477
257397904845471846877922669503433801683
853238025218577234067708879832835212789
1638593452349413794162806422109604898
766497086397550191919671926131195956344
6136813486787574144275061144986620922607
2694125271579431786089398248734543357174
168312931028809525271038755601666663401
2332261058205885320561312893245198364041
2180897440378743651054571071464946306451
6838232083235800539767967279551442561572
345637923101298765558582609196539329486
475767742009956792157801239759760357747
7805823041283804257034804520257956935478
8438146950332449621855616699363987704553
4460245863584488319231778373781246561052
4381192243903632807511383467897759561751
858443129969798870131946809258707521573
2514629132193126973487572544384626739594
3682860153325822677966676037561398677449
3134723847175299592112646615400373442777
44219571866849650342339603878840557183
6341123484529340813742938844855616157583
910718095266454365895045424529188691691
8493808754266008811427423578288043906088
6144955082212778365831431080816386167778
18034745801031734093844969168469883881
3340806310043367359845952520509311685325
72253963132470118378251086476222477277
6167603290369556542394848517697843979855
6412966432507741944923045502142882751609
8833825441572995133462925783422789768576
837032329942768267183614192410320933709
5857810788508936253228199878184775695680
6516878028826747281347022178753510709703
7018436388733947592767901420637656853664
891656283511764119110263126251628894381
4751990321119928666916676191389581627448
7385477216316631661993628154178342426708
83268854837197638318341199196290451773
188869648721119832975779505950615392447
852141249153371501032963363659579243171
7228531610419200267158587991109486587151
494269529949755013434948610957021193909
31221621349531822720796349256843982319
29702693904855958872277756961584825335
5875743422918535513952604158686841165403
5534886958125971635694097522783519387776
8885124157106145695589986421879928773743
8254452964219634569073565295253048047153
3391895476233922041416354276096873381
704493547187413460760643695692916017186
50336306302720663694579285942893378611
3552675784393831120263563294273126042732
142026240419427383590237590722231433926
59222262528915069236715620307719255804
8605156511639902201896517840517656242920
6667251807093142601485078232317058896
60994271381588255995399953098392683833
988310322119763199843166990348754904363
40965521137222147019717880136513831672
22751149771697035139323295165281725380
1790277733053469760010084526245024549246
2851271334266353233990612889737436253344
1662224362807398291969599763338311719185
48285563671976428080666492532129511372
4305968429302850897375304381747510386374
34634121562862901980989000679744184729
702015881186401519955378376287125535574
754596544391977749946592341832971497
8114107798488559399240953941883154741
25572475446254890077043816398448978352
4899533498419403474133631844797928878642
7191914236032628171368743509147127084385
80748674124322528389715262944043403838
229850663706111715031654717384789197259
2039879685273062733159681593795728532222
4195228147038455350883454646450560453450
6054733337907887350742156729240032174844
4314838387866505885764806092589256515950
527021658025931869775832334763391504490
827486126067181523964764040741726337359
719761964835269270056029397851605893552
73466686821172169504049809260872692580
278197653753981752552757284992310724000
6888762175925828996882579128649234435338
384541619347959023191231406255090242966
2338298788569134304635119927129916328740
80964493816105424181187482424203846442
855866327272255396992355868691272535938
2157693695424920534797642583690968691249
4343525721703694989143158607738067618
5146589950658809308983617832464689866
177192538839130840711402696798386363095
11174441125367518398112712623691113549
798399394103195602683026165389528603945
950625352714362610025553256546392154628
180748087135510275043642015238622854277
811395966653920543272123814297371102508
9691316133520422729700957359584502744
747518649246220857492026972938686615310
647168463716619184377357937869739340
814289675059836321963033953669551784799
758633945769256390175334314150376600466
107751312893494224542159533359452678326
6059355938826578793723753740733385552096
6745947423006760894059616406732256376726
10524336693245911472985181446242882237
7817161158721401429398062876396029697915
5404142132367418181456156593577859111290
4014304686207421315084746732160795628502
4018391054868253177670765063492726563937
6090491055165329281394788175520645393290
669182157749489317796839301461675059421
8581702282378931593562675599474326273592
33813576343667981141197133654652508578
3607736052940883758003943138053324531
8116776156964191851712426080755840859459
8777159311405689804756525999668654049394
9037740386141664886498024942201384799372
2330379599746946491884122909526620871214
4160234475569793713844374877653445835639
6967296895422266703845035933490736314
2744677194949850674426503284909385569179
6952402578908496697342551518369620475154
25747748537778134309387857696527364101
1638201548203094488270782573323127225
253814066296206867518672018393291044794
8091410790185747679511219032714123975400
193872509617791981832315376698313258548
90883558546936061015702679343627588840
797392678800446050097750968701071838479
8349198117102983248195425392341787465169
978752463210873773233929452881830707868
85682257952262903025414773778592349449
372110243988395827170538310610324137331
9523500019972861909448575578409352466059
4660165838489433588380039885813447621096
277635248189739569962245478116267994834
2999699331989270101161856922327370753296
8460253257486018602645128228540014761364
900755041435251240265925460603361856728
43086134679911958054782110634830889374
375436132232495230233059328598446231374
9748848266704716483831937936260137185901
1882784476228514252494147573777689852301
1804796319412889526074804378962915289892
273241888199809894597711624668224886489
280972804109976484092147929310668152601
286021291284978392897766975679354921322
698075955203609257654103423991060262547
2169560214147736226811019440383115908565
531728358894300497866514270431104985416
69633070554042310154104867430646766682
12873918592360552260258912355969901214
5444026202884163028017883882363789515
5795996067704773507436383294899442144787
48916090714995617529056310591823568520
1146499179344209739077031379739852551841
8051812368288066690054184101580245329269
824548173037452209726656721373098962340
1579735139912117910422019557764422765
383363656896712117814992444141082635590
49617358357918713769607376921379718926
17407344177128216912178520456551334157
3089545597328107909850344670917636252450
188147614789469591025522952958295679485
99846194815297156175050282508488008394
6950893646041168353137968972268210402235
219097164742191326977014311820182036864
5030445830859510105329457035941681469304
59535470518390089255810323168505677585
8972942337355491174662343003316646285564
526492396789205244978611195282963175842
3268659419142282566984898067588363446995
8796125333123439211324515833440336900
9636861738398565910643307333864160025079
60302394939656844865184435018810282173
9269543742675337614716704628337595552785
7689872035473075175076071593780405124
45429239613352957716039546411498519764
8956408597912518707956109189698485133869
35776928296257807755667348324161621736
5987304731316830894058441212990780471681
822819257932468541943148038913981412211
6121808721954767589173575218127178718591
358717435564233226604299109507624617908
25816056825483388853372323175195471803
736162323601666320565011601959812267165
718942574874983515391249015574615352
8315739922752097866216351375461345509613
96583244705290732227346900487353648528
68964913373622412784440317297504157861
9638552315448811846497695511485185149182
64924032992982245378504736270565494775
2047395262687999432367708705973883321969
79559849434529598908246090031501813393
1157598346124040264959712402706513469776
581667080101475119064621424402855182301
4161633397017271930119652142666341153907
2996495296052526636596101353835588724974
672457124407776240269148894113635444224
36517571251385916543343488834152123000
467621036591212180504245885098893206281
8161423289996186352096108090570894148321
237142382342839880480914813945090271426
865392725877140399949624135473828313730
64304204500348587887053518370319372084
14305047418158480365528862247451277917
6616931850427554467491203709196096061
9337399849009054689250704990290831483718
6569207837607113654591344900772885404626
1019599064487723332789357348389286783001
353737918179907925692344494293973481
76406376823797992112949211057612168265
121133364690560011378003454411361731008
5027536857741410462296345738232959622852
131325612180767171657944577523851355805
1683190738749575712290772121719729885593
911055801572862418921567909377099485591
8112299119601125992597774990453290618748
809926631087598829976639596394680196164
9000860459692177409381939008553353774334
7591854398039847349956902957943118328334
20593022380227854126119031906715568523
48982286982929278129347517199921450
373429633456702841084187559916696686282
659825114531682444633325336438430308276
158858712101019922252886450118352187274
525284873065287987684781332287943805292
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]