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
5628535937149369769379463903661997231276
964070156598681167226308979833847939371
739449072631182547829507731872385313629
1930807785333714547481006457681138743514
879754071259033692732797942967013729170
9089239157569034290120258058942293252303
5142759616511715277181933878123541068
1070975044066023910111906288195046818252
18891500732226497698101417273223635345
826292884814422575349971590939192904662
6831320960208731137952956899610327367010
84632502658785936828880578981692511074
2878218115982018665890132262429688570
2675202818131945148743851726301950693
8631829750288071685399551451291317616
3460647119882823540897699769186785897868
1746959520075729262086407573700937547284
6617113421972178647034795889958476564335
8429843354324637730769836734585483632580
6678851210194260685758167680526134116335
6071288928258721584240174789153268034978
1664517834916528605615552888175843968462
515431973788012301199752192938663365990
3275137093741063141839129792449282042998
139124798775885616441160848333978322426
70418307262925195997645243320161582903
77150415798356770262832683951592631419
14417987198172827759669290944415569816
4434967110647127531954532516563090752247
19888225514237522763124127893366999022
9671747081157658905467789372814937168901
656636258162578518927929422119513392678
9599750267558315311489392746112716358376
2122923857024980939015173798457363908107
3777670727523638112971843995958153413969
8876720433208366976112574820576471821347
6038419465973995122594425679469271292
12972703669768596373432032264365686570
9749890722155306530681577762477999331425
485615951953650411389655810966227559969
4685853656196067673055542031945528204052
323817705502598472901475616214074354725
595819775073768654791566224744005203579
388590448136655560656874428685544441121
974792511866579252098598238278849209982
5896352044453696219638696993595773458932
123918880401843775408248701773525511
9263498766242793907340896643716986632
2175836016694103436795023317203036119408
5156285095088240271658775284657817297667
7109672779851346580593269502734338035534
597655576773858270074094961345154074206
4923886813856070968043007586263396167855
8882144668784541244234898807437278029085
21166445003961742119588920130035666021
194474686344109139292334236724961117628
809860333598191045027116421449168993841
2982682410676340502262785516537029032373
8842142996051942515154522923193425659396
80816510844942929678778548880934887873
248927413939107863284896847397012322223
84054387386788685422411705995546265431
666370839852249566379378308573272655287
4102614117536103976355866896271921700
64536023593869130564024517945963747361
6439859675901960771196004057622862218968
220137548656119249625129139933860637594
592881317141189753535795259947249396020
429948891910251663276269030440049163540
1907515684309151788322417555859856444
974893793263999125852069563283335077989
1095501827907337913270767380386098181291
891255013405410537124646836831932698143
979326454443700497132727865988927164872
1177647286379159709790910916419832002
420361142559462983285409534824863433619
346854798671887319673786763279421821249
3429349578198958335454663612930072183549
9647151856514587373895254129125477949857
7747521729147962981296859444624536325
40388019483374276850733469923164538597
970967814961698718874164305040657988451
268564504822743566955803690468747215783
10901957641874431181415639174435118425
339923883221663478514203282372869446191
46136987921668148116305374734324922108
819950396832164025294795667792844017737
6540932823382719410339944237131119939079
798938725960426519277701184790293071228
128687927908425737882511752421443784733
178146551195190926103628788187434544756
774474635943865654733291210966111912198
2468210530187925618413914752658481177803
759975992647349515298425367153086746971
2193579360146375583190092666780153635872
60691694163522851990444905662023152193
1791442072085888323265553951676944973752
6905904938707460769342133153900010676475
9853482870283780698191262798964193009601
9597107466442065214945282075679887398968
35313153659254426155322127102338352058
176965933803072654252187842385344542289
688520272286474542013988677717212318839
502748439325604696026047624781096516227
34212427253159340891955866802967598174
475242521886701981362525518607749069033
93911493551812050659088628344463394260
9943133737187520151933439725526498644839
3331498543162778860241259090933270469484
145935968372348139677471134476034601874
701239811262130185317800431488396470475
3894409045045065584657501205543950914359
1941507340801709428278733373540429767316
663180948061665133338418374958063723780
4052992089416331862413447179391035082517
159027574775856985794921514284313921418
9833354721486248694370832234230064375
569481887709808104255398289708961673982
97978362335478991468947596502371744616
904028819731661480672766678417058018781
47763409624947936485362993872529799102
9026617041008397102178286729142964323366
4886117936109708142245933918158681068729
5770941215211950797819094489413759348971
9842420452406942283512517068841996826737
291491639423824457130972836107544649908
4471214810642781326032137200984552017470
742925978557941483341979191659563423622
1115164044413618056875671597281794233
361240613031306790774374700253379105979
12184086361720838558300772830757473547
41887682509784143061131602810619969816
80457879302910032495123453563544815145
58209063570699198284618723713718781677
32355652639617842027623870493439614105
527814693098963734681348439961410878077
707246604311418767249186325141182112558
2593847630227667157092427531548969698969
3168329211809053945761061297294788792603
469688657953110637377185461903192335454
141353743110802957067614151873099382484
8875393919425241832947103225146215795537
311297376816268989044151103332466677425
89881113009559384386977587641514925833
396161963167740099788123461377799280861
884599034530331510638902904182397919902
558912366263410746909526697367418705289
923747314073162373667378346634879490745
3751980893063788233537762805359793916150
915973412901715884094444999142672092545
97571084962222029092421353983626327583
51975261116961535508534788236614411782
1082818911636903844753086957636893844372
77122910124326821556529320930287641209
32745547538600853348887168925863083865
66971761474236232153603390497322305309
2657829137892766623876767314682988512919
4166556936587826631074226443719967399332
64319440846597391521976828174757616614
410165044590300811104607312278213022592
4228521556573751273663329973417983692487
171665941719945191079556355866691122956
20286626361913176622659478608100940327
97136374565060258663277347964150929359
8052975145988947741868892595335684687695
1158893993185703291150519115333754883027
3311382038009281107015811265274150733791
90849114590933327531327184586224129418
6592806257664626335739528037521347627853
599819471444843858589924433531843966500
7008624831503437294720296058889089319
369947057731721658416534520091070259983
2873696266871386335684906698729370061124
533152029353445314625692725189493428359
9518156684085559044430478083006401277
85225500708354124197940325154895254483
621166309799925863646064530450026195429
3835452519242467057888251952397309011
9343371357584034576064673772320714986951
876438441381065389023282414329062337553
248347409808801437667137904199663203164
703542941762357849003606402535647483553
890027562430515910353912414416435927449
611984021328306964072622482483663666442
130539241885896257473915734936078388511
37519238757042944057146369545112605991
52952673547788739736659325315833614805
921550218812209716430518833278834958338
5268653198693086174336294078514759786919
635526573653954437297686840950119024892
28387896349663223041696283143070105845
46140467613828284068549104257439087403
737553174509553867299320233564773612
16485915891143365769276716667952531337
800836441622240256285040984880671099813
72777539328226039637623582385551291413
4194516647133999746093406163191184706456
73029942940477818762161397110493942047
9105598841195650217515167342714687213598
330641184408262181553726665892539515893
918713274731151383411719788207210967899
5071205683588105376611887199161691587371
313237822472123131149588619764417664969
72340488641641083199552989995681459756
7145484377049069430050139048530180005526
1133507575808945142959449574437141953971
2084180636255237451312932922561817613793
96612842242211031967851289510041773590
9564817347956375865545478448420050377912
499310535907964813384609554866911033671
3576326714062167436826439577221872867968
8041578359305347873328788111890316449877
2478125277269923273368333535314218111135
9695625687335178453342967058723927925
5838130934013843769746120721198718581
554572934971847918123187579132674005119
74436148685945874874403295603935652043
2280766801597482353788753571042805330
7830234843371156451799773615233229122064
4849172068481010659966645817204133811346
418146890078774833791613108410893588810
310285846471877967895627064474425594429
464725681111491131044905700744866985384
920615971030425519336492943639877137548
867862330152259091994576943076625968
12922637508705526919914891887334627219
7679492167939876467736403535270472555404
480776879084290460461911418396578903509
7595657381337227350128436623217819622498
498768387911915856797107164706527656874
5823594525495541230238624573114843565639
1389829643179782327366054550249869791983
1987212945795644436848473713516840691136
271039201829242218257152153950285393161
9420628355669900743950998909957593067573
69035073787948893819528051424290919199
890528969575227286246384427284891640
8330848648801674712058805038620815367759
11184090413813153312544394358053565213
154453463314738857780064099748047135158
7421803055796280904710058696673752715906
991564982471059985369814432176201680
841689341121417116916664315053838397466
11088947495267488039276383979999798180
656885498297250513833203936596625027239
642351387485748848888631350111225437167
666257776044519346566878955880255279109
5624447199862512895160898347441420626732
57881315232717958000281367261527215163
131911422036311491289877143217068941657
2886474418312882344076293805535126001614
4193444529816038259585189419727065403
61158070376107195791049876673314315
2531399196076186659562726829307461234447
5228941813503688100134462116701053195563
996217211231649406958098784735634973876
141928062157156625405474603276379426249
89917779821285494117513528849509661628
6106223030377399656783855450503240415785
28727106449599612059428846643642288344
508278803254868360695861332749717659535
883623029354691175474559561161598919049
920587558572195575917702759693231891519
11115465549223018630168993572065466201
282065552033113938117029460874320046344
2080667675962270537724055597208168665
328032966454226195637111205575495833176
2689635314608024328167195499666079353996
18684846383334446620284887980510481151
4067721992993990706322099341358086674131
7245706578148464654798753794891993923934
662476339075312775105794625787482031634
13622392519927646273642709210921913745
515607611501384823759518545227430197275
39566303550892183130801489859342059377
5701270180103478645487017609392392165413
4013769070316480893596031763238991879794
5709937721277361766584231273722922373733
14229445441171796214403681522058824669
26284669106507553431292350220179698049
8853996385929321413228492463575577562447
928696028658681570572298267377471769946
20406624323533829446344855486613521081
67236042805221698459168307539588017501
5797920454068943771594132143907296244457
3347952348054506372142062025419665523626
8916528633253370914027065666156975053073
14198007754046983546259529473710132057
505390703658192516043367657716921552183
35163695493407179895682226902510796572
9350826467711895792795117135500984089859
8456889032631218989214429344579412383
555876095842263398401179225547512957040
862559428305502844542231389926965609896
33525777628654349699668758254988068
338011439789242450078907909322358249580
741959452706865590337934168366069671576
5370625396938224436229399883187052449151
144549706388501126771857755961313504070
4743375762406758618620119349557023513477
5061821338097775716534468713671344502320
5259545012495478251697897903878934843454
27102823818380425522458346877552695073
66555592767330096804333334544913797541
863031675970850784552641710470347122353
40415642624745035452420457315799454227
576527328410161698329249637513544804183
9646332669191508611664606762168333815111
48468847200670533435890818454761878069
212485495089399322689488875781333951032
16527478591934989355255152730956059252
875475977819934766198578356853454625962
3170713563508884965357391013118039355998
929922327766516267184608309287972005261
4236506496354996504263149011616000306
71939460981741717243739799539523821334
5986752837726820161850309355182957443474
7625503318443591249768953794264546298313
229766964217275621624358891773951949528
7234230246951983960444307900888416856407
555766675502625581063674979630967422838
828874454665918935294807674176822627378
369589815099425973102201371230510886371
6415933812862708501763063945823159678859
53676624764431773051582593887169352622
94794092893113722416652651081850249736
106049639809276925984079866577434837703
22803979206567461834415982185095188849
253299179446875521385743870867792671277
591243471412570953135634376865099445738
235021807474597995243724652862818839961
52482188234949235169954407665706299745
2441692041055549740637537459411479609122
169498895660550841240431107880456227
349264297885410783743249628132680985722
623336644550699126227865511567046699056
17783195071307281752639830845265138763
72557818365226227401511036152296468890
6823437518822014752881369132901610541
713834505978233143393758528761933309205
612052853077478167543001980131162702216
292169733957288057944436568175580779472
2816509976145973530075339814619533537787
687396673853764248552045176400150873048
90596371512126834916676997950636905526
666015823707767665368337971920840859478
538349961707028691915052417847549202346
98484502312865621161990294392540873288
7360244758476214856251217744480397815966
4654994984923209657443463055623850299934
793464612451927171339552315344165898177
429696309162390458784702065765488902909
951447793251210560219617507854581872309
3370789755669813985452885629494364291266
726399319739762534816977144733949006629
9379474041676590602325813588126597958315
536367743799320181419855805601443778040
91471878405777696199749801325119177883
125723214973793728851631813793830112973
371814146869716369614016311177339965479
185082018927962368731686997810099616790
353520395887448985356376210792289285621
72778998725951552639617959998602312900
477743582033584819281644901550395903961
410147121412590693771482300426670189726
55476507821522537913993333299990801487
1832781928723804408396837836194377038035
936468257138851918095929799520661023522
7454258052947373292642261378809961517031
802536567944523316492669887692744889610
87485621160281157142401064483681902586
405942558509017810198586719424645173985
2267869461834818263410864891183013389402
3948464368349743705495657796009539266
6958457580599621761991634773997585509992
1006868096232912371652459925485922593307
13498365806179915741039706720588327044
181045953656732226694876481843621429808
2657920965708564786342894550739461559465
4583932461836363831865142049618387973733
24751402402676073045778290030891111909
144878226331199982765919762683783223512
61030075669627153318812085678654301941
756952070897757588541131938274049393726
6741113080037749673646167232893790438210
77031556789276267341249200710196299406
70397908815744856548142485268810413208
902253796221632559570112062194797217
4769568740007198502577826336641219512952
354613283617658278712366063166980268193
716972441876097960507155334201399113
619854322967176810015142741378954302285
6784193668054923803791374593268267608911
6734862631989623503573261400124227714097
700277982773911221061113890470515681537
164181227693270957879219992420925396001
861980357416503266197122550116628872080
632833205462178981449872015361726675580
3030371629311984702226604180297760791628
3197769350104426784659008257392050512900
8331123174362676485444901081359113056972
5345983395371620519088588487478021605553
7501523227968842677778057420651916593746
616788102176995932532843651925242877001
2130969398728748815370465826550317091719
1218971620527174692670813715207793511965
27695237909779597167410050567695845240
849199821185331968866267740615297171792
1866328941701633864645753195390572379861
924827581597070521528858415911725612272
7550680341102333824248757486470014371120
515034127231346561908554179649246909631
5521425287711640197635831771314679138381
1709643458462825760397403120418371986715
9436199255815524325159633712595079955697
5012972381273631506032006737251354997399
586993997612105478610975849567055077402
1419837627002192331681576748302672306072
8455325713292645761098142247666836811279
5315909373261050994950484145739130092649
5900832983035282631984285268469319746374
858980431003812375715143688570633624799
5564304981159738157079715188775269543
2939971153657894695950539907174357872422
781650372307580741015626847506053077579
5514717338767820329342286211265791306663
2525548419935401138231683998428144641
761367367329611171214609292184163518256
1224108949053382612371846391684424007
371048642365680876294521630267668265820
698170339190140430376623961741281919912
43605924854578853757248753111478389164
6612989027444522809721978432198410034431
8604834393872060348140804003506452553701
492592096770550964161413780388879755771
5811544560203565231829488844316843406466
2508238629436727752462136908521284041713
3662665121149485140234651751798492683
641251131966607335938811306695247873121
56694800512170231537232661931496316910
771969052460318437511116446692162192865
681481771768425460535611733899193237041
8293250354738457810015319928843871359395
32570792749226042924736703320014337328
301519787287120143012266308122372931572
7438733579027993294924597956250153246020
528518737603717433192861388355897775337
530536052859626134118572584933817425349
97248286537617066721661611259284909168
5232957015743880274245457171315834186282
396615368060465455763118731249996767
571662019994293498276167834889328625029
575649517636497264829722192880058585755
791145273004371958922236904428367639
5456705677234013573767701437450768558283
25328448595523659040561965921830696864
44977911523795944494812562425493915888
6739201568696322698897127196315699313399
2489255933856038351896153117475527129418
7213644161873473234533393142731406162
83796435212245510572810311391626576280
86768301867938236626910082683496232869
3372632042842058487274479575506255191541
738596161324545824485536992153364114994
545735043026984299475301684824396222837
6985340958632221870061208990360477519146
167347686601759510629434543269341296187
22895547057127787197868910453841627072
7398676832029064404684151143612313921813
464664809323458750678962328635291574088
4687630766333482666668194385442513279114
5597165860352173168854577952814376612486
1626481347782816491435701463418374542183
29242671267744794236513154575382597336
4206949080677009920426922266172977169590
67865815119500511965164015951230683481
17724657200898933502013675925781989
138719535949482628537601721543772705560
478398467384351798041085429133365077357
414190066911203094194086910971554063382
7453161617345576756286551693242451144338
890436571677612350222459203199512885051
2263930844261782888566535122841335016127
3459546273469235689878623069870630573466
48179717001641258109132762660592443455
2233542345109661740494802610490889358410
403074643474599240048289396810671041402
97197059176556028319400196063856328379
2083156453901585893359151863578131621623
8756769539137293718835071531248894860
4689364820579655465127877580229582623763
509598779157260698905844761642694348681
675781865424385120077139467411854885098
31386577561710245894204620189231267710
766726583946637088838332212563965649208
420298117885922717888399517423995839568
187124681884556697023920647661852608726
1363960186291070984434926169068207514
54917966137707589583855488375597865426
659915624592836571554866850439997931613
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]