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
3183359776147990119471348334406898186227
3018642788885891758127784004594491251708
734949315536452733746661451254824797791
559670152265952139992546263642607387118
388372829569731566306302961596357213965
52993076352811747203237986575657367915
4448933961026048853011529337646980538421
526773223006091750091688902965568564383
314026845124781056329676751581855614559
982514036279956620984643162931243222423
4184480633498532229684079239191155682316
6577762413675465844747599729229368429
86647535059983296269355954888877163313
653020315633730716464068115153421947242
60973439227377524429918430654049664533
58474319986322538379117159158090128345
8780875359884678849464422846930629309505
526923149646683727275847269171569329367
8015288338266489417555899304598975475786
792722129993452450784804736418819443297
28247423105776459066474399322454339498
5363722151372088731601683988641203481
3271951684809641887213456450411149733586
3060695367101059412738182318676385977737
87856688104771580178820203792448028876
6309653270557955875623968990141647323243
619187093759165276150002836437084637508
754563223249720250788653726811211729
5237563023808156504311122951537228115023
2222918711188665582436787885254293646300
299656721759157551824010598917678117435
20918337214375819314707197406668319727
131568109273590264557226587546531016311
1179481722139973108270199421219595514515
2342686146227110343548944001717542041709
7086655685537378993624957290345162777259
6749860793274567893727358496868535926450
4864621414536104912968452683881995978173
40461491998837956510391327197548345022
3068369619528245015981350196289471347
80432144274996567303567261370194464406
9161197992831724240021091333319196947872
688174493347650905741748821384720111931
844424936501174163868060149336853865597
6274162452279526263760248247719869654849
12945711706388075805386735388635628947
8825927367626074308164784386838679305012
812362659164339742029956465648049308291
57030658274914929128614640552455795804
275534954018255249249272459808934995790
4957611936246550913493532139441459744
548024702630379471775387702392742363458
550423898004539751423185871529323505682
927551198248825104943273022787241974998
50464903707757911614388519233753454213
38472872846928392332853715628761033933
7533159966661672926090806577952733692088
562570959039910281986766752856963336
220459707919322548753749965924469184536
764092551382129765930972009243062023035
220303564759334651625319250550272587182
9136625099635303108177282333558355864401
147550198028346018240355991889185476461
776487914159990172638405291220672752162
3182167094821616854289419133471691031230
268875481784281923945056991288629328122
8743943259493684223292855383125662639091
834258901119326572505191907796531105707
232642505674589367087772938631180128084
9054626972674650812148364065961840039694
3989988066197012261899465151801684988654
3013500599484738569858671982515230235274
686249215520594087967580834917555771884
392135759806479760725243673826136297416
496589648153635274786602775696720965698
691464583165560975304576754744408290520
23691115251659619454037470221575403277
795832899631579752408601788421220419245
431251438492500626829944318843556723986
839721318836312983321924553706675491628
4275255815527644602326206693979044777105
10137227406924949899623601501919984543
562272425275170620351688269193753769245
5561844272401003290988826357456461041590
258880941899406163667137309978971707
5802455052222391172919628515556994613575
3593191970785700601420933511459484895510
4764633019216163865583975087672968847284
8507613678774424875447388761101094112462
631063886658207465282232192776414439749
868895579091385757768968436592160388455
839977617511266619229828358934715595127
56133602813503291181917298541641657786
2124681567923552119722714894835459175456
5132389156595603875012496860449565308408
37688088865520057667632329669122796659
2827651784399560851795802433741557603211
519520128391711514171228394743747078058
6981235663893325585297935196777371504762
5958811465217514276345689614256968926517
7675494161639794822615988632604357847260
7206989566327539761440176545204576919953
7502550733147308272788268797834281873374
532194738797821272429936634326961424905
4346954566035577325782914993255822696986
7400126515749445714961988673715012228
9692185382169372388457209429893853859672
964336245506111351057806394659701636383
81092559965995841521546525645916041529
29095533889761482021042595215620999180
2003782816281110135445057931420469411073
3369391276746888493496892572379564069576
4970883648729371202192514932342023964417
773155021174314993426064822880092394807
32622907703756436735436675067724467596
430789047522287410478767305543674987499
218064338863701640458796124314737659623
6157141123029461710575785316697698824
57473870628160588882505160169856896577
322868801016108798125154522913022411908
548145589943000708450277725922111214104
7711842442923537448245113873244285173390
57854906126043021215769411799845492448
4883529932506984693153131721191573888335
75091403581121264924946574160290913148
805839637196133979524769306983449277838
41926973380268147612317465160818355369
983572814291302656093625191986186773530
251541698379967400463164283596818579767
8224102350382956433658351572307855648061
42957164652974026762428727888051423640
660048716833882253358503584587012948096
8517783661703918865456987925352470069112
7239112135389767123054771436640279506403
479130273085288382965706381437679771820
140839847668469288410395221195634623052
9384891978198510981385336643374141801648
3603278659317037959095061058695369535087
9108449658089851759287597872415877654516
5916110627317571914776406509179150632668
120347404970196156860616366760386417
612711564564418890444696070621579558018
9538822730216223941751582849333276347
270444379177856109152914245285263873620
4678883929265416740750029277120920915798
929041337009170246231243164843568327362
9793383337284239294389673943028622443
22524376654494019534293815037287403465
6700346636317924166530566820713072589311
1322406073604993124430131575176365678477
5809511487349177541698667646969841909
308688126945474971169878567673726341307
2347935170063843118078483288691795108283
9162853354909783899751816280431856772173
37477608571324294764674946603644438967
973916075010720687386869190727353535735
3268227694792170816210106662686694151986
244554956795610996874712112122277974396
27531501535864896651637985529026176877
577932632516327497188992692389647695215
369444854066177676378415827228023486521
4978372027273719525338596177513851463800
596879121170579071388768011913545572650
111469878552571419275428877813328195
6181225242319489672480827850521295992
1095736232637434118379062484259514842142
53102836796489719451331528996867581241
4659411426255275754959462489981650515244
9383649270377369024916623550960826127
7012743530357247409770940028851096373
75273297589103310438828929370493087222
73125183786543557613050712135787825656
94121670857836629072189430112948356473
1719365655652322309820761765792926573423
28027267988333177070118481683570328212
320589414107595612673792076447773286586
3145777279248639901766893795618671456538
29508046651991977345083970376518225712
717834495130416811882634084339512743772
6200257593575512258012203198815377613510
5652527346483998498843174679130424270
4107217125359811297127427487208798288886
43282661919569734071505346716711953714
4319369954762570953079704524302465285194
606338458225934422953755657540640671510
376276758849148633919629508628838249027
8848227021457601450671416092806255437712
538570932997466344912971020652966433360
8026643729357458579069674385229828913
5335082788280379846011288634542904062
95003068752340533907246431300194379483
35758241896999355781496200102072652045
7688977958752773533275672945689584483269
779879665456759630849003110910270997406
19729872353274682375167130161952560465
80604767138073361048677666737318762255
6867606733799462438279229663924522583368
84132780456956291584690185306635433797
814055438030506834424578985474656504124
3558205797521690100356095875411887385296
7859840925476601960847522171375979426461
76793601824353058894076361025816908290
9762821615307562914038375130724616166724
607285423475501257194301732386779912326
6327983347036107431343445615418392340
523774382134471092841939616925941941459
7857495915225147725351531998210253539225
7172232332372825148656446286608839556981
679960632549462772047564299781089161892
19532195924745216934129237988928427096
3590803252612226278922576981372920436543
686101727338185278316105661230834326620
4760891124506039536359519801748983341116
38928315306582983143069503975852124891
2354998362199591488351232210137497968806
46939639249197934816288444158249723886
483067264421707348749005371806056074140
791799346312068593044238790216830586445
7180774059306882659966152401484620437975
6368271939039176103264156114607644675884
6563304813565467614797168042838588566360
82211265962452108200611312795990879314
30824656772913502162979772706544654816
776389437828571718824432702577832950
5984304451228127362766409233156662281252
304159692539243758728048394323583195527
4406509990663665448637235961422771575647
7216406625886973357861824997949840257426
9380509215104469815717629987986024802465
7527769165257995475371232695116422426093
308954445101662948969112613790664566876
64004070458381427408278208813978919633
770975527238425177830824997336790073442
469477915086377780857574535723417985480
585971307571706295710041980795833054519
2452148067621621561279289119928342399886
25876345923853421694342758285158196379
39469694717645797528353557746340216981
33917163279345133049201133373392597877
6584803218177546822245781280303980508705
92838713103890505064636372399842421103
236974861075991230019000515765827060
47094624162439621689342623953780199114
420677667332562686718155043898916637
138352149832479582387152896878078455376
2528664727606679583287395848461511442476
9840410646469042335944686674505246176
514548594028684169457988877185164067087
3661203365446274412590978272958153231123
186252007867559714833454625134041777259
979162561503275276461932830552866703260
86715362449196314316824932685012152503
54366539415725406942517248578711507138
28215424333728157559788896795295663292
8745438938298949902255351972855350365
7350140780041017496336687122572509515
286820239715125686652601158279474067213
4124355924046874631462995470675712256353
630855459436216412384358487267818763287
8150223172634963407603940014585278446
544473515387101945333188512776528796925
7071535512982904457585332080101249815348
952099592389352472496081637334856812261
8825776214486590374619676793408675855573
25637210902969789959227537530296462679
2401452594239013496334127801157345541933
209252869617833125109587373421504033063
903929256932998995379282784852528782632
544334080069009665338377007457625967353
693137401086715557368849336869513178263
8984735385232277385261528270608470766273
3294304193444071330026306905114716706313
659238226219759216871314575665973508625
37559332771443202730557768616218796837
703351337773101218790158544276440608943
5174359541118416532618560634888291568
19987384601215215031472947143194108983
88319809934263587843709649129857353567
67875854168156392078736162162126002817
4034202493999249434661432992909022104102
5498604597158438498561732776679139304114
26073784186725846892391872569050763683
178755161835620924073724293687974189960
943877662138548696440904931613393587040
263697934533379239221869275928031654449
267216475487883199666794694467413320
408143924398249208279837164173487601029
1870755086254344319944308691714698989198
699483712531760941468745657776695253126
62023013976091991616263536844157554400
8941962496834087641590403384880894341261
43304312546785962741648578972887922323
221213779840212459977134182062142556438
2978493516514122049103547974994803694
6268568724626210436667241595509522542765
517369612502006436633691089996312184505
1051947884786382215073772352715148803429
4864673654408788966832055212316839876718
3409746514024655993071083171969567072789
1501554689288042821687125541655754659
44708623943994498504240729233615449420
476341970116163952234654229385685229817
17947058278592497653157105527722387151
317272950813124755592756131565529703660
984013802285049200483214385284114053643
9776413225820375372899275610739108963
966653822559953096538455276585481486948
386419653813952281022175921933781355753
3520125316588195462699154308094461717
397486225504839256679613520364669785510
5637556733095457516773131121974673426850
62393489760629941234901567590918318309
2631338961581485548162182386166753047304
54031035533415748467584142235956077800
315491476770157335571337098413218163611
800844359556229028408968990846266816720
3769630230871199579351552392299086474826
3444394475524771657892026191837221941719
909658856861375764756859596683976946937
6697885971658573692992463918937250439194
26909796493999691090169038051033487512
17615550774576056984257883992339952502
6317679370212687563626521940984233077
62565435318520588167489296159032429359
704738407358321326679764216711065518823
180572838133170667346385172947616792797
8888442572389478699735546699945360475265
249382032949588768409670569216548377568
324780675316270365129447843864160956132
915386612971740848910682293279752219595
582179950432512692790412722943232859226
5825773029232792433189668522525860797413
1131178735675252698261021514399188961576
197178378499683606971026632753877713978
554579831848700454489533726061230913730
7625192139722980329836662542382179145302
43953405315938827415317839271582625006
6497357438999276299221493920567189675
864015905826345588281046550642724917024
585386575357696380555603623688580439796
2048212942795818352943600430442056525
350050363614373571056861646122569418883
4217336181293256496128558396565348236075
8327493724397244878010045884474559958857
5426110990943211314489022896566018782036
4180680658747594980992592296801706217
9162404643695483522181534432398119512280
1994576679717373423666205799520886379362
713714537351614438872682307877914288027
71566323900636494517246636688786093350
306673512518638753573445128653712992614
546588880074776497385933717468234756376
37429496662532313026629662982133982703
252295677924535794818954141511135400
5024921490084744207798315907624455575486
51413075106222431592417213755107206177
61088768749263194419476931667112457806
1771650316821296482718201745412779206159
1279536520784453525459998558912259901145
8411148662383645364929945697611233787619
7567289675604282923424099832645639136174
20193601497220824343845494370917253648
1081774285363624729284494934496292599160
6637351668896216910660149909273785078692
6216145392253037928033486970784567028655
81163161980958192632603952723772759909
91144397820932389282580303179817653267
163498975961929505947604082647489232567
474172744987688267076869018882076371014
2923386941849381671426789958977422622579
406548051906700530174212764048298493408
25175333405291985145034723187741851361
5622435246735012659312196302714632825585
19522935615922373641127418367694796319
6511989297152747302196818992748842994
996877084575411651301756475413743903669
1848833082785130124752441209941642413426
6046666375073556602229276943321597026579
4087544598279376399881605661692032183
581574538685719340938087757622170287298
853619763942021122050062438946825428169
20463359165261990091955727185826201534
830596387404264500493364032656141685167
18731491889491472508067328438347166462
26932810306340193656421444480488431700
265016475269917832831114806556436352545
6964339028248000177411658671413970249685
171069351819006825714669805822856785155
282698393069838235928305473421933518569
786618038393003537444548334469359476137
9114839723259294911951414768693431028158
793267596883597755378768399500124436142
23091993207847604129884495388814451123
227999460546965330240373087406179388278
66187852467048147128816659777652529448
8401618969221967305872709862260322392401
7691470436972622967413805318179964552305
6971963144959012691284919480291618249500
6035420740512758644764014257519477074810
3778639178123455648027789810203111500
6381735067602466655041793322474673201552
868046613609936310973295298975705056741
1292374649457854109931959145201592021952
273151714312806413498294742594975693585
288271202736650777144241713528893981759
32311173182331604304189929832997306851
995466298949357287739693495648728568023
54264941719966726529244546196675661833
1450986893845249339930463175743931214307
7781365246389832908328544326539966388240
91686921748751291431411827114592172685
670826760729072373163113624718736611768
420872077159479038397257808525046954451
313380672605690472156425323464676675446
151425958630738534577131142651382012094
856853369099282949997135864873779334723
100180766748369033745539821777499592987
3351736633145862351234056962580316344199
13292017995869919066808252846217462467
55678747261863499535122884316437373914
312364742030715079738227864146475814178
7408178031941305107545359496573166183397
7096415056615230156788787268601321179959
8864604712779829876945794799704833206
8494443430124039704376243077725436022191
321765719497295524880071332176181745901
887334217037108591661572166184912355404
901014211192843246586314947688463959433
34013975901771313051161602835251341268
9930480860422514931963963339145112606072
639520325045327967197503586952836514011
1435562549031194266383101873630743928452
8996376290606318563482982925559390768091
464715624794799460604770344945274501154
4428934932667376654719392810189974296160
415451147153223187152633875158223897374
1833614166924606231812583325362139841069
5252353510943952870335818670273970213557
9245737679877509972985938547263951322235
2485960750161389127172924626546541812011
621770197418284713403073428908097009380
892751156514291862987229297932243935625
93458623438368669866002826295837336096
7610267055707554480427267914781565403333
5095173893118889812230058048292539882
9310658460651732639538513286391057022318
1967587745992103289206139956466998604
680380081425984887636204995916917727699
6305365972063309943483255322101188721079
944698888464074060194690229207973961
328217015549554853933735616161992386289
1897322429088122174942714022379018957107
32322283590256270201573510978431148595
310857126479155733721258246286344753951
470575297696436917091414101566653408919
5501869584771241916879174249242758554160
394028267806891918824101631328535956467
432628865330202530517661806799439463059
7679576436012272590661387120606540342375
614512404355971431198199281894263293582
17628810915189495634174684534651961446
8092303882553645730568111341746574993
65311384810957399131104655123342376222
887122732969140672213891054988655982540
336524446900964273184777833665314761359
946260784954540790324902324204227216791
33537245735936999556534033728894884948
661659567183689971851458435305348993883
695844141208953139302517955174798511709
8824769417114951492856062265783641656546
5702594259430505889884378409783534123
6808763162519810413211592760215866694821
4703438653555922629566593144744479797905
263810684235793627051600862014283353759
82878819673408462145584157115575746630
639851118584765010821119120407793306
5725223166710030906135679146958843510
2336611671905938682653242019735022168194
82845433304949669597769670833887645141
619260344587969622955226251291583121202
753337186079033418161851478372417439857
551252557169352639492350790693044638
56821585437164653627397726777970664552
5551330746557525852721772866949938609473
6299371244483588257170874871741423549908
5682268318406876858694516180660737129270
8959831126442675437245039206781332692848
7007590960924727196452799428380153537144
5943686670091836768541079429920952742177
447167676471035216143992970835506273
791078065954820054211567957629868727
3029713739474366198658984129794720931265
2492259775316781220341361306424855952925
1827531950247188871028984278527183920
577819786012899602225906138711210401512
1317569311906063962515592103287135606797
292480158581057424073615341934455658480
9373745137990777775758765966289146287
9178400970251784945272307138452832642373
456068042396987582948139439543999137496
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]