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
315324271715621892421248805958842587322
73229601424687940324448413656173785353
715576992466964357798616637456763008824
4084339480896167531158509320649931698782
287611057455615378679463438256283778517
870435701851821520812657036928721437585
7847709980886057135266404530949717059404
12648844779266053690245811666979312864
192478497786410399595478870581095359494
3975839496442400872112827853846321417770
17889276766981439939026509111606486772
4109251714799833574436837193445838074729
1885168774884998350413074375242531615353
1275248735619356438628802514725871513521
7396618168276140181891217059395080573
1594885250376656946240705943177614021471
853799891344638533122805237952664932976
5244337454143561331440929807645523561978
7552837771745371735099648247137742189582
853250141211359812475031437855020842758
66677843269257836074933972282731778312
900826572622978414816883380300891958955
5824622175458867164517432593878840634
8605154711257607600453001156573252214582
7272751413043697525080042113994968457784
28514322964510884012780383393126142375
644051107776629565499921559842164972127
66184131675436354651277198052279668977
65724148163757579116170521231217891713
80886840132022783682279637140282548230
433713355462933715444587332892175183714
29697361248501638412348954711168691335
5884966674476647549190424789652672294459
325053278637834517006276410733837394713
5049479468757979161046317974959957822089
1320340061597316223229854889972216623658
45561272747888612789945725117832855530
239029359571396380693951012460174793036
672956631286170036638851296305350455696
684385149687087317276125560646143566186
6284173272115882754447191731329941641013
495379722940161776177039249358421083547
849347122143622517573823465027904811295
289764163202359447926972947173345814887
968376079759284159685238840675735814789
524427278977083901844008793487546317692
80436196437057404506485115772261247037
7571242882982697703627064491735367548242
7633549223353942543451388914701281696190
3329307853941679991381366757772855028015
6512513144696583787337274429182282315619
44709456638288292872180987188779237358
59363858549661258831631442226256291609
6501455661692537601692187782223494364
6873230041004915282964332675750499568092
811487799961344821041491877407852725545
3360300080887865516737498532636869821087
406134961067576364897057774386699422960
514117219616652263850649687107862951711
7545891883106098451268887298756533133717
8545609379443537973999198657342166867396
7869102843444904249636365041558214894125
4427845912301337501325289021373185086826
8222522175845508445793595773326617721180
6854561572745031310832824975733916816705
1637522878041052200787559407820926933
8108608028246013680169431892225237574
213955114296944754346005047431349476568
955267809396398841461422185158917321701
62776097454487751286394665615590183322
6718893340401033263655076984259981245170
741141022821543160497355999389949783553
89101079111747177678242605774972352506
723486005766340891828682611672188498375
2610162190881414803999741197787938152045
41611319435462257118678193476818317953
5391435388865175350686315552258928883344
7732748777398282347643815922669684336664
191561788274421529378639588544453067545
3173509658401538448452355864436278937455
6850314316947519434748066570492370137663
9479247269308526303679346753295129816536
7113260017907532705139602650908546741314
2381315887217409951987468026333274989153
98297162958568613109497950783875898019
9637887047451101740820092779977247601725
949164812805575755019431135283765927205
684054143011848137226325283352926878242
21511596218679402930633370929035479398
1886514289162907753075655082392135395527
528547598993280045827022723675527189190
8173786682658868480023315636761947892853
837969552415531985499386572249796047973
17449810585870831011466499737901485514
9082460268351203294619717720504587921884
98665212920564395813932236290120142268
71307464350963582586928849632522923649
292244732669490491023622829310991976787
766325939612964541920054350561199737884
43674270285637379805511867989145620593
529711953665704431585351289190526016507
909247527302160255407571533647832523437
79651912171487515632491633062736518520
7763682992045538215877229000954279027339
1027314885131883998315720977880835694
243170695964969561608896608214896372709
7452885725856107311426017857383110872870
1387179198139623680427485710949436743093
917519091102878969254758926947422377162
3552760847407050699975977359919425603154
582448409173972026111963295793370296189
9567643242395211944640823397503942266983
805881352343242552739046849980533248535
6338243012576954510349684392317740823872
987727758522360495734482635713976776404
21004493847363318724065268447377687817
94529945492414176792425544092813927716
561717198214378997101958301966536614289
476599767683848776147367716728485557
3645696860933874127388871077390075905544
268888739774474769855142526374835189095
933324348455236071731277505044676929276
619148327833247158843335294456898013796
19279385555472104167417832583353237630
315649254192669952619778458466265165551
694882694914720195501045953329129703424
573114516602663256260941824273015475
82095803639533862487635572195426865190
5253802246165366479545144295572582051
431238263479632778031038323984496908981
171634507362917843813178453208696045398
618514122998203658888546519847217235893
88741366048089840244628523729815279031
2059855291888444838538984944188691892738
4867908135269877118046725201894643855286
13823268356373684538609182969296641449
58636923303485423042402478306311665440
2158371783756306969029733121788580417572
21479086099058467528896155350320582130
8908175196595230856711021871303060535895
206733037928545066011216148432147724777
53251746792673178599691910421042478151
809392411979425068971088238898118805299
8385195397164204489366569484478963536582
7893141890786568560230865778139736281242
1158903745053985604098374848820469232709
3198776878807081215824295814898532873900
179541057698247812747788364791662974289
873385062292323526641918672098384885691
73692367875235588558076936052625512787
8209184235753534482550184518726224072509
2135612734836365169671643367532196912942
804742092712617382045404617841436859290
421819086603768281689121037281425431822
253862258599895298960659981506521031945
31641927445951888329856650793393436
424142381669230525308460687391203557901
7569835483724839601919493338258272847052
566428515323375766273111782474251063092
3026452432485225560954289835365478286205
4356245740993004264277202934198157561487
415553363497390774636647902346876104
430890249859969841048175106447516514274
288986198574768788976345396855315485282
504936597528665245605012101486681435529
3005200538287036478867123698897277098
3960234729019501668528398239915985378512
59881150562829564080201323843458263708
447416687086719382784937766267657363199
51337206990144614219484213774174479529
4710482955079508189570779570388542856867
6641630588343195833720505443463957717282
3575783128146716810663939116170788844
9482725088694770437365634799203840395060
1744941377157056454933222473996065915417
58044654910222739343534475835394241368
488029054573971138988828495513544565732
1063353750213266640690962246896603413
53439668197913784583095406599567098364
67624877765847122514355481788668377332
6155933563478352206168696688484511803851
628876449142348896801028422430446013497
4269612222353904433818581558591688144073
598619766968186843241170661228374411564
3154594797195649763670576270353128474388
49081201660949361261518635648409054581
71551157609154288087919904661326657176
4961337584508305198610995326827976926502
351894025360254059605825238664091638482
68359355086429920558433878433642991677
114064004806829378036656862243124758243
3342327041085241477598586768902935671032
9009840883893191554036279265441531743446
137197375869798159886021229122484367402
18526224787431712275373112225446378795
34655115373670511674022139777654522849
29699793420362068893323927075114138767
218373759852786189387341573367166607791
3108384489251289872172121961917454185340
5491962277335951968235066878584174079669
147999916203703686267999050645910913757
825281744158501615353368956369826742503
1274441047691826942563601302533532589359
27555288416681026268238764596193775037
20554332546180313705936885752171893848
35815079238832778432772918196620036202
7714816397538937506810088559760916006034
5704673490325657161158543669534981574347
785140987337205948783756135967104051564
8684846747477374963754711539795308968
20829439729757891384746025191632845073
9279824055435577231422927797432138524581
862130439082949938406420643876815107301
68134482201544170163487967995093217905
7444445070067627179059373868674659304232
6263654132368418420278777420850135993893
221135514103282557987875648310881649593
4408557188931701101062492312483769479
1873848891749874849329006317727761304965
70823457549695561724619665548921693253
613350001159629822546368172931184734344
6301620978222078579863451312647859821067
39783401990630998779904619959232403271
8932929038558997912820082295875260516592
590659816832914621169188594322957793
3503977050437268890461494571535737101586
16608505323914974422612591350874063723
40813596892430105287196954737551707754
773353434593547089616434905252962682958
1105608972057167962837698880412139467244
420312452698706664364007057897034665105
4336534011875700358234809262846177496244
8897418531534028721943849972535424587841
1002915495171283898750943363105913588387
8817794523548385450064287181116033718923
169240827547713910591330396371474371
215377941603925850968255304466091887377
2283390819243962974441005480960519462456
4585220143566375703038902777143187081684
5259248071917698134269509715692861132
187811099122126480028843679354750574784
1933147559864801967661383352634898659
558252583841121927716069089483338557321
4432658632033645861231179030682444579813
66521660359891018323743340736160035264
378325152153498502758853215800241987109
5329852054446756100253721801731148941
8818562588986270642787916860114237983921
382122835051784682026094187422434575155
509264601653418318895756505796526218670
9112945480363174465145881019575958076466
806272852910542654004040291790191558459
2183345484029815705332257222194568499
699253578005695940352797451190303465690
702026952216761888124333864144322609196
994371616026194194628795328840341235315
645174689112263059993595324239086489068
8155727933785977375673233794158558848780
5890786750125656888982306226941862130
41876485551542760770611019908224048728
6882709421502574486466908041900329226047
166314486687756059312295387549865348953
184632191472584686747588342960810491924
505560682801834217392829423254496091595
3087351161798013701610449274216633323325
56068068901732232605711146685219524346
76922051966689954076352224396405580
7617410318254997684698567859523790106930
775463618953110562665321164219441656569
18223421352410627069596719848463309406
2515553253147237272817407992556925218360
918249893222456882147687911442225077099
19784976953869887963979503857523317318
412463977489635279641759220685923409278
4406347038796823916882067241869140443198
6201878049682221170480151823122646902497
8252201097003922833196406247957183517316
4935460479196803257790421719241170604284
842281978266850941943486745156043434027
289787135181958792101779988691812807256
79095680418399344243503902960054184319
492092695164298640449236583468636563377
2377796719293452316938656705594525755833
307674393033910782990826069808622777
854456943448963511053384186053723246370
622094602826708865828434326436961755052
8380968963658165165361435460819631128
2665827919625423525722232801416282712339
228284954358969815145344795743348782022
67156278739706784412390459188092793776
184118342272876191115487539987153192942
55358474547720474021964502340315745668
1244877442905843515312361710746429778404
503873447396916159752665561915043824770
83016031513854156415888998134072431424
421581049803554984311453749496349407684
77167225851543182778546470462252287752
11238772910904113175316957597792954499
440675581100588456798164540058946228769
606358374757241508174307275595056893533
4803549510940299629699945364719305467
883747235411199796775527447617261900
81004339147666043289222629959396836182
923983502845960978937883587346324779327
800866818739655668014848738228996424
3993505666436812827177057492344191107097
30208072462093144296359325708983614285
66119813688157568592526135109152149653
5305258295968675325341066009961819619349
508272193815728883951587287221656987330
485038338067293813463798886796167049289
3149214999212315815599115414900473464230
8337287746501136473799121650977181838768
4286899155706897821848569242419379707576
130308333796365329595935275226292247643
507388532667624736151130702731309398076
856686247822991143923595305893662803086
9806219320781389852997234188125562274896
470266527344845836099822129617532974236
69527877255465725705543893409056654959
6253202951952224212458126525795189842820
72696320135126271024242138325359210309
3023121351453632948512955398530654018978
7750914367541850324938873444929736705188
8502309865401214483067625842755758566
511186271396238953431041927791956979314
7291725267279364375266897668119827852302
2136358956585245559457242376842278443655
5475443381275501906398637717327465046161
9079830513033748297412993742786486886121
2871913453652445834271168711948029954047
8420862876112755427931872120371430117439
85236068208988991365504135278159257912
836614704286789347151060803737779252796
8188588165681581705784716448859941230
44508755299640473925466457718772562247
2953748222483585187018859009817384883200
843027258117580562976307730059955019708
31021752457837075628471811143445606828
247127844659610706394685956235423832153
89774519865123668688479145188281739360
778269343577206663340756741770664129570
545182677437727833021427597734794753
718934788189155316192859730935291513782
6332175710000185222725755179564847939523
5178140583271968686457116624969139429623
6312438417921396440546975195906353588265
610144581855749364144591963869981419025
53135759863928708114171590349513733
44458305991596496732336826727148626875
674529963662477412145729666930152431346
66932772360506630786884850352859959055
355090554189883268870693189899150949
5927543256066584605483756535690290308340
302846063894744541491636683535879613797
53442444597724999873623372925897826372
4529864926204548935539461153677793855886
6599609859203110770016834492549055022366
522339605484996061174171160131062828181
2028778377831488839098083461446558265627
6219259677963212426269704886864264704476
542945619922736185119699457342646793528
927189147610696425657091999187789985253
403183911315557733864538298693911498
795579316390190318399762844874765153352
9849578784057396442531469387908193
689257164664477395447525433788260891656
8563842652123050792459396570956442282637
7078372246607840159141354566600917827000
916021344960311867851472924233617995087
904593964126091962448929190346940429497
80975812950914860228863691155130785427
3366626484397129049017442245193827730
757863116507607168541783582644280509750
4998474215244857683862332623834844547
90485899931022331790605042034908771121
1382140917892451564069979075160019902433
35886773326217586677716991236698418383
753283219615394334977385214649466413971
4344420961307060197763369484682440163405
51376439495692152185392871284012299355
8844905940795759755156633857132158080
5027228827762046278465991666107610774840
3321213674149896945395933489122875095528
4104743918352215198792182851717129176306
633845169697271888064373174248628272361
21058723703652638150660125734212521916
873012463940473337801896237735869971687
61009163383556571311238971031121252642
204991296036766720260181485490853297071
39412095502331195321781691548384833330
63151141270398725998413617821222758437
350136319284360776531950400433179761362
75571840927122404924184574151723072607
186295696234693072632138343549273205680
733120738370476640324232535727452837852
610430028056293194306671336825971192342
2936896053379120280482387728823015
12351805557358084520813077263624746049
8553916671208825135813086056413317914443
11461499628078647071758059250280458626
3249370427247698764697694954982579395158
7154610673993778467856647030154598822106
12464198667490559393635513329934929078
2900190948289387378119515629598916958119
3925599446503339874597572964594759539539
967536001186287931962893893197739149493
613352671745903643412615619271896597990
54454537310880615955541826616290836160
84326034847688222078629839337924297097
93169638808547730594789977295472203343
760247288137118840340611441493331599792
3969936069614283210316257963872679027628
434958616535489935265510414350368695984
3774723725874740492668297960150738257844
151145732771130985117455697216663362898
8083237910404325680753741727968941664240
74205272492968777861708024813282848079
28106105580231815452589634797791425823
70185609572787528403033428856013658897
787329951651428744049496110324871184153
3550872149612472363133681198211589235899
31697311463652718773471787436606058633
1903709184023913494064137660553669406672
181957935955585365802634425747112455282
161919241454144115959445022412579513366
4523532022178342320616426237387855224688
206797877294113436161626712919983548622
76194215677353215570391780722289118141
3451543261748949363871468188437469012596
808098721553706886339248976375364974609
6466449820581100501053818243161470281649
91145368923429298869391897831475987103
1870757095497773166851825961397587512653
3607309651951706398566519603991316755839
4076210679861468744535752961321254310000
63134500122475975888878830878430404782
4824758852787416370327125958805475597
933247177063208446199224115413006924031
857785179353858931889613838196156144415
591622926492656127566192057535446722030
304518422022575213583213418346135472
394095053417608345178443832597625368088
2703816677918283811110309050152775125582
797445173741241624729615687592335716263
552927629032933519096936231771880942029
247934013991371751411242439465579034600
3274674117479734614841119916237418707799
6318765221256358238016738989386714144286
4944504897441711219228386295885792902754
8996827866922087963876899604562565914490
5575927941444493905285149314513430941074
497182821624034431630969166349454485371
896425978977579325622916074354123815764
790051881264822905355253995979216523278
864791553125579321043937260462547907688
8417784838738089561736272810625283857730
791356653059295366281289115471645906243
73242526133014637064648706132869345760
435864141180922183767630222931822779
2999742462351652588513134032382833026245
147716494603278211205352641690029301292
165051569679590191061149633531477604534
6579566548897343757239887538606520881432
3057977046667251675094166593353849301717
90086536996961474443393546256833476429
93265127165915799355401427467780366432
704542781041173811522579492736012844104
9540816797324938812549354233784989054754
7395904840842131395926665705235551514756
382330241115933343678941246640919535540
5541298971769704053378857930835884666
1740107690899358366027837809774361694249
5768916690657771982257675026573086371398
183353643183553250726614947717868324370
3304134146158068161370588106101396835028
8673240334395940566053617588179995631958
595989184827755353026735939190121566205
9766405981582409357762888021529337754952
48343061129411567906787724757863438361
2297110569798398412376306706516331619413
8719825914744481396664348724541672399
3570405476666221287991539574341960052360
81516982663891441835846488554635246157
725245638241339323733063339250936803069
2451846160156256498865921827306192772997
7637748871886894277949474279271026842137
654416386869751079487727498723546424
8197551250117027706361262294213168022788
5041373223672631353550038936262592478198
4989580426056525309782451427518363592479
407971462687221462425356746632026454
6661104049339153861736664713849265098371
494754777826221384884295302626034225362
715360461293762515615258804554778944472
4879388156866436980756078924852731128111
394141731403200521976309030848710633450
1625603292151312232632036010536242908061
5333451576198762494661073435346872178585
3858741595209163578717195095656084069418
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]