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
73129454906337343303259573210321419221
3008208699833289158741322406880047614045
4280137335484980273094344982393559992968
39056628376395875149397314341889299357
3619859134179095342377613254392443638094
8967844623919281724994254532751924522099
166950374049330553425304730137726103407
593950107418494871613805694742723297641
9815256319768692431920538582546611144991
349955975675585325482523661757685769960
57414210444789057348762924511089304848
9061444785623236590833512158778755797
33773951634935911564292881951419967421
760925756839646536238591468511343733201
954142368490123162678462486757974872050
437582767113809551440772457173857178507
9780788524086271792494926761709046166595
444981504715528131038655321811966196878
11104534405222217657002827588317307205
59669120276167513137902476365682622092
380265377341433370190852112157217519
21923773155335019047800314914786206476
12511355244089858351659924698589889261
32068578317275195349896461425303743251
439082027285615512743678901218654795832
88075625785365231866443896575358113603
3107372681648136534828667358707171803047
861812575757937667079635870848386251123
66336951617721324243607162134987864759
447944265042247183735544905733966562459
12199818860428498402219524775452112420
54964999818473599695611917972360212563
1476198666269119486137484826753772936384
9339742098579748159556688587424462965
4515935700611511751366639337541932274
7033224695437157586327880959209347839
9082519778998320257533023571494515017904
518050596271606636212446696323119124940
659756876529724153953339494373962749
264943337737749138549875436842245784509
6486882587386189282977886075830820475487
9970849790255741047452635543395603347
8009731219484987787941499005135483608515
92079856889142975066109008876417333498
7216813154181391534757763005478354816831
430078462933287637461815387110158733831
4310530256683426313472837066857686271131
504327762253675196378886365105176419967
5438687663211099433596381380363253103164
5756955271526679417684845249399357329
2992819296269084207375865984712199886
940317594588954152929367420362521785205
205186652856501525840532782156791833993
61181156879584113325806850104415314132
4226458929865853378451213469463254331578
1905492179558560933388078636579997319023
71831055949745326478596661695585996238
58341845186819429813837866846084196907
252792674963764273095477147917346756502
615637269289976413966774344203734839359
61300893891957247165835365699044975970
3129411412367404752435982882295344283179
7769561926263009216029356035190114534
8851239727889363568648704483166027418281
691442317640380637653426221816554866244
68053173163999756126191765360956629
4466278840593020145351204662375067877174
300393309636665953966028580611361361539
736774169362958521971644450671219712323
241863577834747860114809595468674297967
9026630660536067019831751000054397142
8180402723598349946748549640342934574140
213545573913790321248863450119618103435
24356982818251774957422165786715439294
4455174697256782207593909159329551917837
4980178867306817790397336067993479053932
552619440479258247998197550171689232366
463089444674825252684647744358235334919
4788709765072525883925997497902771493277
92883349427437030980399150235650591346
49098373242217854151867547543457473555
3816690195725273784191869950238577865403
1503301758066660862776734924815350537811
38873454735569916204802386625785903375
7738680584506012310186342490467775799055
4144162767742215157953869178728578797513
2191936471235022902959573063722225493301
183075171053642367206008386395890482474
585962390323752422564881989319567609880
60577940385745038743689999732634395409
8044383995258455826581183793909491429238
6316518395077067558289099216575020264127
218325477225159870841638602532324492266
79224994742549367778816257828973487122
9725413117289720726350546053318723518199
96756949394232722387674212648124576684
9555907344134730665038994442791024488200
554042754433531289147164429630561467083
880477465737110456871326537818357099475
89607272482371904855519124061774803016
7233558230962258968724098727978266567597
97533073925739164121474675341791318140
93664731890644743403666486759993882084
985029312778962280414445532366113767601
485517235398551293431439197035427246593
20783087329670409649957833811294851067
1698353819169710199777388059645557635465
834954226751983510363627326263642493497
979935245783639995493098140105250283256
7453402282078988571846864827750186589288
1175612311002013664646359261711863607417
3649491596319086867175773472260891857369
145035724995893827441778893675758952
951966423221039466989063113409469543508
7563534699954704148736122451355185751
89199136954662216053803756510220133772
6696189766182943202478508245411378078775
130023927915708451717264408463785764710
9488846444237677336887354300989219187293
8270841591028058147748172130403731505023
354544519151076742631006295875549258252
807629284184628911658699146394833443754
9192347235807820337853823256338061855973
51553438470248594387747771259459743656
7066929021984161723730941997908533626262
229087165945474405691582664340571538719
9388778882901671335741141770713814734940
3761766097262542838598248405211062379658
57324949527884265763540789049293780
40397318965332417914448506882510231319
361294699234511688264304785479954935673
14674317392185621041574294478977314975
95067777600336939568207296714839383814
5317610658734965675882101593211432939960
839292314743142427872849514905881351046
54501992786886998849906534446168013604
20853709031591931001119257082229258435
6364751375103717564548692707261369447412
8594575379362497201670397503679858872
8527565777041692184381624343771548029975
45852442204566580567199874458053531871
4109994816708594770238184202160353283461
1539734406970034995583631834518366475
430697963044386165947048150244081242710
8200529220866054991381134880755169657242
468598347270137104275735166688177
236717049584472019456713990223540843549
8759726128726331331851103093271481288499
1076589430898705528589909255884055125945
395588279784625426021263992119644424641
5564873833304302851938101630146653349810
974644287198859816787794374828623612366
52189386378798212927754551344713842333
48783403686360161128163343213962703239
662076652679707234218733839881088593582
932589613393142603560222837239012518257
552041642627191738479235377929756246537
659643145042358485431927790176393901
218284905903190273692415340716246536695
2693725921499295322262148814671922915570
4299696819718944134455169967171538802469
3107451402615136961588567104921034613
21119089947092837856751626901099903943
7034344097729362442454042283655564327923
847856866167964376156339101445737234
215071157769646013105618519054571
9228679885453899146672822157015753770
729695497336941678979341012972110144491
9790492225415069122462894647667063328827
55075313004961964154346774402516266634
3167119070091415160546593168814330078367
794241997562807613614602548371776367329
39079148975550955821012022537441997115
5667590111966186498389865718938371418348
6254779630731937734884422032287184397154
96632613538807652554536169621479552087
3670292185382843202562856127180463726028
5633307725087756747848513098037583445
90793222499554426932059850235757720210
49443233650190564633834618179161721358
5125163938561920368615057713226196149897
5399455373329268172121121724623649198675
475054864368791757012699138128138482277
14211283700163166744115782637253462641
159643879239884950072490119170195623799
83729564796330247049661367160849248388
8634671134824404107128269535845513376
429535149706550172029592178613152671598
6425822013572777585894275402193112746139
7165309152471628647551591552996779756505
3220158048811739166120925322666074947250
356476353217155853961826104181950124243
443960137291492128242269923981813741014
1043471638848404649826446598857394825
155546289125849580736536469450756129009
16431509589647316911898817235218896981
557589107777714464821452537862048905671
83696308752778822262318984931526488452
2265835959852715340236028999960270757423
7911814046686910188135467990620671233315
81874992973236124750118458778308234359
7698888225242674956096262187595439101228
99659793633736366674135437728152998368
552085826413598838191436921274359780422
333524283144986833787392476931042909344
19915216497943036766516886137971167022
288119953823504599006185370319274765544
4889298021702168652770563858080341840
6978729183075004076801267778325769764
17275459999826362268006724452063089336
771867523536753329913076429637584198622
98045392100009425961870405005932810715417
553552734648520835533461096417773599713
1560836124652548598934151894943869378416
828560185741264133016148913746792945649
689887693125609205996223410213134764199
3644179877484909910642519081378456162
308476052451318965425839162828382779373
553976322504964992781239278357826016092
5325416975841911167363341338242313552601
3699188960741228507517676073631932378290
901475951704592658285046433672318857817
259082515797326766511391007477981365
8574969865642333446265284109823590533638
286536850633685805023415321303489252411
3238633925235627318242119912501635704604
3129690498722810716831668088624346071272
325522344487550955665574920473990781012
6935974640584322281284228949946814877775
61198401791881562893562655751696398274
9248350710456972593462174397709997693213
80807328290383871865348570599735053616
1608204241896562615261503530159767001637
357381669255261562137324644292982492925
32918427260510641467506158460476962956
346926515558927831667380464854779021118
5382770649447620140963433410242089332456
4144244618783287904258219188640240372244
177541098759773487110807280944481023780
6205958289906332563051046465725274833112
623326525724130019763172038534591216584
500252935000376265809195176677325717713
9890466889533026366484389317906157381854
991566213914515750536822464992024220411
219925192726869286015209740413229202
207286851414872022967526815690179454833
43438089896568751698743382107247353867
699267821027327746894335082450395012
247314933413362546349996819743745385261
696538474810900424615194863467932186149
1934914123856242318887096667449330937550
4204165569277168322999881414907520835043
9900235839306994957641785024521565116889
178027595208945245823940399292095238639
889373383399571257182376678628790166482
87311867117937705129875804546319037960
709478041109784587544237879671271721152
4054414755188758282526969474854761182487
43112346443484784855867741452913508
935147973399444635092437873665468629829
2375147551867424360813155307155733558344
5068757338736541484691409768520256093481
9811189931414367681275649071270253152623
1367650047036119696332799289694992759843
993217526802408714058739961816067004751
33987474873112940066058588869755143457
9386118055121668666446532986871196199357
4986546626106428652662670750183895713
4515112790823080253518394716326643031337
9422342342487731427719634939107610768972
494276056529274500771632651488281974447
94920688246407446308044761392371577979
1274367423606331184765356842211152075006
111198038441643118155452418143544433037
40622236640788133450583857143556895211
53191702650975546491594408785126867653
4363507316807660680065082009500132017885
63949224720680887985692175106593048961
5813392824114261665838264274736176474858
1769602332139368942274111958112220709646
57506448864396716772822168644154651991
776570588737559271781269964804389
1527124773333506970643503897105294296087
764102986224211289945087503700932608719
2922815591325293866949417330562351683212
577770128864399118351741611549261008311
9981967056429624939840495426854472611799
86978617934641782049662722140886330356
55226325949934064147205526562155139263
7339639693034154188569913598280458425
509470838668516500672203315442153154406
677257638187126470646626248366829168753
6922898394259090830263626748564275759339
89312481575115461171781951979379498431
69105318652350552213224725221274232352
811843492674502610825863314078524356718
5421695680989966952648613820579642639828
80773614720585621921913064381840966955
843821884052812353094331568924245431220
3035563859446719540772729582257866512145
724326497715246495604254657455977623564
371564746574562019229272263332963403392
2579977640778400312621219835708967315228
394979751882393078053931600527633413306
3124695646113232787302170480402152870
800015404740852590671672789654495189555
8825913257978365773921324770757872693567
783516394654466910057587179815282344728
32735076254648190899224309974549894402
13014238580852464708625677342156747654
688327709660704835983358702565739282645
435621835576311970213405116993480857637
613237147712553995938564544257857038511
992284769662629918176421404351461936892
2920190315839347536421316142890060219730
876514929135722289752074296870431688189
869982678209864323013930361394740083226
97382133824666773562393982565808191970
8652508239506887166672495004457261704155
812402279146522596644385096923189049825
2055915023282929582818824179282758238587
32808313603149030661949182442462715597
3194757775419450568254318419912795415
4547870375916509418052235659415423669220
244645913942066550844421388431537054627
2020527899202360687641753139560871865259
3107611250825591633572651695186027383489
8226623814327560365431299353418266808187
32941167958860683104453526679674548508
3416765787567855863611124583230042253130
918585224239613218244274668115724988175
74665134784426939897833915875769938902
522156562397392523934064224897926692489
5166602085614013816621727294608230197625
913540082299813974303259714603827392469
6146858886879818812054995466944581489
510128447656752307083773335753587830
3095126478023990647999414689241832753626
219340372226423641323708099260971416291
1006749686174779666194277937309384219
1850717077811520198859341442200133652648
2448763856657059460651536986480332781753
438219855506347988895811309832614749372
567153512210614524565603961810267522621
9192323267765555599166522263791154952166
5610773185862485859258083234409551619272
808258417208878073278662882751828913785
8580879963054774836697559762824257535182
24755164658501154699399800582305670799
1359750135488868668743273486982576924
33369751668287424271533617285519693335
244588136166289232998942967162333260507
1003858289226316353257417629463632163835
6268650389828661605847954305884987323217
997121088330781569542702368384816053809
3114743818983099444896129475478272515843
631556031713272428680747510712981639
8776869697066423068658178908637473142
744321931628429923966083807342221152132
69972837928675259872787548623873554560
60577889662508735145496560845789715394
5108329264606175765650205954975158233776
4129361971724512406922894048821574281234
8571308544108652356451584138497207952
26668957694617438627361929460657273053
799035143762129876363929902604632757356
455184017684247339462511582813943214429
112347524635715583107932146468717894429
8685251968138290629556772445508296033455
7250145017356004823043259409692123103131
13993666342373446740412619385797806805
3719767495891599846451035596109054299560
313944379494461754816884630734439419756
1344759656347468587069574946374163705628
288512294605721637727457776650851095498
7797883375972477213994372983688971223544
810784729046206228278951607128827717564
7238326798257865452029677758454813851285
57816554172235377514287429011155363086
74151822677250909603688759550410893270
547526492881940437092385247683364555127
259975758474638055436238060175214088422
988128723976198463127173996882744084361
36067455949380232537559728912392596302
85916347975324439026131618670189779058
838023775729552115092059722823877452965
53061908156959263149363762977858294145
3599668774146643745346377106778999501053
171362471834342319985269816253653291189
294631976967876082947117773195943925032
973624713510539852054706330648785323597
67529850753753507772573278227980606046
55805831565784962094949589514737255421
2088667162101793839430267092278429774154
1203419441491498993429036603793963747997
39229929966373546694613878225962246675
626712051687878360873116321508869707696
86246479582427359137128882133526783811
1392872878906422645588173148231939229063
720022499905462050840324152281949749646
7584881215733071598648543479798132372759
392347627606173547731919973283166366617
302290241460825765874835202477424622253
672126379212188252287452939996497216742
2214513457155614126490921886628826374896
714418291183556233932926483584819325685
467593396666430338701426737819734187451
650353743728527172770997359194347162
79843861696695361261766642311186845575
111091258683911681298205622973555495489
1912386252383742945582954726487964379274
374576919040449821757666164630503705761
86992800543065368445418065665246631843
48912163573196225772838125539841772675
7591802353534446423430905501102425961937
8428880965123185172270654900849261144502
54792362955048576768644575414759259590
6035819134339485259062968297729295077637
6654288453068355311419587267793241207
95141596196222256653619276815762522754
726048827339771163192661138490018147361
937120791700364012045875143876142529681
60044039647729913239728080966403553987
46741797875358848194733785718715329
282578819748292251015997030904253629158
35114961901181462173806695730953469210
3507331076736438311757112803697756036853
4859641778351339371813714123901468110
345180733445120395595378103427756503431
19723037851877644504292977572595896275
19673717366057145072667015922271326149
47952300544980748174874844019454547622
28332258591583409065771672652557047640
8336752151665370211838584478174661218870
515165567493301961199682251802912175782
153336354110796225693121003152360379345
63002626931956504281163321804767168262
7114673090757319015833078521249872735
41599661644538689597645825859171922434
99852188129687097807134018671448588864
942523886879738428231939137167087977577
4212764298311327901378282684977513734004
332944156650530472915424120588281763784
538037982730670432052492592537119992366
414727677125007110237928068224630468861
956824229024516230793419565551847473298
243990336405236044675436253238082925445
1575787867175685245520108816109835365338
58245642188918246304026642483665097708
8954238881610427997472428167458538783
7074249495289866293063872081201170528101
12103337952672111727472171004664612621
1254949843108161962549378569283596222783
18071329723875621860717079782161552628
96011668377406882897703356217899351280
885270948145353624973767656828615904067
83532643721270489630819778457867878687
9755107869497601234727222173320964551211
532960879598651750912907899037013959799
26229859480739292898117943654987308835
26334461141111762891908814961885179964
5763780680621405793672885238802369545515
5193762542736657968203402372939962918
45048012686482679576115147542315088712
75923821822142368548104150808351547499
3934673590325985901022826663744743307
32361815484182074810873648691194897678
65576953797220851891286296041374246557
8105778781329645412317351246479042513
8599145664306177882412198386216636755987
3471511339546178254921368270212518811516
3659146397186022153271777581486575141830
77101618620064381483994810552681712513
69785926289981335992713869633935607477
4837991704651885697374567131620450804
627330938608477654911870711152065451000
81298519538273753609815535749662244819
6109874663294002263997313232351966277192
4357330469361678544718465258101672763275
283936151619167634017617046165237425061
681171724429648481069793630715171522024
9233286668657176834813891120783592276
900143718373561673094138211154235339271
68624641536528245708600458502820544831
653750881665939561427824041839190057000
926758895344097163042256842169749458387
511677635064136327243256373314039241579
284019814714418597754818931814339518033
2356323814454187115726327070798142784858
81775385134255373541285393490792819030
869841697513678239305378645282164106
1540447594004501561969896290127399326686
160135999426146826751165690931840773697
514777937013456959752738936078512557366
5693517076173700884547539939326184943404
9679934282671248431867922429170622404370
367348841489949869817924994872988689116
5962275842854277256850569852357377226901
6800762761527861781914475392046151620
88214515629178423169894624565913365
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]