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
2092612689575831685685115760444421502070
245766123054423937743483732670641365529
2380196919462410843528393443946863573390
4822477648871621272541487734668718489612
1310428056401199566547712412800362897172
263759565339300435275884765232305139382
1021537240423005687993969982984595
223099306449515406764364155385799513660
613665476129269207734422491681771828386
127960279155282497279972334081801644430
3632457449680323449704574491840915008
27439384763605211826298887282872195095
32493831515008778277921561597375206755
1305370112809436420238115200521153373084
209741418497147265880837253823467893386
732386518286917536014466520459923106898
905684092167424731321099534806214331010
41523087063874743247167924691336782813
8505548625463520717710644847886347203290
4563525218381856095280466674660033519
58663182664389294764663965191412064663
13673107808175846137959441275941248350
9647747589638033638886645583959784188761
64457139461446342193842903194261951647
1676849760265899651714086668459030404380
8153457878571587929583823471456812558380
323199314541492997955865385998613735498
3820340494906205853428959054551866932046
79144850543872315188790121424061083451
263382812681823952506052458825978181488
37389818142541231963876198901586326599
235366050133636996356098958843915234572
4818373514639325857272747007357518712014
5290735554102694983022843120884815761405
3468795552633123675427884008935716696272
9189805275883592380738155879377119792077
3694693741422197303861266010533671871535
1686658512726786441486017802935121013413
395019741646537490128378644252023459761
918635532429114888238449826173126001223
52054368168257343096386581641615061599
1693902440617839853754289661895183474389
3371475955986526350376948509661556418645
783543817765191841061118596819781706357
75869021709516696378833959611393786022
4751481859655329300458707349901060091818
761230592331108227979635179335269607916
955016978410184555032661531862281
1818349872123533150264187690746042394186
4625770251352173735884429431167634873537
9942748341758066144856899390476482854
156356444209143371330424568638196434830
9467346343926913774038497202441079481106
84987132559209656061575179339225682023
98241618875914977580736650943222679927
603078766432547839845854537330336626608
39874813077182272835427305410388494542
305791161938813125135868528170573214922
3443570467616724396648433659496285245533
645302266844020507382248941100144816397
53399353854537068736251314819238425058
4340885175504986561794358847611013188060
5239071824571076839851216192471292887
814255358240208342733818811757747665740
2916490983154125257433269692062316983
2372554378628559458637275171799167521501
8936444027936071684954333402975770174307
1876970675455421321365844724789072063329
6805590819977856189599509515235884986382
5928172076417975551570906988719957575530
50897791808223972671973298674679955154
9928820062138557510196636663876748891061
446984126048122829691254126570879035762
59425990165360070134130393497648657487
264493143436846611238029853191719053312
8315727583512116998248533373418843594701
5493540698117766874558607654776782835396
11976526290641516669404194321048505515
4336798096045340113318938422770889001
2043948629675886165413808573222492321451
78734739123344031094506645811297586912
10832289374145876271236299886320825446
4508195093379531608745955067403132715694
7541655484323163894626128468857062655608
7095756052209166585159237959192677503
8969366676946466568945245500855865218048
91431486394337831547504217960062083784
763610862967546246928305598765849381090
25058763220624685124989358716595318987
68296392401807171587335564418637403563
30363307519026972121622802159061244364
664483717522222995537141142251327897579
470143846878495090705040131291134519632
624455059104653217862892280680676533556
2241870308313245340801888311195521677
497857382043879810819030489670299608329
6689772436755036468793412924601324928632
46491062689194478840507796702178386356
6476265226151206384318623044496234458578
4164689652804773175921155376441656503
2705180965629418183737469942421310
788366841989073712923864629112552503748
52769229478923196822127680533719169136
757434821255126462671743985640816933799
4274618285948349787896683053590638587238
485960212669133998581677488521848508839
263722738187602703896176483118177486975
620781598481314576401490746066896035155
6746847222034504381843997500481735121260
4574871630832258687266069935539376561
266215088930955779992059247916836179984
37196955166471318016197004399314346
674655431346497274775416373584432745843
4451939972392827518197696585110421538501
6263734216384977406059131548804293525468
120894151502672735655577407922799110872
4165819697803528847552284650657410558086
37532194214028467420642065547452100001210
6893642726292963602155761112166384197378
1462375622387110784054647729937922281
4225262954746153351573709359118526601544
2778892973482572643466669874790159088772
1020367728969584549996457250621414363085
161025265676515517145661615677880915414
848713602417702260415369291306554082498
733456227952697857687683416726208290829
3601685226477734457821730216738055808
6002745310624718753282052591647728616
736376529874127788217057554457971384434
3095715724297333251366541625669498131541
6848345517645533987623537200362467289111
22725355181240254624993160408282057137
2752863447026337146387308233358399567762
1278180475478619266736247480760194166666
8627316950105695603869522562590267425764
873038573899100531433004774870174135170
3011888121369898889280548365833450743486
4715765786244525979773118254134422264338
561385766678925203614537619111993787547
674411413310830791753371031717385941138
367616652744331422428918668536481748177
905570963215115541112968976769642757440
6088892539246426823639845630580529309574
12106158678200531394960936585855578435
216392351494997230837239463394825422759
4801489685508765267213329398282414362222
538223831113812343493537074384087976191
7833979578162144593161532377647338659265
372566766667231135439282909165231902606
4192965379277009474597009461752182232662
955497193247798740968259209019350504195
7751819748697879328462866574283640
3423744550399874667281426396701348227931
13773696687458615114223774505180201960
879498969728682242442062287878268611897
176748477094109328185898627213999887391
846713867605820317373298654453416031416
396354978303710490896047857184532348959
890958364016362825221675627167262220967
84212441560229525084571040163160754249
516214559294867310795546675556441998851
65126799464894922983238671726948894960
8456880773599605859932484594503442007630
35356800319633737227382625751325108378
654218145990326165823846921461262017722
685719032483625599477052248860022684824
61590861600488777838860243150063664383
9928392431401346661520805608412689116848
258427161326146782048647447273412148346
8190179337787973406512867066659213324354
7667422311153766625683789351656978719566
987126521561900273719342165613098853833
7893855066251325891441165533689849268242
5689266149961206511473378884384794721513
4718939272310387975634721558492066636
2305772789367910636365126593815339744537
833661075855538199009087238890263180
99774287166219101587352248125265864175
1815785969304186130497101313291632827210
63937487955725191341093519476938584008
4446198059505154957355297989363931346438
31192696255096403315833727561496134386
3579507844219017776166452497483320817488
3898342369789636129492991857415119041535
9724303821514811990980771657290564562584
6147431175637468680110253641403012044
555485022121648140988889719257049313018
63643121953483015551762790802218047845
69396917824556645351285293671811554646
6199302116584050271413472161961592977983
27454401288681478502654338918457185104
286433378895482678148229162151379899192
419657071309299638133175642593757987095
808372132239854313775435304806652764640
6042547850006461296643734847851288687873
5187323353729080430189088761977546944653
132021328099923797594863157570605866791
6777356613982466926638573119320945793847
1018989654669812923598477080189939722458
5849427729177245590039001207905263217529
837786664034180068194970201268588723366
276595933801387689737941141725046293768
955017059020331481871585633294435142532
8733706975465436344838362831299858574088
95178486597596985435778346318974349461
411062859688461082484358912762713041557
9789365351787856679507136098864812652
184469724768377073172794710609221183952
610160978476571376276846139814639287905
6618873589584485615275974043256725752908
699781711110425988391799335414829445572
3347951989012971625120873445967698272091
8782369587158461426164457823245597513473
414057281521855313151599118454263941752
3307443635845691470723484352533950292140
971537094971754135736446300290262182218
204131502005597497525458843601092752751
365810302922657894796549942740119146852
46223683219280037655587430691087868412
907855517505686034871328266315373148174
92329223504924557877228899803744479231
313667795304661217507154912683694972375
667111511909550456225486885119261854632
516746003630730547275095161061339701001
381285686138188396029438858968846156469
27312986988458734046675164225103856569
3391784117236446259550571044301586
366124203823672753413948523449455025807
6658609834012317194926546147188353174330
75516295121210143258985367990285963772
168560281760186656132390427834684959793
56569097462636537201588110000186476979602
372998468772606739177212391204097833890
83499123278180142291194224506482638036
5960170076516617889887697658567968836
118572329338518360175022743764793999523
714250217633760593066199262871589508634
245849939434589751162249306263764227228
2424569766528886912062425569645547886755
9301928393455200724847263174812617366284
1434263615031713885754095250857919309711
2258696979486604793693791940248034724098
13488144835736281650946227627183398157
4225219859431869229386994269789034452996
7121389736206869177468357231678327201455
5105271869785510935059373015822759263323
14738482760297732096653621385275389657
458248623337369845014695300918919793219
72032773011774270825942118342792165409
459012447677786113517461780301177602046
1311638879244281880856667891998520657384
21959491154231709478345730682831759340
2325943370067792929122993905576636996482
894377206393310670904802607079776161665
124886925013201618382314403438427504131
5171599336596588911773447683267452801298
90985031731875999742869560950884592245
40235066550679575284172451013196851123
5614649492905575551637634721135131799518
7248794433502524839242995544797381229573
32913441036958822807607401477051836932
78525764384737146884598540666642718334
7721642052101800157195922118751084843
7447526381988744819372749520388606146
622016509350474988954026608753327409165
2156304169115974867829325194966930515557
6881497363786215793593979623478563116072
9976742175745415437250941989429143391232
340090012178215286565858873941915587700
3416179127510265848236381176437406322
7844412030189234282763415153802951244414
6600636866509043380770033822670761945405
723938418085451517770867425480983928961
46083560733868866437332386101787014811
209831257073528786837472573114871464677
812921776378225909525354291459623304817
97397813921576148357063217531593915920
784189491464450126365207965272511816545
3764683512392957835043509588180928188
2821303650953005704550015832121078986498
685425122135656593241656836901676508903
8246757032572519980025033195381739115847
274688742292836897077503119558229487215
2492579470099167224325677860838588362
3184569595100322478904735222398679197
3323577774324579709401915452410545374
9769835848571202746438086319942025173371
113274161160635275103402591928863413662
902568887423383844652254217310361426598
503541511446926065384655993744865796176
4739261053094746844981276799199788219818
4834408636207284686161933211426271803
6295270295833649809042829637263015521745
13525822389666427678539895664723836309
62547075850069333757805175699736079775
593553238929476772572728127916997237577
5175288590592945256587321088158524888815
495140855635991229835436900339868622243
79474914883052051188405526492044314185
33069745895182290216839124664278453477
6059341074892739265332884350840123795945
5757925933658275565932241406261845845912
820465227536635619511453435448282991951
713930574398881063094414230692128097465
6665951195413415079797262489016485725
177811272654516484656390869232211245
45177440531146864773998686461948011889
8052976471367624307389758322832222077580
59037225555319593465116615855065762709
296981070554579616511124488101079453347
8911775021524688940831216744633367038706
9037823593889774313732862665309651563335
314256719484726989495215460974283444685
70818978717282966437120993866370755
439741899363459617269606275555137585147
555778324336704869036173773658917055318
274673244767167381369770742349189318
4951872091373217336173998372226794268530
2559836515074863263621475798593120966360
875722956514579574078597582376062258080
20920680687760240542373269471473914796
9475880455356207678571758042407949959204
7371551257421843203126329967668263924
546709450773127957279846366717026664166
855755125745446373891525872622049549396
41272512464717298383421828859659936758
529555532963814157135914603367113574596
39039697418491249896738619592962159787
8903619260687967502259687331032068851
674068315512315396953027938987042145454
3314297881708683819676443737407610468455
232169046870755323130672600525916534958
161483824263646105087381485472982537806
521443947957117541297349643335434022193
448559849269362618159751323973482964518
4988904646341333392631311832828288986159
9468530876761398293551928557321242879236
12382802360792842479068883920649962184
643052223067179406110046757386889091297
5664859614078350434176593076434838583674
5015106489228581732489657759867085645884
989522158022200767189876858447515527075
4422499825446758792378525765412063036730
580066136451997091327552484268354031549
578119926614126599695230169586722491840
207919525758263620381928871657459498171
655250189209983301360088544597366923467
6111738855237241373962359089775839495635
647344904675074475457446994191718320
9043719195372739569083442430680854247866
897121907135732444191964530749631448674
6221128139209649242280313219356216013
6823195056886334383914315652671548853344
14594322217941771236208360018557755663
242427841597811558389722186874663061649
6493525565213580379979126114531387496469
5817668240839817846932185703885538422260
735703646867961582027765162396721003705
6639174815731409579483184967579318398
944038221179491721582195462368591418831
735664199439539813223635803821324787
456238345437121934536420838011393451897
2815174316948603954302150499576268
2971958335779382523846668099963370735719
7623462523766471386515619534583672739285
535553919767547621181074767063302275735
142999326885711824806592925624432414139
1117629225104678677116205294262730339621
89285955304258776177488985511016867465
639251651397425249954312674471824349590
891382485656270335992808584247733767399
9537292789342270596754321816607255207147
3861552959876640734594267129816892461252
2382277437614693690210198177030172390
861896596413367768067666175830345346015
243948752371881179168686726742847742032
868050775170433253283290995826164371180
8481808469586170610913453249521534356191
287444467938118930713102166285369548368
7639184349046025906388995641591691065054
76826321394502639591305978909080336212
78296888371372269462458558878096594672
114988833048059879566787524463119232696
54199144340688285862426463948164264
831943198849488275873908641768544068687
673933334624672275902896325312716785428
2822423947521399320341217717303968524610
6071163090879508477893384819542945936606
18195307812198143670618135498979555463
946153555710431687284867675471396778
4971184422533375233675367761641085256230
834961333615590947603138804692619478659
68717545272723316365238954207686173105
8949555099137301870142853509762896313118
192911218906184996407582279875840645269
1725498513627785568955303716438781799721
43048783315988956544233386744958151798
2158361772839195872062257689081684393
8481772929104714199468401920164053652318
864013124623967878588708354965988352095
6438751233062235761889781760140254513720
975053804908130346647217023740164667557
811432289552717381613373944237380986648
452994820035738787714995634779548157645
1200151357673091701382472118103363832767
17677913240653915648054663802552221
680614386269841716218940235042261625891
434121981125685813792139688649163807183
6374976238716797911117313367471724662
5071716629681856737917703540483756308794
327246707151914972472284905926405421373
4650348587674158292679671371170691297632
847387154751867260219597440249533398633
985430493936525182052048451136019486332
40847874823199006630962168039790997512
32889705877514847245215431531274789641
3083737963795326435955515625950881206461
56765440983949535395740675324084349253
8498557327138118816704528218744573075
79710749037417346851245710157383705348
19085854539994739529994438424515305017
5809329455127687687247356210940584025414
9843741976877329745764329738171315064003
7431590277613064332355275895909981999745
5951297899084621250927335489850064566955
5417485650817181812345824527265464071652
8191371574059410935689311262619446972561
51352561914790219544709364472334389298
95954058481915825441734347943602148313
734120824484863238824043225183495029813
39338585193479461279115740593141906
194524874985531726728551593451589815357
5087761483714186227556282092432236437222
9550311274209418433716625053438272874
356504817748061076267584785526409709
852264112854214281231815904287174617577
3274340761453381575989569851182399888767
7206632437147852918155523402687074772467
5052289174683249384475604213929146805242
6453965141874326646194363049708694582754
157315363380755882111015709949035212135
7988211415694704859590943663140913606122
63806899653197363665626126430491776143
3479803764065331943215047622192326496831
261766578885417088918601394948678
8794993788867596393548626233781364029
1080959147026670535335158397963369725807
45858948143503975778506853969152949813
3864480278881779131702112987872469984
63791425263682139171903231630982492989
329711397715392212319936493149259708566
6958720445963809615190637763675638712646
9953612228388937411073613214366726376422
339120021010475314443879270838588266141
9611167266836661868856284059438744915398
57146343440278339847190705739034315945
3812519542802134158224294388233378538796
487683169532472396229138489477234143458
52707755826276474500545514251039227929
5388510847265713985073714081194096862727
8966507144497989547031522622696713074580
7368295671967941187481261350406438182170
6864167481727318618278406860893951637919
23553039811340146481747578779455006207
7808889933534397669166638527388166164889
111073729360715516695821342721763096313
3444959244211667844874902227245415283339
88288325452796246363009247131099691998
411842633773256212040417858190384469264
10341856783740779020648623171127163071
1917373521423470379954026938011993582
2329727085275724973316045497691957051511
85247607639662435178008309331159806597
26911364545404838399630413750314426
6810749531921851478978002284752198386865
9373720193154764324182158945178452267
720467498986334668758657503187029228916
9409202421218555171919891318507146397542
879197376013652015375849969970718269386
6293776060493623185882316099318811441149
74647515823255194489634981768176928006
95845571995509450174291451243489072106
9286459935054147638199972629267916219119
6013266328127582199245159103300530906427
398339597005190119029455242968577866216
2828852826825841536717511259895112659464
6529558298433636588283291487221896156308
743467825021292083503844913973768222934
65792356433745727745517871515131059878
469041393595106464780574903511842604995
89137205957381726976874017757108557361
216746048393315676515129300406271531072
6904368884336546866112860405668616535
19165011170211926506483281764110771078
8162620288879475978131627185467067935222
35769243335698586746201904162232993730
682739331783280554369414438708246931766
6914651956576137658745421564701300146
15547628403667293832531281745614834603
3576500285649914462354698086314338577305
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]