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
407913993709676747847204720829052898
87409909320475740424217528598715744614
449237525265851778473494183813671171840
875389625504433268851988917745341619992
763319494294001052921053659362291768
153930001134163790583937715913182847847
31625352992755163596689393691462953675
839482593033156915956555228165560388974
9029780025176172950039647251897670371199
206644636654950186585321726297723578
9196947152185421866122322078659479328897
57967271747923277118524493767166504073
56503237710111877424100732358452562912
848158012709174752661357435604393665502
87639258044543886412829137335439643901
31305716381168677244220614653450523091
265038766843118575126664987264682554
5137846199159588762171384916677373782134
48542385971233307659420545774789309597
44454821421742724108560117540735578752
385380899859653131750584088344955443
185135756184492499657927334808161657044
7909701679931709399851635862991015095167
6492848340661516708560871012984862886989
3613397061633140671975656437989033286293
505537655029360695417579773848098767526
7782458110921351308240299119868539964673
58434378726990811435872475968547857207
457153041027433896946970317762098102642
361063877276538267878149377662994381661
77448549096451023311084102375335936623
8643719945583049726315272245243267564003
38832326630361034329607556256729364779
357417647069604048418575251158041563049
427085231356724695435208387695390224928
476033835961599726682650946962595672372
7391622746705699226741143091482884348274
2979847139032670736415874807739488057
56666070005791855022886902670718819405
598543504047565355766695749874666678500
386125442573035496812325515529533091
407888199333496769686275423748723642516
364718034240591840561723377568519398708
9952721381525539531795406960775692183007
705422171110500641174905434783469103798
7483498088148947597755581300467268173
953970762846958699999176763028649941456
820444723017871247779564522746085205254
3142339132271390765953116118712052225
60031045784195595117215799594868627836
346960793586526335330553638967760654366
2384804985521505540841038859419774954332
67073677659380147538132753705385921878
41666884815294674753653299292348187549
9540349426326440849410617264633931725822
4321185027054500299717297807409814631307
6608151574608795371967771516289832977036
824399177839725176774759061279380454831
376553709401540932388651776262789505019
8476602081088476619272685554844958161
646355915824670308146466377730426825192
94115185906898174498974835840694187746
2756410157133235748981704813664791798
5460126175625083278328262244743661095363
6096168527369734428391085182207130251382
3309425025341261674258528841410460308414
452081714276686958477696686230672554855
7723915474757353130973167079279577161
43235680598443087587181403731754493128
1066262349185451375421543981798872759478
12307469900744568383305878724984222778
2225819981580712195519258988587103977
5869576829196129920670416653466958148567
375135504653263422101185374962324767848
6942227123257823955893923268746343805814
8347428884503677307435986940170577215798
343363877624227992221635091823641989081
466383634839804087558348416982775752688
7551455368228924194915214473903180743037
155189166546803090981732903636277241451
553121226714511145364503098510847821789
3591760672961500485131562123167542796144
4739521547927538896159959684568621714
2973289155081577612148464074135221924450
78073971218477692245835528252490961053
60893456869878441273424872797789899105
932184505262443827461379800119285677438
1483930423520548925204672742944785946
194734722379485216067627144114828114277
90732238686982892321724645619236299411
126567665417818251717002606465562552491
4522170978282691563352301160235541769
87901486212459335279032932154969116891
690643241198868630232392688683884235856
8131600093752212273811273614299756816077
6656205618757396859788933620368523148451
9963688075154456617229279252995257434319
5271285749245653718725483043339095753028
31241247439542950465233552513894703168
1129757891371874842026317153181681157
5185736817310104668300760850529533125
7988486730525122760491139835438091687390
33484016554341929127306347854378223633
7014841513112034823038752717382793884189
8805968351749632632033459142217515948699
630468743394438862051417777973646892476
712615863471572461859557454583656175016
21741722674346808300755536352282579486
428932757717450023451104188723243429176
5278991584224156926773659966988649377806
8307184980307301642912136272504249917727
630818474273690744159424224242057081811
87693474690078737507969672108947119615
4816674577398476928467536337426310076262
94536950806062779657126106952081581203
4789814855695708666670177404237469817673
498811829562789313915095099494455449832
3811703017391741403784225189480850889550
6998387348003711108183857669315585951825
28956627476994595199080590331851269763
535728335823953893918027054674414997831
856673422169083342986079297174998088296
7078860123328970351476978715319023193811
9606726229585259497528418688529734801
7151606445367012710986468857941648473205
51956766378438898260291252101803429103
2093923886982120886813658774924155997
7552404769136028304386217559820250853819
416486466588823422503377916170886307881
141752693909284308898191430138044944233
450845125466435254850610373907183275
9025477549439351183571606926879491305
9950149271887178710725583350238815192977
225585725317872696845208907968507167208
19302872665352329161979934053382171950
9778419438809387771753995251683482407834
2764987788662842160542154470488762426051
386630245998186859473674579519506069197
844453043632907413624695927158166766748
562838536849636106391367004610747678094
145474767276027111851813382882789934516
7717131233645563985599455695579390989193
64716397555436836972409163874444939731
85750593319109832145502139872856157159
194219137174101305073622852596577851635
5352178368644938308542664300909164128354
5375780778658506918070902162613774133629
4209198862389830658983369797341517884569
18348767262161126308990477415921056363
2788846781514635837548466477143467928943
3190537519442526852687704175113780471834
19521224697235984567670927358985825192
5985131868565977745595902187591625901
481623971944925167633835180855957131069
927818792221305444789894842191990649419
896038173512498713567611890944683131397
232177229439563572182669723145264781030
172480949827739655816125973489190845920
508254216119572680783992822982483985793
46932296829821232772739037380534803351
71442100677680847894534212852877736519
814333189945551092500099614937688389
490478573836653869449356931553790455819
8985294285312617974488471748286188549296
3471666380487288716656275267296910409285
345611108547335033502429169812265844400
4696704553463564819529498619413587516235
883776288831124133631501905126565421122
6127567131250156304458571172758843055
79551974192767962512594608165992023826
40269993743486884019133147396312872758
475355398843537471787064754321487228092
3255932264043741642958803804854268047643
874723909011907956115788786596352084178
314773729755571729721081294804575869369
3889387459769033343113834601502055886330
292212221732969799956156081374259236022
373262661951749069065054373335061593482
2704689757946189197869722867375136774596
648267568197120591383046205217988318673
618313128483959315993161405219967454845
279148756141728058201053214363892296746
638086944122485750566858845389142155834
360956537079319293743321476287743315705
5354633055686724200214916330212685837680
354960232842917478686858496311336053813
553661862251574181961473139159614248612
3149399588659741136232416145355726654831
10254336400195389114321935215372228692
959594470985192832941457365402797719940
78954409832718724222305277745128784125
980451031219412687851949230212710977715
955252928118589054210296307705322342
691711709012349316987095462760922008256
217735299325874214168648406530149937770
6611576950465811165667811812364561101827
1819335388678366401112044327427848779387
86563891368083502545930283308042781382
772468555301493386324964586290464213699
4434941833715504515131177732325420414594
7265498946791220497624733148582223312624
4732119638448107582667332686667568338867
733417928610539078841121429230035232444
2173900221046904193665809901334952788116
82381303351911447515277053868148563746
669857564385748946734259755275648419
923574143714699549056928399939152997836
55607117349577955498456432552085165113
1315205633176141255339377677550991157951
778222768416670409057766906572315247274
8416805213674287241527235222787924669622
473325455857942449469739265516356309505
7133783139076194108516942178523559716290
62892171059973827164798946287680771
808590506726191669113739257572776117256
100738916177245390176526105190997890493
393443629860250579076087610496779216623
4550231961365699358390466637576024944298
4916379534511295576138007334699051097361
613248629022297875013164069940144783058
351865489852463988488118785868061701197
13668075920350889709312654398621977549
9116237039519031986932907496852947021585
564934183963504984892714662254699120377
1948630472939575959797426123736015379658
24805889138841133034040919813792418269
9210981753089161851786155803423413306511
8054702167664229474990516638371176464516
16848258484956568022351488223136060491
63419542347684357861528718163232129821
9015235478513972507189782413551871975597
357719599450329823577215755644874078
37768299729537128927732572277661711139
672228504078154302579803725621728837272
3153878138434332425548311483535272048370
636784517538872263549321202224765197
58148664732316082744261818683757591545
3680193886728692296911679360740328609069
43055941548045137286776036895184449
849623613968788688691747417743977483853
7551752297287069683124018231692690879604
8354209533149442368274557976340943547958
491719255363629316104918097161780866
170262048257517025022852557319728664052
192098904153474910808832881512004296578
133799327967701268345211622575081638121
287166918436257061143764853964059271893
4954928285932338218668756490356915089813
311379262445482117489839333975327412129
90034721139828084523412328431421117517
2095518762244467050306938443727362931
464244175269373695964323317763028449159
741275371027980496195113471759679268247
811118711170663449069003400857337425892
928527993194184376893449064277696918407
2161383798899563266869812506516976762276
793375809009322576278211158349119955618
271328512088772196295490681693078843610
7019515026196284765772261057790990033516
3837657085811673317569884350292976506586
2368318161975826308816388642533673063765
963538144896476521445853224789421845811
598477782961797491708747794843575023551
83740497059441585895501988628666925
55508802678087834300856276056085989225
60865238371682178559368991613110934321
261341137663603519854373052741822265656
986147996830833016483339931716484926630
50058788775690238935912618175182163721
326395744567787165887488583120463993226
369896052978999227156954344795734722522
5026207329663591778294024027864166385666
8068809218015398532837545162300658655679
37019354009744146716505810429223802772
507194836445552434927481831701011384081
990296182269529992033931814753066719
696068092152389358184585022920967786708
9032692983719462651282216190133935895577
673635187356347813386757774895157422217
2379439868892599652019907635946567059256
15791212264385468752538719237667722876
642125316159905733843647772164336233235
736040615733185825037337142578510963355
981867979577612735671559097763140002320
376513512662724650120907467234221771849
948435561063777935557269776886030872837
9916223097128744639881516039159472405
8016185237432119760077098503410217022474
20436512302296755934788857716623938908
9398207827064548519128671126941765727381
3779508075316768962314993738122078898916
166962137652248990927684450453565728278
1741956445701177393196472375379171340
576673466833896278356598805299401889801
9816336797185351141766503162625881879193
67545816669341495537153783457575003551
704775149509358833385957471574817935114
53368496864248796046080494941726956279
18725314513276331348228123550702832884
636644714641871890674239640998127208786
4202133623964663387983867941573189724060
8972334841293668175934801355633233245012
387983553794920829866171141938446306404
7797318173816172366695217630083514156
728962281923489182625468259977360223291
1696185919168362770640654926183657298
103216384302849092110391559889013756753
3518290915272174420873751300570038274416
38254678116641530482408907947794792337
293540594192521971653936820720277155334
2670738115303038863585312889241015462635
4634602064192611504254361113549053690
457531369214635693536628585388587116424
7538575710863911262128094514755442843058
1091355181949100593392892770106725412439
8803505115126985426233929446261687484
205913081446262031539918129219015609499
6319470338284336637871742468434904873
881556958964485337562691517770678403297
574740387573948436067308908864453585918
681265721074917623196664393421917632946
5818793353406682720028825452798967187007
2987561371608984715949841085149431867880
889091951276443814803496175644073991730
108348740242465552034561956630140313228
4024567858163694441079623525075462390
570064908532525537387232942939076691096
1815753049857434189999459628470265502506
66479863696720817189702006943631032648
3890820955607322277841797410168331892019
84254577992844055660316687932086459682
76456555185517033422404351125790258664
3352578738126268889289795931553241613554
744849589136817115625729849641678547025
1145952081545811425171329838980580976180
967881914631588498036641766398362182291
3480966886776719768493701124611588964011
1697276669041998458260885267759736621762
6503978061731252620869146588267778767
2872841262918009234911797136440819085976
3824772684987517659454909973896257947122
10118568553032636897360422461316789238
100183221782468550178405812823224559918
168052039786870728411375852630226024378
83093216248928476253012226877165224171
37273414793733624151465469599655904959
807177236582966122751044153137985151643
920015331439637032086835515042763013646
8515835740802167565383024694320138781569
1858220753719361746877247204899847562681
770917645496940729457574253944092614052
762345074619794891518949832285832173991
2669918731195240757669108624135084877122
242472784132302057712761734174229610
8928785147794835529559049340897525639902
438212303547260229235072946195859433119
5001559699512733786144848964871305514
938721485895250414493811145190925487407
9613509056497995127692052173958039879468
5049763215111304754685955676740084358289
285232751287778271606875486753469409974
1625479674686154520621279321350863056987
139095249541944202897981130676949013154
193602939727907981065093463443710132830
3020289540644712155126699908101530395633
1069638997467079492554915203582146218756
2373814046971530845732112926220024374830
8092242261412521316228904164704811271302
4909873455957193903526233422575998748435
93184783128068288385462268461838064362
4702274931842858221413553880619715644963
9655110027842316146843608628405184139836
8539861120424607469057481887774568346044
4161858213409211832385864372532639418677
4483135836435974664364577486447332000
367234329137343357292211512323777703464
18764741699810271001225487179015434559
278476625020491681143872977746713275245
91672046501636791553322859713438102566
30983148330628960782526602331944179973
115530231031308731913735760052671917975
52621716304768183474563996005391970763
90656284466498496562811908271631823
9809507096823377115162011560890046501937
7871604379366253810436747469894621701146
86137564903934346529320531649688138261
6269549737917958116070529148898153645830
2606006837343871877990893616137217818
46626656870593223802257080158197463196
8044467143157922506533834496712014453047
775313444735986880268659203325396829203
131917515652127567819627868179593194584
6630955481367549836094125092793677386427
68401852244663703957668280958701710123
3255193956832115435732815759751223707294
749846536637381291129931921696418316439
24403656126721024440364266386708412113
7650572224655186585764324067294415712
159419070555034235541781883805569568710
8124171781250236811233836585955184032
71364035434660133707059833586383185797
327310658912470377917509703829837446741
7817766234423435333553068818665213425644
303530258631519214352144160512850852730
36382387684791773291555762465999136433
583077283562117281434527597350086467992
2233574968001154615684495164439144865940
533165519465432446511179325617190015340
6812966281781298460944756943370757463487
7811374914032565283613733720916178324614
84271066739638394021285986734751595337
12954977167069526910523294284621088424
5907570187496516811465344107525080379624
2044172792457936252561478854719961296939
198554028315280443852659800236220127005
4784708366297418188248742733575684238429
29818046880474432859947737145938977760
951639624266226700498883727165674259353
411118088945217278313478748793506148
4464389899912140148913958036170876811683
8017564211282747592698776814767841204374
6981493205111505637779167983041861530
99228717781222342229347771293284512724
7093291896105857124404412493481372271
984974848984578984210163913274017331
222592386259471163281563421807515823572
7053520840841613899571912145528634061038
1608702932826555234070653694209015829206
1483699075942870918810238074143759432
615693481904123910403228509894735796382
36575953064248592936582633848937759484
8078296474296199525029206625896411318626
242863480384003247546865194320177384450
780219926887943658302546731378514530956
86421658154027454151483481719771181032
978067109024267551168512540221440899696
1319999530849869490859603387987827826296
4275615388695390117897429807446579614732
8130394713708247892114604925329093619191
478082327129723129318060588394112769450
8853434076961216954965446430339242973908
326824785207283134093395931800955905021
764163525548330381599114874949148891904
876742750405633472083112520902950287517
864759712521212890493892056570549636920
55524664047517375432084673702234249514
683710923117359719673818992033619513139
138728641097806882467053627453305734
6069860346674790264497872700586268873870
9073727079963271130838067108306494283115
187069565911266932951918834276666028021
7366802921131340591988763646273531118311
30130390852108903110033398427264327049
392613133332770698989214739694353312594
9902664913487239797437526610769791111838
385976992404859520478480370398561592460
47562428101867240341236594931398519539
4111395522147176530395145835844051577319
52606936493310112102230248230052459798
9698878984889281412225754690922859028377
71634197221814714106425763875734429369
51092639560856728694145706812539014349
45951827165484483487870774727967626237
7205273082143537622315919630535924465169
9622228391083462498547174133452394466
6077326558868352872624306980127443948839
948344559535414686583223115874616704216
24622595622182393167080658138875902713
5842843119239466686086811859981558568285
134964402210782497127405785812187841071
5188620670314038479598776579944882747040
236420251692245958417116358291685536055
7384275255725301537698153474879264523080
2914288746788440362759387736179493254
869658972170418257059873649140368565614
779777568632601767290306908942853095688
46999755742856200516711825965169502154
5312123191586560471820908395113791763016
377795447446607564922781746259458972
454828855135877276751852906426246751237
41452588167561792528869988310415056684
727719699091493128374069621437560849866
296525038832272597732421569727120973391
1205273425382103858699532483754983433252
6704212692845351511549074016151844849794
77851170564219528081863516138243344880
8595325299617256331362096101243055731
8481229857036707308841047571468310543200
5200854585532148361913938546056623208
7738978076419342163576408612488950763671
7846973522871868303520171955076458932
308171519766510332172474082633335495943
691273957757158249852469765630462438468
59893776972214492090675018821906445
787965722575247166252845085854973943252
6855684188938822667135248699145289277869
4981261126105918942493132001206764813384
3436624241274671671455527467139353857489
919561221558911929635947071865262944370
263971798046666921384789226773043336979
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]