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
8159645982745465802773318317658849958456
673455007700861707398730364114228376
512132435993631425436341164475853483707
602673963636626467862313177850387771568
86663057107704779667834911923952979663
83588752906168304842247542803791621621
688364532961966056842061718657125317683
449272416585463205645157129305527824637
990570679228197420079346307287597636512
4552713257808112169748609490973282543039
3832146645437285214311968773610833657
413124908928105866742935602739539983727
7795230675173612423777983569191628323117
549056593121780168899306064528464471243
533126302794158810425683710296342197536
4979853534358282950514298259127532011119
131246056979441721518532411525740331937
50659990589846686753884648918434731072
68762964504869805881207165431284487738
1268833768632331543952293509118897842673
475088310414273531751051991946274784447
125119216670101195696567150655839767545
3220741897505309939692371803792862705658
876127461154647163562429583770708828444
7324503385103954451848683733499740179244
6836944257778441889461842955974711319065
6199906025448887266546815104209781575156
9078126416803307905169652293367541759326
6769749271307523911417225061467254182735
6559265850849969473966324752145198522
328577397452559235645267874796595139327
320167115216243176045034818809263584355
9643983192286756496326695779295228837837
55038808075794321366867519253605826629
1538659955233028502069595594232458799948
526646926425301463755067142271226385144
61176950005757851294964322512059731383
1297283573941769961150774114722523315331
996197393431525934422088122347381921336
3907569477756955825612323591920987717237
3406764571557811606894216237597775798
6911934672419066397278494212468947278
548780206028322252022354014307094534892
77275935335796317832267862542785685953
818446274879160943766921102339486494080
748493771408359479454254439738473847013
439468726069324477063436388946428506277
3426106150887903355368489789142723311
1061967070324481604320412587162691006324
548860133947800536449815810578148069555
2099773215914389337775342486157695237433
3666513395987025228013769653485557642
404235642473672846081824418171268788157
879613228363708731369199921314513357124
151446228948489691766989416168780856666
176619068091490346271453354420957028756
8967889699489728745583194501367850072414
335655313544604132875309946382884143777
156320666228783641171717668870189904052
3186749817399740688181796446546491747054
240651174955802061017879858961595959820
96175249150938551398861124119261058
8581853029688617119223084036186535804763
251975025211141352998932437900493627667
629626457521488963348292172370923119988
257351759232023748242376403359059851832
887040704606144435778352066851257901445
125472956422754067827120233952834872489
795868858540417842964703255661127005258
2985372234257351300669117859435273373156
9092116881951902874269698477861512664
251946334557186882325562278779694968718
5830656528594428550498436262166268663
541720165382881665381460981793941734882
6588945618493235880369753020163554594455
100778167261992310409301526560817047573
51554552234114893811997940644966108956
775746221682096449712259598581685021306
38289836277864221047187066594263696249
54986746416365386024037562815514445309
384779941872748575813886181796233124811
3884228195842374855902068161418327581
75783822336406477362569833739324227442
237330956645393616571434555835485580997
5912474629993760236947778059805948091116
912844632504267989979018989679079696931
4635648348907404148574889235199082724116
77021784506480922605410832399614844140
4351787199981771956442645048782787588705
9013200544942626028556445797783541160
414922741789315538333355273251418965670
8642210577578883871275328655381579949914
6112016651450033040666949201093836160
7382319074002073476920693619767878571703
4880876886198231211251688566771637719164
2885314396873427570328106357401689179
7780821257153187994021343080305951645783
3682116620626796157549061856623127006323
47937235680294397815195917216760102521
3914984547165231808569157622211182159113
346038076190423382381092112745283595118
2556359391674089917322271966413774116202
217332246580309212596143064423414145760
4041582493345011598092863837791215255083
8345249154931651695597017402892897953018
353899869106349455049235308743813024166
82865633202476415606537066341854477577
34191496487438496985342699197011571946
978089725652304996396941219528191396203
2081839329628033112272432573178759607081
13263247880370690125281545159623711615
152774437687513311181966156225350413766
5238587868292551632362567278898980461937
2846549088405208688697902085872509823
6367222573886397759281065362918429954112
6064967672909153240772787588494031465965
57095626673684871227443474720429073559
3686948563200260798701167200161163129
6524282986047900154325631441415565666154
378452923201733596481158517690874359543
19089879363661557277884956071875422255
54784663614016782150175759732287008071
500761061491898480326278745434973336924
20311532029343602462277425502887991312
779676704495579069559573576286915727731
755781569910233996982775984695924897161
648072003218917531373483443512774152626
8160341127923054116222544242138768042372
939675087954178639979515586577683954
789936109558860667986199382312315292789
4704789762824094651731005123258784192133
973024586927466310353377905697343329585
4464254487378359216393566522764807992
2097352386485075182346671018226261759
59696196648069167715586535669056129741
864753623415473189493169377522093615936
342724574314811553543987152884634929
30793053379173759023029808384037077204
410726757651661706581134098106413697614
98716388776756245000269454778456711993
8951487279908852347665474101379226547672
1375462795851367851675714369533272514840
142817596304951867073822312225873864983
1785546585388235393519516838585899512579
5556711646204819255942254930194729804186
340398485492012912888519423859580053727
5061880142236913987431631133203810015984
4458537124777502726161755448280870485565
16623972266299469137389322655642252655
406942982016721319580846375160551502503
17495358442825652384404721702260992128
5635148649915367678026169054919339797849
3297151046473607582954903787558289055779
507120799073179938359850679095869423608
7199843054561861539120763693300233811873
7555250216174095805302645710891662229
4165143213833050347380123656164837901853
40147097347479289833725558670229388407
3291655923662256723793851066331899186073
956421085170536047259423539654866978993
851811582620587267927045607970398345530
718028268506769727068971532796411692588
448967537512766591096310622135526133188
91181802677467654649260673977597917396
8706299334779812843628771559168631472159
6848348058069892487040572948938742721390
4482534495959943611328827254291577732846
5717318796142468651069488927164791489609
188419235705162139453332261394126078041
4616885733005514576382316298954966414
4270377119657550986063697654875674890
8495343618063120220814184328679744323256
3854107734104387789354186456436490917406
428263935926844751579468838209555799425
1536944667856929834923116790326195254159
8142318198775819784233475677438745321246
56945813737588968998962711963026219559
586668766078909763278617157683366258226
795520845948798324474661359804743008296
2328743328873935484492705048645175805727
677086736848249183005425370866464813
6331711736134205182973440419724625663
826549882339215939410528769672368614876
422834493164314691521819826331076491515
95222996820729134793382550244396392671
748867335910999465662014115175836240451
798398218524273081092628526961529544326
7071884827394252597015563816255176652179
8722562683495482859505106651374443667
3059880589749724910171348340778409474
3805296918163896229039336215331381705712
33491831625175660376097349640111433099
9535143820773130867090751914218616306164
22197673746752361504807109995780323887
59452296897960165747272342695492454442
909782159262718112633108988620026292358
557955754755395012264475111356955116
921471419245030381464877754467898399208
1680610978893942941072179053192453699
88273339402855356908026261752209906841
22157279441522993791461291261268554655
5561748592706020281025545151716894487865
1694567898583543541355796299605047362186
629188290407869812421465331109646205786
867535863626259319974109890988813233880
9664438360814947487787946859482895015882
3320519916747081872839005182443724674490
7551260575327678029979973314397079960
9293633770858264359854621933783985268864
1002739190978424500378813966645357457
274815895413158348060069307203233106932
3463529920156899544759367600113457043066
272622777293394592799227078230887821804
8082699543229751380690738227318828595223
735012874626109624729877541064737864
4242822684621969363436769385545924742434
87045833954754764432770740093548292993
720087328220763921955577930275731032900
587925397785252050363188482142194918124
8700321051465497160720168359425922684683
7552353738057758806075661107627889822436
365500746522363639778224623426177006492
774147575023759790448833547546096807756
55907342757259642488855461389089727104
28472791730386832474715435736864172806
8643546159764016135464664680552666714077
518355342157895751395405536500097798115
306872277817898869682996282640383115383
68841011742903188667675957023514161433
1717199410122638154871225234864461002672
679952581033703975760034507598013664091
802797182566607550325065461809016596011
61842161575997373928920142309660824758
733583892123471493493274462846991166835
79473169494173658319792490337658546204
7522485592146483117268998192247244979584
8866207193666821759049011977446044645358
138825080964341881586786192286536592105
488446980467166363331850915748221179
390255025703927752276586416891678803048
740574074816312719337882444332384117323
559899294711705217841445586627424468
9227289660613364429910246209370396438682
76494822578369291191383106217826868
712600281366626411163815201640645933034
72306093741333843582479662347541806793
9912736080964152962895477432743851576633
6951765961801171382257088895263693874161
48412609982848833964895125261269839667
9975467273081009368458545943241757658293
99454141456885880012737294450875649317
48025771315193249745853218643012596284
94517956228905347984248804426043154122
4968771699482068648421141641320053037848
776872419501869533268029943778088675835
1385116620425669621075702162853068498132
28749205959399929053310969410808826265
93288894286266273736654241333263885939
47464277124815405575373719793075442455
650491329645118857832259837411114244163
6018232862387893676265153304120679961844
911253347933779974174006411612612340567
2120568786274185846683293588924529879557
90477949667053482408551707738551795520
772564946155423171762033918633599247608
35216699659887212075571199489136459057
5452741496097269985061216530166223355203
693263467017801152685038119455396325285
1087532417638932347884103171979887101467
3494996526811015416366220812530857464
44062694981546915247766856725597919459
729076531164581269153789621395148495
28838822274627661854293128052323214396
61461969691383786512480557136461603689
507662698461309937796915011905884117391
333016874351085281665138768170895596676
716761673295242382851080123646587328827
5166105562005384290924926796998010815113
24659213227430373662298729578368913819
8678647550385415654438398496890871874
639253383290215955474993859337132186515
2156831819433578349846805490764268217267
3250924098585045569317577983149055954738
3053584347223842597429539412615748663523
1491464352086811250868601926663185747299
47570579573369861287411392476951916433
36005152215620013615651906339754537463
7565570450502956109580684651847995223
1221524477485069829864598006725436222578
600384706392280718948376674217937403758
617454556342312743504382782074691375490
1172280663755958299598314872202670791783
171674817276540350866069656355695221762
4453413087915628598368748575353718636664
8161334320994338864635219466853763822663
942197736084318440162922545792183827975
8393937072459052783920143794726011116083
844920343815062665283401755420989904637
745892135879950088659942086340315689578
686911875651860234233429493325893796537
7601216237892192904483982322950079251654
4597580235586102567277347242730656876384
450627841278512557671871367258671381488
923013003293802169296718054266056717863
425844692955642654914227102443495575827
890240714752388389053982489041438191976
6448859465822365087161927124482543349
41216918965465558828133977727372729170
570092615403605565546363958131747944749
171716254678771799416179489248826195532
8543451331642148600470265719289448852490
9849181347662243888489669901912049605715
69341137711377108155382377446743148127
9915881310388288968763817630788744513
20474855535208428532472739574055419540
611710406525866412076440342557722928812
291464981666328115988978972731933799418
12148608816914551453623360182760307739
970743043164531022796570681513557944835
6166252121476122130789975878493639414689
77118012161513381247417428610082398917
309933672879636212947218279841384021656
259465063859655107976882690270190894335
354229782189668475355569017438996775822
7966665775735055123339647798227334728725
977728505599878660934871034222236661844
8130309751551882873873489379957158791335
60269740703034424719835749495509539425
269958135631234030543283926340629505585
19653952971754345218660878172025701554
47268993838178971554068204093748909188
560051478863139255089689254779748658842
612239243017800122547407326498044898769
47831510162333973168334794653986017031
9664490183506913384311557554989860321030
26582405450022499165144918543479221715
586487877156930462924082243531334384767
6113337939646190639811637474242790573960
5262567144699996353989492378781597881133
8822841662037959197657827644131131879034
1447356465089275330982149653526763160
4841330964595239114627967197838394787680
69182475530082505706805536672217858992
9689818491871462258966272701943912301313
77075786966840129099727313317551709397
140545045872558908595437349635075363288
4981675370234085449339404641145824684183
5761552174784237182325555771548791595471
43177652516664597531642747260616809227
7865941077584871295589581523747045051735
736315565778949522871851899554850385304
894300233162219280391246158620458594585
798263489191464371338216440626863463844
72354459159634329369631470823064175456
1286417082645692852286474123923091128482
30872311152165293286788641271633315551
2095562625416373234942846250269856632
30937144860873313344308170871453393854
6910970446396327309637574992393648432554
6300464668957969258436719477617869188538
42939756794199069229423319910260746917
206634191296802257549912531058459712169
52389425480207782248873713057847694184
790248405217203697627010124329822731332
8304262230784879147720631880898689188
1264492134923439303979443597422777906720
72377587572865143494714607085581607967
26131032844794437183414041697527538233
942409664226166169386925401842420829587
924291495046457767551074707740194756564
778767529436663367884237617690077971704
137284693078256944451154357811439281697
6749493429593476696949623584277274297259
672619913630948191482504327258413531953
22443175537645649708928640639651277888
41972911759950402454721199852526688336
409849653647126146537028816716889614122
236328013912099413193846852374792004644
114641361403599701946193727521439993
7666161725024991231875054857469784471664
191824767383234841162334629527592031230
8280935832712364725238302460237751772897
9079156481704557993837693059148725794293
9752539159065391298466532094318948455819
448311515248725168289356644745247391759
380763034253471901232378518855650024219
22428821984823805917042604166210691244
72712709215421707954208282296013965475
8892902193567491839982046974508655173499
2289596085146772392634916511435596115660
998786833768272476514345908168611804393
26832652281017688991024050700571562414
1654937682832608240856968397953194244136
2599669688060012214665087982543824525
23817447306838145861152505607694445909
5164677378624429821132757449214899868257
202858631526372273756847607601048517209
156919719304889821374257673674575734876
5523984741986705345446863289896392616
881975674619300317415155481759014135878
5090672812629395233724447304713385383690
861186699673461052757227946250032571336
2948925227298493063013230986456234696
34987205217350623272829839768818327499
9516825571082959077878230238072823428
304361795885222753827745981438357332112
845098267038479586299483623684357981178
63609898829806193553976633522950911707
3595848239970455518189784416187984982
820952153445138737773518499768160651431
774734282939719430237379782678531573436
3433977668172182861922053102649777738499
571716173691743559936174818816683898794
675272187458161647793595994221189029108
8484582075421438683170331037613683298964
3921677220548134507432676799799652883811
5838949683267399341113499701159306625
376859356841356286038621706890461463356
77419586234675275593432402571686439830
313822654088827875393719819151616175713
9411567850817105654791155088526676451386
901616213664158017973384901586054256933
4887626028899156753180638200596352867745
500153153320845818314393781374067489540
8118932906410794808280526153428555672
232515583912830092247548089717238222752
9801101081094318968617146628551356703656
86446229128161241758693169442313477809
32037717462144639569678583966782821336
8900580743214075196271645204315493306554
6608516523097931509693958415196294817223
1775966379181444328071789292901686877989
130741752571794347988692757502191312421
6076537831766305967491994599991915914742
8987791762797126576449377670857390153194
6943393642912208616959103406542896689182
19395763856526301670893392439108334324
730231402166796111973313810591015442141
8475946425735509719436665933166683691051
64808926786029448149209794258376801803
485159812732804567241996408790621912
3935453371612102873892698706199617050
8071867019761357209878768518480459407602
40614295258565549043402861769873618869
134866419050740555858547380352641283605
42456946508773478687617053206237833200
87076176888136566393566059860924444826
67204412109040386137870930947455778289
1363387071411807108942589885450216708
147641826303647157427590171852553164657
663031752047347656472419331643225191719
1136541831424269003358632746797759589
6414782594441826306149603380156993155266
906348752560198596480271667376949166238
3762457921187138643982568565987760116461
776937351779843628670745111985889767031
828073195855276163725377962157620508546
7006124251784349661805201356629627427
440895635624180601209673833283589948
528220823282547443299287409969629941491
56979240369587136969379840220387802217
37753723686239854007188486958061683318
377188318766853204913178491893513029525
16668496571878594958561399544772643924
856412949690190637533274545277926442066
69647035134179570671650840968121817861
6112243636928583429295833338811285095463
5918655399471485439410908777266451284122
9842705928474960973956974512828719951375
776914452797167513736496108921755079567
6419150172536295296155857668822566477621
206212496281968818139041675284251854050
3746465739859120421258038567538664992578
1832650136625829737525979179291859768575
8860555511677504534082815505643275406841
144750204926223802375340124664681804
1399794755961394728926512092238177659758
883789074929868182079244950830574728773
151443022111433987434056823836044196214
9364730344071333943625033163399202660
550010555249477583299413473864561996885
7782498245153201889135139777601795911044
3680740418353201124447283156525041193924
29495226769814543837523600532171131850
6898103288271262666454707316752164289
2562388156186849235931379875288857366440
7724765244248246748091081562777677383955
9154387427501541181916057169965757849796
924976888843402542170777057264368101061
392160628517200858557321018319124954705
9459389799055800798254365185948696847912
9022972189442659459482914846592595902702
5208482730158759547968481090336927066104
58928080887902426348920273721599941063
265391795569628110816578231678660406966
15478639351165837012183579867171926393
67535757563880061353667803274636198681
148255394960542844786716497636362355688
5876331764705690181944298796259787296698
90896925828814333822018889249944154087
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]