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
6268799390316327104713725537890237312151
9432979299835366203567115926726175926252
635995579692112359565632757641646052761
29184803186370853602590542217160017051
130536022368574133967463242838969971695
674989968467297740472098687430084692677
811425437531961570742083766684069635113
4933883687183443250368795513781027743713
212787131510514210822537995133929016171
427578813695503422316641482459456388791
8524526223635098799781626186058068276
177556982304041985031136142856516723524
217290536519792492381675997601444515987
3152296574985519273761843784611555296690
3479578736282218278334885769176732875787
8082429935281801617513995373151493315000
59409554141656328392307154678865416037
6607358636911646249051208928594795469
7954361040019731400278751401003302993
690255673217613941261927739708350886130
5521437134598034884921493677744357939205
61886464351215681570429393116201814174
9068051536655179306724264188339827211
217638936581393146685073934485047257809
584172668028864963915538905325533611432
752640615359219355389496584937163239015
40898479846656652683772526122021610636
4592597614884727276274756041855124428972
6084185125077383465638489569505796230
514581152192780576784487509178483496816
44124324685398616661471641497899177397
28374293657268530562941991807741921692
1226819962721435680172024867889858651808
14443685703537968254901433274333107989
4148950996266336436794661682612939061
7879677520634738577175832052210424621491
2263793034454603774127956732317017124537
69667884174091605495698134881536095585
696315648318911207865594077949079304465
46417640997765414948581163687428339028
8839631042886266422986488840806535785673
6342965510024488723073388750889161505678
824398843936470165275401138896156807948
115539919282584194455201778733944026381
5634270312077710301865387059338524673455
562356527539665678750664547424552819
460487735457825922185539424387099194497
794186282135745248132604198310384614312
7103157935408435396499394058550596844
7434136297199025136415916841918260453796
48877448416172329873350651792772809226
99448081484858947325324285740065697618
9717939057567308738266591521245124405451
62405615146997755656890670161939524299
5168366745004795912585595641576551375516
783747201612069947313485150903621168033
6241822053751570285188906916172687008180
277915130807749848395999354677712448966
5476286132071137605473473948306021496055
13397169972929192197394243362483669174
66726843601356265381714586890264273647
9186115443312330979384255920924914058926
623159021776575037434062675216706385
57047967725623024029805339972113343524
79145915877350745931522488841987516878
5563402233837273216419484177891231759112
9689895245973940261020431602540521071178
89123054844055651823608616884881403468
79364848664423922949485618477294008247
54983575069346116676248688302185914091
1774926951481899611263493363212761945713
7358401417692559566682377473224517101045
6976206568744191930757109809975675702378
9468111492293792438535541076414869702787
8950682342203653866130464141876065519515
1394864235057484601843237313891284889149
7753658288005232115366761814657040657458
6826561017028729981133765324531557754958
7800932380938356596466337127645752797825
4061310495443509861750554731734697914861
373685609232275642185504521978591789067
83758670568277798288943276534646165489
4879488748157621376013313529732998994681
4819284585675973526546928702481614631126
847082176359501425089035205124554587778
206578008213205933654998535331831984305
853376938396194035793978876021586472100
48018181385772629444398232865436183065
24006011108011492995372662137989311226
321272401781840456978116149126181478323
3475241747925711137133515327268837581927
637473946916135316243455993115156618094
463125559845214922102282468392956282487
1514160173038252350879681472311419922786
813286687630284648205284667197182364383
2891864557986034454246018264266388206
4980459051315811163672094213362182917418
487615137496352266922842595377087456680
9704627459534982132518532624500396587236
7944484284956294138942461239158282053528
4080218248734923398290906395463287342213
170384115823586777618287388675557765
6213484412257629829380653629909941466879
6214130286849586990850794299180887076013
3792277876037010413117443535481464705242
7063336529169120942341028302625551092831
2177578338358527157413245310482926191550
602535657041936254495869246770562395352
3731440882382548515099313757467340207578
97427672466573828659252802251755326349
9925283078908182980672741168885484488687
494974408693978432283769174130862096596
940128136786759103511597627545063187772
461364895165638157284542089114891849631
844146603922881117176414764683808607327
564815636488607394910590081019217725
9565921162183762452112205379686363633281
7059347056653884813758654702825614041051
391149284289867354019631890428928707160
6377231178625662402120973575879454078569
3023692327131972309878573638905560813310
3043590676247159593873243397848692303
563815233236541745758439950364941901183
1115653077188181944436987141612774721311
3542689414774875445433394799341376742962
361617550818943446051124626285854266116
2084499764993214939011697396281196489
4910922327791587281531636533455531102754
8261391745135150454448991290895339891624
9031558213769939651178886461803466093342
378262896532776863629419461412692861035
4679617181396401838320353451550299827477
630733367657095779995444956423196622413
45181861752305757264835636139939403128
988741471636867546489887756821427515324
78891834895673415721147261354857256946
4159840378939422502958638830457978575295
3189923679382089881925207789407857069604
496634196327101559251156647878949917517
791220919102553734060145444300097941365
49071225572814545508575363195613606344
7451428264578913509740896498435389122
689062477580795369837669244895272179607
2907447696906226376236818285736743379100
7605764939778521561521450682917959300
385740542906241933821518410764689713495
246099138262234398626662184495642144988
7493849930134475995136033001493515847517
21118844173381495203734193948823531754
2430379596928243384313138665858776491700
380716658261752308652067357837386786277
278815536673121896935328071898469424578
6735985675557113788556619603924521835166
84027770235699567969743686315557974540
308562661183749844935393467594794752505
6007147151658981390532719699340439607138
1562818339144072114239722066888365589499
27425453619744267725742521950576139086
4789554568714583636097954567608555541956
20504338762259984511877511783608502221
409911469023307662368990676441971532365
4585587620723859694330536747335366623021
5175558178677457626411988030330036196945
7415274275921232062258257526343286612
793134537297362190199205715061307661463
72539019771529835797371731665583540165
212514444471287913423516825137967349074
586374661637429604410961230748014785099
6873413863050681847305520815625209068
441742694147985060716298671348452925273
237558603130870518363911582385428786454
7914658668598778355346091441386229681267
484034950594383226445353693946589498717
57893354258282384664463305583535234616
620090818328660307775098772473325635726
890191387574604508921965146253752385314
2304855213779763736836336013550872577351
5361216895283357684692006987579479554288
350219675549236822742068632256628591
3503473891428768441758943354295396572459
80383195672727729554009358930807085804
3538357994071827990684118604994521251020
2165891096674782619037765141714474568241
727343888251407645075692945221487988918
881855601602399722471595059108513122962
634126362186291407711447428372049768198
5978677696338886154242954803234172491184
8152746552795975364247734799886944348
88369738181502490076005265926490788169
52256579479689268032696205772744355185
9998316598328820218665767854646273558544
511956776360649644136204327757132068524
701112520368211728667098790998992407992
166644722113229720312486991178733836885
936024253267418582478279632983589719963
719927274878425963425529521468066851068
475348255918817951143231484750251508544
2725448880667611920563527264349717514588
5970530322449179945074862611953641193405
9368763848393832952748611756603779482941
46911516665789465224255116876453327215
5094792350248014501478625148177726443919
5154341626559403908118271781898871834203
215318325332693184547011230924119848266
826066672296308125458327203924825147108
302691597347540550879111676462910493965
390134585823218566505794330685010203424
206646117940902587927258240319121637824
39911136971175641425957246981819255651
282432963939407828628726311943816146619
448922636199264619001006160239872453076
688253488977117377073548345350015858
83369975592065389569178242551529101617
4381832511264423582182786022380547556864
76585471032525360304526237587236949423
6550523851178963606579756315773560215593
2275297350994440393383938503535272919532
402998613146640987063875128997495273726
55441428259566848056668474301154948893
9296811478621399251574765336297873426
163719302877770497616767754715183463
41943676512112495171514744571593118714
3534671939615557433896012595865674319
9102429214668364270097407159269755159762
10060897999818249673838950473152915
25989272944622237954233701798368413999
599495175868863013988426591891334415029
9742921457426088831715683222917880672719
401579432119205814491478647432749499850
813279304957600414465239209264131517899
11525078763011199501189993851313939021
5483791236651763280775385742402438044553
909335917609400293761309162566711959932
4325621122237443681461878200264099904139
6632224136843052366383324398695733205059
4393716319091233523469216346772448904729
53931847336464752737299940484462632855
66924369418623225017104362489518299845
6228788773547873799332451141797234661511
25817003111744162653321977053252943476
9283440437979342980262459702458325316877
306114382913783200575865801753064538930
837288431111865576639567821961018150454
409013263727708396652283066893728243557
89478533746718881378870396710939948292
4753426448761187114029943170437354099503
185851325783664341646493543815264649859
2153188329529684193089737174451535174803
259976104297892827439182282354554996430
7169824014334345495138674635610225421962
68575039465782812298537982936843049472
181940427241466278007116385898197757623
97496255293650298111965728646065694383
486396868870577424608681266194448776499
640091995031941811808521776237599394560
89081359667381239651991855171127003295
3246551184347818927567309956374322157
7248500829716167235752652597238575299959
3108698356703058352235618157405439821898
65435548329165543815871278462386483030
5373970465551795704173803256413557212481
99276705533667288118172748196058066111
472979995095984433419850989496783693347
501745412586810545638483678199444824364
2024839229171777150568298444150620573780
36339271965187182165537096446070794106
898871764156952908736433358427513906411
1863197673813898611032939258337014418605
760293942196286985650164221482613746951
71045022797086079936983850917973334867
5191528273703393900813806084904041995032
979778535341002920425738853802212704965
2968871995648719313081942290749860154429
642234393970199328548736741645182412830
708073615747671957861669043125475202077
292534994525857884322243119461021136121
318129518586954326128066392186275125382
784015648712777424767103152916103974
4575762492452302774823898359810173294966
8313638258504157601885456499198993182953
8169997135607978374377723537323459207061
18632344453935153129889886189349738342
71182813416586097487968751314332118924
8462829266683752569377057614530640661611
3441189237263335677238309724582255678
739075949589446536923824277606310017822
864585728305591584390445623286079496
340873479262323233532745966354508333888
566878754219551857864565269329199523474
199490834178979950278881992276569062
924635271536881202664721720304362164035
1389169990058297270090111056788067728424
94554085804034298703126823869056050772
8594583751727998773074379972496976154310
9126999021544073470013612578538488065448
8289500940843100731985513877708312074375
22508349644568842357767155432324977771
73074995993949772795362632822249289380
90269815323666449269946069668057047919
3509551287916847818948485121106318134104
55437344617063516729377637630279945939
6812616261028415746125628518653426585417
78541926302637068828457866812340453559
2157897951844468599922304013594575922206
16889822729561074547854947301037835030
157010402052332038913093236407449276184
207341628097326293188108959768489582449
5020901495461257897663154545144436391487
75769942166299365263689159072185717572
5752494053191561202564526251169584363502
9859839963183807392718486619793067984970
2731577886176489698066987115216076269575
4832194923356779994015976694598033971890
113593496202109462175132893030972888
2863937591128651422559761453368560626806
5822984772109482559722593143149822305097
407127763839819282525651920733733515812
725964982904932150672966619737181009639
86255939682364533900279880418892584099
3772803158401395110882761396711273818777
2176502255438888992684105116213935372
71781263560590814161452184229582555425
8954506164668947566235635682483221759698
5944252316627779452667315637451170307871
121494721067884421477316535202384983480
4306848153277076487813241450712653546633
437081514876505505982651961307022832968
943369928124195199152479326314518528122
30784651479063961486738649226426155575
659752695351177947629372572294061184735
182340984233491822786975994325649069369
970711203873512719648939959412328489833
758579232545133469355758655717521576138
1569108990435037406866142544746732319543
648584731769974885373461406744065008217
2634417912933136372721415515671150679
644312302475987728326104266882072745594
911689140368692274972934085635828241384
9376849018354822931226527081937222803499
3201294176099402379886859554106655031392
7318368184812924506364622141918715002443
3604417563276092639356456685534440647871
4253818533782280193724007231737476197462
352940142592193809390698745656148608047
8881257614501220111380289409427992693
185019152020959768150145366284426453426
93976118755867187535252269128976217182
4318794915355952407867921490593135633455
30743508225323867625938611048592410880
546810298363305596949238918292030105024
6734667213245244276978811883547470921382
709131558406700917599821427312873640991
391466342356260250528457341264315353619
4245523696698847440783128275440782424451
7354336024572990665344824006463298623504
9106962935365673135798565292417682729361
45744529011136479363757776537579511070
2603763289533027920524117476783285743
2581494638794900470192382412442018627803
5944429475324076524385891068140670881030
2605648014531520831165006933207968198180
751752103998504458986902745669475944999
39873380167266548258755698827719019669
39762067654517737612970779941505536
9891699479639114359256204486319528646420
48894648659788045018646510695727875395
945645712096090713969694952375656638083
9355110089519809757848844513479449232790
3577435074821225584934786867267583405687
33883854777851626413125213288621623901
77752738192681713081375815702083535827
543564447353079926776794584408253373450
607796254226355401292593973152054357822
5548837522921697527987391101719541538
338655387178672772902173176013919799339
7378909198089483624126342813663559489384
505624641250748072448191843278453128233
57946406373438864913901162468011214017
74278676865653987448606749978791112173
6856517629404972326296812120311769647079
44228692523686907673804777975335363559
18308413838532665448476693828659196175
291149475963456288432137402859854666135
2408924657539620388325442725463734204674
4346768572526248469659978678308162564
7170188535444298261544394858331419241302
4400223319784329169160067171566287627671
9752961424401696868274757171243867762004
15709427147189944754963376971980401181
6171136238154673482643724584298157547931
117410479915896758863357295622392205247
6901865353277030497869344394793148119966
3919157283406506111315387305795372839528
155592035833910169433284935503768952645
438541603433674660216812820431516828810
28237213117135443968915041253533751612
9277741110091691653791318369157380741676
733336455479840162926995679545237121113
6667471964625330186317814923856832965792
95795451017395866406246582506197662770
449835902760762299957097881728745754947
4883714948395427886109035598508483712
9070176781243510136260854134822657621204
61070971498645579792395730927347715999
6895790582307498577919472343100437626897
7585439477386478244154496493946914249848
8008707995971178239686293571926321228060
229115639787521944654935328460592365990
4335620360891706461340856830135819843750
577192585686887076816812564732420799640
2066300937848953381824455501443091604628
951681571244159936435454479145475703974
2433302875046557199118941464303041543894
5888868994068742923972426202335228828903
67222599579261719746099778407979172437
6972476461702525637651285000508676497020
56107799766908874761530848961569556575
381332772977943905089837796310866278399
156470647257788610649063062628234935400
834325544187487382895059999742180653354
681376456504711135471655998600287349996
42374654221765643794366478194838184150
25756901707326059016487295606787808220
62917157861233324294624057814247116088
8834431986545897527921935737825488971934
110312326999451664924762903361139658964
7399594911731325261670607028635736177390
8233394663747756920060586202779745401640
966534798607711966662396708729218439285
6286437871413172822667374437249319559957
951588985988955106577157073358344235396
266731537531504193529654654268060458519
4520526577196643981955381890721993494483
9410914311139187243287363839335327692
2492155671142680177365474288260882571578
296890847481599985176244026707716191093
5822685948832301483074264637739892376089
243658238298846253531445949331572248849
3805603454513522835540592147507283942276
3852506928001090192442326780946794066590
8929804250543977506469479535823016666217
255717429560892490656322056739307664
26638066681369228764941893545870262777
67627073142068897886384954986962449219
8095786979862781942134855420324910211
141185608757855145827902606717197169461
16196411967673061721538729456037563751
6738553730766728433367556257938716158090
570426736046702234398307874839779134283
158570938297900379271111650924458955936
93998382990849022007998972266115624585
53292668338207186414764600464740924796
2290544087651162687135499236653222383141
9183987611688333189021334299914060436147
5387312715341218437697183586367018315647
411665343461139616977337190395183548749
2066631280371364988972025370566211389827
77637964377209974992774818520732020
87854446130191530225497489733572668180
78566284188499569718163614622763480627
107868032771345554082677572231426659
8959830326536545433227494544632198273751
78042712782843456081523702381198312436
484216144766651393921497858115756555787
4028905645208556242265805784238522048011
3138232868165506641041506979438587837742
727641223739882546093967930158745796394
7826612662633338703510004107422825531121
20966915884738047791749442493499438986
2261323930985600527580349274309465226078
9284783369472492529160786311292212333702
828220786501358080804865212423087243583
882101881232055865958566086516765179138
781419039934100187688551445560038648071
3225591376464423778434352699761868811541
8345722157094678462710159779415315735984
4701403277081816219298313701163489394635
617676478950180504570097967814767303148
9861723340806911157781934884710115678043
69173059839282696322746611487248795152
17236550532594618233259888403771416652
816368516414615224881261178649096123171
9813404522409794866755763639218242376634
52342421382976628218322497830885084294
408386821604118013511439819331849248541
8817155095591202797351127119237527792786
584288335292231695621868841928155551111
15546375472296776054126825605991318133
8688111540558723672488445798453554269831
373384783509204235991011296878629646772
214597743714525681856795796224762698338
8618222241446165775433155310324781169408
47851151293565309380929961311870197924
877663383356480410555032164417898133456
971235541274269162766365692357043468910
2114814585991364791330841728692468384
7307857538781865670551885467850713928903
3972586558148967817634507920822295386670
9154628997621230625552126090624580898740
822417855264753176417435898490906556534
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]