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
976981157070858767599975931653033978207
1877971262315841474686759294505145134345
123864656098177348724181522299087873841
460363698481509242983981520816964733558
678084833260510642969292665992566477550
51931698511577283116387561789609532839
589789124783211298947444235503554469512
75475874742035161006940915727440149629
107812032859769472032733661519931513886
2309699990086208534219208924559844297946
88511766035514922997392793552094143198
41156607503690491495678283452818321997
9085327373414424240826518585672564792
819532416470610887858681165988376251910
146419872144242754042811268120885185310
5378832282784337305452635799280982198131
584652115807322765478792265128118996719
4903193290376562706888768269947523632062
739196917056514839935068735155986845565
50369266030649229072965944459410164680
6540375084227687804918873067734073978214
35281651920728798778975673675870535893
21241751625112006516318156943190737809
211775595688950468121814428211751631889
7109190778849652736640187202814338377114
9827206147212574237636696792144279099910
70058566574377073877482379495683404447
802632231907768868099159398511584263610
86656738456185446319432278286610929617
507152234354793727633510785917571185
4333682773087266158492184741199472019167
971407579714265973804878772096974605
916974530248506331650463992332237571613
358256342710666954661367301374877531971
10849080664411595586546315720747700286
2009403294939203898521923927361411778981
7066561368051134615199438672333475275463
3361403284586281256835838689950651844395
3247719462662179507151385122929811335742
7204418879594943948972055186673971317897
8379441154656901472082226848645241991
20926816437328003297412250496693762718
3761959728358980634973061613580133864031
884575898732425981453175463643594174873
98954262118230121026841841664554461798
2700626560018851059147898098197638701
490297017191905586847732197853596801812
9348400246523734637542447104153166204412
4221971329016869745343884651153480604711
69196211909220469328857911836593695205
3848248614305773239536534107684122034952
772441163173476965873792129617730029639
7635706931249855516222998759409428344212
124349669436397128525528213889714405728
962421453626503413795146855857716489553
873185216139995040624813891733958516551
445929003181296484959012965060524558119
7205844393649307712935655771840260108901
6398871490458432747399123620478767447390
264850955934567776147225965998337258371
9739989921668198557418081701236289376
7880939241303725928916239320563370026820
5438800846499094257265151908336388645
2385525639397411538669697255497840034709
252771749436757897651949908237025170
929376416288145675811667307672815585083
8794044550970199553712838370325055869
7243207881359161489429895839123812131
1156294054237497420331761183308941482116
544879208270596874941059865321045445610
62336277893249761647799762624842813722
3445483855668238625859364343765856481398
264712547507841255472687723924578683709
543572090541664861729894572748436187478
590428032524561294144157157271661229450
481450238267827835561541478393725937
911960898163159315967756614617155552184
3987201926997452267372979750724363691446
3981526978442235368095534345310928087106
31312559163344545237626142029495440675
711899821064578760531278910969674165462
628484493704049144997638761994818642460
5940642357839457483894519736776483819622
7891371172207329604418133402562916381217
3634417714466344716117329380824120609571
99346024502357260406269159918989574197
606768995204310218874339277252495719879
629988212003225046813608345582459888868
29864136326956861744942549437292424362
2927920466038659433644491583134567844013
3962515482382843066059720999058326052
8716993247298684691843167352361606297
440899788345727347634365677667712785613
63074204562109775758157355510166056016
2590436489901443795623825757775610927116
706756145295785711657658094131142096015
1356896297253391847551609903567647217067
8215281273331331233871880918143031532
1025399694139241187064719757429536246456
733063020263630741114951123440874747143
516316922067056226445231653591974606143
70243735190735815858254757862146763328
2817685844307410606431191431993364101595
5933119343553746460895961948316795938699
51446763112187705373132144308954506952
5818974548201562495189915821648265105353
78591374957585055217577204600737878274
31551741578476663061301736368573683086
3668920434131733140576506514567789159010
114917349073521984197513415470121122204
66692264161105739704461048286645941593
2612889510192875131994385133127150719216
5789841984026643883421195817273465815352
87539762256540375162235586086897514374
357632447598590746655287360945972089225
6291895812552717221895512262388684855988
9762296272746723500950623633249360611236
7191875842368219720151842947210458934231
5072313861928035205675937734182943816946
96031772718012487821168814078309751093
9769989141643923928877902058984031513933
7858597360024308120666689178119850686977
7492251719418059447429278832298056825401
59403321388263929032296081634521463746
8942691348823477630428833008497840781208
48297419235430984366808389514998384082
79141039885362178583111230263448399718
577011534629455025015798801578018778370
312251226957711961497923728784035862578
728173626876369294034773238844958468478
8846766312861571541156074782956428407420
1887995558666136405423186455682318553633
9260433932191114021646587776593182300
70951039831280227950166724354982596253
267219379723143478939539721724041502066
6969131641332854536151026872334811934750
2308948947487064603883713213211516693415
859199031335737696471269175382581382893
37209369692997386011432579817999509876
427390959835886429668964059745092759992
3254382495752389481729518065345011084775
93962039581385025337057824223313177953
923652915378420183641583990676251944906
8318929975542420859196262125555669737390
1297473341563328253013361353906704596
1575461161712394140620751118968262927892
2873843260161561598264423555380114566071
5344166905946069958038976526848249068
4829912825876754285498114796626367013847
2377285946217545300383997959328136183688
740197805854417150828767749962588115252
186778803136439656850721858976846681702
2929711865877445467392635701172196819874
7091434539126103648324981535363144059040
4642135722932652356438671785580753857847
931361778220854472111271166663926025851
6614534314441721438563401228891681997523
612790384141264661370942823712919077941
6191680482928762909469574838757883818167
76506211490385483899689642649230471001
6175714855665547454717417452886419952434
9258734555057577750989908734639787794101
685848693147858258015583681918257913
16877347495514895029848459806712164732
8696641635875158986971705284541497238
897849782112179268793195891758016131107
2914238555656188832119018355435342928968
92479780410515186545270753959106878564
6187436878365857667556799113490928698487
81851188198690187444875462523491966067
443765028063683251808534647026796418790
4469973939574693833390297400657391031134
3258913284364019926420941032626027628518
48002878779359286826895499338874501727
5407510278092650167091707405974194442273
138263199994000489160495669721748672163
79902566127968499119119799242017603057
912474071598203495708225642499943026987
8843304212687586709854325035405884001540
680035427849599594926265854389560744744
17204238366876520268979823132712021521
2877427360083910509860507503916292167680
566375735635277798892616115762899463806
617575819725455606250355783703811631112
1015262117969558699164869221320894831
8862458475511408139212438716831514769975
820920972891490281742012723173732161078
758269374771631994112649081387477841
9534870553782984541783287974815943422
2898757725824196633241214157952498468267
9858650149395245601524298939787758632371
51549898109370873214526793103199712936
481467542301388599290607754998846096255
83935824249592418539895257420633584925
2632172020128175195445208232236244981899
6678113611566394871072232931460675451303
928755475088168689708657320233112746917
659653141837760464612536438470399461589
1479931511273126945770579381328413673
4814419385188196479496487081166935516974
702239971608265099248626765987554106420
926387627384804466107241401888831407
77044053683248381443139203317984273
454928456326477744750864126239054426440
4690670381132101558527335183822198982820
70203377326637484112102040317175601375
62313921437162282569825318150334831
74116343356769708038544564002326071155
17839146424508033164988082369319195589
5576867571869747277346311883554958584666
8206402676071858504566330258759616560
4329591141025139687685115941375338572969
1794656144997481501998410896831613651
83556038040984610477091345221479667261
346535672461212971822224226225677215405
2830647728048325732282475418583136377778
32496280568358728836478618420056907810
216876084826399610494539578808335312441
5770887952972960605398643833361461712264
7274569072035523725258403884728520199975
76409994147471661291733156344471726142
610876617734438947137712279444010714167
7150855342599476727758079543892256296212
3017149785744242168231629159742112437959
1437561430317806641869585588433770368481
260565346323194465170255836810226726924
42928077828498277584962485886490472577
2418839643967966976175771578163012235274
671055775976802695761263857138622315
849245365523325778759798416214298976804
858171566962607639588325691486038231318
8011587618306983778433833499453786063047
990964234349467275868116020193259687237
96599844215170211134471424320544326812
225492219608501947072178726517445886970
7043377728146746614089391392239798892299
309444305676562695960184281932992498382
30456817331637055712761660091999243884
37148681658392955261461994962513742552
3957604258585149937531123285230056922071
3463573947432076931918253088182522334702
43215544609688067881658190353848179068
259106150916532402759757757492853555882
455875213581843203553073482554738459698
6361305667711495408750306297985661288443
897942514677209968056421538578753160692
7495235082105237602670237785873676571271
642847841012029293313689550929622756968
536748083146510545785694765027683861206
17595835307943565363211926333462111533
976296066933184630362516685831760778811
77015834776979511763074747455059487
749661403411464961282206588453075903312
2023482369626305838115548538646567065500
8213778431398778110012911789769794509956
1049308917241805658536719586203480837494
1388618785648427414823921027707801961
24145228469447171531176973143199501912
1011173912026244912463548992602629735995
8690895360309739933392783912749721136
46801339957956354151568566398785326250
1925852074218495576346201631495418679702
1933267847344261988723978116499312535181
46482542113556274603760972435636162311
5151955643720776638969370130031770361
74548744245510619968102560132614467500
355339775525714232609913636459502284686
71219485113277027992080840198947099522
9161865693138373723980495927947697958161
7075932148212839472953398112798726787166
659263076707958259201648588345774808
587419324390587703663436282071264862
7994254845967820359642815478489941643222
89397036800422355966376983232191727128
698271614454659777231655688089950811393
7849413168703466242248066668303512801080
7114346369465139305156776569732669003210
88149087870961540177881598329762306048
12553178152935544533494587991540802694
3317977743496754904897938465772458699618
1885580448121232445514546898774153122464
9280723459701731868558155399931949521
5456158469912507694236355328353136235903
615978683111392255583039988847892105598
312873677045509855582756474752916605920
8336115390835837639216858487701658007850
8505761269232231289193562092619223083231
62212958080653337610494989910468766307
739428729965992988652774233027658534876
915870896292833316955936409656328684475
39886878558573442664675991795270937078
8763282350575440431773457672209149351844
98227389913895675849652582838089599322
919012916100372524176356644972265379662
973448767795084854314179289770895082987
2352871935843925462238693131646926734735
6544102511338462838998893985818938985866
4950242760849502973451073537766138198
5974384328134076437528874089981263724335
17144398840456028893750385472085681201
7197629257725087906724065883483830327387
7616248465599258278033789627379923023567
34563789362035350253516987322439169404
9185400280724514385915788524284260285712
7730194247688650306178687964516156368940
2469791821126471434241462953657597925106
8505650295116666318920708982923686017724
6345512151008735566773542575429935928015
611196728071226031479068685359796417595
2497490614145697641022828706859491629412
7925801144629969649859127889518218737
60137799869525537133412987338018271
3107845684698162558586656227861546205572
1670674036996019459838329726477365952209
30838263245448694673603303722864751292
21284474313337249921915806678183315175
9437272721648361902891592559106399427593
461811334996106828514552991355458654240
22108274377900297421792852621336776704
86687683961801624690462458915495227398
496164801218302298860143151206343988764
230405271355938482546329023140061392836
6946172319248903261346696373304498353156
7950800930168581755023401788834637213174
486564345963752439168617931577669735454
11396082546731388497992322638567064323
94046365552245290747184227145854213420
348961166378561598183901407514983712
5099645481196300269247962433949611239574
675489694565549474597576009242125926325
283337413145044873871482100681290416511
44655485447947414979372490456120987981
7054215945796632979967954362631659577600
959055336926480183579489697333489669393
366085383382340244239841722787024622362
472214099894896153659285669921043999
1879119359304544799750907473580978108237
583227059641737396782032394314204084584
119574966568570287621409951242277095887
3355498794741085875531276570438382442581
8096831652185817383375844092597786497328
1993215177161563330892771677514432668703
5676884277416932179117355804960620471374
63377322957699529242578783917119096521
8288468898712469608930134928969585802485
5000980337254039680518232886495797757
36155904399534564443298312354594701329
644288055832239772851575919460558534614
543167892268588031594337296436884924140
926453111066715645446288775756028424457
265124551253494183094594004371686149705
7006845653219848210833177151790330888891
386624537339840466287239553819471756204
7274796657794525251867850978193204729
723051878828843568559214576455469540799
3923439570225007397130488793261923677
686511788393395704821164826315716554
515401329206780543549586073220390983242
307737288074020814275507186671674387193
805676944793189627785993268829766799874
772443042916577723089962840317497378853
9147953437189108900293927047511364897653
527816006677750112084818557487329412120
9241119628219453582208152073646446325
9202893868153303851152741179907842526877
1118785341942973385031852703834013658651
492099282114440311933962044777816052121
8115671889273196841239516360239582402441
321338189930652969521491323163267492277
8229934255980145995704577155880369823
979139069606407609580432965986522853
7458373933943607854534077280814942537465
6917498498038467271833478565664576153413
810035977681186356089873161834757995206
3623525245395317480059228234972547208126
497684472019415397592956229601166147924
717877002903537016089960185484354826206
26092644776405265316545070902747524154
8602731945819198515750152479393703475
1776476035558511104081293537703597483513
4509380537228515655898263597652421802
7166571057737259345915676172279177838
53119476307111732150198120733939993814
8495788327304359732956356745835362119
4933961682953419666111314940217775427555
46344839691251092169903469265463549453
6660945828414167782178298122144855841993
49434714046945527376295794510998611401
219353082418713336366337718613857557922
7145285023892970118787048332991730947138
72452802203665598240753622428085751780
41985141016709424084113583188086129104
5827681450088547253694989354502041271141
3659470654160073128832788185155857212
291332740827526225974222706006559127
4566633055299377184964479252699140622173
282086691020129298475007651249285426
75904321634747764352376937517774590781
859858948145520667331573521435252631228
877917256939606551847600812430145967624
3703593197076403884315753032899615099143
9749620979855175388354497041456981039923
15902115519536837410662083816086211611
2743936512937993183741228201797448569842
85298625193069639336191548034965213543
17634519285368041825225953525677408899
317547456971234061687683266664463828338
9841755468654098381556745981543239884667
1675979321343398537491424946694815739153
353648278947330341226335966811292121177
90845300612877861800520610168958499876
5129780618964582777917902593932710813953
9404702842511591258875969458943358928100
781222749504181279465048968737860576279
540933504350571278606292364831023543684
541883916097484829207465671684331201368
993259249729557331898934901082319683381
749783533637107811486119129212228076131
9838393254426043469755371252568958346
858440320131796460875435389341843757593
993647863270711421639904333145359067
243293589852478166107808662490773803858
9505493612364219449120056952703863158127
29671244668674248057520747030678237137
77943183571468417785352945731419852555
4092468631098932132768395222564299056800
895212549995771279768893930212074711975
6280668443335955487553783107052661234
7399282590562149469731716184509321375100
9571325164159151922886699917981323167204
9847798957708701342459878688687851844990
9974668536268674508348232917798395648264
720965431621848848161069464119930185608
34132349472487757578966648335918975399
169140666597739440440098588693212332193
9905402850413931386361164921244412483280
852072971502215532535502162452397781079
912748802394714860298614650927518112006
74878773350740736833674985931016714065
131281471682861990546388139415317663077
3156201930394555582483734518518074851319
471127222573932859752457170927972874243
779247296534591014147359582173318031946
9154667363332788621836029364444067091711
2797622010637929797518077024507763993353
1157155461055177376241502793771768831654
352247083855581889647307820356776597
276156371713061951264611320490591113611
168027577624207354688316356261352406325
9845873230292618767982902464330452963132
21448975701559726304279590350177673960
3577200695213530278109341151565109936
902314177220814373594732901253526734774
81678312446096144241749903963423364790
6922710947637254683935382561753251645800
31558893142140365044026370216909093375
1655173010195169920122229976430743105551
5042387028477371871967935052500016777304
1226158228347884993779471421668729559
54353189614168946152243373509619595551
80129087264215858677345142889506957436
7453764018206286976680281717551954185426
379193896628280413005396406320069657104
643726059408683781097888714553457271511
274864546292282091985997788212275853983
770542904205216891766084169162661218580
1294122161792594420879167756642434716519
8426739382499582675343311670980820784068
945975437004897376655018367704274219845
6328409937407728134675596516525164888854
208544185682460707356806710960047598213
4655343958009140746023025775145817098392
3445640061972896333463305834775645778035
3464353133382822665882998587475293268878
531177922039372363095799644288530323763
439261134790981269726665975481164432466
787848603316575147657729434549265675721
94929185180381713519361393493629237997
46388284632074067330627485082556553991
116032255911915133169893130101742229842
336186716414286262242497452657933908579
545551297701723732185183103239085071514
8144985266977752156862917648595220451095
644139328761173275938924105067227972649
933479905777144247134739370962189194407
8446240845695438843597153759804791852891
37763684350280865316448220642584835634
6697154297827872738771131480406459592741
403942688226898257309324580164931374579
3000749179399407724224724889225714431467
4464624474078168420659878553913715762
166450808376481888423830859559903693941
73865657970351892528197245104286288712
746520558838348119941467709648318728670
6997189327088000980346589373986113913780
6578749813187743743147611399596625382333
29096541975811778771585908064338686331
2593187599726087216148028101374538599528
989595289699501040934089569548828791098
4894425993652466436441506448473514259
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]