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
8790454050039171360535758738894426777455
2083949076714491925581613696377898711739
269898557679163941749844520756833122109
887353047875433294469998628171086204439
8221639451993879424325196548460758962655
108229681524257783494558439375353450
11869194596046337841652855622835922778
516047399509194618167208906115348394943
453478264988921781435521455949913065
331154371759574250882967248374059977171
9125480498201550788585013333973963102566
7371660150525577973269102229507888037245
7455844185204880919438417440176475504100
8621887870125933469068502351583927263742
8631737516617360258236727654525444549666
28028230243619928942321680971734065774
485499303052034695593152522742314758047
706181097889310346786955881510441278907
6530188278661157431263698835565106200
1657866567559936322022074796870297224198
8155917787178560169726992921521399406807
328443354221450354769111526147475808210
428583650816595974356194793775551198363
82905861951485192229369504732295447361
1604222152343817337373938531525615605008
54460214555993353077738455798251214814
8135827248670376248424473394154209618
2846756124174290696468713290891769249533
48325927182228755095045740927562749635
3778981062011747409084153362147075245832
27469939014744484077377307983405044
5566143346949020991794765766311197053779
874862537036194171786061792566433457236
2577494696015986166985241038476562606647
7829338252483963581176611516530987876150
7272284452415208119793001538872163614749
910333351535596732701532111679506706690
3157466947815973697287002524874665728658
768533728982773684682257409867419959907
3927406830533469177978292122157075349871
6410903968671755477423001077973748843943
770058839139155278179994361454982608095
4586252395702309477753315350960441832597
93341074960642233059124230951641522721
3065892019842058698045738941881092904474
2548991971112769748553784446252477253034
6855887074195866721564915485782889392721
1624451447776824205838699212221771943717
4640128085572809357462909698720036886587
220351366081341247404573381845341776693
619419687309279982098637361083116001174
762118532084883225224803678322433738707
470312715797824129829550393851964469436
337918917598647564979443268472185288980
27576920398986628461263306166229261516
918311347189827094637575655249491142893
942042797971508361413218428112262599569
788329253426454971511005587144450926240
5825248417183824942216628849556543343
563314924707947151616556252196364349695
1113954365906154494286793222366639893819
794791758718247926967913570949647696879
20133996866020523267399164818663157458
5651996380888260898726971598148371762
460669077066880356144311943141931449517
5647116691346038523356793309949962305139
9019625267866773927853274603733933129305
9151963280176001404590078401948821176440
63950041957499722041785298154299928512
361326695896695853522069316448766056888
1205487063127460589155802811858391941608
6139710914336839655948348790761291124
5192306419846600266387413021267545492103
666240743973798429028278772662225908830
58675967758701832853824594745431929786
101763685153432568765936511613395044280
35828405122914870922011164161028536997
4792814388019937571944768684417531644949
8020608093688828151213836226867269343288
5670641346479007728135592928701525339674
937167805109267982561257135766874106438
203691314144836763856727992952858791928
954923037551426816789962041817556286140
2361891966489201314763004966690890985994
7384858369242523879313395961554466585499
48425936884333125881633225794849268771
4095460796497965826151283339900718381013
408953974034732721239897455952193239346
660623583460156143587923752366704021
5399448158723301811724697071837994101144
3833865185658323600423626131712047298824
5877475773898645779529109051227697932823
6471498050914126123727322056740673907630
14928755686248582560247196940021254783
86682998809616542775433993439418796026
39756209993887846839305332795126402637
667854907925122236741636576469562278174
5987711596168943450556593289937223473754
16267675384330716639463840378340527315
8006450076706610247815053396857213634506
177031393868765520063732571571197654220
479312362584874469141838211419196357884
52827855288701981281192094941894351417
711915261666885229468698506713137011397
489932515996161555173729152528492693744
86074657727861685930439563211124739881
6830567661819608359777111765743115744743
965828816281542197928638571856545604895
2058976243539346806686696365540772547750
856922037794061731299498480589718141887
548158822204501428868580943393774801618
375178623486998247466693268952788478116
3416753789066945375942413919665356545648
1774621156107888129736495728123825371574
8214171885965512179836754042606571099868
784232734820213799719820135772853161210
279147885986156262224565281425948323832
8719917833801701875280994585130850828874
2436289449644420584756211549752090483834
45039803568524732331499363431185809904
956846413733470113940205570485936173289
4505670256465751159379979319485575844273
602328083185531844753447527164490429109
2561352431078618356723075407564867617364
6994943152340540184290810187266413914
13247403702245851498544135928553116820
64111190894761461483307249262807159371
469370628600177713248478348955895813011
687691626618203391909412156435576716731
8548516625624180714249692365512772295917
6290987299902883414326814130250198139958
939068143931492611581559544975192788497
635227058821030422808935352741371713
439924012129611421119283918670079398882
800338983984296424336517786755719582185
57715595892696428037050739995363027799
3038880710823076901754448895347361397530
154092717747146328056678806351594076529
5638148145541333263078477074521385588450
7769569843487935202975055771819422629071
70707392764262272165668486658612255967
5775110095444765957587578066472267522036
9671811376257368679917288818991222863199
3094619473877295596938506007447152878001
2460789177019509447111652860138247624
5928505462274280607221054653758079886037
83569904614360754338228522126412717149
4832593864505806827763461187384963594480
8872334564941800357510881916107970986740
705756913193168210344564471796444724655
3025914962551825576287065147988455354005
2783608259334248994279254936294392631984
6949226134446452911324353794403586267275
284841704494646331567149432582426400
2642114740667117691451387566382251374976
3061147311864502576086513883296588667507
8095277878436999103510151149996977961416
74254555462720664335452955918759086269
259286889043872811259992873892688318182
557043384035631754395905721084885494790
6179537570903291331469251000013212967718
4190119146128000164560291068347346648758
7606493389387803864347171896730643955692
1084244466525639762290121489150617541286
7468959887128346696661372558114784159045
45165693726019368706594183117423617417
841113947970882340841268108253173795128
9316738225333547858713201835966401082
3579378521417827206525278174640669861529
399670402330201367824337574858867914256
175628381912568624499226341181748507683
42398208681219393992452958175720399343
697385422681813771642368198954321425903
83698391604027409958136188175353923977
2574445535704786726376346684267522564481
40877176765726798753612276860821308720
4708400371277666941872621369835365473902
8149679787988432895215315903653941172346
346514354889699169072866537176878981230
5706900716789035965519325567823780475532
700745401325301758822112788307891474933
353420706579759430615757966354466847883
686754129605389784952389196637983637679
206436649413440259925727295752340004661
8874786115975688503724554162361282784
8367776339285525803064112024974799105
4993211913383983613664364449155691163409
3371723566546992926512677055839825579592
401596371373789157929982097970188496444
2774195872293393649147127132939355199076
81017102554530599012465615225209584963
36165725519372063427821691154966793579
9378579830399466168594664979871672869
635996803579322283687324501167845851797
9399946067006348914143538400720583101665
5045689344173593239277325212455544738215
376789696148599262609632128712161722
802518276949277479720596237785925306921
49339687423018943761451922710020694340
205863889340821237871545429464654654
13649707830977093805427176315847489966
383231078050573539663462428834554631862
578086387729601155363497660273498131433
87542583227268387876317675388463583024
3516105784454675804494209519725590493838
3942554632183160524387751994956432757466
3614878334452436159497139743200596661362
7257544728682391455288914470448181969199
386457023589607996389657852898539654520
523060147728447473577723726265778913016
255155072474645681188415766961312071237
4422327784779168151482823143849274443252
683983775785950549140366203645504808
4894793756045655859683647318866019504410
136612809351512388574623764516626936817
36091724179993362559655787493529492016
491044443082676348262439672919732395
4889239129349642795384848911913769823
347557863536727158986889075100640772084
2743539280025577862339393645146665045478
9717216095966973490683905385101567906538
175659018145874133832886734132077086417
150979499304421028688694810948532947503
54198822901803424826019578416644979233
161570830649127275365294041287444851351
8524280725826327748894552261729158403
933729752966841127588890171966143136200
6564509753764397237229589375716765759183
45124153604350455716653759354799784443
567452462250856262384968454924122903222
5630965828557888978247022038568733211
7564790493206523812346921202554131379085
8260709158746544597670829047735888151
636565223300951571682695890347393107891
2362250788872010660934533534399853201627
127840962667820496070246126156182977363
72483188045266802756661592556974846089
744441733510770558567357927550751002293
505810178413616370116989173816670236506
310243774956341722364146937697350808207
473755331352319311934784658382342236703
63002332949043874042449687746319647996
5508129078295200141869176633240255034021
192275104527212681562272099722486434555
7241618450142968984770594346194062357886
2120450992210214898439084834449579518
43897028510244372276971743450854802855
3051750170764108697625747533985785135342
4340272019865954620068542151913262404708
244888274440327724871799412610928028304
6368152725659486742894494049982962637
818646706486559472915130875943107413
7981721869544115859247652932144268047298
49918094725149390868258863123922381820
9527364525143009671439338063765814418182
6337170394688503687326183828794226572973
9168709659694535488029505479720649499396
3553172620798263512055759927174268777316
18979393030721921585502417499475299032
1427162858422443494379176460372735597058
6090525237823237428276399966789355316
2313683321557790248538289422851747993192
5808960273286957494533234072739280774806
9221145443942072825325259303499189098
6610762481834961193265452028934130575362
638527624931539582498922630391643208569
7751790063632666590548745143793399419162
31549701831986091892509382954372413014
477548231141458383228764766819537166770
13496735609363650299535841865572409
616296119054656350217149131093115104475
399383221643413447302341135633491344204
408162097431564152019646650748273123046
8396255832453462458881643805172923819170
9484380586661692379319074657231566948959
720186502652218987583349776886243017656
32689214196227731952044258519033005007
5171645022504244964642897489965331233608
58647858240363885835626217896321606975
656393117243530730512786450171277757985
3399458970363210120378645713782445868021
99908163543822771421662550449753744252
8550353126365412984140413632787264934
19112168826317892294653693172907833316
857419065949501071516306060623669447037
326187330414233292976984844970373853870
325877950138281575794977913353117486
49944364508352656032804691713302223774
918125835721323443743971810216840194660
38989419606311372787224039126112804643
1584235655704127486352794940611949523633
366957486857316355247998058349787289784
10235030843365252525577431874282257946
7775104720154576739256996045586855075246
901430699815884544615665462699793111802
24612862585292583249624310824112441138
1646963869185543566844783144874773184173
2345739229507118136160628291977553076486
446052586133497994462173100141442552528
105148237030349639065533055268626977375
1960622997165576597435023447591543838561
768722012655160461531091700662187906266
3411822179387337179377741863767979986931
573047001729765579813659429802549756416
1937961791162330848135021514332510416845
4567059573592552186885365868341385493
2869193709213884546666789820516929276
70375572075514437567288700159994385713
19378158247773837673566321291345158886
220614476135634956934749257253352718476
294555245253486935385387274600758159031
49307137669782135738074548921079101904
937196291814850481480917517962232551715
4363589497247739989927983312378163891
55324402223710777206382697267762609952
732647335910889886212032258539940522798
1081759035437442868481864826911211313407
69521166458798433992824603886630078333
368933179732767975152358171666549536091
257793133348607722544547914751087709992
21861966666049004425612039449182108498
6011824846874052504391271320854326558571
411926705729503490074238806108669081135
75043373843124461472599681159526401984
484227785230517186504188378140954711789
302971682346542573039237140156872745899
923589076563124162453803666340221962154
9569768454275051740368051000405389294380
4366655376625751179375942689012191046
499811432618766288779574114532844656234
2368518250644835435173154950447139048859
955240099190830104143038591908292279566
97557639047854915838373832174988631746
1936450046621666656479016796915444475863
5645464642924624859847099791429649978221
79123306135551250337932721212947671971
2680486438022025202067251738495431696653
9946372744451741672173241527116635178799
631113461290349349188873494259119912107
7206501770265597731594305735813502753
9684880966449631719739719308588732743363
241521743039130396430044771946921746
352295348415853987275028664238572511490
77495558545241998828154839953096848069
25903232342122139293957141718035857615
3836465096462346871763296766492472719843
12533032562346473218689468719707783522
8236952923221823943872524573653766488
1324919447462825666394629262632855646001
881555424204679158474459704280164069312
865374502674662971174723428147735878286
9355394829947190062331274445259858446
16433658384943389541138435628961972167
48903418478494173019325221665276367801
905968016346896946396685808791249168065
44242551853267056760075523484231947715
90666106800738458817543832017074547383
5855111347283947444641417688984995983028
6318539090796353363328958003949496906088
848912613771566886719922347564182951694
6624653984912301677082718720651143056838
84932088718412440421358308296068636601
72284707673416706299937183743143545682
2772905098269166150773747048637698213789
1315991925703110916946486262739013892691
8766654584778325514865607317150792362653
520674343259675692976558865803467199
45749803208397114475025400653414688531
978770674854539600114372675190722981827
6770631682085437125673067708709947878373
630619179215432383487717920984168845989
6117318846607665111489953700109343871629
3415881028125418063543564992529433573
1309595753784093225088321644892899353643
2703778420037129718314647269788974156205
947126555830359934159135672985262386281
665699020699082343989361038153421033297
2384775324672518362715442485229077548982
9680497250206066379578548109687915279
329968578483124714477789291746642314895
3278385523685754270199416635304843862920
11537738933466895099686703818484326814
17551666353460472341910637328331514612
4454190158688978548923269170657361916717
4010996995571869138180828117556479102762
7301292613258916905970737973394073532116
918822283865513502866501474642910004473
6704430221925453906294762562459932574404
6530773430028452833363115313134183666950
1721198345857432377270145429543248116052
2231929251541208863579732918600584626264
57918838141454036056551619750185355691
8262182822127583220881696958896252227
853744617710583749449658694558156316894
5911422657269132291092188956933550253332
498873862209286749713514230106277974564
30299984400951570410683128793687263222
716314041146213618523379136902944257197
209588178329247033082931556056895568987
826586258045215562858184965985115477496
648826725965126541748917955389786179390
315099641882786619631065588095968005984
80139316982496458029194419617586168833
7643842978192756106740064953998745303770
3246411764641523787157208557922262391849
9453462510016265842148029746815744855498
8070403630427720724688131751547810688612
427691207062925332229454339945550512481
4272968768419743818046698069552370587886
75178977899257415891090102843764257522
358923399207144975146341369216048943221
1310974736249373219791498450163668833242
661671828516547890513264197395264944559
44994744763602833884203133836591205957
83195542653412906429201575455660737789
7521442481949293980171592939109859987
415765414139818161419489451197481756291
1797319531168845723543336396390830656336
79901434776931920251462972284735114137
819231228214782896827114557821582428173
643714671169912751241138768239563175958
993244625102129974786325225435292284368
65755111132969661141961809870546945816
6934863038193080216311663420337011201636
577758048803221023199694162086372035544
113453636506667159239360375027682221961
67063459185452778901875010091480083347
9478202155358545394083379643415789559
4203884089751870893431655224244577862035
423699471213668817556483134069191934
487536905164494220781515202367777972649
4576198266610256915277334849996557533
561250533774960236061200316555287958521
366931671345829864972517840114963013653
1269629169792211350024868711481014915070
1235322516360096208845222458314615810
661443387398100802983315630210118205647
221491432516547738898454116498385439688
111633818133039581837734178135032213805
328026665497701542209353698963699405556
711180991562889584018172263917048347825
674473427588918482923654694822795825917
5857541921212467300375296347142655994208
466316631155139793731235502483757755362
73182626938726326041526688449128094411
158606178679444970116406733947510765374
227131703301835146239618297393437942428
3116591829748297368712018094204832106972
903333506062619815065167289995865994748
9109906798530511464625903983135583593
567263487823536937563378437372322853743
531073642034493625334369100766738706820
479267867455318651216946310513947184271
308244638892704900674057017583865982928
412616091014064148979664279240967094606
8359201244717052205165782630286683766
727244138733957840259044772279089809155
8999834623783998241147456486551345202
436665381208276252178289392641844508034
6202110813496756871495994106718231289123
4339509441868157278644885650239558661884
2431853740759609129164527049547612274760
336713499109699307634751058947914139342
6319398655201565614452728623133934219526
523234011595985199245677662479003533988
92484964255892856489087263950509931198
3174662921657839529571465984372792138882
67133892234164170277000994140847908237
3052380976879455142217851000660446869241
32583389282954695214227029554624575074
567766924593923195674984621197950772604
701062643389613091126234920669345553980
68896187467560183965197292551970843292
694724848619980176009359986103269757041
9992551378607637562331908735366740814601
1245369087684727113862895773211089147076
851695059827833975672164272398630371176
305693261813113588142296612767053231814
352172097567934905190256503797230133905
8817225882616565708410236057800984865915
8173528221104861433053622850977623332493
900896612036809908139942047989378878761
46240186927397520766761086665595637025
6717497561021084755126718010614064331598
2053667529950708491286632329024753633
5931129199226740623280608682171258934626
5928890870774388640926315718833890904310
970169397296158132915564639482450224808
276830578774778246781240262519139419834
14499098121657064365310793368936832210
708560649413648193789210000894296548905
1362103179825964164857145553419594717
76791232252230811480461628860263838059
4770415967479840150229553294604694468009
6153796715478310122312856606594659997608
688389472705054262980104475978458311382
8043194459972847573811413761227879655695
99941101241835722128345513445353496716
63127372785440378179962477991112339770
6871456997783729885778211132428433125952
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: QuanticaLabs@gmail.com