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
544113462547312270379578114254073318231
2716822989947214189584057971420461809239
698342658773736084705966160098131121783
610561502639764289902371945728373463558
53248131772621922875770914684357948143
146177665868345722289605272950551373489
443320533153206950792964614262026244231
77684629779562065595342296173031115650
272047672757570652358806591891345061367
326939380174777458262812190889576998982
6557269070355654132232019092706198705351
7998858813861295986524308615753447938497
782883465155714854458026711538349919269
2558500390069170700450164614228447165705
659662254162358481019799133524963142837
11278646363930186314516853129119416912
25745085221584154999596528668010334419
87189688871175455591562086326673658886
1536114488431517436288983999556626933
139042319711118704497962251716515871282
3438622129173362952688173294613326954
987144851266610925753585170226364405895
737254254918314422408403166447907992163
572491824292619326271778551885479434414
229740637248684930243594622318522674438
148392631171539538415553851483259164755
50651732725421930863453477494797953235
483145698256941213089376052783885395
357645801511959219757111054952786293889
922687914435907925812947731995228169471
159967164985996865712239145952909863522
27405527100572622741878737336074463930
5990899744345204395978488016336386128049
96042646450215854222372655159759649346
55273544204181340562648831990838663706
117451153759182760693493638430251074180
7914621743041766720925042311377142453
2902283714262922878436579923499736567670
9673353613984354366558141263623814802
60564468907998762421840764957721469113
8582585352747948751073464264910482897194
60180792366042188918221478279947934122
7477440634263756666977858221723675333854
250126951304870340358462381247781334830
7277943128232774959918196000131857601001
6003730379145741581370823201496356341578
362554806396452953513426655828239086233
805458896405767431694876812188432123356
8036720330683308450464687415657586164198
4930279995943615975716363851429620021282
8851346245023803280653362447440143478723
8480386307949056174805427164964754862
72533075756076004912761080524832656721
9252553811476486438010489266103251694605
583932417834490185277947672824316547116
690733552543788277229193554271747648505
5625181823886072163062955746448613073724
78036131233920267416878368687516653816
7394346567198271311174573996998760559038
6659361163565704659425258264179146972783
37697175391785320513511118643490788704
417293783651057353414363337521966038136
8955656693376841106446881197518943187838
724139055328345150173033788299759346852
139546803454878330506187758478645624992
965166257875294471601338728422528836111
8187262419805487162998678716151284364913
5248259266569988500538677724231062403411
4767896937965684536131498406728348596201
8116886963959875662444525577367136017838
29022258860669887396754932299063103264
999917036019672171437019115563877534393
4823598843226886608557872188287769017358
967034115229578205712405595215022441688
360588656602254938660892137540820549336
8671631347469430871333853758168049124526
778574063875438069542412552949311840
2694606943648280632979959026118436862639
8072632223686144785346512987989810514193
497517664957539744064096122720129415944
2587136789302168118933483228799295795628
3759742626261646562383455168283480801
118415029147950691833923548376564221120
97187577314807675108456444256279761344
16029753546820716056463398866371492794
137286048847557301149968978248820029104
88909669133349455527527680659743884958
143752563555557115970003835825711486655
453198565496898743085278341731310245983
3895334242109976288627579134899684039830
448894052435919480452270255670377052644
15404411490993047721111726086682325649
4081364842969995854661859797937143031506
885272931830498464304539440527557342190
879576646867663825644664816514896401688
8207741370759354303636349806689594018752
516861106935773744699116381438669621337
8774026680734252799393418182164484939
8311568859564608113655046562997808035
885848151556879984070301651661076099397
529583573807794256792652817981654954358
7562903389923987204763887287817516699430
43403371283042186116659224993491909552
27016280882655798956652359323893555889
691711548786871971329192716124327293267
504017358604654166765768739026278683224
7683744626308741822510695111676475228126
26779450392736084001229663551253234453
102164743858812248349218224203887063691
970311723634836239477999891499172441375
306463641307706064037931815116819577340
406037596966626745714620547481796313111
1433196280634379758650229352404882542102
971473117312357479089046937152873241356
167689209623339087994613369830193944826
16348797704447817690583200329550817959
82317516588472564133843731468712758720
7117538697855967713942776018160848657
949242641712589850732826810690162292754
21674264842440044317148825661091166672
6256412757121755262757159513467612924304
686615659216522047902390682037348911123
7009861797571615884048965524354864318084
986640361265647291316954564337574095345
3468805163384937795937093806330432257217
9136184636701650196850285838303862943756
465096567846225296163618807386883355326
567099915223724351361034534138243177610
140681385130908178579463562822227337577
426854177948770766713434730337047322552
9146756687208370177080953672506685226916
484562275453756958594732577382695226785
1858606942692719810620148436902182475042
880540149770153663145560292285785393544
4423779160158105860465818276789656719024
38697151742622207289539628750012739254
513311015281373692023667552374987738022
40095815920509569185288714719525606723
788691701289508774437067278454045248264
4168208913765804432465823623855886387179
626920845899878422628075942543283304246
77081396724814227292281124543688608121
64274819822333247575912392998425278189
830989289745025253745677609618377176035
963505710899949987977954917192836996361
772274767635460929708166006647281467500
39649368627841423555653514111097959635
7569877867222027228756266671412246962624
62953929558842237354286769393592573164
446390195195287078639266850830417876195
52649504444233416553701673738892869604
7801705816653078989812181532300771428769
53527168993905015627309679980241803783
192140235579244163276526975848117515380
775962132373341268717049106795764045340
401859838927461592226038938935287516
8363675748744555371252637419474147429
142457141816481564955216843555427168742
4906696719802066449246034092987937274067
2158683696495706572482303612399193733696
84464989504824437788599330749067076474
388374952386075743896444147143218864567
348220317494722368965818138996679795743
7817696685407360310852912474296331931166
166370069715166872719283775610026443598
9593653991374401421989862555970249388380
2255570382895396449031722129451032975661
5348844698723788102993942258564347192
791465889447322677061356888730287567434
2418563234197608255676244548444158594142
5030612473582880761259074858985146051
648195755245288084768204265719315903805
8050711188771986177351963058021720357
306653764426864787253554725309523971967
2829447036495612164566083774294996285524
5139166384972094772697867062881982996554
5006712336173367305766781552352935002990
438646289383496819448439298568731126
513022339074281320533601754304826579530
6846609255878575632496787528354873412003
7566616390776148178519735689435510016301
96891989902356095019989738213768906140
371404329006707315341833278976832891921
55831811200060768985280951255943849418
61961018458976087770841812872053034817
3769624108789762867475958728399156256
39786665757190933272707715334782252050
290443489477284131169921758478117111344
278181789218335278206296944872283439672
8100757554133037487395695736281453423087
9590909793751008388153168809294967774119
222358438414843428254893531999121773
4087934445272965265684857732585930019797
138045682071177712245841945759343831242
232243783040346286457697560896722071855
34405402058512846523159673913663836015
9077883283168703690296922460541320576605
3043178893378969324963373404326682247826
61079330407429395940699338191457887114
99131528870643402103526697021937413654
9357462597901591215566479983125620434389
549818525867983580942038205480345824141
32409545813479373246264865584006184694
527530826956526237620085444958926959906
707480327950861148337598230335887627861
130331367199411745688850919479044141116
7348729321896427468258671749396130847702
24809314380328285037432794880379819810
955841482338720820478488602792323394368
677325225424131372147004724335229100800
4110520814169172078834515232830901794
484654103858364431279527399226545522637
40858875585960496295116393167955636904
86035086871443226080055506628335992415
4383149774454732719877631233156865923436
1738885555884212688973802290541041806121
3931305462921436619841775101326339189983
762150487744696159383733490800074343942
9270881714319195544928885421895685068879
945148864652495527925231477924461759808
532960773523731970276028067370982886630
6481689597656579451131246306146554811310
37812396897886955795416579475759561733
6044169054197626506691523519126873192265
813198728843756138585093824807013301755
36471550548228581858352837485970498617
39917246442586151102547343110516662195
794842927602370567147234871836873383150
242050307866672499962520161610612217448
29112568793434859778386611985298882045
1606634374629022739342582597236073847663
687428246314880899821793389368077153350
88098278677139950247381150132453705708
82588546437371817776495687592407083731
771390873808126968376314143119769131729
209810982034778834082253965349787161797
5668599025239884642960024274765546382530
641274369993912714493833880912071567431
38647113474346989636265966310377669993
366466560297787849337638152549887416604
81529015269328576504801337981444276848
528533955846837710441786623393988763578
77824837509526172343391775606390004125
3146404467214241569060715070216585141044
4709495981632176671404117781357176186
9739644858061356934233209447824123293668
83692402733713599055886806998866434381
91745198474091764738434129106885285281
40056653613788722328288460258544722320
748173917983475887756458348199680243919
59211687908548257487587393433261687388
50950056059730321690916730669131155110
77844905274329332376709348743754302868
82558695459818654264988355808739147114
291436767542979261567211114394356723118
656993802230356476153879014515553943508
646925405549264280925948499216594373134
941442261162695888967071428146314501832
557738821867355274314708402822669916660
6134090988127906214569245691439975373
4232719567318867754168267105632741786105
643760925457451600836266473306560604440
2577663161944052733599891079413844418652
666520522457942650561708618640489787441
179659801817575825559134139973144211166
341834563716691532845115136461267774404
7019488838604865531612387558393532832621
53079723848544554717574368054558562157
14367043903272461395439104031416685806
640116264639366514236477166359599256610
14495645265257369123073785039759742
215711137325568639948579480220715794659
676833653150184044897049190489951289148
7142695259251486923690953882449374858965
28660801989524084792416166104447494635
5221492581098928281028488019249042005717
126688345957764262365057923390372761112
3125673335665611583191739331485851078045
562538013930704667599181841722991744352
99161924540166229014446987769459865203
607797334840446241867479719149290692383
3316698783787878550718893325439080551300
4141388482578540526127125875953738533214
41219135363464054080568183566656641398
890686302985430658777109621423756285397
6411745041427360187665626251604556181606
98411137867017355316071028171348893932
6668840124477101537370877607458051126764
351018958016198031611722535120943531511
58539138280037495709778938157635969895
5660999482725574522993895413970089323760
711358834727382343597002183186538506958
853520848604687596608143445855781705800
63607879078726871366286009625880195243
86987980636554482345425807437299664091
86914582735766740635464561371629224286
479173112915414411265047062464798335182
2011588426179288444188725968854677284956
9301172742086308293311134767866626973314
2394921150335870960368735658280523159195
8289489144315481239525979600306870573887
4395180927117653602243416612422026812512
7445111988961944203971432352623639735632
69719681620431670871695197122118524508
375664263077806606496141881848128771534
584614541921211545687841461222984262686
9797565189916008828945269195475049533848
4745879430858948403286027993878932629543
9872913546397383291576122369315764023331
897261966321838629457418290708956321179
2243452773205634734983087135158559096
9790459872083264485511332067746269178284
690787255690732840637649852264487378392
2583298914751218852173265525480614689
60205265122117497743318114387354932414
69751363239056288484840672812489751030
65919605371471637438218782970486823970
1192472515637384445163331955727381566823
935538368372327453272968580880920529539
7540166287462412371573199522406055128649
9849292463021365419226048063512835759339
136949273736815467477599601169528242034
8308862426784179367574716087601560312452
70349347776230276280692646274726491441
435329045290268143979325386424236868481
895410826441206842355546045732286478035
8027824868160725243351416619753512427
108187198020253189838869674865477271746
292757568065741752889849347773652136746
5024607211474178860180038733130054978476
9178437649314526885933224514855315249819
768268987841420342868961234668792891856
3964271824835893535863913713388334675
2721287194624016163131976670868769990
3615876232262912659427737416506468995322
3025689140768741976784857818163739731
23614816660433131811213715515517138671
6385564421379229636334722677578140391676
38175050597414692148758381416869364938
9816944742662692152576805592529690025188
2033178574032501812979671701113431487298
648922991290797827971845685436275899380
7378283610363231329745353591822511738452
6922446247989234746036802444993561143077
3650899778217486153782807804620950819117
30547677460424026925575717173347647140
6104944030973358764934732467158828623
7747825329926892354025087197395912632370
89819797954921363035108513184480309755
9379268383533651259438496114553528907
863597757320558275824535968338551509118
887726766387188512129518320112141808671
332960702308262424842616243975053555820
8375989896763897449107684957171457429
432860284549936714913999303937614354720
623294046861388791454155095108984331353
517832102523185083058532300798669341685
702153733679691721550928911902450423647
8704382610229162144555139891473416824592
5418980319084675121066718031153983834881
939957399876798043007874977781548751199
109912829021878888193975958132276745532
7663987165288922105956818136575816891559
2212969294668940645260983836751256261530
731630343695322566453488490361034671453
759382045705904588728396228902840102798
88921727847561554837873470651008416770
9951883267764912503725637528335474968474
527949755545679072934773518719177063
2805277694713365849289203760565517569992
2370274288274818508686714009903991487652
7742547135619119597634378445844667161898
92352123469646636483822978861475313160
4056102117153159897742406479260915426242
149140615932953917651799226857934922202
178478069592550259779376767416123299224
622726869525147176993641814691352489332
616910858652840548492592687252170512739
219113982407247263495412698144792744551
84301822796787611185360251995685468535
97312347943138892934389606521011951530
763379421414831191149919129259718606443
586728298241171059047375634100662382827
699954156387100212721426707064308967607
9251622984077847609090375032898613373393
474139675341152965810608374436183567708
849642727687897781674471598568422984333
976638426768781353978194479467885451322
5151446839366873135568972689692280707518
9889493629828423173294441148174919052474
1952905579237013401830635618221887569999
573947415197876662755175315216151724489
822777142710509581896300270383231551762
1558577264777447882497185726200224129789
86771720261997749024395138535686678220
16516834579803139087309133551450778043
137295671873773282027950769141040629892
184610383746551479808009964361970407672
125446779671934280297674332932955514542
989972094046504383289713806970918955434
4270619097526015260710024200653174262865
678492895724874265226828001825262821059
1853246440043907618730106059427022226726
6657796292914339389291186453451561976635
910148069287498332322739804797627961339
3168732500446388779438434394133851202
9010486712609118653353188241950719898970
4993705498151566771980761912995383875352
26062770522989660995659951785977619389
3451769795124539843844775294717866152663
41003915625850707143287544597689679752
6921681232751631566378420243579411787
8151339613545239388675901141168513247597
593312971102969521254867721277451757557
687515514046860316616698153127048667269
618023754653115815577305534871651239104
24854908442764891382571144977360690823
792171939227374498021157691513898152602
7182857460431219417143395635215948249664
2559526456533735261437812092456062316601
527851218517201620789203638259421496625
243432154741339595932591134522115787457
296681712205140770941156966854764537932
974713429044725358774798194246382202531
377463532552668734335467314324162717991
708028092222866767566363190810527045345
369566348277330217072173026475880949697
88068396849968113485841158369126768771
1449856248791706813411325518550051554331
88579973772407909393454350168712012885
960146196558318636319944347629731601679
538019834988995177853137932745411194967
966343121589014022202416881541475818
1396493562184330642436293939612616535
60419966201264584164574208120944927578
2326148344549780353323251036122366137172
866079613860864555401583706825569979534
15715867530551318888931314299967964365
3824127235659317762343806029752486839
807063119144677292464263521546295794528
47532310175330148535529566370581381290
368516501700577332637354695762319323594
147415514667495703741569427993435725235
651659457351730169455277647353434227627
76271379147459844281423581270596762251
313473143643399128171575815470689072828
349576449243756947869178316884729220
2217494576246044488867021864694631152117
1929345653983989323976028282337795347294
194586657937164236101035469608787542512
451551266754356179845623932999641061693
4310358791945955634630665217843862929
380239784106784517058615521488980277512
4827337517994591517318887451507590784262
244572965683513826826848112125427715381
844465553535345115563168614215343209240
628953493365687307517533251382112179088
394045135720147374682537162374765577162
5049686366216546516516980519117548466
6288560940919742106598154447269584566121
559349865371721765725885598789141073437
7460533452742178075753484895557185148
5670677437287695107881877790334693217579
207048189357868414277727063252471822885
397272970141516305753544710488756029
9759827241186923195428144930565434356796
14261563604216155186871407200648582673
14268240351993699079771736938402862463
74665940818966642629642561170129808315
554554534003316089124409218694311105000
345128123887348157294266504032616067476
9806479586933200264660276274369781248396
7054915925727331817841431682320631155635
6812991140063997297617975728209246806643
965728987992051669826608461955826329913
989363716343820726211653919819080277576
242377128179529351683993890189355316756
129719348954172492447902978423693305509
51707407521088979311922852279993657546
8486321218136562503173488606251814588555
4451121464206036726118784218477775236555
114016752776971567349811586431811103888
41478827046274248863583087779367565285
3245463854516757968559725440719087182
876012528235882532693871721139311212
563038266426339017637547534136604737705
675200632703103707872126013789680902754
69672538657940475208318074356416948712
412310961583486316672713523163476455430
33093119928728317025807159499359847393
86399123442747385488136553651391017755
750896915327228997957474629259418094182
8200394572755481496150755339216070552415
9892263714701954550678363941669899938702
4306387721951856885478837443121886712854
792894268659790313535019858505166463845
9105472243231977977830295619286748487746
1442811678248436867032345311425348882473
5551218846835045989510729330533272316102
10466580108050881625815529841539619962
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]