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
7664150232295490389612701731251846176558
123137952842228451237778486246548171476
245426318321233590592449364929075101532
8299936573699586432935046994837687073852
1360701631676314292102089527324693503
883035143323814698668171065304228052678
5784202321558980820019452822356055998408
481194977217286231914939724750522623669
3029464648581924561141143155228123538830
5199677241842423881646968268583559465282
286664444683093932517662225685489133650
1336889130705968584697858227152282773691
8392518913924122709277806632846163648627
53779617754602653936497707921829219427
41194757294695944818511907762443056424
2586575777973572714284189732604883717441
742494259239361504081921267282618837187
109242418955566524123927942166443314945
8662580098211117788920697807984490411990
72862827587739316713059659245824501242
1212341372029646778164304284194710238533
7130918682918058102735672373253382298741
67862791965835289409689486823037029591
46947701803316165623116435809366860375
7793341194703207447769793969147349953576
30118455177269222274524676840739718211
7736687226582996704107530816578308364
53731435912354824141170264184238145170
6073675766927095129815343603546569825
12481866965539346037957987687927648901
90811732215783656849704157341170465659
6344525592538202636342713359932331621963
86814496267951435921155168348444530720
530790879854302141777970940397323236383
979489790597545284453974986200277978967
3008568134665669953048322618382634732227
3838601194078194394924786885149113688074
1043310972482338673927425755621250449635
528148392004611718797361643868579645181
7386895190115533292636776345120849285694
6169222310758709859391822362581134680
1252987471172698388954294294262556862425
6581288323294189466461559877301844698769
9051120281772978180771815934597785474768
6744593466574221607634047269601515328572
9798891545821276724765319962932118858
140877682367263951607506915696139814669
7036294490423019851584374719545053185541
6693599359282322516217657166628845105529
5405346896009861724099517338394037617910
2684574469264046567937036775303116043349
1802701194852180668525096558156632345253
15365599584913861445965365821011993951
2679324854214453846676269229550494332105
8806713237416286952697374622762179094606
88382121881171611643458817896788412753
360323526954511921226982771681620982432
8809129196488256426260217360347482968666
754346848863499127756737415136927091625
8726407279411840147055235201519296614
5755169225386120694465541505505234731731
334523822713963246729817776117047877
169621416678399737958703158778396737267
84672356157587484883628092205823259459
17865269413407857188957863184199795885
966143209118335221262376764531682196589
53496228044982025283212653514354767517
6060525167361135683777933263348671679513
2827271341697545937829341695723335032548
332684053800757596056841240284965757376
6410174499618337974584939331694069498321
4941198750004149409697604156921652747656
2896400593721026805820527539624378595713
7325138593717181637972554480603278914905
555999162256904663812527105117265761294
94397982390932032622437134081968375720
4419649439610434012159124994286616569
1478621455569233827934009636619230361540
937779041141229163858750513271031984061
80965631751760941051001310342683391190
2606107165889189767875050748268572429
3849966938625502285572495332837096281038
3894168715136380423053988132185682196016
226970889483893258421094972403214501393
85279311991762421341357689895830917548
8069785734831508722445875519787114134016
463171429023496320649105762750294279859
2814447841949557138740455612692586085673
8137930052378937213379802712071103533
555872358282117926413713526894566021186
741891765442713453370428395763712994877
363944631341152132105002718960097329474
832494296882903650659777959502695609769
833111346674161864989136746412386218606
671340087112409430346424114897944888031
61540537013695421774797370665442859015
5768788672494270473476123739196078619813
761379739127138341858359822265048367
394575928501213186809826424906626912622
9633411326131047928065764346111323369983
9104310756165844593583427136436496179007
70402558416731038462087713559126816999
28372416180758642556695456467697699109
8455333549501256167125591186994343055280
484716787950154048873403591115576191395
3088103693284877677847452765589065068005
263328034101726909287298387231524226882
9571142751088793702242574030530945632158
969265904375339180683873720330041961653
9434973228241423810683799506900334474107
57954218163514423792220599120968301037
514310878189500864186787813317747338997
7182321192117889389374264323567973356
212852544884587107286982112733231728347
3043806881365387454283004978389399417309
321939961263734728923557666754359472199
3417885624525351557222674644880357672303
7043229990782444331169572842606032251464
5982511298793453346024179266511511548127
7516922530222126662929709087178748394188
2041250175114135531915542546460066752574
1841294031337417535465847613998755864
6370104636351214093120958863873902930
22129541173140464475714222742174503628
524718983472583346501705655727802417
9197598187571734558128807974333245285147
4417192529044620396718517576217119727392
28817864518280231189757405328729226709
834215687114547744690238765145467462562
43392612009221049843378187862463104146
36273882700833819636836060816591864025
92898799293163644493943700121072139932
5901165818556009536724708637124562001685
982827764571354431424868788383965357144
5382344666072496364366104568635344472722
596480968685800416837454799784257143213
56036489567535264023209352177040336473
176794727689244194130712174463768308991
664640387475193627732385621576327974070
58637449691637093911150715271849146735
3443124399595972899556115814747374247414
5224231195621508738067462318711682519669
1457216664071416735385722725675793066134
2081217176749258733814623796279629751901
947136815348905278634786904450658096453
54953112285361634519299987760723197793
45853787379741348637524968642926906942
9663364719702248378657421463938650595802
104140261267874172878343754873828982639
5230223139544864567231207323139626182600
675350378468933710483767679191457477311
75623475764484294364887888688528066859
5570765166306039968025744846839241791454
460365714772796769822574756874217984995
1714253345321684646835899764240355004326
34713333834999019266372631153776485811
1452627445345521148461533034837851407289
8233303175957674409448194785472904852
5988244770455145218990581775220895624691
4791383734916483845757945082659352922638
552797503634373892598079930110953996187
39872599135659191798851503629370371523
1355655357244896603161743718284986574599
3031357423089295158782411627634574344588
1102542350966966198468663699249465002677
9996540792816572433232981601486053647
265429721404005821724992315623471905755
4917837964856162293467751088963570894494
2634638623871819576322515036571313208146
29705425281427764132211245423150983282
3988734238225418983680448183430769865883
266062514160759450558355644772752413741
308594627209928025261195349753265395944
318778095647951289173919693694449651470
844219362865471990431564683832429908297
10461339804068452714916946879180346304
8929944425316857212135245331479897376759
248091029207337123866651113422582959861
9255439870326766944812041576290755396511
3270119137147316774166906426743593275656
83035651145589670494852086470413973078
554436118563175681515856767747555805212
6706452831955045220644714072643039954043
691251372796259754935357338455773991998
4773368882323864498876489837232152902379
4915727010000432892551542112271420314228
497511595931901820320376420352530471532
9113155460547065715467195848621474424
9929953218481237154082456533260882915195
4196183161728129662495329030935734622164
181136789329221025648768618437431662575
2029820833733244372865472588990050948019
657124834378756270072147034552436018596
1082630314559614267948982830886161628701
5616516274944057226418645015684996754378
6437500062965863332875773392273352975179
1531720195063723257773153337523264453221
4120128355454834494812969959078086452
768267293418353576985699026987257270
570671898827757659817908834776389119294
32018759322293129622434034319864783756
4186904762455407934618717916392293373548
618699119243270371939507804617101155364
188826342037569717482287773203293607631
49031763981333403801242917356749975464
2887625122959926420279422876650351843688
3740409570629260360635546126963093922396
62116980931094659859478854513235904865
8087289737885112718461419762385582638040
96425911269995774825532116903042349443
429248882499839220668250338929232850
668714395532473244821421804375059605499
32742725135440496716302546340965541195
5654790570456324757791524086737799353903
119828803473762352748615410162370775376
4043289154252323591561899051268359427018
65518101771528864361921250474098507670
6357975854201398844090905454728852419944
111599382294771778081379976253051583
975497412805245223015304487227270099274
640589479556234648374176236379606181
879587373315261728147648101237038763263
971069137774260548164114526871764416531
5084383112767817136827653424738411791092
7525693784922318430429524626798849661
754785228552584327147355763350943017609
94918034569476312675100627947131236850
660284353224852855093431617661174100813
43834131294969548040902708322096862354
151330953625394236383709666774251169560
87391125499195362702308506143872944612
662097548657293417330892037534053232891
611812152760986953378494788856471673455
864514026815791559288561460954967112156
72048042814380830864401764690954376802
1348279436174163480085836098371855867122
278905974842080595779676139990729765712
82753540678142209200763847515536583465
73664993183814011579149496355793733566
6931554217152892554132993976462640463801
8261473118259037375787465355970525032376
2782197292768949572162022601814177693170
75226578992654867723880636498155380218
455248202690322173868697936359551269526
48011728885124179316114112782105223030
2677364031003937232350181399583151443505
18351244524974929213087456358341466661
6476102749551207640317652699672992536652
43081676105987739599134931803585243417
46625722852587394530233699587167803761
845666399894174102417837870747160296107
95429827796538907648152767581622185993
3837872450321647100185534470627196081008
789536065820443912672467216579157029679
5211747980623349965468168742871795037818
8282847770895125679919074410285442171
42791301701361522783097978488001824944
4822229670483178572397815214752949850
8256766535346492515968951710778715078987
381838723485601899499341091684476829750
21496814211883385933743215113773637418
8563798410903710522024236400781978377292
649986796654128138626757835768979461034
39342146842976220753591088242445914721
186431319798398181220118811267873979331
1057920520058911332852965495219116573
4852789878754329173382356724830072003343
1472447191062283327232597894355019907853
4072760435438622353314083568956395751427
29481942798517594272507517878332613724
56377713366419071882016041444413142846
206631559457471846864943802869412604461
793174453822694394078065831930464129031
406415341982140579365542350240259605750
3284255086566965491952592029672364917555
6575929788042846721857619232360057075522
78635848157122397447722535270194454938
394653087637358385416468507534475801
8029309989209780648767981036139579387621
24602201259983497983305747341197065348
5821123380819251536067835209079789590
938999998199381339039283147170761556823
4176816088016084252580443500974436126705
57779800284656041193382507686094435999
3658600086481766721778128406523668283273
3505652265168981662068676783267604
2534530953823022511990428824101760103403
16273200291340309579118554612289374867
3604940249337615767740095464378464185724
666541787809779182001087112770548520945
308295937189881019861100979073264488114
898499639353263989571968730851984292183
3259567931784150173721428259287172524155
992575654644357664256603138132837321441
1442371885412146969540795876289930743348
451597428186830745962831125325729452
845598279140912231339418714367332187499
7672336863156341193173465467120115758913
374422446923104012004147147419495624274
9779873917175344858783937080565189289
289551975752582982041957450375159747041
3430433662457390922093073975519990264160
1625867921227090187991604871799865272650
274210096728850431774436220329818066803
678381138340261764244505948168984677600
7413519047885460832390743915210695847766
310919303250769434325779285599662129781
5971150480665779682394913746848240542032
8018991657882473493152093811894725976843
6727141529447758816370203955857464428965
4257471833106738708124959980933821581731
24885544928280371599673652486159057012
25819967089683167489992597904989751046
1201773956069678997099195705481118684179
5538518341769785854534877588315218545926
744239485504801594692763457509463702211
5005984496093375335021962960761889359454
8534197452942020269879735109384546491
314766491234417534807826189990170193280
511940074020510162489853364382226859515
7192399884699443984979574962975694594827
608853321609181558696713559220693381157
217968872928123433583793595180829549032
46817149602125068539672140984419163086
5352327997407254482653043438791234942941
9549162812504377629652229125891415818304
2318711187983945483630653608849888489199
9655529213989264443941038816671028986
369125353143271158976266882483265603379
514719861487051776777213925984717878087
5537946538866219975443875412925268786860
854315837346526573561689654261012278524
92157660234724117907332437599707891884
9657274926164935694640541693510328625577
8521510917106126375846169833130139648314
938745852724201725092177333325373568821
71630825264409486116103496994595463906
7228789161243913132888319805144197125011
702975884512780477248876306645141516751
6058587175025623594414558758712126057544
8975859295121660676589305698480489283
26797536774467331650373288443994553944
8539375959061839986428423805512817568723
872641808053262673219914088419853967785
16792876703986350812362100987967285179
539590441276385478072146686671663811581
736651004412457224156815207132650515780
8385755557919375804768469143413685273623
66287137213574349265665150879389268600
641765452480755354154875424850881537466
2722305793546720304770317038249670373413
8785694585705466885969961738523716268452
14149432947619466364215250535643375805
161333652791405769472537350349560951710
7548105178138496242052703961415973468053
306997421218321596526566410429465790103
78907342068640985382018818224561651495
49676292223677171515181284335531696679
2505299070422625845440221794492027577952
4795509469544086941447752563376983103983
24882040689643543759239541431189015476
229872059357961850953687202489484353021
840778181596360510099634093388067817577
6233353743105858384355878603466136802347
1451337866002995979615288175316248583540
869983058283551078986928038341260284986
8490962278458084280098839937458545946931
7713543537517310277427724165676387786406
96953694484929392265059693439438745793
40626828485981051209840629002685181
2863494958177214668495351393648456381342
443865385091803449601632705402934896041
631438038555742392757228325147132735037
2637504075604469999715835842483377097643
124555893936959676697202567703819949155
1218215165548370645167043956713676931912
1645984379045649731490071598719536944773
489917746363201287043609490827642081942
59266681231828615007445244687072142383
6225329893634687154147945733155957244717
74344377811602615248732584922976935206
366986991899435056283771638143373769625
95229883545898214059014570812839061932
482435212275725661027668624832893749437
5042299582937766114890303393564427373
475927301466598778843356751308857677291
620183343728371214795069151635156094475
467247571087688246026512409017893453381
569395637202749945596798460783964564830
18966796093460742854843339523249227324
87717958848978807542733249250355781268
5530791388284281238631404251606291736652
5966513727351815497414455832308760926316
76013755221882460247014855788711669734
46234712547813854522217429536673972052
89688417732618410052544361111285729792
2351945117959937386370507082399851343979
41772695699488097123226391714726354
83199319163129770021932469603861999984
6269532116054825307420259449253760822
5349613634282234163237211886201062906169
1829805482965379190170745480860750456013
5470366961777299650935198443902346081362
9069875793035976850654522454426281169
4535680355072649554140945118432660073953
373641767529447676571057303540026497965
7326868389858688887909008328744662534
636469934028593769184244996481889575535
532940739224986183473523167503194464566
5390629947130946923280181312953631353
485030996482980641244108926847411509529
310295439206488185420239879328048091580
767270291098716261621727481325389159834
499877451283181920234983702897194788084
913135776642894395976728336590568744470
378994366217848473778188700890858392586
415628676694531677776858647625277654849
6010168650325178420741151129785634692954
265628234415192948751692920890196184899
6900734636289097465525233218470095716995
149853744154249422317401886830959068845
8999428214540873321819427918826373585
2489921317008396980591475518944146064293
6699366522088439262286506365270663472644
21284838141379185887846575475160241243
2639586015587274657678291370190573514500
856666832723349390553277814157792709420
525070348139280015644810625304933699278
5237722957804055443669437895378564938072
9750360828295865479511936498150098256119
33116918008597986698132725830963544967
9728127829448524546033123160735069493001
16068105369240586627539030910917299491
136452294535779775050255168975749019173
1553689492408471899477481528601952639643
2590241896339253382601103235665742364
853836139301949581161592228664678381196
2776128747758691192592989726382647204137
517623232274134477524984353331017462527
750751661002101413178455243257519428769
40083903970369953641455349823913321404
54539554902131200862938503398071078019
534229257113124380013444997064977465693
217518933848136815012035393420157338822
2295489652207818171948137630126202104
382457257192157416946238031131744708089
679440288790348253896313227217134011683
5604720723234142843988956308138997554104
651458658501891113186802757932319976856
99444924635568783269835274770675163
5316392232097269566837541131311785023762
89453562365763535149229335186219056992
742041068795281742430388347708420058070
6016977131341227556894879090415163279044
78709705143198426636107717965627869756
224283906938808078658426942975665634116
75465962147992776286535318172946359512
7378838520442412298194279445111960405753
51083088119083009877468108156515427327
711310488917248287842325502583345607294
542331624612626391270077800745327245334
8104924555251901327483511917425394483285
118425851389636212631179581487874276874
194775058394298899382274574376910794510
1659169636028574197193822584605257368
30718211137374433407944693589115475587
673314241152337656485583851991691795212
862892907637482821963604782836531129043
530935329111654050886359582451043338731
7733782519690593852208668240143936375
84747020113676217698316433161723823831
6633468472264877508594676546897142811867
6220802328428917564232573696918942
2253549655964020423937746468295839323628
2313307652644153744493003605845717243341
702265707544186820852795712667631296797
614929235963251121298078655881109578
801093889777948149896287637881653738615
7710544841831229102070421653965218071881
387167483748965852791412844998044139542
7744212214421139224169189766963735863
284610467362314838863312691387014108238
65462789794242546217036010935090786881
629231242327889332232105547054662626519
2935961499124315804750063186944033713215
1132012794254485546572460315368712210
7127589097382684815733582797443941116213
421447946343925561137407495179217959437
9591315977713163215961109705387336824291
38538613740210772856875634452025832147
62238553568251112833520584872641732525
90645128909690189284497823643993728103
93019777144514970418406930713497811308
4611857652765366675841601921322858037255
5836703868587379679533691261928338002817
995813272213154239318936689118988166720
2986885248792401423973853374594877129616
932790032470865034788575996534444361283
7295463899205021572021079347647931398266
21995326672628864059616438141562522760
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]