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
28123211474187212991629668985347664853
189144353090978415573322399863008010
6961215450141750559556909282972244535901
54334862353715912413106538349440519679
4685671059006171747938907987625155052776
783440712527663314509298710211458003694
399150983074615718174547481670447999666
5672638976310275094699148766916123280
315438772345043328394010353063086101
3063840055318845204378235462732425013190
95827543384659206424506936210932985906
697796732402581249643364372468213283767
673114245551973697667865315578521883884
797352560245015209668212747128621243135
569930541255248064490446129565301647
1511508257741512153194211595488611109003
3103880441767730451430932984365278633657
7862628322585391893149997695283969178250
33937102956345057812679269617351306022
78257562567462486475758838809669686114
228255426496086445105396428069235521
307468247874780421198995488534990419033
42354086926397674019600479277032626201
7583181569173594730551576318528812945796
80045616027860890779553910456276708947
52808996000713319121519830741570005629
4199241212135197224893828199225814425853
1028593740365820333074134306509537607618
581737617644851279265474340591819502685
60736857848144260711151937846820987698
1508137848799059465958976489887763107558
341562932476612356281803751131367944403
19939767833874017645882883511955082840
9484806933146299619534798663915081109194
464350069263140883318753912899168029893
462210817201776360174259449422498829036
339947054736262024243086575990872053806
4547168815653758376294894036398772383585
46498892926999980855210250380587877839
183768816146865461552583195385036721719
31464104690878897935827838778894589778
263698395363196966072347868891710932892
376378249708153344636341752584247677142
8846598388851462212758768263816482223044
962393724255790067581291668421559391354
367974779123832938889553087487941875354
2800663166758700337133146071614813662021
2521403843118563426814257739283987798823
8631800930492162496846341689526038518
3776110644771910300394576479166533852722
205724741459104999420651461251911143346
48481832722067516934907665484629141812
56123869680712617607135697521361332616
41894096164454194272182402559349932883
33133344856616513188716594866931763135
607987364394896533317169415060508164294
9971430320382386886765411891637811388
214057555114904462426590956355859623
1883423391313077435844407418992422189091
217212338492471171541647921730878883979
19996299273537566253265483741278934834
710224003007990341726821166946418468203
557680607397322433774344870370461788150
76897000250725644964239257731579118703
2283135338554101306848051525623582557688
6564945871567512870044248278210974077829
699373711785938298981596921324471478545
9345964363019807832167701293761676766983
12607422638785718642023163616131727126
2911143254275906106285446461522466225538
550991483521179526566413578305993324749
5175471897787295372760241079266379386
147649866499251113774934186806287098720
42987294821643466348949744260551861848
134415106092845824897156507595622458807
3241744996556227228597537622641347746165
4438978392518676623068755663183592612863
562533401006401410437001573901911266025
6521720274528241095980266464178897057
5139582076665218787932895521996864918635
51811336797178264727606926956976491240
698576554912227876887079096355882064466
1867702569719730483324992878469070247529
59721727253936679152734932596963562350
883217534843366532974636434410501444032
12939280117795290955537557900872842209
87335038197818402116471206518398888277
19395314353600641234295936894271432747
72391081936369636985654168787976698915
222294062885127301699651735389065108773
13908719754956299026830649982424228317
4453304870455449238948423018808018323943
7373450051314110590414546128434675904337
9549142599881510235769505294740074038713
35522462883746778794100672643384975678
899362923571895394798599484564869298731
6693241099544458678491346248734219561207
48434888566745299833893627765147657193
252851244133647579015051291485362278247
533145331959103228905201051122099599861
126684018493655371704435153897442455716
1954438765626363072474781782836661432
4565749251115777399158274028775316351965
40817462780268716357846342225031596473
9465505891779276917440689335340117578
483719436844647316491552788511838011930
5837967847958034634560966318852006445
982615821673933952147328819740264322369
9006344024861908221275253551673515491665
998815685107583926017714959221828296337
8620836228988361617371893291938401171
8061716532369078511276469164734912237928
35513111061266847219721499739024562380
573171582844847097774129611397614578568
29038543134609347072854053716664622584
3242153566081026381513721408345970299
9468517985316614506782352980783262699941
24075030380160588807899821340172394185
1970828830229439737342096138220749218576
3921464426092146559953502531420080632037
768039428344027943305903690013644011
6672106973440990154024468712610769511
222532087506475485214057155716180831811
3696315060357598818179517551708286106693
818895224222439032549961839590935228326
281431937868075117570063108951277891521
66971013416331487524353119848230263702
344267781900866959971134502254514092140
5385959598229686415230263558575068474531
7658733646077154576955157924562653067834
667719487209962828662123993095403042196
835246133310475619942173182435041806075
5021262945642492774553788135525933988750
3466254589353038907275425246821313303086
81594352334925662634119287625044958395
7480430624827481400299708352243592577645
58823349184775442471572436489035593999
968309273717847689677557399170655867
3680497349143609476799359310709973047344
7321982649068194799652135604848829961887
305573259349213931017683508437068789286
48881564844604437516968936282277866423
4844593148607363287213904424368457857766
20896381563552938567155066536658307764
4048516769168094826498036863908861821731
86162864516339518858330726662874891051
95418394183624811052876794342145216364
61812148299177052059684425533629302992
7079778442185744123685557432899965924891
3058999253422519662816548699721031632271
6744603879012776454459014500441534119553
40038805485874416768128682981269645798
89942719178612204398593694079522504174
93232838712794869572906861588987929415
2066021259335928805563480734573968893
1177551793351567491776652294839527601716
78366520730397586096716125258072429866
916412756722417858651192857234236225094
108232796639561244866354844858819084468
8384530209473551953776614467523309427
6405641732494324902279913326723699339449
833862007088136642908873560749099940713
3778433325281445516445527224766846791896
8676814365461625701423876847792935114280
3363150982989728244994149887256878788685
6835664537067918972686148521570775553
98624021726948657646282168453935486743
121585705340766933213744987737160494
50626812592567527611842733171227999232
94159555564598876392578552535962662940
5639321657212045877725489922394499682
5391899278402702137231345404611488974103
112947044622918710443517603143434539517
430592943994301944723781843927683971180
9154731734858705671082214859209319326814
35668419642969551483778233411294527190
12287519624762936061906480658321154759
7396897948784537195798266916630772259163
299736518145139645662153780076133800471
538363714762104334712512797441838903479
239566322238450118261827171073332296199
53795548292910017171689120441298696290
922182882709504683827484942451670225542
9273505133442070271695429619645610137210
1991723195908578577251553581830699318161
6488518672965044765235386493629511285792
5082785022644207960263417926552355734196
1610303413783861620021859099998919778944
732888764569354550686723874795385022612
7731326230271267298275388514751764437767
1156371248469254677119112095639253275532
18531311711762003559272385577619824742
81521346321650480554253464159119877134
5680627163474683526089011668492670983520
9175844555602925990270466160547585729011
9699798486321885484839007845989261623729
94046577218842354251158576898288956818
32852776681233307732723567932469808781
74517723222364110816183753369684283967
526291269579922279736394868433726345481
655386333493902787245368726472061929338
1579883595504831909130173143173916539505
4925266058563214401278340817184395224
825936219633893290791784755714344198937
605971091167683684521454763706337033720
6437434563179668569277322836105318747842
12303444513036267025202658275955639742
8053289567627800701828117405560816145841
8171461282177848769048482313678827472515
442277606181115465168062797299894516245
8017614791203690107551364091710222787483
772787732811929598171777508577787312089
1376623451871981460858907844760479168765
295181379732522497654734162140597383127
3129608272795538317371087306988491443243
868671425588651226163412948596481902903
181891988453677453972712424317038677100
8209807917382050104493984102504741679759
1415377854285732039575736463125057
1571990871807405602014107092644027597045
9670111131338925443512319661348122329438
376358517158842851714721741292540946911
418671917069374250445666608546274257744
8334482021664928634975771144802659139215
176116624569597835784043751750294144082
530878501622839574041842969044419260155
3700230817194885412684943770165096673
431897721440227576946929245196217282780
545263597255165612822299369265577081837
3293140545671168882855496306351010046656
513276393823208766273601262165472741683
3299858266154056880499844924419741395094
6792144658175385208199875440464918762
18861743139513635011852750969681819879
4203748100377750699862685349816715988
61255965521893093953846161169062921
3891943678981294413696953525626791918860
7107975167963507723635717811314896264565
44974780889369861918925511489980851826
78067763279489595171966476880689007695
4334844613955660829328778149457093179029
66042487504248225452239734353154051832
8622625665546504153465788546710175214464
2866968424952892946235306587761465653387
456535511764813053893519752592486862768
3033533110588814392164469391537055834736
4341550381763413449651119763716021062997
63264006488489831692606691625510032526
55706726847675205416435623393161979830
89516626490657616081582101957531269681
84786531419151206146649113496868368196
75796825767281432944967954517763588770
702860931369219339436211668963584201613
84406695200543465621010478442663899685
4937660489126103926424051342458441289984
364210099821230324376169792846242629824
9428878262027663806386617758761536075134
82875550929013922882439857269535128097
18251108773290245975124720705258624884
1511783956744679971017934872300032803575
5641645615155975188529597691998091112264
29674556438195856488374772498143471972
56683321542274726848965180525292902765
518048737726920618281813256164458026793
846887569964687250586877874296975936067
4824307224335699736179316750938834753196
8216691060175294777143654893449091585956
6152411582066757844062766770701970591898
940019937470244324456031778814493547788
656428929280452270578468250230855702579
73694891244492464684981660351888815493
1588144486161563375238061895828984068522
543349314609119597769831887910876487968
4084328121541854766429862681676086473903
6689597233819511184375066214136411247675
64872985165197480368888119670786682935
2922706700334567598385653430032619543
9818110812185721659931399571064728947
9487361263967046949535363380784095284802
903723635119390654967438210942020769756
941439800827293571172125351146255286
8135461023503961392541289785449055118976
58462677475678383153865884698216431853
206673193233514742597752346667098695663
13033310292723135573727159025025969978
706195361771265127562569617123099269871
2680639583712068962228478771615869451569
23743000747537955096988494759083577224
4248479114726042239790402154832034827563
52341467417854873722311764149238931268
182823339498267612253391311848494509515
7158478056332611775722345539710230206190
5638102681553911835099145338280715374408
2251468257839842304345115157911511816756
95073621259848292055567592148436077540
993926459346704164608493846517877686631
1035421116937540292753985123697879069946
1514190733088669281214749367884241073132
6811741328825104653733360955350099252
56123162049564112228625397832143274228
765672017211321233738393482936530852887
259270806440774433838757956758081176285
81133568642281176132111625516670592922
675162009602473962487184713047712811487
48581467809712185397364989959553608806
8403777071307492279399881939805782803840
13642923705643775156127889612957183056
72164696071832826159636236654569736539
778113063574222608576354337133404412
869919473932801780609213227637949909524
4476969816591093861778014432597781561927
392616591842218890154075913549562675116
3954217650132612338434681753766025764730
2440731340278226947670403621369031963305
5107928581284494943181816495192925756133
564041433684798974183036127765089571626
6444647065198984089148874651799662634
4917385966708307271537545496372774837471
763367101738108622114069007478079245302
16547794528629253936616763642553979117
9116182536584125919939856036286602
94693225075762223736348810581301951882
8378380784065386204339942522821464544741
4291436549104590193390998265434857009757
41931877749441788074396547911108992123
62994930702255723568149971680866896115
186344717897961846182707616593120393358
2701719362731976620432726445757952286072
9076526024969466683784771273251153713198
8964891467907325293656707558150574799238
248938041154505365291499951457651392426
7701229390193831640684589953806871243576
45418420380534911593618493665931625462
2708852492214673778721491045487372597
2195257352235538894870566700706915469689
9575521537675319918164944489723902999
3015888493148936172707920517295685398
90182616744895839865777570059830021521
8441314959162063874099029812198319014959
4330521318235949751335178287260122064756
6799144379223865966727382712567549683298
853036751584419618273863324215192198451
844345686394318164517432342447714569535
8990865022018106504265912437902440698336
279899929978572337726156311817416688
384960212279749207061084499246921627891
950921471340253072726668579048274778894
235426812722909121046129511616982402789
658656534493119198170889544744191106877
919555885466207876547018747807463094904
4188552352456279449856074114812769388260
412198546974613279013243707952149527475
462245284958467120158249246035518089190
4315850287471353968158129801627412924224
33139401526809281451868321819997198791
7019322094453054499914463894522968641
7768935395762275792586016615746046988480
79102776765491192602533726625783888
380446249752456385467237799952650033909
1270131189366528609343103946928571172635
67504513719916834059016770598034011586
9233376340474818271682586997369132675233
463095039675887986974224707615886169139
132365201374867127015545269289579954120
4195641997437330715789495061678513649
10845745814887592858052388380312084889
76128526142525622613917242346347293465
28277845572550759425796958501454117671
12083523983491734996181803961232801380
3028447292125656864601145793148002347
928218888513874751367977159842317364
6684707860169742859316741630810074847506
3383667218022606138668373976743252637114
79334318230831715031226045017449070
15589934653423121914228843088712434850
8825592769526156327648181902311445922803
831440853072987560993860173118967492
47716749798598561866861820272945968359
5537599893037468688167725542503418654523
63592095359173990431371231865804679619
6867978214899199018135833553099587736
53935857759378128336352963578032122952
7326605887059131707728266743834416795984
95412371655223146098696874837533264994
3080669112532417684780657849247081869464
6992721189956913260628787130138054195660
246983711843593193181515191131850712098
618479896988246300993788364340594669998
8873303469528001492828169432167639599053
6881116371732556641817685168248092098455
250185649619549583541651149977142171823
16046838173823356121782389723892194
638976452693542487345496898570978228685
388595943888588645858599516502050236698
777021094918509197914644112350887283142
86529466215392462016057025610018376652
9448143313429928746568093403690433215799
3860362291844699152427507609725898192648
6393633542752657997356520399542287135
1114583248459918230824329570518476902457
889831425747369484290198122455527362348
1805697772071192928461127312143571027988
873032577058122935371472878164902414
4336467986989555185998308779335979992330
99386613998060078246453940634675907699
74434379815409836110794851755584968442
57011117213910959658908004245523648899
29283600638639101321167476216749367907
1968258431262307861168928219270021089342
3779679033838359819661436868880820455562
4696254635404181985445285897380531843719
381643545310570866957956210677834848724
9081904339610344227419387662174852022
9647214773072678500945215152514556442197
77158365865876840836688172389318094524
427561623128367957048755997595840374961
4807610377831486786779638779441323279148
6625857863365101466410387003833774582649
84781777156901684335112706287183722695
847799774991125769834727794458495386818
929967622139251692817750314151756447068
96902602604779383610763274161595373272
6759715090454156366767486308807457304006
6197259119947958316835629753840571777778
613124167014414316406525309902198893752
9457489330368933383611876884649375278405
3886091129893393575457291169394055706
6784905341852133103877596049162740652279
5103947750198010625382555488700475902113
4972455911289378328563019934247315358
106072071253246828119985566194489745910
757543638629183886291714507171535855621
5451654948997627534479189556845683043709
2345124899725491360297115689540791346829
15914419455712196812197094831260216773
5081944985545236378380504197290341582245
827154344972197083857485760634875051439
3380432611793433161556345329781670618
68081836156913101280990058732542744919
3254155644764026132183852727823478784
31815141955938628780873853206523802656
64298739992505686365312283720911369642
7959326237418528219123274119557077118831
63943469762471529963192172745865346720
8992549948023022663931547641366783738794
684766778142255953038749907487360115248
4622430417802987963631869512933045049535
750231092700886260361044371324055854410
3778493112358034823068183530258674714607
5477786043969210785915868426692529306158
187977292183304852339533632620764258788
14946926414863778326116745548021486243
402241256025792022481642261558684925282
7305568151935028668159049603427390194556
682368002068830314827903755658452807970
218981375379156419229462719128718309218
570230838560857738532991609756957374539
4927483561388245902850007551412290293270
831776476547482727162733917946462296349
295669132758287678532736329466737242177
6273941044366132775779746720518510335977
7435673093136073096219179076526496
96769683781381448102934555235886884854
7882847288087125509732445288751951669
3599149857062508602364817127470279211
2055105494087230719874089488482582104160
50870999074554849611805018936735718078
209253010952147725956398256651655402215
425934875209435686364871695936972304464
2789849739539652771727171796295089941111
5857806356495341390353195061244322774862
2222966750957953104814681776572926959070
571965229933388640195400353451392916364
8612490478992156364762564171842226848321
951188432256837325640496620826234581303
331760434921275337675824232328256712553
54623277778845369300656993368366625584
865186586798768199624294787051176364716
55720639397658495287372412800771129959
34929637125662189914642318258194416458
464339398733950224685062922442758204256
8106314185037922183246712774441564117818
52425992767778579139516345473412630382
565517325716586266943325444278145486886
6925338049879483805584235131390440838472
631165249949169787591965367590068652200
51812654487695014715222310475691096349
252280288675655397575443340429628229942
937057961146901770645679688859548769118
26785338773866589562958340975891615517
205546357198937264933235472387744791301
30342405199141364741902616201064195905
704993655218944722108413794373432873
9275287894996003132944635427660389481578
659533345800404437766812897772071013108
24894990999608817737199179126671108778
1844891861322395698863632764883060353649
2949314497066381126999524599693386981012
145719812626960636752851695815212829818
721818354364739208453187403841725246385
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]