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
446471054625144378221166139723127762628
676869591802448130450424423713153239119
35253086138243924761955559625672262474
6480834867039433986587342717586874668
8718659432654548685211897826813660094660
7961408433357116439750512855199075711541
337516762799363911564591893926847086289
528459241889635713086925834451227471391
724989804602690974381436216768525643364
2692464432408575483578948485975575826507
420746599425591633241589921216175694340
592951695696179454614001838626540035548
8670880487783912482297189474287677557169
2498865075032791228820208002508177089119
94324171577056458562843875018654919730
4837518299295467590261442891185354717348
910589416668601445418535872281367737959
744556033535133177454989361352634145861
935223341883121985538508174344097438327
979355863797830269095201385931027706149
6105520518761166273169694771781853264037
132382331739235649592989271485283013935
514247305756600385652681378542497827727
3663502907394968877312361351853527337
21862140278835213852164270815081204954
4727970773186411431277946971611697569446
9001337086214021130817351157623540233364
8378487831212409403952656415928548374554
8191151825992569437176798361665749593479
9585780948411842413138664401427134853594
3382421275229140782133701215442566593691
680396307395893265529125337926916194496
5013134983322294710957768986491657034921
821873821489470643535028358401479502684
573185053125981353174016309256338924692
703667003682337417351398116078261332056
8461786984867343212583423892148539922279
69672567517364434696606284998223272532
6035153673975520294998765063718639028288
7194350039804357489866242156462614149533
583021585635029926364586563357290345298
726011595856756529452535676023867776923
14607095964188261697817748626731928700
869967967816689428974227320557413249750
4310831314224713984990695939720194037621
657490647597400807985671282835234927371
43588684836021217014053339460789075404
680862639316787472633398293651138370
4316741266017242755397132467299781379114
901341093631473892327718463671728521263
76912944496304877080906899430394855903
643044112968682693647932347966988143916
804360458630189933028591108556181012184
3132707334579059303598126717990329919802
2466685481965168194435609255939276283770
46208162853181675005801519385514848050
24436541717869651039331494209516717748
4630910433502199455620187754163475492631
5578236780136707942020112427506225195285
773132196205783775472418306147681732216
56987217323965465561995655900996404145
630150486378968078678625999246359196752
8601326842723301822436804584948099964
1138221532347637308098884575247330524453
1534705960635491123887366555160611868341
5706204358567403642367866931747757573066
9634029082330144564036649366793884727
483832285513993575091587134898335034204
88385761944134238784423201121150513354
51846602833671157363933359213610477789
415676905541434487631559383756071078100
665816139373661635816934255961741234395
11497566817163266231832537817429522729
694485298139387585237224265961016895526
325090827183157242055176031103753019493
842539042504650388782903488518569401346
32861465052491999761865702486312355155
12914354745590991653576905789994342966
1629722742751764883457164574963118976685
5778240245141302959140166930906543575538
847589059764667086209129595966267476163
9913987874017307805863835022963390212639
676636045666863576044063739892253958702
1084396660577642892981576765125525124744
1277618188167244812038694937266296516
4689524791076173953445332668487713682099
743348981536915091361691567537729173278
75668527806351293237999203523948355570
446854914127110773990858676354189477689
1721926922995073462784127379690754935146
1027513263626868244986469399249796856727
275597802719206499896213408633729665353
4466302229304069661587964990359914239247
5824293191297032327213548105424059702733
5286323288238987727260543208519033316547
7365785989079431467397079256730093467222
9585276545142487769292784080974399248553
756318747621387385555013770458173725932
343995891390759361641186622379763456425
4266471943824813707964734974184625950
417923227070655512074904502164542905895
2495965894133002387165372735832288653594
356348968562973472460046348845560327455
5269173177245370310127695335510747548775
69982276738383895701725061744068975547
8921687274389402966959735633698957053501
3266718976707825654195018236402056919458
633020567342414462317226303724582248368
42873728789846429057066416400011219403
9845127875772021909241333091857966381608
3882480836182398902750651342831473876809
552453393755574341347963911161443833921
7462404118834656388149828305958843922468
56574263935427252941550176171149012310
646547698109212553222586496210164361440
9410535683771744915476111714674347631222
9900889292468053831873092010278641674798
331950865049999756808396620823024421157
53605528436860964815213102890453998246
3230525993894036739566535408768288955652
187828048590377278123303652248068952620
99147388604388736997846951244975059202
8633729642281104721686769172561557643
77694716204098335947834780987181707612
5172389214404939753833651895620173119438
2571314732377883273945795537410195874175
4669312957054425756426993664153854103145
4222244521589783687582917653120581888092
8109610157681538426090371924622069986552
189478175772677746848903426081724761693
505779376307596769674815211349941086819
258059625489427621438393628988197902679
296948359253533631867721794522135725901
899216522836870935056042922848213887176
406261978258397057779544798764834650854
2631768053032365825940823812936502128
722232628778109270069239640586453239359
16802774722029986647703178689514108362
755523363096417828812969929055284186476
7211895274203216108578066246477940851820
3455107389815663508030709273645138049732
186857046422734888714864768413525353424
6878226271498509571859749716606677218616
3578405865059942935375492180682559887517
364916171129446023755770864188121041711
6124726221257664601116559596383823935
9662105346354407289163524880276527254589
5830643648931263371956019385653798686739
30916315221688891375459860401116377865
514163754711130148089863759480675542881
7861980342818993277378769514547639169300
3719414199071541397891572736833174849418
2385785950467100333285543514130990379507
817324018157764331216188836745989262023
7650240046002149951913723346653388061889
1261645970123381299397751632149791126671
494360719177599489736778032633155533217
3462614294686745119650082270854078551334
5066326279552232635919268786864286241156
7987915899759851313144363300706728795375
5592237733523670452736628968399276156230
533010271911499247750717140735765156893
626048646935966218804959760522786365209
867879582794229732971681867287452894672
3352351467214766573672544706876397471354
911059261286669835905613790371274162897
81016345488610097974174774583562127782
680491133545357945987655794014756017984
981887737728387042295876883310621952396
916643736992647916492036363358463319724
59035111967491751811593405758114412681
7765198614765587388044089512876181885
369381710442943321576837392151953715237
340137927239367120499671236584863334026
286329666651730653941932501831531934291
517940521148670922325861523203536502331
58396689725417001613060541437777721632
455251948394621648559624653914957256263
71961835842106806197213450994721768266
449474093536259777015899106619815754847
15628293699934125266442653338401499907
3668483610469287985595735510732161348520
310265108675629448483575224572435325388
22156477585865421897152286312256452390
5689131496141864036011001912611061160
6547342692795025930542591479514294451
181240786716693992239667084969082936975
196843412634655098859039294384690452390
790553549200217958317902257767382434639
7473349170489219274061219152134043242794
5976318220007923386913058285440189566586
374677760006412483053545648199455279821
1560984197873889987351066853361433
502793833441238613719378898190373748387
7191684032692608183678762317829871338277
2051650454465071605518516827218234079825
9276161813360970472677935191452896572
3424156632268413528138058088976804808
207750405149809686013132666469641364391
641659301563452619609718139338660263111
5082631563987819715341109826193382182939
785119054610212110348507936263384068585
874649344141334126839858509726912199703
247946976695888163285151174886968157207
8425566212342551125217913439825472082625
1135403421127123356216497050326831843165
4796781910052322818366980945362318624
4579910623112418901906541338039571600
2192155475299118131650584919826362413548
538344394229071549075632377288224836
649990528391407879510907319268773001532
7493111067253513351690922023677132966291
19623842428422612323884581244245856088
1664975815803720159062054129602633231070
1528377782718178744484846670763248872250
691624835716869354636329585976236141128
8501804760808779435426435423925264175
4657757433028496572984606480463922029848
153475576636307720548535629625639387946
5976967942931016420061917063124826442722
9269706586797082228836880384766702598
4440242867299059332615671281897823129761
7773190817841629950795314708471017997210
82982749176697042855417229228874962875
9253480684767958805790977492184159712035
15979094486104581210781651778528666076
941434796239291751413375055927548252003
5846878934105828969382948031367999706287
6369528159817494358911921167534067559
41953275468344889856878747820458886374
8906405744442165190348987492482066587801
3958322058335607575894787639469422164322
67166567640306124128601547902274736555
3389944223917467981718288309149941596741
5581784057962718784062605172215260039293
1427249726901876890816157372419487305061
47303842493992987231079429522022266958
8091268519179041240963595161323953326984
73444110132498035235306585748067327193
9316265658865618782869485866135694805
385716347326147724870879139466643935
757216317657373141188795921978535638796
63912337530815242474876888336013645974
472318378257294178644679746607227286771
99949627401740897661104398331086496886
210379261114536168567666956112493357454
653313647956517046552821878129288402866
5466357259331017091714340041651618395
99721830065846503329537385766989663
588065927533372998185603052224353062461
7134729868745049412082548485545852456
778574454884901045723414465369593627810
36715379689660434246961372963472805548
779451225593716412716741288850956131882
4677765166458526635173342967250268733031
8913351225914744554795289912460274943009
1606527553886649985780561432645623927221
12776931926598040464657020826771459947
821861083091757779456347488807634721347
639460241466368595694376414815569295291
232381724052458678914743422308669591668
57075284147368896732616426754690569059
666372736933577022608867238540614311794
328829006117651271296502403161593948556
3139102498207383758319073664676675882920
73882183392590951992590876059877617
629988634128738491846505859597364508442
2533644698357362419373778092801059935745
203929288779803216034894471231383421705
27418608692663937443996264462528833647
8601937155465191594941017900228517782168
8352429172587054416922226344273018
983937708769650888847735692263464777
7531294984145617299971448019583085145481
639562394702281125049317668777379618192
4775662360503970680850898583160355559541
11745629653059435539557710204071011027
4610709833549755454585605214110315441269
677456941463650439559046190788941454842
11681922874821027537995952261804504532
415689837436673352307811668321374611009
612182032104502579411282476449716649521
353177149823771103326379423401417426330
8783425473765236266574888538948755243506
712760515638666441361009513741061003033
1799913978513654397587471144336399511543
508874160944351117475858981738942025924
1868939739145656594743181655193669097
3745593530726143693139212932763737533964
4841526088035375862241472708900114853015
6205934237118430658147670226729913226
749739521486506192070659998607594209801
6890913782553435124535681080106513387659
614930479571425686609206611492028872155
254713482013765990712225167940859732117
6134500727606324324421436178412582647165
998849484656704038513923165335019184150
3600306816765268612367342628933997443149
7704421665668461642897976423182573771188
7121894253597167659159682003498880051831
645336739487508799556452181975806461187
6221849955254723201140048123422065263626
78527854460763166095980700212248612925
470444834916603655317041383383984126185
5221231411433098689096691130550895973022
66767675625287524728882793592473142936
4608528994369994206036834882342726783494
970708883868180939845179211351365607357
77996181833986044626117238113654487753
856854107677867537954552867136510364560
7102443672318612832466277300495629506925
601942244763516242058103785131295341532
536088929327791599611500243165107117362
1341267389204568356270733331427899126
3076736657835782428477202653660114128260
305489657927320423723139743406823609365
749230089770856116738081364458569112079
4858694332412420313543401954573202133
9077951618765355567762684269977570887189
693336197835935534589449671620540605417
264995482934299831559398349390947882870
5640262594116660310475347111784483249
950534442444947981998218253738839336525
203452185571894949396218292257542775750
239392662496198040076658362461063856265
2120461382922205224281616081741410861941
681156858833154145675930304762081899278
329181089342148476873841990351665673657
267392684271129043216907695779623177058
6954292343631950429578997953518093924544
1928498163458276678451088984770662761513
60257272391658378019657231184832326085
9180352196076129161611542975212661304
9905498519139897696389811401259382151327
1822624063345739550137475036575593484730
8462664259711424525197414136594498329459
808762318327401126598928206493686678402
839275069732451692533588911491248272141
3652916434217042122163804592963123946661
780182945530215828623176803530382877898
92692495306528455979860205473662358791
937116368128125382546917897220146084163
8055596014167207140528701025691550584629
10274114392383833166979545425310764795
1594928736707333529750057115186166109599
48197315746430391902251372077384134
76407955570217746331374257269583632141
29914088654442281568524567406364731893
1582174048902846871589567171306719682598
2233372786524033454834289114742441457
80354163525935798682926513953444028306
4562241276292854288715509590252347373
3701928180771569279039173232157559678757
1858881761063353315530200807887785576
478199434574192143412307107895061621481
99773167964108451574772837459614352453
8788540926132775865687397545771662644608
4519291486209271404235173694849996232016
7203572528421397431225681551553334531472
906778992395718891880891485654846373848
1291130335052503306089321295307518312491
8195970406202528322795724887656891020
17939207173811989411997606906158923
1168561070435206261327143741766630411397
772765366561546264473637177781220075554
519673354033180026266634741189246295621
18621610385590765198356369665734233491
7921718793818088200693645075144058182
69783797301766637416173806805450556099
99731186963757286175134927498515725202
369399261022284049438642406335774756552
3223758181399405781440462016447578826636
855930942313935841245936596658570665209
5799224874521879816892831945555163698939
1991239875824117994265922351525594262
4645152345864870275758892165247127469759
68564298491884166025817108193958338676
97528832194516677399144991540397418270
3029102086381247972152416787322490012687
641548292618408621588284252383851704517
9327145356241629290780436466247583122705
3436307518249225871972321681722865462998
4460151119958975780695955233576092544252
3662576575036829809343226926161578969209
25025839715831558467096716444741791513
580986034495217792577126378773560787991
492991522282596248476298846495015263362
9374109191674112376968921397565487566939
6102809664817321329758358430476831331
37528934918990136427978931439578815420
5912877610958995463350188660138779924674
2822297193995207594740877199616620193005
1573816296498315536984301409320024666519
4916470284842263722433315519834267262
449243777953716106074279471518097489079
77175559933212696769172453205626137748
29743091200508451777439282826457270744
6697842674306667882938150633135642437
4840883628427493305830919067465886935171
938921774527710851477021191811239448120
467423246438291119862009301763333261750
43997495360975875520746614337294804240
8409739210991510229755892429591921373571
3471288873167041267135987957505470753394
760883871403613517915032446228948946305
859861685359324455988374123119565532471
887834263390781638365205010779442411268
753473507450626364897108253549062804260
5979747618112630797651009145668177554368
966683785556476527432728128628194432341
85599901023981772830765306909876676
296590492655278353518394563358143559684
59745684025746832893323862461935666509
359026168444043770785629847821729516752
272471706840275742971304770285236201510
975083342633786949832728394241458739557
501082609582557896833619085552693523925
10208185839442882510600350618026521241
913277263924249247136486132650199130403
231249626661898465349438164571126805750
9274198540164727807315421339658463479737
320359554915570156782597444036567396279
94251940490734526074890961614417836514
593431744478671962312656756625545939367
8523208195435132437329924613231721337087
93382207658381829472095677502797606383
60566823228304658412522135248469693326
3412232264482945945946747987103135996003
3033636390337933284377351474421758685829
8952585434155824393985439557755783138
480726271919659742848116788191932613175
35866222612938283726899537601820562852
56354237246357445625284810324626902087
278916441541185286314055922253215416035
844493376192467325839169407259157283372
563940878753569200318005568646798138110
8779918511595898388091037640550080291466
322353677202820578640801742228520149046
866515921910583842438406763569013919929
3404881039176200781558412584224009472
189084875996682874509933382720937929369
2570794848567016830669048068216166989
184293851179955215967477956262218855733
7654886389401763410739404760727321512812
747789582313532253559279969840995383146
992599287505726156450341668968563981077
90061813871423699742426336484793546978
320983686390475945325142968740946253572
75654496902216387515107647457674855278
911921959766492747835409838642123285393
719399083823874551419364058608353557434
7655560932508801774728905352597878048966
553260001141393417414308146621920156851
9949365361736551121023087969225723532218
4848778144333297904560156184738922472
1766651297099045912524466043835523569732
2810251184556966127395601301809022163921
3508368769141630301350793620716588475114
6657484625116135933944451853599373277980
5532426494887700800396611451248037125830
4938229257504755591621994720600275509409
357943132468858975393304108919148778236
5077443952553846449086058520590019853089
297060558837387112973174360281846633786
47797048265757985753089930707538651102
88718377218722010135224998444769573637
100911927519419325542188660727534569240
8195506520947415164716369650635270776604
6547533226397798684530713080727176405354
9307225078571861978397374345514823621524
1976968068959414882821511450223723298568
47692491866621984258846817154853969672
8339639907248175637660996626481199173
790961379903176580073639815218292569646
51814039194134291721222528261803226056
6585733217525172743462397902139216878095
2727452911482784625838488631751183671456
24747445541102391094053614468287225480
845410395344869531875152614598242424850
1822196743624152501590841346468693001460
4132872572089011997931486629218820611799
4251318671935824750534157212505385633694
5763364045622082968597704722223986357330
442265695686436834978026307429660593583
558577422149241929648911388876365905661
5090711382319979609929353220727118702
7591493230766007496242356333519360047679
224870892109563284545458755456437958759
38632601533407120752847388310738238621
6197920795054639762920549794689779836285
6358902150649228527551864984224260141903
25487167533536415819118593952587617
996660514252990315482761246990024337578
314044569469164017011271326852019351513
2022513456415247443435265463766162788747
1351898852958969660083782362236462076436
36378853265924229610994678834743736253
853890176827489254811116149314747052712
9897696147396722683172575154003400932
60163031416609967993554436918508410
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]