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
6720276537046020250013117487539678056888
2544368766773362656518877997668596832233
553711987382949877332189220995296562227
744125387082630371869746466140873221455
46062337695836475219327935210785179246
949615727098135488039175187550612603870
3333941718706260280192671167780943791657
33797534551462749017239136225362484639
992399802350452890996948182543974828611
507598636894744352233313413236635197129
9251590349051962552143878668344548643
334974858033810894258837971508858075667
932270053174706817326945265870380848091
22614958338877240430308324135967394112
47751215633588764126247941868738366226
66918462677382089128559186616486864332
838121994982673349695027386902388988425
46163694520106033645841092107920951996
9893474370462175560890634835426375691625
4897177039837342557687784466124430491876
296716145117452905348268547753224985652
4138582682221373368726649931859679653
805798405918488084852948471113531651
806066132066703223417033258929146605399
2977151644523898418990234535221078807103
943570559879954712828459291584124892809
9938249494378885829531799179487727377188
9467621166912851899894709117778192883563
1517649967677293151126775965102853353160
5493444641027855981172556243160617583624
649836011232399497399747748655320595556
4159352193552817139203706996721869913
50824764498158536236642312199247177290
964095433000318590851026638521217799429
8473930625859500442990624393680669403730
837229417488762216436686369797062859804
71906379701430618100140848877355358905
571990577788958198826478401761763488140
85662303058805637846711739240433197571
2781389838860803498626783850736011299
340962726834344712862907129577322468417
1171574217076615892519278742295373712581
583267356515233610665635761632890801445
7561103523308456244633293084713678843023
7220827812521698316092662007179075067616
155062328323934454916139040242473941083
3101791316047709252286118553855623665543
1210364145608511657889886176726838060
2175409717951681340179237347294898174776
959791913905585984264164355174611763046
216014130965960359880273171211153751861
19509110117141564912215793239344868471
872587519821499949642291912651952571
50728367301618424256787510779120839467
8056498223214942593381964178474693099306
82236594158974961386729447319176112
810214048248875514031754293103446307925
6933132468647635710884457432603652563120
558348091026321893094539701464157943177
1113492712358329291925956275434319271443
836555929606308421953616733768421205849
9451928737897716807511229978868338981255
690347649796956811913106885617378554393
259679322949953810535929597636070157429
55352763918798194722990931383508072056
22644071487739587767537269469982404710
2360359654128579286378218627912568895954
5171869593559267236999352168923120809716
9624344978522346494752072738825091049411
51772246615136511721275483594185413838
13017395905555381847653536191492639143
4904308014004904697242677943561376505720
98047451932490337136289717595567481744
5120793828285324276115537096982585409631
5286731719628628999939536478481341873901
6901758138273844640972859489388760036764
701654801631664176668811410434410733683
2975737963958279602515846933753997924104
1595808964789761528122161633328674606188
797013168942659403447345533812082102568
296215494815441617677008946913728785621
851386153298519211824170348290792906715
661797906754257226347231888953154024120
289724585761232488707195666323956554
704978645315767897590515268938842462075
2032361873249125467098185728795768233568
4034957365921762874187996930236929025787
3766600238706739247715149290493811397287
9468683040879203350990267823413379559467
9758193994692503163369096912118229351731
378345829107230060144867403888764962700
520395276046641068903538768267217604463
684836433462593728824157386311153882879
398014963749326020187316141384277777579
522871462345959542777877810138281935683
5743345232334307103856517246549011381246
84712720775646695614605434375635766016
137783452458625451418403527984472752943
17131604023901765253391854914712463836
425464488879725651862746138210088331543
7299308397859084699882437586958868124154
6993124113029602952456624196472491114524
231524444775498366485386241039111425289
44366345678712692861095737796153866287
822465644063224679948381291358688858859
97205631386874984138519783947514518905
740363998430541574262349405340938289762
1536778137163089803721619625496734267467
757846562265622124751240756511028716785
8862901974888453798648861845630075778248
4468835921154467271314566968872532253341
274334308961941250876854801685974805472
2063878351492238100800186073567809518
101771182856855551956014586162269721
163298997229155583488126687550231757100
12155062872874513437550988647832756888
2651750021502239779687852253177118995301
401796413716341433751259226465061230
99276822253656211611727942124831011709
200938345456418159745687118861722572
892591461585616228354885855245431577389
404889376173345100311223357448289257702
804948631062908074068653100842699694022
8922825045789569407648394452721595331819
1722128290422931778529838481265469985392
824451559852888115674767429775958281370
463385021393484152318326267655838251808
111468455035951236734999205686037682796
4019747063715040516406813904529632239
8620252861724697345645466299438240591949
3310702692786562904529278372408347928919
783736407749566431115197641430574457936
7482682883418855107768205845279894257539
3516730034677358571112231942703684087674
93749948093424434478776245851740146
9391990067838098809612160218084644782
5477488043428781224898649303264531489125
797318018517407313162903238152050345068
6653219954549155567821941508994229365357
98262388456834553911182562248639302076
3664415687595245485273387805334057302111
8053213060252130687565307468170486866
438570581673475159259163299407255515708
455946443373393186621974823821456655632
912794677502547063385576802051363972103
104859092867488950995708070974184346894
58354347946242959989996675331902879062
842839473596686042152475422760810717615
35802015677059757280992175231246342321
88182144654236539129918662293468576355
6378621830653658694366959508210580819061
3714321613322086290064949208341333057205
37302624385978797443547655824734193696
64811473568030858036896599326719299790
111233138675423886294443987798171177683
44282336166163768818508551966025426661
141369315190843525257728885027925086505
60989497214746196916101557296860992933
3631857612288167113640842864682963917551
5579416468418695088482938380318025889
3173715493679990713949779267891962767355
364243231281523149720918134298167703190
165555971735544635533320062034782281
335620545312266440343901121491199291746
708924230058573872212053290402393935259
635554397317413976239402339730729392132
399195843451384511899803104467727278123
2614876410395853702943383438259182784186
4662588731011599389355006319732562222056
700464696057893310229866562607528021765
1723413879149995548855027655809148338287
1457517720888413300983577740484945323615
42772486215838899649185636575913271679
724828668654705983793072506566918561801
2080114624676033455510613763808070546354
574787909899219880746586116649587673
3644478038292505870783676973686267071
7570427717428042252991288287125435592417
534325949279485331746356384423523569277
663369390504790107274707384292182876
9183439563009901180223949518788863436354
417804855725877760998168480258678275116
30598110648126499305556147463046325424
940794225525868540384357323249126682271
5233971387078283550348694907966754513
498867512867269334029936582145824148325
4812236270658842258549535478638762429783
6395266180875837407283215210583153856429
9626764349887207478925576849456998763060
41302449755471613408577290742784038744
3200649293546896298817666786918484055981
889174796716961520574981973628955084831
775760313885040994170275757514915029755
1234384488594967628686565107750348027132
83482514763655259512471154515153422347
54225837211115071050270397993116111308
18969146489178658723819027430342877583
25766771167774774121222273512632505984
333647212995734833336037394438955416279
955245912373293993962205918464287478119
9503686020531087576436152913697122131067
38381608608566113879568445234595228439
880642414674685636561922611821315019470
309958943114492723788696240277330258958
371711717660829930197007555430549557112
467167046259918455235367877184784274950
725413055937643798924053907773723603719
96866693435515225836247213840691217874
412864456328178543906973928529289783222
82124753551855798688976987548696922285
36530465457271060958902067532715736511
601406690427904706442267871106339196365
885349129008182199892342453505020529462
793895451869231045913147976470755209977
950528859746159922064634307785617586233
6030333110529818072376216185885784926
9372170187781321362972592931114687287366
4865237578294663668798906893248109742
2559877841001696722837559678190352258
9457434226906873252518727668227286468042
3066477689032843573079774170365557367378
3750583891348257124916254407195096964139
4737859345864557327316091247942273203309
5027370760547967865389924702794032239200
9069412663414147501391600368335906415
246113656440182882363445421412225676120
144474034376018529413272765221990679277
930368994887126749698588588792764337841
3043312751824893640895273594657492213387
98312026607396876165433645514982534899
7543891690178708120257796705176396189
359314686398344451565426008372568579777
521805362194721922235079927550615307839
6064813854088420170242844894696571343382
57356014115327019972194754187881112090
5365151948948545942134371959800856675278
4927846329542054169714862462753031851828
694122511546207594876511446056968597922
61034229904961956029451639618272242825
2419268858069597820863105636642021377314
8761577270831657893446803248147022564263
361977891720105026671786305443431443183
886414325158227827988005497320815448017
59972447188645186181615973497938332155
7925168655079294543389512471807157804518
20769569329494101787637573357744521242
844950965485572886310363011824329535316
290876282060528976092696462710143502079
6262418640241408668062849250691424826350
7002460745559848330474213594357742465070
933320909541618907827995638913299693037
8888795864509308664839379812370886535138
31122284148065871274587640115733657822
263923914739691364170662448436753949805
7529589046113088893492453076185777026294
62869771885845743393582767988000797723
110099067154581238432389972503259637777
242518019348247244407373725597793939644
3464959816455198299412851743626342730
7002803144237677517985216933586231673175
9979567779141822518527599853428767294541
945930235399499652161643712338733524440
405558088765882205030168350920742433139
7804934944005002741858829491197063796858
599445796407730322714991727623327853998
3691438141828675804029884805375151376447
4872756736103480842734574806336989543173
682216621804477586348888189308069366689
26583464179245438855584816603862844271
4850645566757668425880993398341674347868
94861945564401473679889288727770546128
6200307535698916711299169993974584429576
970153986035313718199435325096537189124
921536396477781979634903304343344681999
927980089969638537933981793747172081204
211119331992802944934803661701822118912
6251582577367774754755682717684655117365
116022251879863890367737241229348803784
448499856114272394225559412294785143766
7074238318275119649684396106763982881220
522275358069244670834608588139411398417
4917593059559668725885365241698394237089
6764532471486674285738742945265958103073
532792234363044401657262838875285164156
23693639581826743161156474498282183417
4860302719089490990633544217740427164305
277853245425785863561311734830336908739
689380579214438133366042440125487317324
5786309168737507565379205571879549485270
206789522841636663542661807598130015203
900484232455176532147369720577725486319
839940089384506322605871125785824995486
905578683078991450226835470177219269347
520387203099007827590796422756676814842
198845119955550349073128087170510314258
5488548623143850931632431384870858273039
5841414425101653916419503281491361211584
8384834988754909628631454358150180661954
874802684089935831435069296969723102091
6081831022915555738942513273956629755389
124047497464843821890928957241839364004
89259327821732914421418146988547466643
492347951373626814174766676571253853695
324283185307017718214833849585397378645
549660053475932996187883269912673873361
693020109712534353885409507952843782670
1922726257804735987539597906967243937782
4526546799465012435062583201435139122693
5960846675095996557267208675565852371811
362259391900161729091235023803439874976
59521674995867208436519480144630081149
75808364845250021834856438854793481252
8133401296907159726667730854335417117
4731121577475993205283264319566684445327
9234540823132450711551508630926474572
3781717118177344642096835186455954527114
1140834920895122785925763071172384387387
3143881688368492542692118794178316117
359147819674827186091587167676375474659
547385604153476943218635728519756275322
9633197273698466900552468172260042147868
999715157852712350110446328297837026406
374641569633078308254376564992865059491
401868436799263893589279169765931929236
33327420405183882108107363204036786765
2159954661014203480643947780185043541698
1444910180411517252973394909583538317076
6969378490024772377516813959591499536295
241232376936662577951660327288428444325
23548704697881041575944610251454851565
9336347138508005804612881544576159017212
1757121146414538793630528660201967557
68089140396222426822249954727503229004
806094436658760652121027466586856878155
607812425481881166673768156669332903303
382216559947697476311365019181062724644
41247323898638255430319451908877832771
7285184456185876917243277774602166917480
4560574973993299711377936440781783115548
1346911776672967422613434177324311258424
6077579025857721150760575066666114182425
836518165606407574780165972930163044317
6771679393569421366121636347935996413
63649346998659454679518387392166578938
6129908040619202105340655642248465583320
315796379231563145319819119755770387636
3114810944055488880181687417372033809719
92411454418148842752263274493315281365
892881915756975956261832722667017925120
743819931785691308295209673565089305730
948751417391669915486761494769115558834
997247684438385245067477494655246451971
11638140921035491245296236730710468857
1686938192129739111232081353646559953100
99148183881321549098704655737984954311
92665848149821623023496399689437697557
9664503263083507311493703256479674772062
943172202520348330424647928125560406832
9417668556129185418179483214720732404778
6776576880559919539626257670405389797697
954136237361242091524373991682059748266
53496465617868619264718428822473133625
2070539092606745955162249238278853158545
69398860811951895140145340795565955359
36202244348440371835066796570149643832
917265373987213559018274188739627913013
26086638301210341569376941571843442193
831397228318698034731897121627635029125
418602735479493100545581046309691311533
9046444238304702781040599251557793254739
2586132998024459268365281700454422379327
8254273341733964358234377859097395133
5457524050054926939778595110135895935971
620859449621667775795762378959623976711
91667328187311182113352690688713433620
91754787214291244358563791772943845195
911496129818220225652544241822055572346
8835436860579964805632482831519723657792
3304397635422947911748947357464502620
64851303552672250844826556193295927166
5537367285781864661595964021715636458799
353351646620178413368044807558832797731
52938059560693887557653349598519229584
74418679966261310232674454355805253182
4388774463737067665736644140944267961052
948551352158900286393847428123045374309
584628742955621432346527273337990867578
90593033019440251875655738399916124844
2928891492609144229383809354341147907
451584295379171932944656779766152848657
759954701736665726666795896760942706554
301275241115050105993103660113897015442
58793709817393633463742333380554592447
4746594217759790444339375099806815161864
394612855732115838257226622380795847809
9919556811839753388933052558908513345426
984461679731976894733631591352411095672
1672394675888405631467456704230232903202
8569771810587643391882973132319911814737
462399868229696140095984457435396284693
827062557962934955811589117884916496639
252144344122623458609315115515017819406
37602001137030478412200474390306607879
866397285548352924944329268195588419718
1063781583591843974248034276870322116322
31923167852692269355629923178083859433
5844365843219356177761743518363539337506
90964612883971353823727430730495508743
914867633731826541351126302575511027038
4918249050597104753844933069013003089
78555907445559981623111459236947482395
5893258191683304895667864736117826615057
8402361076677035144232634909280225099266
26916697998423958222312253386386494465
8000642977932722444597658800263892564027
891776563142951764377561500535716475297
3053793391889757282854437286933059058
5023610027409189810724088852780841992395
4232621921088376256552619121793168075989
689110165977757617921295828944339909464
9208113613634469638996081311572360988807
6619813611046663960963118595532023534
307585095404626637040839843992022091034
7178628949928337488687175499802847311858
99617678526720962052268286658619038532
882992994877354907627405980442268234510
8996222384671493616651763552344035245151
2161625515706598276439228071289187813821
179444542813164415640569729896230558330
15386823373617150707472742949470933894
59775334013360711113335523057522821211
708561591397205575079999199394054396520
5327143071593128474907466640485872356
83207257996416426791193868258216393999
7577855117002609454979168334213143706
236470791231237647196361467305113486300
9654655756714644911739089981273623492982
3360747305090787539793772946648428549
250733653184326922992783340265598602119
25624782214766989772991234821742913134
92296010466123819406782394281774971086
172343857243716470814587417878276051841
895676486414512120217972149226538016022
421299507877308783640763275781317414840
5408894666447661732364433996813477923699
607179702176530075091740904217713407790
11461447462625508025826495951691912380
144353441384604680078906477273450946298
57158731822198881324310522248649224829
51961660783298274170474731175392767355
4463591240487982183277448104222232937536
904140902250552850368753540621517028249
861760836076141401052633944863215677767
7098810927732633321565125120847279486933
9465270373676348760939228048343686967877
7982518950839978658434291075186557734796
258940914309253239341606545851811165609
41633836520203874314788949380772121894
9483105193818587204792541898890928564861
1737554762677289997428091762195367449668
2572181143522292160927206314358228849603
1733517694775538428314457950548963274432
450263545196360345638494132677070578230
558092628736953773181048695413192335586
6296599219406826508143117792266232636
809992889083750929809524251108350919525
3104871767904322323978369116606316321141
351328857289512737622537330505856758417
779436402662890674057104113930387831777
353951864885350718212430390533980322492
4288925036327887798647703262277344914249
7762910077182934129191154818132583826901
3120664311489535774266329517276286558122
11116249950549886597108375741555322686
71856335782091578118600752448031176571
7761529656782903612064249315716321856599
629672117017742912661049555830550716330
1312373446321895147439637907471414196006
7992931752442235916052445581551497759324
79606264516123042882809484263233467518
583165137029779352974520755472736885102
6385507654213484556256782927185388574376
6681979717373852586666878175258594231291
8467224524102407487880093690800768355196
7074143829198267999646056915771975349671
196507662455818660773105895487891649337
4550968047844449865615949472908318628411
7454610298684094531142828233959373713090
42917617109499884813802901711897565926
5522968230103373328650185433748134883960
6381229953777289709340542536693610064952
1860884021631984838216677551576181204514
972798463658391922975326683936476386646
30697805892409511246591285320216515429
3946287810105060283864621814207362594582
181235306969526438650363099773585405778
11616925577575634456563529790822393370
73029532564125669068717125248938825664
151852722410519611417235759657732304975
6087340331208959831675852436695776520
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]