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
728665721561491546760389341400938474492
744280423923157517385587681821319474223
763712662930572978796692254716421937393
768959711746349374146912218522364967759
3247335319592993297096922239962629862523
84054811743352747197179502726097774723
4973961617740412439114135921917894506
985477534484110517784951931628461319986
1099199886919859429338344435273092397864
100541698475207906551842538418613306461
1774645945946886824765312668030195020
3690238392825795479467181315428248997177
3604205968952321101729275510245246512897
5536893512642612374376568854131234679982
4278557742545648267872345716981233711397
372797545443828838256041719231748943746
9493796271658371324476446820147739913620
591560535971139761460918832640755521169
385728319198403455917108398208863911461
8911985136003084123895727359643846727245
1225570589369618359292287788479193026770
8057391910182076235942428892461218921528
452612122218923894229681022550538682294
930971765068237976555992928507144902
6720603818483645641262801721294741856161
5069719817668646630429698277393364976
975252388405441789546816287606753059933
2344804994688518985654675428799051373869
994557641895240478588921221668968795276
2052187573043889432249331837517074364545
2917285420177281759310894710479565993621
101288689828489541331571662807687903821
57790290743415982663162511327890155232
348036129979158257053811578612678105611
150877399208326795146738623320759337946
6460765226559085436232896931811360008033
7924946537143661368570597935115401772
3165412099175820982274773624675851526290
7835913494164269681453219889319138805154
485820288706487598464489810209441485274
20185673714600694896188298539523015083
3683757294999903550564303964450379638881
381718711341786985174559840082717173145
6917858399804818763550663072549391589177
6044270242283737222833981145226434105
4270606599956801802311118870102038682481
219270426032328296984288194502428068261
903485266649497324096119343446557114916
5932704389193078824158711447923110730
668563323577165864182999188587163464745
593795434816680085186094468740012933228
3490667661661803694113273416888877634147
280341341292337885783301971951986935705
9742120287913009464279812885999570859645
860848632153610890169877690829650041207
781082415045322168056175137629209875511
405413795433930793235187932235379969390
8347753993242430678644022586637470416874
26279964701671825593155890270281556074
75952989979065295058987530938995125158
3489139715205787300617919760864764227450
848471654405613770322065165889630399763
9306522135679924112534837465960291343473
205339128338629953691840459047534795305
465254587447012028432824777241269590
8447537366156419874083054033472371909467
61415554329111016299840865681425926830
4233779814672291252938035917899245964
94159184149823890333644616061479861241
3943702173925396897869288983443738552285
645685085055570267761109338115675875537
986653099586282695678054300180483753103
7493773669347816581279665937289453985
179890827449576559825859798266193357022
3514932130795734915320491254834078274516
838752144809237764120379502522893181
4160427455252579816969989796351439722
47005941755441388813394228222879444301
780725541733267530864834011647323888818
16391502894841778358677861905165683
206616792342058879821032156324388014746
8167550653058784210921608970357473107645
151971234667906744189695229422899429659
5589324996337223251777044391077078374
2037722054976088960196891232584840616460
777799608025891656760684410565441033733
3917455762497576574922973275129526468062
656986965253496439897247669219854674519
5008140279484203281590715820631831777319
49836648170246889775044624386448446472
365062549963770167163297391927867842786
2356222893462676224258485990363651626699
30857839524415073698798342596645601
1016970529763839478664142140841698539480
759798127717341477229170279699324364698
3441874494225846489839542407363838859788
1327506995114475887957472321113511933402
40545103492936756746354698345838133088
235793222877707987602924219060618261135
609676799596836295678633615584888203282
811954515684538271848515508663012194916
6862112630295157227882399016744428003666
8025170589566885795738459639709133102316
9874715567963145213270974391428872077722
9047848115792636954082714775326576956190
6570268268927599581175654330880835028702
38762451503527496876305941041579018540
803638859567216273588576887355592078854
931990315359442869317432249457070379515
89658598818861324255286266390981464651
4442823452302077807097497746373035294904
191831585387235143445712540822793418988
8619591567868882599318408404452724646740
1948656051079481861688731642118247328750
30329729692943649241329733844414191431
4402184051148828818835799977166384893
91718995959228426337400512530328816721
7412788995272600470995326693652986395474
99911540161367956219784976587454701323
8357926158252505338984715620415499364399
792757499430246188291717731193967962134
718684878265966677819976956834758987456
8984825147519362556799492235943432
8136443554295403722316967128600664829187
447046798244510860919725497216759798526
86286967673261318109837213903796709332
2091192510141834674499196926464462108038
1341632048224482679986016787123258719916
79842785701802379455910895727204556306
821647231201258220122766286346457651
331147844165612242550566319284848856685
8682747137494583864834218821314435265
904858153044424491530532486930466458895
228571956615176796615402287387786654775
4485855733145496585737715656122177306233
91468428631285055423460616777024997434
236359324801233402789775832518883612659
785284142556225128355549469577160118273
40748546755420963878476276391740281507
938954754040543959788908714718395154840
80319344558232549530422181511403935465
24918959949360986704490898281289064591
6745959427529940217434888487466773264147
6276188551123379633410386512754047456350
921484455147754210939931322416011529619
9577292574199606683319792321475481708157
629669263172130788355264746831196477913
804522649519134132996194675517564942124
9052584610472438238650765835719296521254
315198544365844814319373847930745581372
76096886685489458649437800489342036151
858930109403357812568374619658961105188
642869714397505123588276762624901786989
1588734488322916943337015053946232789229
85858498776872961093312135322233113959
5085592091623759616611528741727030673713
726891729438683535062374250588289087470
378893654570760057605165565185482832787
9042405789752656291848232982337479065806
4732733281886320777550421467366996522077
67301099681544581628933058524078809433
511685235492584483037799612347286463739
767520241738410789632573772585538842501
8184806372297685635292223082677258019640
630611043405810177221273213476291864537
944869982336566998245858123975853466580
708968376180226454439945764136395261025
84886841173880804446942822625682046935
5181247492659925184289498718723185937156
6767927238235405319616313345492760337471
7853208916548159906870254365183143929086
5253499990371495579750326390751583393478
335412915294760525819531908628124297263
9200867132661737666979362150322103355
6899956210932403626767116813967110176930
4148414623236295493404878140046915175
8721281136299708117942136904624173807528
38437054435874290073119004778325081819
494041348387476147814807967736691408310
120936813269160439111002902140767141558
26988677540155508885972856960187652376
5741765240781371536853407865411378947587
86837697697390565280433807512429593590
218113097868629191075252288694682485718
511192695689899407084402345658988627999
3981244484849765958780319338971013893366
757498296017769813911893265223784956461
7977102816260569341883269861167003685
445650219136443287183576494083880649303
2545557232826069598421294086128676132800
5077548393184129527137292110886846144329
610059051185916212862366448391450604058
701156684195342765523457429311885079191
7146198494652746480663246757986875679450
992086362269471915301231395635925828013
2071141420164286340027686610971018197699
750914172868307222954879393594299699209
896640955469971489821378511460442405228
643527928885994961088533982243663008510
61174871352026191213236798764479674091
31256927659555647852287647800573266829
8828294921497094327579734218826931593879
1679616488105126254449639829856613694527
3229867462112339470476744325666151382584
129093207028747558360685534137892808176
3669886273489216406017085936601554575307
80818700526586248399112552575781092280
1828837927449653289155608311899063843580
297935391377983179287465132781092186350
4870640332357577584514333612648662803
316267251222341431159396265121861192883
340088176628143109844671951503918998
653162353144526766310528750387846496188
7669269958409273170933328031205011956866
7940481792516125427627824547911631929024
762171710108796123918263604997354015422
2769869344297956248031217899707347705055
497310591624810514347121395607623678043
585720151342655645115658461647752521921
429767794994422454698316113174189494169
5756769114581746878789685264686124276081
2767195292376648702960859983598565384103
29701495463041751113108755816063543463
7532752690229628376046289849512611899780
5595133238635092551964531417812476571
9621189725949696878959208193545169121182
3406118720251942782383946345903581781629
639550303313189986842865397887652661474
4116850496281496928330123183143119274456
222220795089220070129770896586422508403
8924994897576525896671804721543744760
741832859422825783237400457267428708307
67936912936525919909225725669512526399
9070950683865189596721325354254550382432
59778872436379117279489274350050465417
5162102790651594403849375136693592007622
232620527168819773294404208528172067290
92788005132947722371239668057792688238
45713517246186855331173958216236233
2054239220328154608498899104311449968253
70016757702622888751041412893133535205
3308552495258856749326912337509477477459
5946521036976366602486036228572439743595
683137253566234384324846986130852923825
583330750864742505724931302260265673234
1843380181424436960563215979683858422396
9378910384292411417416231738480662247803
205862166510427925188117326996123199834
2883954485613220641659005907117842946
90056987446160151223561386801492931519
968429038470765586633587011800336338354
5536575725794515197914181482587166891948
9218764191229490125729962861993836449
54995646322515029640719658788677199440
9278225703983207945256357062019327941
3883456661439251711376287049389955904709
79136549066622159358436414179771614819
382553482691066649140664792516620588796
877385444519121253891747705364999151922
743939691049947922034735253431053775458
314155408284528765451558419865868325492
383278763996370597989057973778990613434
1275363985234928435612307261465944023488
6285116163683947182253533723399405679
23685041185034385603200368177143257652
554799061408742843439175479357919333315
8625213046997940847232332996166762087366
227088045415850198369416387380186341886
22775146705757180459203733872938432342
278542796065797684958531737328787262639
60795426308880546147469127420262303719
74374038676494902385733522465346865926
5153586948908818239133802574877023171624
5702676559453155312359335291993151465809
343533471060368918832138624964791887253
724266796030869557417095267059179453025
757710942350700828123404880182490198579
532696976553629798267596884699161246188
4257704787382489340282101222152356099316
864976747890790870176731749406228991128
877647517592443541671447376313115995133
810740834025837139819982936184695336
671792468803361927515166969982194623530
442584342157613346171973584379970652017
7731510632732807950526491631414562117274
3039955645240115793860667419182547639
56423573402331726791082156288213981041
1249363595818711382151805935467848827
57373255451342427298509417724905242274
366548808914618894628414110702688142347
4808678722944098667935427239738165458691
909784591986875269604814889216257382964
8951514644165026829561176567676513099311
972591643557244984974360495073877748834
114868852659780564735246153799456722271
99375705638679362394969098632899177865
246781343901454384138898049766327387260
28279291175670437713034409115854293003
7564839688216793481438328767979496505004
7683580692353584760163158797755849017623
830812103033177665869686157342361264122
6361852361621435604938439474591144652261
111951148537415762692308968834784687
9435304633487531615139434289361310181490
9210568461973354838425188557917328806638
1707334539265695408024447343674387931474
9510131261784268121683251063881425096547
320611115398304390217144633151549612069
1160744931045280025583381581531354447
191213065851596185344601608951264843677
917986015366419846182216188697610421468
921477446437585786189394186189517897685
39287808437767873019572859164021655899
884515594217993968986125293802870157411
1748809987429481691269531485802565903
755091388322904664221734943901635296494
56629961793500866901045733764157167220
4051580033557808375261018868886183822981
73991764150596183208150954349091345650
366315002356483502974761369426940184215
5390997818811852710126156554332151605078
7634100183163929560552513332598336324
89895058897692221008141650633656546841
51874103138506884320956662976264898980
2521487969695531320002118172834206595
6049845463814312816015036378268456043275
1794698958432772276368303458719714039236
6645743929658404863063338482354129859981
8024559829858477376907780827251718055
1284626486649200142618694638442357731817
2455497210531975430472072754705881191340
175729521502909740660786554751954584
7651345842845279609741133656576761117
4808480298439364879161723247835176176500
417354259292852261264450268932847778730
7449550860453283379062936577359911241836
84334634364455618196606693171764823508
2562480331913823206240054776344786832701
5369262241045395214864459020475750002553
68093267425040176240679397847642527809
5730286218677707322380664381776849887371
4139188256155161515876733276726159763388
1289923354337664402250969126698853651843
321117093059944948315585865938024363214
84118626260734235764406251555801196339
52975874231127248755272287667131006404
2364422965158346294005529150226468510
84673578791297999904560599931105913839
11189207399808341154303168711002792408
1116177472522998931810787257366234085005
221516156566230250363561169801995643596
7241688066775105303175207728115383292649
123086774129721775072607056182929258321
14193983833221867142273612217425377392
329292125154601263722082976291554332389
9955731188904493258134327770555143377518
9286834618656444741535002527869371295070
320523449430905326719458515695041822476
448641744764225275984640560756774235237
2628700875246851126074991714200087479294
2965981535125579236717079197912896322954
8093608252676926610811841118205492558402
178131703072632533024986361358341099284
624289134171500682174716516258167393576
30937525270128289432736683190446795349
68753705340611725821332934917293371305
9974563862598082207038884963286469539307
1399749528263408688382007140100173897860
471163968239578270593180147684765433106
2375443453025204373354617412263856113960
429725694567411090813018693882718064971
22699427672643745272915757109326075236
9194552119785405105991712149135499231405
16154171354229945752693466413153553140
8755790623408370255940319835495417673280
5772515218558752169122158648695678055628
9705555058378881103970758103186914962701
117582401205796414517993023405462047109
2873900658374568218733144012762029594015
2443491902120960006757923875148484913
51454019408736333965364564338747415
4584755820252950484311465328676122938110
51913194451346919731500177444665172339
3698511710149191725424384610392289037894
878638661395731551357675932485372321
203659743478248961189748430087182002380
64939709775808125777779250938249641390
9357908074642539594892706271313243149384
3194628829257829212090904679978132802182
377065256269692927382583270612426289716
30065700677761792911434713303835767475
81867363726872623419297754952248767123
86484831111573628814465189172411464853
3424654902438999187777330362270597811
7806347484433416389584226994592510836393
531768446992324342184414448465262871788
60985815925081888634243744999771369009
43299645147986676982977658571897633100
1473891919443162983526872189466584359265
6553535266086983813331855831201511783721
9140823341972193687475604246909328151245
3432291961115455223346226549659624708
2065307242725610925538435435209012674938
881658895891084720851087407600187453442
123189561053259788013047302998267155026
6042673197578410303536072479171345498225
5611035182249254861972749202591913647
745393461347738026596131742821331023855
8105850783742636615824337321852076925878
60282500563424117285727872549754328315
32060878013708112381360344541784511991
7128794340761487476731867877184812738644
454697666152876423329999707137758883231
6475693433337981193534592987773969579113
918412667039419327819407751213150257556
910844622336265793079197614227148045253
3773879947289629505035901407802764518967
7403637453829678264175623982748082579486
190887831198852357595588469737540733135
65512888483967898806126295417997863954
623920796785632793150928201798081489529
46753613982228574760317952703825489
3662194810887204361835209656352048507201
461436015875796231912841120514679761782
717745322808456594215351028096979182
505885734689423186882706666191899448351
8602197236533380366231651206558853982435
267466963734226200874244920895944071072
903384565487501583744358323932219346825
741585510198042167072017578710964878470
423191889943990130176913816310990468859
7648410833188157538173681227913236937196
620215413527119245086642161934020472726
497457921128036569794648123165080226317
228798041206927884346161145756357405688
599997221601833095516677438656158638167
192886927973759790091723431191474884824
150563947514722702175782012967252216139
903699071672454135039191429483033975791
469235529955247698490552325536181693657
68998881198974564307394985598561713143
33279446576076818172436162844135735949
115516970876686449061337696967836063544
964968817686790608860774097168757216550
918253425595864323741663594816419548538
7913232323406563604555027250101570107927
289613222851992692189780805045825125199
1513739444644553899934968829062547617
6205483048697347179452961155550199366271
6827569164029479471595663213805019442911
7142523294184142804789218181265246443682
257724855874682179238719616192351869783
18683871757756059008506486701783189578
1513819961809995762091945643162074049
2398134747087727409962252644464636143921
8000655259658691226832075258776946682132
5688122117827283905470151219928094801399
883978564503485795346380557315614552989
708353749897872257332494640999997566530
1632947641982542362316154867818488877796
5759546822077141257838810772381405693
114974316472400478501356714444970338854
9490587396376444709582704778602299773814
808481388690610928046900921074569177913
3311843140343673335369662158707698587914
9227385626859625415390953568326883929095
8253701360393426303982613725882452978182
963898678929249764704256911214206673973
1157288130101433219132795277430647044
5773100765576339626886915542915422214986
73447124921958855838477357838157461815
733154571856734941730166916961093459474
387241253673337343791702960949032829779
277097932088893940509242292148415298516
475494293086562226373665140317028705671
8158635779681114123881777263421169933572
4383386497122868457790311082914918468808
3759268574166453950161846014324124635515
3533351178483987664312626065900477481108
35537501436339186156973165578573246
954387782948818571854395827624091401633
390131865681736354978731997627850378745
4643644583383698552181505136666092765070
45511465649642541287941769212651382006
1395593440175449654071091773333704913
9582459629593646374863579010277945481542
499744769527782973549386209701426535442
6651497076284597350697727888504240998075
22651659917525594499944030598612747266
51892549518171415236954851185016273321
196381757695976798793949181020107493582
588630194625384474299186639343615507988
696348901740561214049059717976654762454
410582740774723100483154956146438354148
747640718204928941027755509512446459562
6787460486286589392889403831469644363416
419796284988205661392442207816095241548
8866352913037898216870007138963210919751
47205680751567365349246332333882647166
3108306363837754591266091740140019788606
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]