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
4211805725216463677591074846125146362908
844098167964949178233039401567810711137
2326200590092411444926813903561381708407
750930103210744979119909606213322213212
6274731590721158660112052915504864659450
6849382812634720154381931023733657297009
591132885312308064518221350723035118645
7922734678596741567042595461108857985132
815682069679323957952986391353447624
55763182740273223484906850151811339257
255716383116148746637597423460147887645
43998350420037881512207998211691909786
2226997561823870925556847096289728562090
4424812967104837994572422505297470114525
758092024025867095915014692258934249885
597824723346339444496829837311465837255
798681452121250867772155663950225833296
212527845827989220496907077182968968665
8689109771911568157171715176858632605595
699047357913608081951439618911597929654
718863166717842971302158225391665390618
9886793594839775505935176143041024798
6413352898051893966765375991170059723222
8596672978747275750767584131181954668646
7032386615849932861830914644115293259218
5742475586722991681772306727260757061320
261776266764903842436133386525577191959
1566789879042881522614665572450928567766
659395493719262422686946229039299965134
987922582978386830353306547230202585766
9000458351737481477832385807383519486140
9916535971056364620664669447320933035778
7841218833242703947695282062318012409826
273936072791960960480786267509960198808
4211627819493496720396661684856118355937
2481664776756834360188433692957460065651
611335892662981533125337977202056299024
6310621898498048933776423972979987004366
6134952679712129544411386861445619015689
708894778879780125015329840557952818535
789093874840653852972114583891124015109
61672302920431523694133178540819855502
3341608629251503396046658594978029504698
3089158499907549921376228879273819721
6898220682841912212214723361737939443650
9473959053975417610467799061842216032605
5952462199354815263374725903663942288704
3151689018828742343196704182421684177
831492190381736137163131013924583836
8046883143622264453818523305923620595780
214131436731050583934581153974544774092
21485936327455221064813964588311081265
695081636443127902281689649661580218194
848733463537084106224117315553210398040
12037567566111016646819790670059331855
723180816776429345868054324355998355501
70457565454593964259122819875613591595
44194065116361313039692222533716345591
70130525244496137647304817474194797754
44607494915486165215735157729169427591
179757188981797342612479167741066012579
932240036437280217274400384066079776348
477710103988148832412531375997290414873
8380749982687146226128233733059918
4546487487624497515149366794732312606
788622314211280255452756957759618328537
8573112787246302439756692889979975072481
6357447551139481899857255954264463296598
8243122675066876102364783801961561585738
78736413669062872074844853218950293644
770587247429555173191823447480563156872
9104887645716380279823021555488553891871
72194617809455986115768758117611772883
243566907536597159066009301622825156653
425332996706076176693592922896465885328
5130496023723916905528345618513779334783
20696222484811419895177780876622784113
263861645378129263882401027632568238134
6167209773779104765469684375229345906823
3908499538335970157861052717983941064809
778616539731118407437833283660281937496
3033612264576232375752345225884455335904
2224900750984533678727374912540142068870
339882475990457293421953861384165331447
23856665032699743049088101601318294754
1429603191270970136568134289163108409
8363279114908006771158867767613160096139
1764326185868712652920425034886349968635
49734023548150634970718648860466098645
168678893858364258044566416972171753266
194621013748617682272897901980793404049
941072878983842760649412721342771918507
169020456457900512772751165399786087679
184182394453656388975689234948758463002
581830196367500529326569829447268253516
95459561487746379783983134302678471054
3973689498738971403785629411136750623266
8912415736042577192481804246614980029897
59013567520672787659663593613746566740
91101803621479127316623764084345704177
3622776883638454827135561873310321996
2926737013189688285928862432548126443395
5225469721594316219844539263306677884156
59753888693650188444712929260625664821
9829435594373006604213726255319886157422
201759296813121873337487804124229501343
380696683733552794255402914942057928989
56996595208809666986542928985704844232
711448503623603735574489360718451568212
587456876271248022492802047236914423865
3048458159601977715178464853483086541761
9465435290257095632209332813218767293
7774868449159692259529309853532085166032
96344225398799533608942814229021393620
3595170695976906988657308105663248359059
642318582050287410163513663060613516372
500173076021340226941164596545395761
316141461628670517485508079764388712539
57980486122361563805728576567112324725
65032916165928386614327146691611255459
4016884363292009228829535418596553673835
314115878884697531151144400573210072136
95603954816774457813278622245378947028
1508137235775848258735098869103290311275
7433349280538408655026454093178557081
92805592870688444183805875786587379923
346787329994344115435404682204254199293
1655484534023889234496189608349629575236
562531756418391544055795412222582567424
7625468110075221151553448398903472978
880457227491694565710317546647194921010
89426287807319465281043997798353531761
377473757821966597526806227344578463417
429320339634710591498173410433948739
1680536677918606375099883015367649901808
768699576716788969874730172391252796
2380348242535340119081013706312775908583
2209838932206866546683491270429711847249
6646950388422139393112906230884168182706
377923821706419792297754164258511658415
69322186350497467786287871119848145937
73982355099005134344787553837775275096
8813647472596722506600636591176656465
388755974886991854914670354089695469
26210903582807149204002873521318225127
898329174379410994623504151388653937247
3763494386864257480738976662194952937
827693763492307022916788549468986152065
8667279647322192777221955002458297843792
576361144578484853558618239584028177461
21952623489318072116479565201335182106
99314642763287914606283480685136920347
733155576218392673240382397825652251829
49217798155798135961498231071946037447
3124267625323370495814003779827070832040
607956871785872640659601326663880633582
78013453752476378381420556059326918572
826435956258621446710366581836219125054
557676042668331713340321114137882043475
512192545432876937617221125074001747825
310656418131663655946128938734564448951
844023079264115379454252605141923898096
7748554328337094183719708334386725002748
1766105596137293359021199817282151452
936958201621360971473552283497744526484
693163214905824232090354997463665303393
462878948274760993286826311525120234344
409999575961331915345931419755255519155
2255645729527496433124099699975985366625
505692846045212420241225860764366248818
96832654268733142104591560063152528534
18899308602089167554991148186951717664
64494390580688326872872299051708111704
2538631649681584715729474327895108832
3146499531635966171174815703828539781213
293659562085753373645856903640089462262
4672718366614748549853369531967781672745
3464330345619349474542483467661616189
6012989818207870550080928447807587906915
28466577504017638161673338987942705587
815221838135223060568180771617765468551
4075927430614396899585198761784785481855
62122796153521122563151742841316512990
1202601715304234958690323558790015413322
168751184390941567274672773442730879
842663084337728031693410125237918954013
9392999887548488277412763887820383274750
9072725043241782226978288464804877368145
9034487322323017738233736011860835505651
979127292750311785857588592738996955884
6930752591034817750882052720363484444705
403419096195984396638186105612122143840
4128414641689477538747292648615092238934
4637143493282589708550332700253164066095
4865373043635183316470959988231892393359
4325725214535930373098919973649544211
971181562810945422994082399159431208130
4044145219997920409716495387740762081
8621631347599024826172991953993019986534
854179099049463075426251137414944660
99119661652923658557515214771864542285
628437767904839815232972209812374474375
6991632998411458630176118365576620551951
103258108490400957223467379835823857664
933870833253717523061309666966321176710
34118132227313164511604340979637971590
2487600066714612431137869868156756203498
672926394343299392324954489787631579216
832049079125651515772941798669754446210
7662350346807506123611847670864557932758
28508434709244838338545344929293653683
943702082022981212545068578631970591943
9084936462076462179650278999766241506289
7649789467733237580322635685152754369903
70874533108959519922323262341058654339
15897535622699689454415621154811962418
644217719344466831560183903567069884057
1266231723505920543620628525752049633852
262062125530302965768097955963162195534
9412106312828923887219159892510415801548
330710953512994411647088599866433596828
565042026469240292224673634832113375004
1036263561656503426683396288817390384030
7249281913373067978443198181814576458257
7985839897387512651760796621972985916627
42368785820588949741602410336357529391
588756118164259440278787225664122616149
97626122297596340181099428617965878732
50153941308300164515889937345683168923
8633208129106034848277239662666304709
2481264732207462155210852127303866568311
711595085475372635322009548332982137516
9779387874612009630348192978456875657660
93658184745847660875779150141348784565
76396745712294170025343557901761541908
6442149246918195457791934058100124677694
787948806127181852469164615779383136819
863014735832814115328011557171248466874
1304112112383795708711497394931845634424
411291339593815644211009138620676905064
9566118881604184627663053060727785812002
26286979204776538044231825905131647979
5125289824705614817251438786912049584028
2511609043801920957867274956289091451664
5697642117866936386945349244210569471590
474237602446858162476366256870473422546
938645744987421856563578952462213715836
7110767659086825755296018030363269166123
7781540955983402562174951359848092239572
5902643158391305875697881001131736623308
942357075493230770992328995402194502045
4965202485533140309888103032828886014722
3732233527166318434651286531151875756742
2325876326231969824615098475537072956066
4950356789758584919252307700212190405299
4080869596986490715619469316716718659129
8297772837876178144806720340821794959
2127623861751821649481066204839952983944
443842682450286693924318830221893669499
53735502117280541175566773168304954576
55397067071235784253677485193368666532
9229331891306290641016662270936272341749
38292715204537183831828425047217427283
4830747379288074681695220202488168480
577520281531410584414672282374423999819
82553639302771581546456716936352322113
34272457505705358077431023730952287894
323530747158532631099580635156260407941
53706577394332015906953988533147061995
26629679229452246658531582670812791287
476439172569328979696504862257140642775
157771047835267646870718462160170238838
2512875535927152410855138841312686071709
93067885209180211758681542245653387343
713229349400977412472431329464726436231
7991261068129473641464143130204674245126
68754100523129534012737173341921100363
77298996306828459562362477465974391453
184184585621749396126817057245051398235
2275415385576743677433123318385916666625
1758638064371967390930198917194172795836
1126525887683264769282463840325363008
43274452533493052685740512252129635
584705629633407269145043277776338548
1003939321899038403558541140245713797
322836141082422457484294705987413429943
790288585208137154583776342125227737845
33730371874280424442355219390161435361
538087859215102101151707881726724609993
769065999346350424116391348588594768588
6106625944342725360825698805186731776027
533949305407967113442238131591635038309
31744164760217582553195457611968478613
97368131163716088787547215926527436424
332575299270408444083387644183658816552
9831994030972021420567182677139339928528
1666838719877912717371268208306448841089
553324473112877895836699688970218987727
54617800963780828913777927355428794308
2307694929319578610432514792663062346782
875948331472405526412423660452512681
89889185781176085476712546255475709633
7688502045083996551933139905864662413829
4249328517396639209136986549147170465312
222851752573431289312474602869244073107
73007954093080290074484102563015643998
4379552916672076624484319824283748299523
9649205459937141169074853311296615432514
4272201241019700658460017976623760672622
936532933608766488959408553476379202896
137341808709405819483559548519934941365
139377341505628270763875031833388001153
6714905080967765115913941800401170792024
494958287768291351575525305633147929218
1086855834760756229426844694859348558
513066766271961890195958179469234913171
97251914495062309444404599279355729341
2135604497136628137594583832643735908
578393861517866405292164568386160255
17763457426534877189949191196562189357
2931900525265481234782917450654678554672
184115699147066708620981904511123855
351392865830272471784529924767733241899
930214232710285213933514221248661181335
1775728545293537683217753314354353375518
1153635153115501800272352234502336377
255934963923365182577595350241365465965
173199661117555883287729167505646332540
5797212767706674679578862663529352397385
8724958429545237486436365541646968263113
9596667071757853903576209522742734327968
755692511206550616965308194733977978608
64941471211777944586549760757568704460
137156308186517514154702676650429276345
3836798318250184807783988276973729918
538048491103965435540019284320778476597
7850559083763269337819238068118582935189
75443846806290102164775349505539348790
2657541017584483732012001107779754618164
754035255154450100211776847681158041830
4170306130935182550322048382027463779
214321501275249882165339266155745804126
852057852149452688205358553611614824922
23714815849776898389146867862969958259
429675664798072597282162995514633395281
3123661376882325724712386327618610354402
9376866382589103147254615629615533431273
367422376500890617425905482342129181222
2032690112878624399730891620189693877252
132789503946869928635058707935803423599
5293183866587096908530964995213073923791
8268533791058923405733471516642611673829
2898892834777806381648145090395524046320
1998855114822534513676524841278074276747
9463424047048003565177836201995834123379
221253399617750271482897542512287566316
23132084186935004567561331445976977622
2054314378204844869827937678922797346541
9017437593361379542921311679316694408
918053179938604366227741674661926909348
29315709426749666179064134992729698538
6627328386507287964550357246342241415139
4042543032195128135615603965659122672331
2244295412598538213959226973946849626046
2454809248612813285153418340972483287043
513787773787564914826367568634876706693
94887087160369744024351316951085581917
135585417980632629435933714189086361623
111211627783504974298742869394391887276
8374607065574538930537391599292317676497
4942480148763583680926479981273420601719
230913218727193524884021449831949385291
144040409176236468277548467677872613880
6705727842201964661843322047963348161674
934250589630326674663173411537501853
1402557777111068308317928073367423506391
772372733394779503814441934335027547061
2661185343202562795932025670699175554375
101013016587717942846079313295192927688
6751822174343217157696417261875779990
1725197528131589999467543124186963244446
4334176574568870807908662880548115994
754552756574314098754501550699728076323
9740351044048728429754605496703118962450
1478934322182345444440034931324075498838
49176195823125169591575273712254098135
93976244375621386403955374272740689584
4701623611119716463712549363265946289941
4288401569862371550110199169246212498598
484159159082310275454548848873274122220
68389395333258022867880157499211697347
1420621723487894969594011961258364938372
54137779431986581094163967536298497804
84512401779588228240438733270167664581
3948345037244096667173488846296489619546
4012102882326570768935083047419984266435
8460741220347192674855151946919191113451
7605355437261563151350193385449329828364
8375918186833414881473835846994096427
4020389134875435462051942135864293328826
7701925689166397603321572805123614839328
271244242551522492558976718139460520
7134902218965287144958120137240379654
14961882171839954484470350483742157754
28529540188715915988587688490551808742
778368974845746762347651491706769961429
1915843559227341341939255229492888582655
1920721712195750284220301073856366629844
34287967929146920892375555387877641144
8460303436995105463565177980623667606163
63041985883970726056455249834299993097
2794530239888740229358948856808286554456
53415565299686927862174773337792882785
5971381729413325203689679870130681057168
670083959027909476293413825782268326628
3920160811186623526087734497398970722903
178877208847945642661275999689394959756
49763641870200697984836843485571738817
867197447966101898361592450292641943259
805073069505183593073856222474920321919
4433810789292302934499982896419133287291
7302807412884631135547571515218879075303
408476577318685773254944718452277297716
253071692936400981383466614943931209062
41238136479124130012695482252821615935
1267274420319228221664345720949464075202
392689243074794211379115447756628241047
8598234478203578640566872230325517567401
8747549568307151670699568640785918199453
1562289023152682472471181675614034442722
459115982538202850869528031636987997538
4835563857822257228215777954796388384458
31683849218986433382282555332534587440
650896249548680382876372142766375867114
871259295463926055877180724521537171405
1676968647805215902097331228120577219725
815971621524179915223657873912616935617
955067402143039408529499360986555191
88762524600291851597144820182892801274
64239862210709140482811976345135633484
9055184734084388905730688404777653469305
22404786176813526238641480873721501764
4599316754735752724994214897550337026260
9534341162603302236427314353909336967665
74277099800294018278876537775099748675
245572428285912574963113322827313077560
3119368876469525745942144379292674363
9222943331972036992291897715106199339807
8748946708390073764755297357960563584
55983434377152561615590567495047969565
69465693067202520529019212330341954982
9191338692579815304939633443779207275
420458924818773333094426627815928997948
219535903291101516038457111470226869520
787966167633288330076979145914558958009
2369148549435703506686811495245583615533
4158761191866775765734911136802191757466
8805128712173071260329142509378516296847
651873887887427369925223293917574875222
8429137159459931140642377901482694718025
955068651278131222797100174969779654978
5722583341818471964262697277816786538763
65143892362502074196695989358138395857
692279851940888454154232675412366364467
795534953606951150645864944779293284182
992261643930421827373875342409329509304
10452568401387332023221908649442286474
470940269116189254962537284009333387
9880330696879510850927951465436153672876
267863195845481257469101978286092955814
562013422038849575021896255984146315602
4605451148499985858657772971357844225873
41566475734474538873110419574474584606
59278031961688328813292769284045650749
4646849275534913716279431571081524695
15902145149374761866605395203545612728
5674505317265960283045361634968217755508
1939279015276401967054189338777271357671
903681462430411644968145652156198104876
55440074678856569197490160573883149513
678129879303780896743314243378838201400
8509555177381192332390533594594486456410
615366485673933385683655054593653641
4625863490019939491131317883342910186566
1864239240315281114943785083548392603
9066682935114656110649746876596412412
67395305622446237651812154419816687387
465114906292955097862117075144443528589
325995834769283730999612092680051097931
92702066290572059419306337266194482814
415366428984391514869342256188385272186
6555964459974429743476549406239310370
1276847418875436120045916066178164001906
333097642663705161074082957646535437824
6823862138779645589552705842128484365695
5639390465977397818229891165664224846940
16831165517609542775187340988569874818
32173433106545533410664947886639943379
5344901967924494926651482803509145685355
7316705858513847265468879451284964334212
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]