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
806824585984840910038156694264543728971
8559146526354571775381304238291542714686
63819223627588914023974985990786677813
3607425811233715326886447659879820014921
4583777944880556179812482136733385568
482605766193517150753967790733847052666
1919442633413852261440163330736438464725
8289453987887978497811941413551195303113
6288881976871012119833207134307214436839
1967519879204020239101986316385046367
333066526643970118723891154391547599464
5641926527585883815463622520276693973465
53814225190322352856646893637016257488
1089272845281478134560326104124352676039
9176520294238908612021759291308015552457
8083869335687507564961874851037482634
695435882628574661697807165321350488807
47752265510248349322062649884224636827
4191694310546782770355608625869476907899
11361554235449066899470582777927041719
297610726339141034874710643316897439292
9829190734455867336440185958181668226231
698314302936885553329814820683343188474
464719440453698521715867473289182602101
723738817074900245873256852661874102575
28386911602790428121838993473527671855
3466408680047942378999197314531875806365
1920201910511115364173307700168982622
1642855550223697502931488716775980437262
762221718544435719835141696467750049142
71301835512224512965862073888148178014
14347630485521688617805541864746563683
88574864023773908396325842142959609040
8690211872973145638780783662993037386
5578375931923274530454774079980654684109
4186620772908864228421943869601485948584
279645512657157720754081574857503921995
47893167334865325553015040940786168253
742266434439243373251655342027984462700
459915433611523815860699934242168736481
1352640112359542720520791953458078722358
664750933306930637788877250643437605149
5205712225356167175119802510098817071
270447641011196435941739125707046417506
4333581263561501599277678792975021966643
9352305356769745158853063047260299364983
17424251418099553453479823602918137677
848254955753352235609089706808472545534
123382851373335919047318851887251297705
2891120236551091186583872091741718835678
21759365262932280049397962784744781190
6302243289132914101303467187016422904
6386227959763429952267785467316412559346
31715125798949392848272481193849220866
67275687484389012475753393550042617860
205072337884239865667921215106694073027
509513416501370827886839871106868129671
9701182884304425437488832218557890505785
824177757717307466461747517443563963093
4140506259945992155657946714435952287804
112420948581935917655944454220877942647
3304481337637896434827392239329017437565
1335217495029633714657375318428948346
1688348778093999900156063491789957144239
484975923897101081301274118724939457362
334796948314893790863036422262291406199
50539338105796586514416408999824072197
4087821839296095525749070096862784942
291915246917279151934812122731453179
3383624721989855125449707166725432173067
629488590951048863649953687218420611000
227818503077680079452029921387896581414
6176334624572174424678605214357619373609
2247442017262476932271201564531019225234
1694119443325546549784023067747933034604
702098788941922696843911800465460537878
7523629798011812610015683351170686077358
6535962471634993705159643031610836102031
46705666821499911115676489174584303224
694559947907640167644077294333842126523
5696543885754481501049815942382012721049
5412442348913399412633413071793364931560
375447134663545290750203709495973871776
681445489466985850449665829465098451570
5471475949034375420764159213472184533543
6088122617317472503561938930493221422967
333924128299974664884550186514252438741
240795088651121228443409620446239089055
7991840657327067508096166841848772251935
901524937286223620885113943518716965815
9961698150726810786984148571488019723868
5302621216832756606759926975122845138481
445346872388072354841263505139523224379
32016304995198468791731259853070039166
563665608037937235264716488429167881819
3137181997503993599486467335291794133717
9012554234448371109271261669495494652
1221257538366486731253807221352098941655
4485404265397744777042971964195919528592
1820945110821441977771269581163946854
46477476407281048397194998907471926173
650854191868280538174636679233983719880
77767221849242369728216833289840036944
991126162356539691133829694578880991407
7526494494115113663338604097889388481089
1023299218713588974340655465823657498312
6563395590967391477977136428658933175596
5111170551679783278630489676285314902105
606694182030330985856783014389764178159
35104454494894111071353327573192936211
416472550668789866462244073120764454623
3901468695169245875020327142799962061132
76493581887526514349801551448641271085
8780524333514906800131838280348341169457
97824364658419925628886578125368806690
6151884859818575386990649900113490591547
5670764865387571625351069255474682927946
7960606644235505537370004895455669196615
15449929382563048641841318254896036256
721721139479422471210331787259920673562
87243849575672215249612796414748374557
899598502541872421964006289383828133958
1139204815298980532982954650440621682211
119829362723250195181038130853227249696
6235162146258426429795603535472670454781
5350981224867291997573428840847652993543
744035989258146680214356653886584783682
7331428698627695978473779110041032463
9759195958414975970092977735222252635017
9673681139865868284055715747561332000
815854714978762603436271916186929535628
72601932382359214482406248570794426808
48265828916857349532103142615245016774
67101322858875409762032959860831998064
7768236320271472552561570446418536418
593258852188632415543948216790067026128
7637957222863312240527609949596779859771
523811375112866942529995129748114896294
727655126504198419803168794325744229163
29827671588244866742086807614587539661
880949619372430215607147302594647471417
210855823792445986198720373937357309847
48527345510506150668908795619182097825
9528365636929309726494129092561282286674
13895085687171032313765342841379195421
8598881881991365128177982845225465568778
7073501413588701827214267010246684021903
1300872481064663726681984574361336932467
242543987046588858578487174363224562948
194066445462977677627859743422873582902
9981395849519443968951953915760762675165
5549527279423938903221374490923242171857
3844637791271775980921686463207297812071
888540316185378449486975510868499464353
86789422524605823229199926857218155330
7998127468161519565243655274389880768461
946158341570321827934359763968120575935
7615563337277557609362528160475045276823
3868770727706944196849608637924694482355
316128557857462359009332110371411547112
5643413222958394163515684681903956164
48391544264279617232587313528952739066
9697779922248367232657233179747749048444
739825409635617870881529462294963427214
341030363336303647027707736440442740660
7576343319328822742125959204327998379518
399519316940505421599458957218875766502
9700392332054328516780304410983570078234
3400502088807345298693027289288224075868
312247914998864276796261430212650726791
431068405963498450676457746740071119626
74881145135568299215005807715316614787
1758844116406059126236002873443716008611
6865525485505708197547719275490831102335
158016561527310784151098042512744679132
39006338545936446493824194909195723656
203894770761204829264997427820692206787
7371489531533808380327308920201864644614
81215481094760418524177911872825192
69365305590813846325079402596532318427
933737993015612382706063830965913496193
3263121724406767717273954764437838913250
7785215330288631137028041801478692706034
98635587151856764245096730975264202263
582172469561713996735125347933884119427
399240519747763789695601596072903227994
354284506677149639209006045646478654912
695946505182804538833131716710374491559
7752309595325761563687019252522971206902
3211828517956958782067534967860133555984
45458322441654215228516617343695655821
2317994248985126613080457107419553983797
6070994597938578944328145542489772718628
418358571244559389596813593320802639
92091419245117628935951572913654585806
643327489007736394951015751951250758282
5350623153474735507653442074307449706797
2721112646973245363180441094501821922050
5245126038586310606759445998725636903387
286613962716779683481781442636514914490
6974878169588712731512483753358592202422
94389961602017535297354849822626025558
915087283974338094384566328378814136795
40949029812061432635007240924057539302
1540404069202613144455327581683971939012
963710186793877522298129834070746621917
98128893353447751769873842315427637503
8859684760068514861738497618212724991125
6312177441123323808977774228651476058
13258798247171334324844924927623219767
102611094941718112575760584655973133346
5342853040674429770759347565796376025469
12848356966591962551844995730425823834
486523667815343869511583699638685605563
25241311641726974913422783896538638297
39497852212623216596975981799896776677
929295392507998517237253650167362448824
47079913066316839315839130441847881912
12559153967959247624558842293827657029
9939895179696556534012411960105558752249
87429127896009416081751149237210101277
6158248084965076735594311367669228113227
9108365090914437519473014252336878267744
41655253632869185761147798592055136082
4863918464667176762645965969439229626382
6330434359814466897038104441401676755939
1848767063332500700219962844732937123380
99322559709947251086888231294641646971
3724615089543796949479792562771824518792
735443358825656247154703811280738023915
179586851801690050544366406341827935712
1708761159138781383792939822860341809380
6729597372492513183859009195554533543110
310579123730393047685412239711568193126
400883478727743653717963654870387459376
1271531480471191770468803352360146269456
693635674909360324168047288476670481399
204219528931619777524238348428300171
290657579228611124688275454764222678534
573515345805924710389616739352177131770
44019232212225802126763020663881564660
439845314924103279527845568979336793545
5881370574179048374380185462468656052685
1429297616186447830594538175244038883
87992604492831579271118810779235018997
794036402500678062531392245820192839408
568138088556614396131356813694659356517
4558948445102541115445494498240149377712
6480322487495728175795817328821976187
8114651338793617262128494299588739835656
70356615309059846677751577245563856926
6224285864581607520914984881453944358838
5390345881072397672715153286589317543447
95025872447713162349963853926746426630
958296390437574525613606900672590463124
815644409805771944284177352138260523716
9583670496157056625459561163269049983836
936848089280918692767982227785451601945
32881444639165151967544951421507203401
1619691747637457286418416301987993582534
6170933037655014943959451805433271738411
196229444796501529711984360461776877142
177783095957505769655050623250387961495
239848737905493590598138551795959845380
623045375775289586517286032501192035226
293487936361418285651798227950715892679
1612535766230686212185387956463212807
4422508322686390218429324885875267223450
2428294360136040108295089465447338412828
423730419236584538191891781922473094724
608330585531310382638889023119282835971
457927143131495996274242064611516346922
322895328625690634918353297911267235720
2169814023004753747577709759988645273430
97053903800448210903032455214724372989
8631870985915895993811289427708793353389
3593828470426255102417245969857720096245
539187159450382187977918904977094067179
3485645035043848999981991658996528343569
790428529625839941232152191732482691825
998385838280862397325634304365056264492
7963796797932341289424814303521515178874
1259326594464297738128689443306745975634
7705501620587437160957908555716472114972
4056273012619062138488269091907925762768
919349097202703602437117562112676287949
1285298870792813192285219940314136311786
863059693055811123959355220179287076315
989813045848596384741925602390837422344
28878270832578336553983433741982096151
8482476746949645499532008478363945895
34403020648907224132982319352474775966
765586529638387685342455323752953956495
272672736741572965757526484748357693611
661563553038027416281818169287642021441
651060692323786317213647782139664376789
9712824387027939661648883380443891048767
5496384687598670041922751928586566192
7788874262788544268144485343981519657976
10532591842549992722148226811233416445
89572970819428302234221816305338806890
95836168395959339028089213276556383226
496268101287179261099978059913427567578
8196581610569587420819575305649732036521
66004812758613602064061509472570983302
6187154177145050984491824244998245985339
1306697775180430259949474839824084596
36591432777991889386420568839160464114
952600048998008951367941445482441796322
9143865331198055450220725385167921715693
8683285043134382856761019684669234521654
588731452155505463643847251543186597007
547095663873798948083252775118823712958
3552414121462934682485224238264151618693
93076740141648376388459364407755902088
229366183221266634663065413402064463512
6272340068834810527740704166976422065296
5009798998482856772231013050510574951694
1260824722639972836455173135754783077
657643146223589152438405414682456028028
93965854548865813834231277983880465710
119645138314283777526668686589135217490
3733415180503969595686267499138550127093
74519515658794126393236642151996614636
2270231170699694589687949807558588826119
203397348204750692898352722480857299664
213854592893301429519858942123781703966
604765443787332923382197646269062989527
6630891738612538985414832362228639667859
363074722867562078185809097850974507822
626822017369701757534472514935036596991
43743327152150360033457697361498968385
53514167275998447118243432129726423377
663570591136477118036406294967550456842
890942613571429972514572021860492375737
357935414069527951498149528866584978880
871850872508893588922529309247567855146
4304462510247332475571347839314116524656
8856119564448466818276217563106275215079
77928003430295241337573208971468724695
87604157772694559779141112604326889376
665641426234144135881777228316722865830
40523927195702956630596001742611195369
188620421720697865459359242610837175453
6200623469716814104379929099635894113596
758898182434494312372616277196036163928
731573813928736697434328325241527950815
2131101014636873528928674050740268743202
702185566434665726623431334790875617859
982571066994136123721624964260875596227
393196806909268884665717946234388983985
42857601718323775633558419026928407698
65043699864353270565816466278453381152
3212502230504107723259842711509914923
482089204446772374665843155667274439530
2931797675881387189473151762894732801985
349556915026113816986952246143880734578
42455935774215447142493227915924568860
3875666230924595183513151889555383668608
7603313784056106570697976625141893403604
345711877382403471313752640932110636351
2177309297572767270768063662398383376955
2737123792356534382036548438650129998501
309174693878393109459539884479987144961
6031245116123541689286264264144777024047
982360251439977175796934788268782697928
627241547928978195677744203486379845910
189773465537251931796083740650619555439
49793398038449722254011699667388325320
51787522792536436991492365693013151252
4583186834425559665443854141824873396751
49878154056357442847597992986270611762
418988219825982548914358439777954824300
5176385735588243768927352741656414588670
6964971526784500867335808235929776395671
5188317496635897117001545320377057482
6830845855499813326930286234862867648711
7090689989828627899934095365360437782667
82631862233467233876144180431467054741
8972142063556628058444152539757106889
306123079126288098681232337499260887923
569424013608295402559283478645974493890
413784446979996693720995883471117324
35721342592105749975824605653197773644
4730401842175716836898559868728327613636
83095532293956389965531802371210232159
91845154291191440943988144416639203386
430436526823660273349768871759117072102
538849611026836715103943159599926606590
618837099055454559087854889236446888029
983666319583549485974364066713623587601
93498302855884365737953478816645514845
490091955453202924039540162959382231285
357876573046884559096331590499830911496
8059893976245606573490213432544610871990
356778096570776068297578156710482380255
38132813710847873941633181257442075191
94369708154636779159273472650415303104
3694317135998579227079664952229065051643
63046064919079777829661480614862115655
5175583293983846496954697312749917772560
42723817876350157416154486556079092555
2448013662625461637006879493941823097
3516722987811535119168577397487242334900
76967332304041914803769346978085726302
4930886054201123325659541038571619925378
310474757517944226119957154972360778066
888315492822170322928359912927967834932
6984674137253137124750536791517977351334
73856687472963889712733640367979375106
431779377923452933494547907382758749228
263432741671630123761427942554282078457
495555947212915146326217970347693436545
163082014272556018703906476611471121226
2101940430905996145998158474423881244239
551124702943560816564635700184312069102
1335461760251639938013236718191521738544
7826174074639211620215382430835154581399
818737646370853312898146504200621851653
423059614885165349866575757789373884548
8561198977344064688785792504736468679924
1177406222826328331310303712179564719896
5183201615644319576011521165469819353048
3782385796340110492953875813544117427
2388442423406376114589336184617454454566
383319156392304755691623977453774760594
58475376124414245326057651585934374017
9472875186697442200078561736878395991064
49589459454459513744227381282952942343
3434312498518077712466854836784968374521
70659174487523479672174141194232283032
792455508112442556769182366175915514577
494364953363236732830329188341287754380
986865495492258794068942871777213239446
184256998219569744849653108696685039130
804076096471131962346564525698352267403
1843109238602544157374206111640491140
6059465117472415153067285673194832777726
4190422723559704932627142776700641491622
2093209011519674749857027863438140986129
8216267331824896320121226212520951816661
67717562146430639240580719561374069284
6074439423198300990937458289306196619583
7351360188806805745184571511752173155925
929283765601559137091823678119540302596
887274261339861115083257218542681503680
58719498929426949968037839297324548563
76206322522437312581783484292894166142
93113492262941928277607140239449956815
2562689641317982573101539363344886174
8041638920957345144180849642338414417164
3360407621838541302649385113125643196174
552460654969399659165828989343696233538
5087651844339214781076317852081362775
4928625793707480285618954353552531839541
9703679720488898510434168961628547997602
903219704355559241308945383373818126343
89308991755728058857548944306043115275
6827928592215723188396509913782851417
868638021053692573312332144961902992
3418708067405284242246515341355163382524
139731422074899236074812563216285465346
18412859959644020762184349833079325808
374927482416460142364759413425163683109
305295478580768018075100455502131529847
37768398625967786976555298489073539885
996836712404274618462547559395056891958
788647595645056841793388868990060306989
712297669437616085612195438318496996663
395791911116883056678261958749018147029
8098149471643877545637449216121853363
7433345017734114261085729395590030307408
384036244508144924495865039103137879381
149836789746944274023198388347518619
90945007988886539413051726884530272546
945683079595225860502978741863772292413
384081329558057656145831489286788525214
579647655481620929683344389525940411309
687086196035422833656493546870856009461
4448539447151644385673285092938765072280
4825564014998954511946462967295301480
6978491831573298778375906747594213878942
827593736408612169466500471854983714357
428651514122124284829868683894949551495
8549182126905971760014573657565887174360
12613101241716599763609753132832069608
9029644438899474979012802892934230434148
93685953354898003558003906779939583032
386064558814347686117052883368840487065
7966963461724966768234339916645349413409
164494788911278496228081404829797363200
8682843985415780508516521862612286925563
103218123274405775751915742283925512916
53387472545983217490189679553959026001
594243984343124334895736358983589977446
1030265250138996106119576233187072203197
4225649157882073821025938454697164739339
634863186156437028356061421502281672186
6850934772958176429893898335543174311361
669159121152444183888954610959449358198
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]