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
9927263294301548367194481924949777335200
89451145493806173011966871550222383113
4835141326065607172014657980946434632011
626640376119803639508971330933626843597
7151465314418836415626906405229225408972
65956362794569574640142376247975539438
6049624565398608479066402442101443726725
4637343633761016271426287009644055703529
352415135805290327445529094948457362
8538381181607517672546021325953639819005
414276435451506653729709357266136443049
49491084594630296229826932528483225872
53104898620980611075736328168479518259
3743749466023928215365091907619741612096
7418655529978143892198906673702227441206
1784410578776996732926774075901868805831
539673008421634378703835866675568322156
383860163798913748724303927255642817503
5483955936076840950687752798784468162761
9433743920483786240527622545864090071225
1569684919243170121576056336291240353090
7281144042543954506258771424414053074146
720135369978987583301469467527724435210
8270818363103534654948421407965428365825
465968842131354607120694936786718714
825576226633483219862688231233537071076
237211384711746632611779974703523514494
3640968697953549638880109676487917057277
9193633667298196176932273561556641017
1951467612482375685815417376913143246661
822656065702383487361323743553758291895
300730291004508956014351372297516588995
6911133038948927912916821638167158256084
1358695756012944585219724288390397707420
767481360587375487744272257530947409
94249911268624518900337168093872851732
463068691936922081761104876450945068753
8683435799301145826020351683457425723415
80239016293947412217607310106670525981
469440215760820360635854423056124446854
8259174189676488209587122633798816884391
1714115584373367921133289523011685818
91213452423427624271381607416136698714
2135446455271253866391371120180901255
2927831870193899606720376502868445187587
920447848374515449343732161471050291074
5202829426329424296488946200150859109566
43952209508520278928106936189288073840
13802737964954446176178436742582353456
7033535813239893919151848076944295869636
2423755725782746782182621815236227134985
491233073714554287358402798971763488276
969428911443281373173589777115177644990
40112046512700854703660111733225152357
2298165089852285270656056340577831397946
3268579514093312958718163606101956293999
7150118086713057101625378243205252588914
89185093998623749253821328677593615399
4932662936428751117647003789160094379244
55568974008421292674172751533774712932
55326929944489958637633229126321387723
6298620659765343960087023297747699769213
87173023775743821739656886537092022899
4721178712278034702859663419505295536012
3745601799347876756919862115743713054492
9011604710287629732018079061578120984437
245173405009965850569046532580373182434
74254522619444980028436456909474598581
4334743197408144765141005916325012632455
5796825343579396489939223919129791475
7486553772341363308241384604673545686988
2405260962963800644369899632788246567047
26462871088341447623576123627280833730
908077344644932915736073308231448146778
479367417047832843956505273629450234168
32474708101029193990873827049003846417
7423630987699381947976005379268993324725
636665446537712368413669160245294522941
387381709911188544327408748503563156476
7976168533172961925916313697663556457531
802772998705800725578658582193222637096
12074747639618333309708015659517695943
1837570911059450192362724244364668844222
2962658316782124212167245302237723159916
1031667919314106861163503146340048985410
656041064679951930842656139148651617132
252910212315291595783273136585038636638
8414237174068600727898894828602481796
874747007326521178198870947381178372480
2004521043137143457875849678970066215070
709836122600188188856006711170639956806
30995504858866031792563439694653610189
664414616854559696668788891870988727163
89404162814104570060807268465866045062
7587666773419091598623479753165822040
4662278524218532159934584563258006119
331660837519198886815062256878160918820
2494881760414164985866155159815442189261
3805726754832737214363174964637850487760
7126377858716506565470991037643734398890
8689372029722597617792364842611280605388
68262547202901048464218084738020021581
2782505570283515185233407085539344454222
142560356839847550725932441531941608451
9219356778345579917676668298827942669090
4346570726182349314535109113760120913356
310863949890410570931545191497110438931
71846352121966599986655451556553604562
480148657929915525293204292709438282476
435325292783477895914844908119940612940
7846921419012975665089667524655287538141
4545144887958644804903605659196420694
467576693228489877677213447610852392
124142528787894145359394455551926051940
705929072983393222556279059190269141800
8174174289812715228945742168322489349247
199983233119828834550518747600595639776
7367412398586561700868937149158487887732
79995984553590110618615657150613515913
8720500586956922911987244523333393587355
59155280561448352528836645125921219695
825912021909639455522555639527547161090
9605874575471700313532584649469162411357
267115969493686491727158205662748819708
56565461428988674498547964442529788906
8433598682602615838375597427697016842006
9286484485294827329754223236755087486731
15963783213587550505789744826659037898
9810709411862166481486458626706598967
898857015895509295492369920856967897829
5364142211492718188811285496355547139301
7231721788805417722497664593895854942584
2503959564180102010302269240429834188
539594872688305838365057446282983642659
473677976533938652423722565740559379
53238121123551282534206553833630436661
2803612175367116949059609118297891470
179958926196698320306105217180099897427
86657247245872687659968614397333729531
780115872477529359552452756435585349050
112019011534835464655684537348583802571
861147269469266646639179455767284086890
135695466631583830112559252012418892995
606716847564098470777397907857913829637
6459453273394214634824006192780999279154
181667168558048842046035872721247475362
19904144383738342849151623354644828489
699818901076405589086519475434158876929
865600268308226674352402187270318563271
94826563663342625736836367854345793797
4044488781161443640234688290397226821114
9306539329162832552374492195968491767475
50227424269953004403519434238157221167
519608167889535392576576510368161375562
4177667318045985790046382100281911363995
7342271734718820953165489245931147565634
777240647054516181581842922325239702500
28345897137490457223914477841254797586
61944844453398345619328457437772329316
3334647460899426690378892927773727176439
959557102591334225489843264872068537293
842663931698770210060188857896333785446
7312310285714467392624398043974125957263
4998311440283449407731292694656838202641
30925110306737151001377334311382366916
9696473865818360572032509835465023362991
82403234232898235659162248790120306289
9626325749064589597262333558723033185048
46377659791762528501694912250464462499
67517816980555738856388519647967616499
2482266469038703146744482903278323728209
5394794740661203107485202620295878744921
7120569395058504226195488142999611182543
59361256218650254222536523453982366705
499454169723926447895612439510847255574
424753188268354555884842638766993494346
860651911874042872469647656603474907689
6383355404817751985995890929893274552
8947884143897577873559944379353998346303
930024683539819407498702589421742743429
570197304462527397623313544721487754542
408169963835655834545552907444387838281
851765626513145480596259498935976579439
539475816148732865282974352671812609
586092126001428151769572886330377925105
3254422847697673421455705967720055548437
69811079549232997692428477971314732367
1855893645677483762489634261853322049349
865988115348401689887372700253923689840
5365314771922105975766471803501228249293
8749543496368398299361602680267856137432
6527808957459167251038673099313161758805
9748624552618949311693912882608522627731
1309321399481478629921567853626234978277
115827378608796759889851475496671896906
756362169359704237497327257465779754001
411057099547802483764453392025329213
60991678914170991134413371641283152993
329225394864447164781761626336562854324
56165866841549870555782232407999186533
997261977747130715228695362414517164708
910813749489280532799368143591564763
4475556392783047404858189640442964966731
4033583955925961928392747523695264598892
72097219247393614365179179986564515878
847118158586503226038672839263033413310
651359248593816787925167381867294226888
873618479537455840012786868777654687681
3605982033821709678566285860450884173891
846471491296733721698455731128599018389
2658968170549993885630376985955276158446
3010946825938593660251898659449982547265
785380296355436253763577257816926191140
833119725947985774428825139545957528473
3256603139533963986235538781566523995918
9355878556772930883394828601585319627991
3812620553298184262797408828972451032870
257981294497918623790314852131828813251
887536928667232524349367777358857281847
188649549446859874778933792899624945
7532761873061443715487368005109925746661
4170474851174853825610455184109427877436
1373917063026935693861546432746481001160
144729382173646387675013692832114559628
991239263260886282976222482889347954455
7841690083634247252693348807223373518508
2286342540226389110921003178744018554
614729312855428135388432009995299722948
6931653157262943835596014001834148197985
136383776613624953242794396182155866894
41401915402870870454626481685421521524
7701421641265496775741854314580799796125
649171684231704725208376135262759911808
768970473719252494921576306750013418150
31329299142673088518929899497141216894
7702682410539511702590973231130719747141
905523927391870815482267690792437944849
5494373119760166136055367818693429226
44551283851216887557970044971453933828
581765075381589868568394361929271844985
392880103739852277638201336658112004134
308384283647317369239069523553769413674
950799522227977877782307909528051744329
55118042251639934872953541607987936365
1693420416321479793393178594501748158197
914225722890401456272281398493796618283
7906246135944687751581072291638923738424
7723228495048490695451299906189050102919
2982925055535744749463404697364720279034
779814398963742864310975976661224696
8933767933395243241378937316464595656981
34455459381622351983515082125558483703
970190887664867228589179186918969643754
74938593654891496779616437557950261462
1241257325611673956473991666436475301391
474258203732362484237445991601762949486
3682231155503835655043384050316844138611
78396726405246585056286915546650721990
8558638543145633316873809558428496639507
789074047735441349410705753172893914902
491521889387272086038952663281480367508
46489912506695393228016420779447188860
4398575566466469864855774205447361933531
7444161235990487184212771827423593465
7708676695324345533346698909502126425237
44681822131329220035652363808227766528
273835648215395091124530164510713161498
45706846249789225709279875069575228688
1824523874275453504797374126457236475252
428320407991143556862086250279649388467
158160774351990640456265950025747257752
396013514818545959529751425539754172110
4661471084356446687833736785147819062449
882592140813559834725882677886221219522
999173776702918521078555287741361736220
93547484336685897529423084706358558647
5609140653467873802249897365292979949597
3080148629581739627730096237676630038888
7865718310311740277499872911173562754847
834436829135965031897484152721550459907
784954835715939604835706473695038057056
9970352535702205575861577502767083455451
18738885674931875262427721782938144816
20088908980619910775546387056516156
317643016813407201813504302672764552621
28276148152314159738739354752618861276
27580797388153771069714774939334252247
205998863719632351074065925360517713886
656118875165556116115443220474217179938
809275347841506312551687991110132862441
4927811623708240159987919837114038532121
3116908760339253987258571263359218322024
427846629165966307928596737840144502567
3602760990252309538877221660449689617960
87759173353181999306897380623711285474
871638941870244821394446976265061275903
893910298355290537741169180084562272480
5034574360484715471015121317615950528699
60436589199278436759704340252982128750
597793163042837345732248036624455907647
82141718799717018846044678393148002029
5385980260952087268173373299302017883821
4797501397209985508370233490939150045818
125882460319007094611299832872643442
6121134946013283631417084133615246793030
6436854480237087353798067564517240149929
45832109914343165957696818414223144146
9246812588841144578843929036454949643
983796292967915365926944788667914897184
9053681359924689900010495838403947576371
4621848922939104457952774543958064834684
8767369262246706137988356333712066857275
3558676675677932404866865057772079428617
4175244653888723327526141881773642519771
616533064567190196324892625423239636567
5473322046825030621983897609865461141786
7196320493922443865362448434361647126687
2454312478295186587465149483282890786980
26622728777431105986746052063950816054
460930756277817823067191300495111076500
444166554031860552705202812793789291780
7795610845686016676358059822584312651698
650486941835264044213332753641276100996
6476177986807388855772783673631518492568
8118582562072275659535908030337278323618
183387159271831804647456922874214180
772823327198464750699883712431143629683
933530587397854266579238502701017427266
628134256167402785950598017687570414132
5725673744751688528529469368347052499
8619548276161609548343553199643646195671
5113577076872188685514654491709355387789
9453915576016344729031358473610479318649
3040486656132117417271879511848684637
329236301996427531064056917851656122249
988558897363106127347164144267750789338
121078212299112942977245300031621730614
4040973635746225439641294632069474446
8931264312637302528305426654292783167
630262098088785577414956233571181929381
116367438552415253276565188682666245096
28087406462685844733496761380636077473
34108568303239619035840165407408113538
554379447188838876192297043225371045
4907430531221781356999759858106515603946
45054894828379818718513955539493600164
90959377962162149924222529772466277710
476615703164413747614387561837292162923
588578114068836488102531203299149703513
9547201392569821702445207253679611197035
75767474112394153385674138313671037824
1878766891808976687294583443678509688
777650088824590590736839738572694971055
5973329863961242439424239373426180784289
8033836888862260438604539797984816895
980073193700770936238297827942410572650
9576396145494133673880411033624139239909
196936723715905949322253854643121913587
730470136161216534881618759658255866632
298246104534132583197430522837518939730
289956209135231951938266896907087026645
8279730180741088284740898882562834865153
967113950969372331739367752712320898108
5192938754475741945150245879262595954924
7849327742074684994451765850273035251826
9863927892305336378221456696157135866050
5470769353713588935783814630582434765981
932563002129872570587872253210156743
83057444867986649828775570868071212637
520396266420832274286646572441136021790
761854924690868173318832956289288129867
829161526849216257813201511973868807808
4084313154332782523084887966204916582086
68250605442885894219626662141110715093
94015516212464324857528306512361075178
6156745454743086161164706133860185990
6962608247821882241832838467488437913
8065629168368524801051464140904463519565
4611451865414220807327281310658358544728
5222580948712299639754416875837195316927
456691229585691931053629748972740321512
3488732746172039355651886463777829811270
2831860438572764668595382552218786796927
940752939274471793429634528976859064048
15439562188910837686577415434845514266
129245035706537696825276701142428129051
492760088076178422043156595844850494707
7721344935070082216764414771016889894
6906527592477508562079591016973568946275
62898127819248636428662275615947612490
463624116714492631772571992466769776264
350976399870723381061697396843111479747
25314946649733156261047973838042538657
905495999166157584567298982541513645432
4540272712831022776933414408624657915424
59758931840701964308948723740012247295
5947800421129177619769304799321395659766
4366694319731028446876765855331011296256
7568240381554878448926142843802641648578
959850605949921069135592146968618799899
29648872932241826789511017379691277340
564828685441462385049294264511976249782
7546569588902740400392774698401637174456
1309562919785433244418765501209626701415
507742862087721709479015901624135707509
31696171332576476020460467755526161544
747542723620871549207581576340512661555
285632529821334157536552717818637397600
75552142157099706794338199668373474483
173957652600107022986134812919545134123
4218865952265750624085824282197272975141
27794706442692493816136065393239151403
896356903190672985829831928375077096472
1893901522168484582184707384903724789243
8549384956324085363976462431572644704156
691396161251785816436420577647683155
247226367967673360395199994897486821230
418483381908997856760549506217418604774
8958941549752213680366935469356544816537
124262405398260476929653156840628213553
1657640857011621221887212681921221471883
437297195220926399950236155673385363224
47544818234566091446898364794151098833
603792043777236543623271698018911815748
761891166379176609782269641944051478371
35371503518123859239796091346452655441
28698758333660394130894266605558361099
53324263795665556424728959986731908543
102916415317591299056226518996696582087
339227257844744203366995924675299387034
4578987545548855689318402245366814279120
362188521179120830595720464817601413006
35711894914162799002493245202640369647
5384153553676669677167096893303775113535
9644490631467801549065547796863414992495
692490576728451055797660753065572836
74114766662291245795589899217834057616
25623528201339874300799822923951428991
7417942793215631966697061827972144888927
2806187454497885325529946817373458194
47492957332818971372078557179145042330
90158635389275333689370483475899528187
55759781877059671888117049959593002397
27718267379592921092485565431385737330
6769219221559503473292531674613540992758
3358127915247297280633919743209964514
390133644637944657676354315427826036298
7697987499489880227665408930652472405461
102567885709583282627540752876643391
6290505346524335944487764388443408496
2458641634025609796257685149392284507249
996226001767399439075265818459240892048
867499216678492859156946546421630168237
15491385778748238674721147939668794102
200318093374774877457253437998516153681
1761205297699295258073379825743962227279
163465547699722973391308441228135410
30584630635126115457681835822853525151
267757351268209581005059271790059355828
87448922499383375388734090958566885132
91903604675542931997741590293676050901
3203618091714441344219246192878135951317
2497664366748803960313223817580174325622
27809616464974134011382883492226008112
62502455992751277664935990572745762746
619335265319168824515446578998640173705
485826141725357386705883321544887075655
53831984268239512133023691367775824073
7207384797325439712732154780228682521927
35453314774545650871698011135836855427
74669942782865815945864106591018529386
768137668837450412448238801126412088620
7943298732981014218846161328981761769186
91843507591170632736305549939169467480
917448285238938114119163653735329227648
8814568998781924301253592767120516818067
99967215482677447513673781963634279477
588518633964311228836851517517156129612
214834736648205321003945564396146838532
2035647240829442593714542476699825893296
7643434032058752606712707646810996491282
7468847399722923653176045436802092582732
375956552789436306119373805396291436
829168499658547765720393552445915377
1259805943505681589587742363441647334692
8315618738320341478991246255869763610
2084997446535786929638175948233295524753
814260167608953220004991923671459319621
7034863787247744151853734684681546092149
6611913847855871333322792882297038562293
6977203453254019664635370457604544812
672052051035808895975506951237547211748
5050259532875179734877319191367826959053
5292427534455305635848466158136418166274
751574154052457257212048732786517009650
9940296466532140185472847091689480942705
9116285959223399871285996691152499042536
1182937925122321964047682958391517897930
8556541729943790979098739767531638927983
469747515645578472516369205431085275114
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]