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
83817466779733892136588254586184293916
2619645799132590800689762179440638176664
5243914616474171132673034760926281898818
786672631984279820951083708820976491539
370862527147962959150616581601316081783
614226611367942971147919310106820445
28911668510591312955279715355010522996
3447366242572718979125849588506981408415
61914604228798428765373226951019911502
42636106586623491240849433307758524930
37278961263738766921832371097798543792
711117548772226391615847884536286671889
4433527040369859119481225160751215018491
1581606336347559497859356888211642806610
6713430813848340175150926396461824965257
5481141972444417482725393147270491325839
9782301732257119145360046711432011323082
4778858068396946317083817543225289904515
266624377013695178274005902863822944090
2841312868770295246760832022049415791
763684593023392139789836598454225116299
56006199231780706711857185966956373013
78629837185550515278978247694665951835
9172952587517021152149405785268180345384
9544956346059606298151063190759138398348
12407008532650984171849776161637507714
32226303192938638053980589528756909130
2302450343787368451539587497735457471942
23537691834485726388625846205436688702
2280673357168850818942028670448721
5549325199351491542584455687150461179558
3021481699734587239754731439215828083485
9878567018859424144118466033248691813989
4617113235114075486396246594144565617286
8974572468537847695802369119238425203
475996668440813677487148023378926209380
2020786423174014994852428623280198265667
26446579348100654172201008475787028368
935054006095650286602693482670113279079
155510686524687128148529129453719085319
2831856328204712561324232532995628293782
562359588924291963593310470051347121210
130216621302932165656709956160360734513
2853577688701775966827053631191585957790
4841468032347084483698562985228364997773
1987492991496671231633748615381122396914
6376322537548173835771206262587639243724
9607681020045854496281902122104194282973
789218901959073421337981325918424901
56382951762366762327629860205438474820
1556267114992670428549938866850078112595
15763173375735361422406732288934587348
3321765252177175202825211949970471048416
21967495880291143515508275544101719986
8464908346782201811758213021162890217871
1554486805744982077384215292049573625
783393554108606944686557271856136108948
829948924308790737966007533767335056807
3396519524973929544414868732528980077793
138732907999859582314242644585713268216
63894569905205463363273292610423891666
812570752261101943981541291650183034829
436076108673653949832925224904392366213
32664254917080327105594197910769791712
6872592186776180389716521530798624399032
358238804526853638776260601114399095574
944153333092593323812811514494449994593
622974444873832808888182195969436619301
8226276037369824471243853240246484232506
9991785185036993297541884462727684537982
309647326245325383928187914226996692759
3309623123084024271697816853398782694856
9570296696485888353774033205646131048304
119441327042652523375641786474431298465
81637368839054412954246427487557347150
977767754896643406667728777343047327150
7812692280881972994870947017647032774
99592382873125355779054136480433385984
3383361934625435408471045869483392022097
74142309385841851681382427045675603326
8507485155605353802754727966282264249506
8236362850086083388953999890747788988
6484323353954684707367237772231849045
113969024483301734731335723854484563149
495119125662688228849025060587488569799
582755678551806113017457586337352408298
33716669651902326957525712956716671462
752420481892555434225072527901681395260
506379373063999105097439644980221002743
236569480112064727267295599802748279966
184472797162664263186530794663366472001
789881442343844432607496449861663117366
249062602666104670709717404837387166436
91305612958542948144222806544382886928
95664077307734641679791439665131239676
9779874671301957809712884657664696262311
723908752424268877966543684965413236337
4128358760524447463359131531373817203146
508360018149168534647978996131234487112
1167761722611972597257101866154222249308
9324634941591637523844496734183772824953
980148678376833231328291156348922252890
2867284951674666356517284682371476813772
7074944880409149456335228185869463019086
483998131121255161948276293941532579083
249596124572156291855034743239073379369
4623125020365331682426019264627568216395
8728844693423740806079565673627399387110
795311334014256857209766505410804922306
9841859073189695542126435233590211197583
336726245274688982429436760781224758981
27128690112312701052132304723838676945
41736650284727593761011518714559719615
87837324344193954193234831235540144439
32309064561193813241103687569628096885
235266952072297303312309695162427619051
780258179540392680056533487816184362726
5515924692739269316478734751208635937946
2817369946282762187283387989336391057850
3137367736571705593536977913298611522
11979504354388748210645391677395916688
295575544767647365715592071265736683005
65086293690671994232359784168029293849
8702661425495869490954171619229253902
6555654570803856498887418750987957605928
567649518658616721825080894945111667212
8213890029409192473427003144467531606516
510895702504182068013958115925475817113
3265714479265919630869423743128864433516
39142674456944413435285408956083161564
6366624781867056726882936545938187442155
7436889189834016618730584660520072803
746418933223145813596286920892387722
76176854561089624838586682705023399008
1281775566252980165286716584897732691488
740572422301428187766614608556840881217
6138754570418893848356701519892353681631
96040743624989072441037126928421342609
2529519131963774165657874571691842425299
926347024831731498698376790664728056331
48683001753116009978813588844869177
8869786568947832690494807074489575631876
16923591441163520792986112567823461623
685139772888975033525307616969915467
2217146483694042306615577344835356103782
400288203854414629457404828180893858210
8073503044934845938267151553817998003109
45570444651595329443309702858328596780
93724036481402594244012199771470847025
7039305170944928113427419857243258846098
89699545620165583363866196216792704573
1284844848494421234490733322294161685132
901515524119284478563793439665685005858
720508816783314300095274186674235158571
1043196644426538740953896789685028831
47874207371315872657150397502276079478
474353108789885578761141112278513473898
992519176856448914562087554453269225250
878347714200699929667007241994712978315
354736187948173976352253153645836943894
689053582871208974975699883444822763847
3053936489146081247729208866853914273888
5491612196903669726289791156774780014188
10543577900543479237639695598428188407
3319186379162613632828682562785466605257
985420358977615279791207522617143302740
99526105617830610463135617615814764650
2280610578741359746787378229241659288014
923273615946244220315138988670573523290
795335073443939025008294074481573273413
603235649425360276434049513979922935516
9825633492603909705365702814107436085080
175030656032700054487815100961352398798
4026664741288754943411592778695398454971
7085850034661430534182602192974717366453
49807442316387757576922773250617444564
3369423022476208862254158081523670571174
272122578441526854212421378140017528022
6611542461256598252949297876493142204428
7707744082774021874954068833422860161852
23236741914477666350932845186037394942
59139281577787766573731863187018819873
5239614168944411876717804860376685826158
9291967062021149691757673335278293778807
7553995392115238718259593779067885164
8217246832952535372030297646357371538015
877271735870723552423602938770863286214
802812997495159508363263967958380791486
69963566832196344505433261317783393769
7199284213825445166485255926485553532039
1818310484794743383491397963424628682308
62455476782462192510629736014573648578
71032556400552368112425623510774145866
93717577745861852439525946183943576645
890297175455675934921556464671590335537
74996744430733411873056642292628993175
84177116341072309262327285858619909784
6036511387928688807596963863366196474
91792644462453668877231820556374009026
85655231301278013032182867863603082957
611195752587411858373647330445781284515
5035321027762330735055011495377373723695
825180221343896480729727380285641128577
868651341130161732892404229730278412929
8356354716099171538365922036962812089410
981983708788681929223951733778678736662
411597026066294965413047646399358977518
5506962612218119658347717064675381621337
5364324599391282858729238043612789887591
13415492578619356549719658226192082858
30801629587699741775159016752994474857
8415448377232723968075547185462984665
465187311130808846976102168616350379949
8259925889257708243325220574378514229
1368766526519837515544486918398654069
5710191329356035323075121416969589985510
954110192104347681849974816899231325
3183561091307408161295805797888615088308
87205902776453289604584559361893809176
9788941049601044300863002094875773981069
5363384426199189572663556120456729174413
76931828844799169685394421920726074576
821636595452828437594077426656426328786
60877148492915458834973292339678612
1293617716473586980815836328455371216772
64694911173625170957207507125450519104
4664320163850204875247466422218115219
97355254910673594286084110567583131329
885052513480117979281778637894860357400
1760875146798658428091937148320437997251
167250121731142642412465173553942552861
55284978094668557585582865171637578004
57739307703777874941905206483215586490
9530561419898384321081412980980654345792
2391570522912783777666787524864848296690
754673471988844266071918922997147856618
656730294919800717133613225410552828750
49433251488980882758959299213021771
152891293683124136984173934042434327780
1592493434489702677368565855703113793130
30363133325045649570306660155338481770
6918829998159181276948642273296029223423
1079676980224242986014177547107354408737
690875680747881603154395000513553607164
1682708143291727575264569460208562255122
469036881740198258411459655573981209248
667987796991968150183087741622671640863
23959655308622499819163765663647491901
369655888133255100669141829209649181926
62275249673359746314009993250679572705
7484179246884247277733393648830487664035
9396941571961061452627558456362554290
1023048185984287149528796765996572370
978944422954222561849157120556591943103
97664194529977897037042591939694751742
946476383947606189836179713753817346723
479637978185183539117525871972576825077
42486837385417277604298485917796560477
90263838807298579626769746582973004172
5356177680701420566292608379523481463437
1972659374998804010323187540955383954
3467189854721368982182459881837298187660
8562885297753484561961439207985955995260
84480488865343971598805535535975894286
4947878720923228987518158486191884843
748708175517095896363667838267675008186
995788971652617848273947797602756742599
881290683801717983659852967683297036957
2606630139999741248632634647052199910
910412444767415446343462087730032415860
5763826184183695554960785690244653194402
9195863353647236892747446217807054252589
153815483344519995541213129383580279341
56135566869788947837000193756053653283
878142152095705057811333663572449103213
335974618183496045189115488408551885560
3307496982319592658284903393305985195171
3352774730234405451267558848896398424117
32037253055567773729954800069053371639
17028485324755631518719712695361936140
68677345455425797634667671812957386811
113264165722592174824974582847640797418
184697712011333533805167152423927442126
988850175845194068618404696753377313878
2470482426082830138687253347247402633
27377772895897479388905812317394639637
5287892610339889148895773332505214261818
1929288243968003813350204462760651237319
2637597669605269471692544822094487269
69287607735846744737235343797494197483
6413619312353515290986059794881188829737
8160741738187295724690220043148368786
2625387064713803923099134625381758214530
945127356299117453906407353578646856278
5892936715358453978244101482399953980
5618205111603290976478216682769973725815
86907097738865969256202549649181375996
9944292265277525125715985599152169226703
3908210940376719808875513748761712817
821642529735051964692513619539876981
17779126665399956713131197930949072099
544493841391737458230974019401665335516
627368013475029295554359709656538215143
15183574145194742721949128149637685796
3991611886479929374999902210731898736
3170749384998381908779726115111520577707
7821589639001917267029802391951294744989
32335248332183045521459933855131947091
38248996718151980774217340162584928634
143140215926794342330871023257988867344
299986018361226824572886258287999517488
1303377729577201809042393639636684414903
1904173383715841295125096555732537888710
24861799610416488587808123964990772134
815295689894936668134388593226299239488
49584920120413539837594390687397347741
109791603783289264129723672528692119437
5826348426735077734582525153422681218033
7019137127761523267138970225076784377
942280587776437377682783683782256292004
74568135480960013793599001512347547841
8923196972802737858259336770423587666490
436152064048517187388938898193063349348
3601806717075840456617909389175489396767
487482369601247293636199010541153175878
888332243096605613190667304900563998781
932426148259588336864272417547661055879
576133202435113184477600451322112849114
881219091739508524115812812474314278963
157247938053774140010185842793942146649
5501221780041098904763621163373519256130
9710352474923504871577002883240456956885
8822933199417274351748159318648699923378
23888869288187950738195192423820828835
205778339449895615246325968433557471744
9595807959414450157468646508285832722698
8276450323579136406599156720757051207148
439844043649072878274522717828596727586
3342766850021107681288039289419359306085
92352017970855454815961523362226146892
7112192051433845536350134584868928558686
8755380973709960701021114799799565665072
18327190759485786851881224971361833242
5176749233898443738176088242862359501665
1367646385869548035290368528972818741
3685939634254456710428697214424378109072
166787854376370959942428632223313621208
35018637467207985775289615450737412666
8718213867234234597229582687734069713548
476713021379438807290279509790573999
6729483758814057450916002082477622811229
5829344949838807288813694359319396893471
986264213133208076349197432266749058592
3674723923374284556783025019786598308852
8603288877197619305289827134395283457
4380594231406591200326362066239253326795
60843734857285714407577216397351443729
833933248925340496494767740126285969271
4544908673488382242256475255189779743528
128050153916491699106507541623139197749
7969847593666661796725926492716299781675
602637353539879985381320673855964416686
865058674887978621391972824859429633183
676029802356832952551698620794557123400
180657048581348831877660701712214239746
840811216355939562471916901722296566467
8805358833284728470553177004685335422620
7571381713548991416523032752917227789167
377168886627746219266329944676750289122
294427720041614216562259641656040595179
5194973771047725311869549823643077732243
17761957898788328410127872489347421223
4318574932147328330052517394952118395399
83946315561537911919256497485683918
7745590417152575238404268865999005596
1919624137911662633881626425393385784605
23423196863357747131958584265116101947
65647079936658241258306296389876725704
6881515356169695507108033527078393689
874624138907708724337389219446230432817
4905656178756408269565075009586770272937
39146074248372552438931623086675169230
3046977716474816673518021907756255387187
8441176589001476343816976236649296431491
939525123447986186779728081348533279499
72551387890751824871021981090631022060
234479954349454781217547485496339478036
38146035626258852027161380666793115024
4483403164547426712666032696684571332540
85939595933293563319675848291351077886
925171635797216907685682672377592797018
716365822886641468121594624948945903554
197527888898516493008088690967183375133
589344388718606771150593295470638458174
642446227291608316514287042153784737759
8527391568967964345030523764385726163072
7435636226554937427843058916425924197901
604381447179529699399325310935989287780
389843733726677099749323258494844231745
365549483384091763789344886146544518648
894898883048554275939237190306318996155
411678496965417167725673843263147917734
708492115564756323045413822899960483326
91517262648662794873850431011881649
1890998362836015795762118249173719874377
176740622473694857862436467241932377362
354235562671904678227362691517684691410
53388936630838172216633480661081574574
720188009379971799310078061231666854871
44037028103293483833534789818199920828
61933192712798362406099956664678304781
2202718466101630928863914724692683484620
1300174729242869221015270954097925819
7137455776918394774668491868814122457841
2548780042818394382586479865934494734495
7163278611064650330581167338724432425613
7497560767559459647559905059695154262411
286086337193427772576870338029150441819
2912159481743056515194896662438911976762
7555978541426848906251766854175151857564
551953429347175321243299618924313956543
73526703511322051852924336458583002681
3485906788603567299748126065747964663877
475889614856778885994746214175636144633
859818134866840598135781663636094363232
299255671788792190914578910130026658925
60772497765054754487403198102682754470
8501773432656331889319438417836384494103
562139453012488012925298625266014895889
4102957243845376283437685133666674052990
161244751155660233932634996937556359807
488586812093615518177483981187124067340
477185286383573485069018331733774322271
2038782581895058356432613169198753357755
1586498418503693310529821361597727477728
8613442950348874844737574445546995629059
2096291840019844702849198359298081742984
463150863817959740321677976347349062318
742146663615102929995166360371062785646
533249756619259791059371543668870388056
1350990997162364300056577957476074121334
400420677205216385946263767400078003474
1667356552217052805271234386247633129642
1074639230989244433210481485991356842772
3576533713291280269974028756381940927540
90619690742348619435836004151053462881
9986219461587441444124933799524250914431
24103615676618152745123696911313321999
8329514284976456564828041792992379182561
7786926827663316883316574847858543367714
483948772105179862974335822601012072349
4495463810646148874986909130712463513
303713169997265672273123381848151385400
8071236425157231578998251175889752689572
22623373112579811285382189335737132340
8645510880530363045814905267384547312
33735366630613922926759416555943983881
601694698826848177755411925619157869754
4744445087865823879651274873996346884821
7433497771384533752893267531712952042
393190525260604104484029084632934617585
7383137048381887381123358566876882401555
562588376584774919591942722515094084641
674314714807429369365679630334643008913
511654759684335122296797269464977287789
893623776027957475245683744887539686
57264607245831989166704012097086156399
72526421351730845186275361359289146691
267983818018487332755065775769323323628
8374345772632242831232275626717088564
958217668576363308049478352292870016278
53444846302413512382428673676742953255
3726156419168401978669783623296862426134
1951513722335589350777939647903084799
3156258306958293743591173983898821102
520479999682468677949467174961933260198
8407124630939284966365128593968056312227
2652801870374127520646055234441513853
6475731883803555385947171207983793482464
4685520818465733731738903829419920031453
9128995074783859635368518297914322322021
913187028378396559893173234898763918
7772764346124318803610507078749792316779
1335428763925776792124929603353332118533
190705938609368187754307468273752191997
9229671892524692758050825598431127315044
17585351640614231972467139319519903404
8554912263914324778417612029748815473
908632301064116666828472073369011713734
6998164077585736399969988673227265996834
1088938911044776259812875239328517384599
476184212855919705426054057360121937479
8631472372152958872694819395581653308741
734385042820128775523811282679027642446
47081558978087769921408921336422929463
7009701794632794802278718554686591112681
9431441783108651440096188967958112868155
824829154829934789366171075370332315397
679828131714760362312596626146344293504
5365861564357183885666488263561688761200
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]