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
317619055731365046546521436198496781452
6425193156383045798087733982532837073973
210034463784459989175756047183474221952
3588814412902523692630433955355910165640
137126618476177764166999548870738902512
406423216310783955176807681494084609511
86048907486439348558622265753757457907
8148261735334476125583238262603824444677
114334875088452299114427220107082761854
94739283271590703069390702174243456664
87138411170363657524205042766377564705
648016318628131328254953386874593541057
659961694851321951279927611134825109061
892587607565942275173816221732324139228
795073377582154978630242870562278414180
5104785020935066899778133669109326740
479850339039411672731946544818166361758
4605807278741317764546957172115918417553
15231314486947359013566933406149241630
32579183380799147125619786829171793122
1832695873915998656471899427083383004
672822312395867479195045999916645989863
610969289303755408305079319739297375
7879248471641724833918513716609023386614
4060685392282884700442786637943190647042
622839895356727348848922431271961737384
4962148614308871243548699215865663973141
1213727523851765258581614809764539959982
1472151716421268413575344587945139716527
376820121131479151293459852534601703195
138052646449526854437107308239425348382
2714622625497452124967702484520396056409
54541679808393673899726160134898587282
650966404770519996204984421174434465126
6401058104496923721032107297210832390
41845450537532166288394618147085889303
678092068618005284499544328683377192658
2055314548758067943918359599154490038402
14346788789570908835801481488791699550
615563542942842567553957394615124083846
448780794084981515689996440206032310
151251852561214554011766482678344986619
828885385278307264338572320703096818230
6973136860525543581613982133766364351126
7977445914623520916089564328692238943215
510823645920991039654478803952630679218
35267629330071704542793153624246673920
1071748723205339730115416405858611659568
36708188222681698335833783607060652703
725276469010170250197006731844351166456
1686139646299245622023102161957794317027
216086973855483027882611333451675103718
8947325051962445893720776966704787823816
33377854065128732463196844958858822861
541179406906999426455899184380282689773
4554787133368002414255655209768949204897
7181430520653246540133422753218065621691
72372373209911929684148272134054568218
203980959682891111540713499993680245398
9433490166354260218082291240414648866680
7214828299147115900789252983230452506398
3648399983892861935854612546720455811
114682867820388346896978228689343132542
8645557772136639302587567415404748313977
187056885257622824873699241874747087075
464512481935390254319131262310441647864
62016062670939458722777623469421065029
993687016011484750965383630326313556138
6556794065834807420691252132620784366650
441372652157192461664603400920082156798
9829325188491059557149876410346229596018
7247185613804371523851493483974910132635
9769640959596370128448511803849053120
530934754901858873571844242123456508145
128638273733725257810043549940162574405
386216147989368167129969160898464172566
85636119362976310832817003483727541272
8982132856001169619089595743690299161044
6047620450423287868062251095880897855555
4996129361831895803592226897814890708622
19122462344741952115023961627239351
990975362189407653839171817346632122892
372860127587168055296844407444003606385
3073881225139469920555724194114191371924
9296170129952013261231391937384367214355
1512876743055161400499771303953130249
1832159697374063801152731463299887531616
20747659894342857511763251457271251977
45644248727590492757302543702248747570
4658863873705055412689625240938870319559
4752689859753402608828716749552684263471
919222968883207165814176951203401128
1279807941197313751193118696839224516939
3147233024116224971672852265408483211698
3019499546807619244487209754256916279200
8319324729743992233776444363797276125852
7180452071498421350786142039967698737025
152033074595273932993552083469037131655
656801620335828251280385407305243277109
36346495923954506271881545463034015468
423093094337538307075009095683751851806
40026991585476176534063091812202153
3922355832111282698625766900376632996775
515092008851943891011915937776214533262
3974270852733984217867315940119677639334
707378747612257259622744084683684979351
706511491284053068079532834832333121
446145819689342514794894195802679552566
2233390931115152818481322792453089498
102444238814826855321300697534413814713
610974808084658862574334728972060226501
1098720443466692537334671719236157316
1859911541578265624612064553850073974485
196660926059272624229198926181067054492
1138690667681582109926149377838246592718
164985782464976382189084312180995481278
8218440414106454407433677736515431089071
1631763030626121112769733520651995967169
5438778440423590466920983567361369402834
7408212177019893531812982888821168237124
5767569674639553625392891325256674984161
52073859138391422571037905674719726579
15348363996883293338561865527991291875
9437828374603222877636106644947190631905
4564540971207979205668775100832898154592
2050438540776484565167178548466977739674
6079236143195259514926809084996488604839
4672792027572357313774068657837438874091
7966811366069774977262057321266395917557
5873623986207195485546949462316069182229
8821326798727945042589745545898229870
698827986825448950234253592605235077741
404833433805553378532506941210584675788
712251467742386680881994698424624369377
260343794306935653829717281387665411824
6147543786086310165666967037220965701807
5042825360044277402394586953561260395
55187196829228437594202703355380832115
2956785399313862201274404812824538771224
5810576968324785368947069906684651466334
6805159390904444549038161095378267202940
96391817227375464041943239049872477694
851422677762465212668768198917667719512
878211697949325356950848053254142623584
3633105047858723495657928383433501795
173054299303987385104691907770724786105
191439228195499680428713703672865737334
9080143260257172108171655721363971725105
23817313964366902228394187346899518838
75517536627424311222468994572283711089
9022306250394709193155256293725254067852
8791313677181516844564372116448479937646
9700248022149552368605725168638999116
206733776483260492421876441219661713606
1826460224735882634634425318823585887279
82805648208678496734859365518675396168
502246681697130444296083826058833966959
56641371342842684338996242427347151022
871325823363213167911893110613812817
920314157632468032828410195725554949764
90893805588087902729278753448470711952
864990732507786318907195958352158360203
3038547538499092626129164415307863086801
486619371453591319134466302483256672192
76026835694786372495328075196278351
60966431726725842425146285334493074936
890284612362716587096097345712653331083
73837349627357355266662743839619611989
789741746089937437977267508731770636327
4401668694927085318530435173181919727209
5144437620875917430227193928516946799534
4723514641004324556759455709821477281161
263191896655710241252452389259752003738
683376849987724029142510388912332939212
514287007512511256517670909791985167596
592161462929362825139734490634021583098
2629671168219536337661913152951287382455
1404749548408724889023904095530553523837
9460719398277182581495096131524518826299
2385403481664256107387492254817885733269
7063851456912581857191138232820312941257
6004698344827195477716049817810317732138
286549515053935215714212214784515384765
1320970622387857884126045136448596073371
1001230918537402160211263856203298397295
2953866238457423871666286665832950708689
68354889604523094189638127680412329921
2102222144474444876671554056811866864159
225360441238474898632304682976582262955
9267206435376966339574547736154048613032
15091288438171111019286136975014033558
4009645140112256512246316235234662385128
5581293253858714905632762515156214287417
8385992733601824848244399840398988193
589347717347842739057197437111779632884
566371579042507224447936638669652933245
7112877975217264885246313750528225895765
98122945205071505029119189977458138681
29166931343720751415486418283779942730
1188758035718949547191974842581576545850
119558595679308923459958430875983130495
8012236228582889305463689072564275261
4002696474015511160466329573649792627473
5009402993001889239031988538548251601433
790667168758510923352203409381687769193
4835274946181596190657805732181995432489
851017188432667841818352698780024993084
10923147112538999928671789554477619040
4007703418996409908158210870136057278
31275343711520483597142231654968244738
974834247281097353618394980287310367111
39163079364842808557786139213335287434
482047949014260099187305673733844029230
425575472035980478864782419950965274220
4231429732557380206920282965111431812574
95559237267740023414738546215777713764
728393623190825281030518202933679976367
4616843241102769224875759814321338103344
81015337909061816967267060504363159119
1749332446557225369317376964914056851764
88876612637220694992483626884891904818
49042149091321944854227731634794095632
4906847342105866657746561947427655866
6283528686196985341234236001326263466370
720736760226628902467604602768695635943
263088221865582946211613718812179812315
3585205911587188584744785727317261766302
949683680624801371924701455841836874073
48060217961282397707327714141132955167
5465596438605811987716839569428842583
911074412477724395589708230490235399
60240396108491039743358807710232275081
4983571642543779713433257947201767974718
80318637125345372039597712379198939616
3008439927887645314957914149902378225055
613779656075399411771349388461292852691
1203248645862157403525388601264887363709
559387005620405713263378174210272504946
422519175010549761032405579618021129239
625324521543290214023162334125528859615
611955015106883951858544588153714669128
849244951377875096745116216256785151051
658749184031275455743937852621734381738
337837039054374929048463719234765166
7471954145257217516587267874581373744
9167433185756011477876923859951810141342
590571813704592520562289982780274379008
7802741456336678644081018638668713844219
519164751324281379270391971258198578878
977464763026952997636300706786217936532
2061428685038687661069114783755222014346
682926777420650659453659555010033623732
675280484314651395577206677927162234422
33573117911669119907142157868617474657
4935102142982069783521827375156436287081
9273649587311573190439639079302376657033
5356684533176498263418685918167954009535
8057113517642315182230197013324473213503
4081179153553707658869438221822648439068
6955220724622815317136982228549818726917
959192917752792882316746753770739911857
602549032197528571294624811354396706405
885430633094624239666579392929238784188
5470195467733819872564738996253673882627
8990831364867764252772038261719561144636
269919508049261189529484841801255799485
95958671874061209882729728125883599753
2275414733397971712562216889301222010
9240462470623219434093847886797241943551
9722645050937721683183518416739028836226
102357937044509252787636018513770902584
1917405437009730163592131517627546222076
228856341909882495284298208982619633246
671287294715874215757863968949251089127
88655521750212267154962085502159175549
8309892035795816477979543665321955562806
696466849173648034101835995058914307602
5789677539343016749192957858841246859643
4173237032709100265246837165326028316983
3094763626472552424518832645695249903843
3152406398572857108866955226407327769
96737063687456990028923274663002936655
4630580844347455710093694440777380988223
1167231246605110249749522065195165201254
9783197856231319297740946763037611721
4892418798096921102029884732398760973218
423294689928833575482249794909548609
436838691086165744253776331126528317865
3714763885534277231784551437199723761
8196567821353945458808333480314694824
49868372363248596871173106299693834872
2018338089043809835474592311288556349
837370792837794769041996966862772553192
500782054542389156837935673167488205332
7117210162873719961856271482679528951087
47194923594636789772974659876710266959
13095858608534599087123442128515168485
56667739226054863371231376831462105531
1631374354720423584963880891929426361
527982775061265514265741482850701504664
551264425843005577989366182965167390
6458496136693123314963355355922047603404
523854703878784146308090178494131913107
1070703317292207625961334757195232303846
2935374543307460148155219601962126584525
96154317493695527002018887540346305628
12051177080579913686361964308694577093
9939408498255111647421937204810460367164
616938947549393322069414026400130688293
7918817150493030115521694726367726035577
1069656726618417370664016344408886802352
6510508589854799361557339899817222832317
7805485054704293182765388385777633666690
448182792287321930867169504518312795605
5420696458879297833454429808247366738329
991536216429188049289961480449983388778
945256179641026589269613539938734301994
94055572859705919423971841645479211
8371221268117874654183504267718472722925
540806712537153178329127675261674496854
47209793462674785654004496587110358209
3899624775261892701783099061754053445971
80026754211060250273141621505911619194
7299249352705192536912338821537686006851
185653328831910210212878415821782781994
75827337512445317453139937804394289825
3347378742737997172644441935329559182332
310667536039251778866634929528332553498
8511565866983948246233554373852432888
4230239462552123859131909124264026674698
387037888912940312798363504514439903616
29847558837253315532351739167928351958
6910318729416062883796899725400125749247
2625807736113914608387793423161353854
524444717237190524549760980521346798773
7777923118559578396913714047815522369901
71737748828570125973673472826123091951
869440666128365509846846418344975028437
1579427361776571908349596860558137355317
5271502417893173404581345440112394116745
520674383567754344267781498217167738294
220677906191132847882735403459929647433
5116216911481475342582322519383748122887
59722395978438194647116400436494825225
9984948252309150886541950637146609822
4780745029466666169942307103799417667370
109118177901193190994841676146587442824
9540163171784674494792828872140729176347
54106230290761935645759080215743169428
54505280463670228433301469562196569
356462709806313458025785625826697221775
522743025577964899652112282522812137796
711986618116157408914317938217984448
3960647412733584183036578158493740219360
781073659981388141105358940436747622609
7201384397193879603155005139871256717513
7573255505089994723954751178353075750
999701839266875324037786388061669376
847230916969545990316158255621631729
34065842260748705252218681946189299582
25415948109139323876746200168592619087
43754391558684491003759944065731594317
62197918221519487538047422891044026214
2575236661677889433948517314679783936238
7315699512791334734953199561125576183059
1089526317566485906994068227605043053080
56421521178262399531991707290820171117
2997920616458590889929123491667696186373
134518903646465174690169600999590947156
310041362461704711352416526318685669015
54202697355888667814262257984740553487
7548633882888648342852409095979015616162
945042971684384925859379598577020842664
5074315362237158352676085071850128706014
74056593164989953512745649948238413689
31397682961136790141861210972381777363
34754682190808951643184549255771138956
6751305253035078931586041721899518165970
528352309057324912169022832214254163471
83087636111529167952152512186841513289
8372914090213280966831436977496726873482
996792537856949836243895911941623052
573085511132438720431898222345621887476
306715239749863510548234127509096422991
772644333415780626899701878540411508748
9756663457372150228730343093514686656516
4492603249334664588774863391403421931685
46137324732452552581080934453234822669
22187314389244863123284220596521911136
833641638500484386377617521020275985664
666763123903875695737103175724067554425
97651285115713511367896939416288430
322592375154263254073676142654544872390
32433216027540447148292834469066342247
7824351534661822502696236104723933930
887927451745689105076923671290655906705
718816684721966412489068851133820669294
275793133956350930092075557090295890331
68788558628368360758274918930876835357
34406100429973256436437358586156040207
6118678892733246406913273797402170659043
702381935179894348329608108502127596139
5723285999314903711825566520367690653594
130913597965864696392548841252578491640
2192550965757355731540042262287123486203
6434259090718799195240852622112257765768
4852678278104913929854227086141158907135
605583397886148213339778174175636439781
67642442655471410080675757589778384988
7865681261046257811153712679868369746299
8504636922312176720506091373747364
5812554611981588625410975259222656743
2599185464684124583895207938792548925223
410492186140290942824718272313554983307
3586702571984244198648012435961676937154
669884438055830595591135859124740563802
36228120936378211517822013384371143319
5185169932889647025094330215322526141
98578237440333364045346376549253061720
75487310160983242657748129088282942086
943271587205304523862547815757664436720
7171515375252370148471518160764721943158
911128257775634182182535512810053433127
7481209443377403502183064885708228476124
2240152057937224154202851433826716704
1170823821425456183950239581222848553011
9030225670983560404555762635535613367011
6660184223109913789979103903586627816624
46998084240241279498077671116086017255
853315096286590118769024482995988437044
36359678461537173113632708693911821529
306445977563778687658304363314679679968
96797996324290831872778869102099536412
8701521315309844157228705537786786655929
3864985315975004413312019011817145648204
4213898559259029222561149777873061799
302477464135679213975999109150479478843
1086279721352284641058114737861314852
295175753999513425879217424263890986193
81512580858854801539969237213058113157
581272818571450975197073660928081573841
6987041594440680051583949432355314422
6685033135295219000746247316960114957
354731207725807062745781304278544198611
303918748715723706588868982270025348538
5793647279759061685216653823381456118802
6167991945247216986081081472974338510
6478216010769553747282439057733337087613
4918604369456782306228763735291967312
42652141236505864338363638366083544
3098845497476020285420228883123540884010
42599082663863272871935656553178743468
75461264674477669904693722207516451740
3433924447165465936519701313852749995
6351757624588312670657694538149844767787
91037147385749923289129322065577159700
280366818315559927051553997435449847
625371362562596340242527274860918837213
5279100482635522160631094237728961369390
4625702594083475042715555749337234898
49339188987538893304288511761822594443
323718089713895019986190927352474266003
6747186124577474258824731746997949555206
8790975870386173226615894523734661317630
50905698929499597263413771939169982235
4950347985307918221392501903151317123978
9492940040629224177362546746447622653935
4428611819806591468398137218309676273855
767266338151693973821208464186592422902
149356449697221211949772190835925835426
4147771844179766855838484221902749788715
825425678855611926344189726483919363080
56719142012477485299131284106135091970
6449138579603988345036695808218259567608
6061271983713565990638435311752932949962
3736576117295348190957578363927522173073
606391752219171159459688814925874474582
704661694680874774989251384714679483600
68231823139851058939188188766047152314
163070052556836870862454207708927299434
50092657415388573431449105546231615483
65761950950176374652995818773209713360
672660847275480941222403373979618215901
92506188283360592421244397392433673772
738388727654561439944277984829267581
650332709050607829723625262386454719
607820017776217343026310744986496848791
6157196719935177687659019708169291639893
124999897907109515463939984038863167881
564665998513598453918134589336110659877
7617896267439249946233669191122332329938
355526995303382479428814479145225986586
85859977123613657703790799706435233455
2851943873796295895725643559711389604894
6018698733705675628140943883359043049055
87348299227466082775803783734544034470
7430470183731485925969914082553162722949
8139665970983277255374748120303955631086
4417546351894709318088925306575762805
1145919775854118159514871542653522695421
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]