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
83581545970786148092053555876496118609
87424900452603994647091468485082049
505162221525265736458536312971671011571
45756048313583296768389273848139883801
162890285727448472189088421000044506670
480371576217895385110058792161914131294
765532085633058845654055298640327793076
193893115250238385685294453714670251487
93701453921304752379894852219182922369
22017737032913043338852510439327864184
2151675669158713476198193086956776606951
3435675117515321665591322634050681508
184633074981680988965852660099222865889
1617753050984773149729449350350360156686
7693980125907651977996914161107155061
789687346444891201669734290397962331242
32604179484435785396255535976558411572
7146183718691834908578038133169652226516
3534801978947802572363602080109730685133
6159578481846547364163603938141799
732832869138524943586495966708568378660
485758011901260783884759378558041683587
950050579036422856563417380125257449082
3885433953049331100532945814596556102849
85017383717247017337657994314443601603
5585144077549043110338393930797226842785
63219564405077483693426456211928181646
513125129241194809165796527619277365516
134020069489333456517258114187680342228
87138300232769826912559864228636752986
89036286804115712312668602312940433749
604133408112318788467289401690538939153
9055876986167460529076703706762399139882
94184611435144986042266723549754307452
506043359928734898107796646353390938063
3219657529529396578388266953733432783
8368680189613337696935508789736436912477
756750726951390872274139467263623384131
186162431987396847126496075230135668788
14287691645819481494230957158136151565
8093970118396322291274019099990317039511
895294398798514673592003137012621394977
996260154344424935043148659278870215654
63291750449812487151419165551959865814
103993229479836438731993673495372083436
327397893089948934283053047325932711461
8264863713478995879970677564100264714804
4349494815018507498897824716277415241306
2140421076897296317488999248742853828595
6548773154045058143966791015416010746040
78152906882999541506943722356187443197
6257777757382923414678636381932017006
4803735322218279391883818624348717913492
7289582593989599461836637405586565245104
565984126187808122833927451717047385079
4856288515415036557046630045310115572
5601347099799093913321522101407197022017
6107226758001421642966094346715951604881
974579214472101487478561877387184540206
946468105496905833732344133275225721951
503862016026315274282300749021055095601
545399138450787273553185777948910138801
19055350722375735221613665729205599222
636928751285569758397634965448730143389
10677842716551266479746560489265739781
2557665314298068447464783443994440606685
8663184568030433941425130670814184212
1177404615332317846820537350354901136
395410972231890128867117624292345731898
1596823944658871741367018224555530154951
5618340177995976742232121814261633244802
205077742545350028618316892019601084259
9801226667714012465047178631430114378753
6514269833531692887048075853575749685298
7585382652853653073232836882164248692
536771674364231099783289163712161093378
714275152780825273935335074694374851498
961368525111308022709959308494011040242
94213958854995216390449094366837039918
56205681009351662918114549361565575022
2141457011846975336431427236770593207383
30476769318620121571745175668198969388
9007418070209354683168002200230552283395
62939601490879517409355185342496474923
8599612181786946070278143666279911461
748984306173711074255408385245172562387
518856394527844633328473677126394765786
8120919794835334151238816227449475635224
42973514922836509760747993756715208563
303325732362434849235835953936234530
8974726551203063178873851696349235
113170879739269372855534980508917994921
31768261505695967830891597297464739069
1372875977248436411037827260675611736260
2604268292441834349262576715531351306287
337512329061199780579636136123791085902
839881739179939324184994672469836068053
99418105461474211427476144916689882491
84679289149757068559386884217555562961
499664340799492669764663055926086089019
113055272558905596958327861800215132720
184546401498143262331679333089692225140
355456483936741842667148902298383040834
568816881248703029516260645473083386979
7542986935317019263918054140161999464588
5101870292646744780228002442113261898615
188894564540113026574448683419811335803
320339621550428613841421799889353663257
574062041257340131420345307877395476982
9088391682306640603448258418687263165862
991680485240495034153380236219623075388
573173914224300326124297641955440611034
472057376103163031725212358439842518246
26619355912261321634426840427598064365
319326138915201847031416745837967986179
581862828199739973026491827128655443531
275681777397511651154873133194179956008
1802588730476357105892418486635986287
9043843411987983444325058034175606525
80755537774916751580445241346184321256
682275071594309944670417246287997404165
223177502823242062802259634216034126
22811654581902726754945325054877516532
99521966692967692208841803634423168052
406322574873428291677594408370478418644
330811415138765735865263480544897930304
2911877650938768931973345790239688427990
48451819933426867024267822138778091605
29627835631984025782232145472178386312
347131932820369646540356178756198865938
208072185410558589946564554693917122731
710857357233835988932796310284138309248
58679455320137734671104424573617893354
8766778314161581993490653107365987503412
2836472845869260490377868641280473556012
5711284889819465275745597263004462604
1112309810121562585699081718321597121555
311252529442229794980594802979721973847
8635602669073609444366137850216877515764
90118359082997373974456955480695949857
1247228295471233455563031689060391818
430681140147850808457572258809991431469
4080136072042934998427896101159450261762
5598804295506684197065927975381739866626
884916138381810467613835602389073122539
7092921813584539672231217356660522502646
14836817856186630844305971071664343322
8412188487025683964471422568938385522482
54824456496136735075363341114611857654
7035665695857817683743496189108191089572
71715248055368069504791659512858082144
295049251841133276627506969928588373270
8046447013125333341569868078864541571221
26125817178904018728978249342064717227
7174857627695216812950303796150227045423
194070871306117915751013904450714332162
94605074644560229243059343151096096024
207968943495287659348108545256105693910
442862087470807583761446617955164130356
913687064917693329214898234139728136594
4242575359017759502389338205550156298748
322494809461819676879084332566639921460
71566700386443458881550547361087762770
4402312129594104828462981165277799451942
851849093508766699781404069748728837
6515140816533292333926333149274591084667
340568299191652520088162927545619977665
6215480520301066548121367371156349958124
5214655962446915236582146215249432811762
53587856251354419358813353183582197157
2193424740252860920388681069232624612564
96754024754322794424270888737787018689
2038450557655198549195296401381539274255
7600498317859243696457892198301649867199
3708651066726650431757714176489243267149
89395940669719034156169029495657467677
7795415329699885109664487846323220769925
7545734512751657257495499755409877376883
242376272374386249210226025698586842981
4979401344253878132335155348175535733197
310312731351202549747610177853580347576
370370276099606917047736143685441289564
755689536628704669972703654275858985870
7418950979699881748459954991462180035039
9374549883233643569965549961452169372871
6217766491616118726035674387789815119530
1002210214616568147124959215361883188290
958366639363462068519475518208895497
2353365657804996108387796869804667336535
3065846155154027824186096309293170813922
32382290371024253980833451949577795334
29673490704581806484452280696540056898
897863387028272412945774061776392094539
90422405298152072225986832284482337725
1967816230727623701437681766942659214849
53714991775877813773871794109173405510
936918916531807282877649670196546764641
295073983142866394573529202323463392328
81501277741392855621484630386884419497
4932821368706491225935138680923478816507
911518936512203061692128785784162375309
12796899273595010357263309488121973722
131693718125715826299887969109274647
2807714876765747617679273145659511974105
728440733447500416278954697040930339216
400545384478369503071581563246326201404
257881359299327561030001490658986535540
4424623710422391406073788867434918779053
30584327329741965738362704395569683836
1392128263249317947128668416330820179400
2764404894347161216963425288492770925394
497775311693599471102640864267786929145
4877226283531607450283658730581614212663
59172646407327623372601214386380287249
720783319341981628050031242830727289022
7111297847941351822240759466905470836945
531686889825440475878851250718493796542
6634851166861721459255132681134616341500
197864154649608295667191965980132227599
4098981076874428411823487219281626385298
843981487881715341727619805531496995714
6274396874328681427718793465069452689
30193996797418510462498538955283504118
12414877861195260138937317174521249477
893997093242648454887061398988120684326
23793875849422717964191788049213500916
90183547607684777953933750674099401806
3948199181349192827771774552297663429579
291983748863826896598259966404177964563
842454437965275323882291965628748236799
80029567400707441818557226172175977257
2669105023287758305121969831138375542488
6864754774133455603685695986897380136856
1264677727656284499912183435071110383
4929192542286401540338065272219363239251
8856781223095109203898535256485967546959
7896128884137509338669448352106540437996
9144370913621724749235586714456676338997
311335113758883832893564661543273129653
9183854639972001152881456479630922433120
604429715753576710722018773952031737131
87802003674415077603539341993670461181
65201403679421216194977546247857966533
9478161923599200824873462339999146296389
4796784753098983514091685358563292622992
5607410838722710712313946950956994158700
7197799790911008592116796951260549524449
300956059599533573512312620283476376759
4229523529897586495190851888854413933300
564735093253461678885437783824389559989
72547577795801582921858297263077297149
6815724176548477987099776741757926323
7647749073143115356239967731946149975707
419202370535780232261529787900044895758
227470585115740842249665217958528447314
7941268810726886225298058867524025265502
2314768139293499214260341631129056912771
651917287489457863828424160651375516131
780932606066627916577365732937212643938
41324496440288942121609504421988443002
834135842178439333813069052394258737784
713082223433781443196846706271999841169
156011003088335558922598844664138225843
494190931336232546904778489207276474781
1743809365582064258174934975218934332
9722539640287113547623924708838311493727
1772801481107902320022246789540496005221
9459456924241665369499472466922135889352
89449404714126810672346874190509868336
63118110838438061899377185729687135828
4999252751557168303637733666121011788570
3528388016123770707821822523139268001855
7380512324434453490712591120470787782258
154321391760531154558797612679967905428
923774493904456163325442700891387265374
5059981684534870617239563134649061013299
265333198864191678779513381573631516398
75209847211510820658119572559069923218
94265334287615119251239753135223193851
9951642297429368462567973766995167624928
953493183542557250879973085991910095069
480534311505901047547999575923873521726
98038365985329953078340052561103482718
97833948263481502788446812433737021237
68228261596462682952905675647662496490
983711252686986690048396237712142406560
5119120721779957190351043200976479827065
5486789751437841248044019779918113791920
8573945814349376812212452587454831389815
237127981188594676489345373934373829214
526834154380208203737919287754288405952
18729782938946235726389619951994884928
56513191764102013693006360746792989208
38986085633213185123206605437662433022
5549216138417310648228924741909335988739
74692467161235915481389075786276909576
2337563538827793229260628390349829825335
32264796086199783745713087499610556652
39466072173040936648909742554633097261
864544272266763827554661776445531734975
2459833247293266265575247317201308743
6232955138331077937687105319415891239403
2136767753421815811411145690985549802867
852115008367860269603452151668666969680
833115231322378635152392886398432741407
4929914478037122444853858110031503778
398580202745508875320124896771411939348
8739247251911410662628675797675626223747
435265482097638539316584351563547124366
3177634662759396902796782220170346703815
503123553149682119336460361907160629741
4053788250381666347339501001837029553559
5086804796224320597437304451273859318371
4058821798153962662835352122731830591269
596180257200462958414139575056791617618
868630931033628493548205907099256373842
928388326432252447755653520355286787154
92413348267738919047088092837129703181
5239789771858083879459198450203396677880
647123215529235333581916899420577032257
15334223655340586361839613467951368692
619882442506099501341613789325023072065
45177952582827765096561413375579656214
63863471979942456757376654180162816732
852041923120658219199546350819773594763
5196111740134294904916979248957927018121
2858231737534447132037706727467725996853
919029844332509578025081962489180194976
526779003108400421767927493898091643366
1443829752855748734927475544932441436544
7970823721865661865270035760941682763235
732953755478700347448725687385459119018
186930843937472490245872538560351897815
2210192322181480228355827760717364361995
94604176701072277927651887805961918607
3772748868819244300111284119293521811320
3661206254384313222260581382363747180
323584484496513220169347027242335549834
5375427157099543121852054594542325361
8581103424140259958639407147936817815
47605996468983694063214133354588737257
31360383154721111717224356457811562254
87242462756740725041584499107342625119
79876590678361931489670239389988809
6826881450619715577899141725106414135501
6062877486906909850142241671486651164
3249734227116824117562962862481069164503
8901212294085338106495597576698073477523
3406762416324562516786442423678361518982
958257105056530757296632398245810978987
5249107672875655296778209436443821985777
4266843986067004817143386387980021084900
9395279723658985574054992400963669115832
333055122782583693906856197935792353349
9727518099517700898043746625587689722512
668778487837133720074399853051094154951
44969756557523286493750969318590447558
4791831671026594359704851725556844499
7352863157952897417760896179680056095297
985754655173851852239989947164791553464
6226759371924149201819567625667655279775
956477473352528734998689432956381019771
9544726043888926199022649343151312423941
407048966046292463372786865378832858198
5918620258471859776073972967355280652536
2853251196427937934076652681321587558512
26039817261706885527612631158370738946
2680987756635897473010754954430122702931
4829596222498868899823823193766767189629
94081133882368855799306074917211583462
7289310196044924938263068518730248814008
949258366739720319921207813669757623254
578042786749346681897646733850241050130
737561106919198549097105786916329239580
250822631260596269208555906618911055645
78892641893295115794744256538166434625
261543001565937230154051284327041647709
2206880930643264810110467018111832699996
778464829514434690432433724394348299602
9045180015426720265560824166633925363777
376715787974268542009413567312022006602
7757601055736297841115167497030625360
911095788013393891745137909588796093978
935458856092563507081362987954635616908
1309927619094644919636542002309368035801
4008604874433861866617334653527597820
4689589878571209678371386732206341063552
7928280962765486364372568213442163467105
6902574072198530827989825161818336352627
63485963664337747098809422711257516273
9712206969821778407235127444600387263651
6129868937206119741586122674929893531672
57872796429352788426735149613054179615
93807016138769492874164108486274113817
74791757301167570757160862691739094817
822169283341019947357253522968771162374
789980529267406903243433530370272975777
774667822974588271726826445658568689676
348115041837962308831247019183888459482
32359322808205992651448643647299773543
112271519696219941643559743742776662303
7579522153769430127154061180443243774601
881090728405548315102678783968924303
104150374715844580409917881679838988962
1728517865554268460741282219755879503602
114565649108624402076554012744543018934
83879101971973504147815492976459949656
6369467257542578778693834664632295395727
311685719470868087361560698030884106021
941904134331076477442201067469520599350
202103119803291894369556468272535037072
4916494665607513666873157416331942541786
204273266159757992103355785590335464564
45272238407641364953923721938897865203
5613692988337209614033702323541028015988
989436022909816811701560835635577546
52844105570875629497705244488639627315
1391609071864330397341291432131138506166
8945672758104063357225667893914641737931
5742979344686313838916523798349615644
881687038781823551919192238039881626145
1559807494411309316382311635764825961285
355023771465885626293903188955318478198
93515678851189908852906891931447821690
6655440816193680933367241679676548695539
86770184605345014556176066244659175769
83520572672909011235205552484240048561
3233532547711993158541145023851872021
72367236396089382679668974552238502942
6996131147291533288482455147865613767717
5677347745767392722273623574224295034
53732696575856764676659373913734596518
1018400787025829585282439540870998155147
933822441066769972332387962466627067960
681461876380660534144199560457636914788
34740254398858972287009698550835186490
80524801402646595115206876974259519222
302429642247160683659369652336445895374
202530108108523412444298560469442833356
604211003236607118341526957715877762430
436911046763938279956543857474627971483
131984062917262954486477130180523994157
79033468989056637885627253672538047856
3624485623566682955815485511744451601114
3707478856852421608694911345739057656476
2580367724691837994183416761755285701816
494632287094225138593187668203762487103
42482588568337089226490500725359492112
8966346360713712861752384687558992626315
6725437560646351697484512602175763305033
4903777955389177245340073795909953528529
8337315844614781338451865493844818040
636239997435717168976857411898352059184
6385726589734977641982783525537464364
80845735870325355187700641347564368268
8890715387783715755292974521106526471746
7970472390802162350138705771382621608916
8922816849056819372837459007973641658505
1924394035488768195225957007988850338658
46865634126116918180148740303923302543
7537716147771387952325089489174327807051
4937623840562055194527360565244261377
6420146281527232350748156426555226105491
340035979687361175739557147543238012414
78969425462698647008008605680122032833
27593619977986284991343657974048433416
4814422121991949992962164972452924785685
504719332022678048388609260631459413068
2863968648302934668387534525140335177120
211253693554576731952186830582253485056
2283755866194677820272521018105638136999
5546826634775051959839252808318377398086
113321685181736295417066131758639277931
164362203648961505436657157487191646
2840381468238541797996971714853785847984
2821489787757060881369709302565461837380
696071561311509935982896518669563228767
341421597746149315491133798334686807120
7839794418126040525634661514475344094217
713126458328870388745642594901412907621
895692934903700683620649961668353456601
781013116891439961492125733121387366024
304360790274688520788995520975729157314
96862206623511811882986299138256671319
350519961142857876597953505572245602289
693438559897591698984808365952689018997
870813827504754291305653968310986989443
7890821197665748682516875466552727768156
2002252148902375606321731828143686346196
95411791613972874808426796033186643419
129226690328946991028889897115466315177
676632074393646973255812401896022284398
255629561043422119036987426305867384073
9887925127021263360259205056368064492536
12194594144919254270844026171195084268
98775636998777977031408517781891003846
35462566710602952551053420834769943423
6722912359597613590944396605862182233
254879480795613176210273218145164714218
8732425567711708515934027780676623289865
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]