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
157751349031596470185056916509331934363
57566737929065415222694210948017067940
19458818023473986224080466639274822145
156125128735936427430406708208910351609
2302192871375113204810772115202923428315
71175408334370771155696661832625300740
1387579440431245879961118345310599093364
2357264313066014770516145130162163225119
544191939071694167368093468541611656594
83853541554386752562623039584791758945
56815427825612333058338287358130043025
45889245226037397570399517277463866207
7285875912011164123141456378434504860
47244831345204917065862010616364545538
205894635222708249507795667315844047405
1524804042435482486178086350634692364835
869887819535568222989094230529501553821
5340871127738329733877114739157941624367
9478262257272349411457762213834250284114
7070433759562299523312797506447630948406
924383440584267156964404878625402359
1320524392362157729969610605798557480
206812125415770169385848157982150676229
5057189410545020198449709383999685002
9980178110696847904876109717898894353175
62743630591234312237536228270626020288
68384185433871533853965280293381319796
425846525241721656242766544984148615483
4466441541303111837878591037716456184
9924536437153110191166087304980390933367
449279417045427093031215759971095157817
89752756838735708658851910832608864118
76659983184350026839214987589481927107
872517152969877148457122128446561205849
808946771455485376678683298201577187564
944525305653510344297071198272716155827
3500912763653452124712627817443190733221
8748634210591079602293205330127077195413
9634393036678344971323595292412799046
30424317182433877167939383664248137329
736359817536269786425451429295887483396
71366751386544175753579412528983534425
875488146072236432564602220470662172636
78186454126939979266737570758928149985
8502516677725386798655345543898191509235
6091882389746234474182552638177631319180
647526385617806211713845777335599743362
860568772024661152724090837883215085609
1969419518211432595830146495907935742
706868396192161012241597370490396047293
9606995530516150131258358846825789111366
39236341896249423097100127278553649
68676514896754031753470855692642764527
808655460884693839940927798077320886
474537024939949433277224559991158688751
900650842820761555098163596630958764550
46853882533066416461490527625867526293
553395125212274675181364224776650996944
504863483917836476016848249291194834630
2404150815185478512787156948583839372024
23856277907040933119377345035333657761
1130406164556813859420871309644643643306
701744549152503420654014746649677204356
9922569094301593797191834360482878034676
9224283875903360456417624579440635686015
2981571113699688294244552749830559942712
794274727008774487151494455593096101920
3257218090574385218662558224305797124708
8651180929589270708757330480231033724
4225348614478777817797005392151510084945
123156142392512737341736865488432542850
3617681933787570545965975258439376985284
14222460139680644484454274893109629720
4879295185839882110807291793347245673
9751736049193676293280786762172380136122
2314530884429784983523335808272616575499
872915196050454812657186764336862043146
164617831784545897064138717876629177746
8281494728998286587198538084423583012779
4704304654349079349824396787833134244207
767834096219137278909879768655108515406
466061556671877506380273360813352928743
7892608045084375505213503771223033139153
9253745539912553451818824119326175728674
1336130665535945422863051214802985236
3961239134039369347737767308377047705
937829911098792349866538862905066843732
284235562321640797213739883698607747843
217174893096677479140172711846658414175
1859986775438076553738738362526014899376
917189220103985174594261594114936219059
717446224696800820640685463824430387697
6386671545124125910377268305516597154793
849158061205788456267099525663330769439
329044592310238753894142988265614842718
279592396625214972155271939618272402762
3409138374016530667725458681541246289027
671927822562729272190741581458384340
271796348598105270133495282970941279118
784332518585602895467105182138626254018
5579582963191964656887142283352668919016
4737952060184136502188654487406950238
46862292959550890026898555518146984138
54158676272964605219625151285812159400
5703409498903484893026586273802542148
408265999407543737932638276479772004685
6038781457758587131419322821833260126412
141262224872057673162475858448746551674
980573668780502820267500784287687374187
1195856214354699379194626735107032333395
85334716631258867124134978365406091758
5287339359415459107241837615144623444037
288159443095938877162964028592122141836
7121642245742122616434484672212788366829
984417658612432969006257886281941861214
950218294946510027707943670836094208235
9496758422287653508280167779369740823386
303854554535810850714773078652248598142
14594309099321597867827051822047018960
1950743768396742905632897668121572594679
69798823562908580151706617148934936615
4962170667386968156633745739166133028723
8996740913465336012342341388985349148
8169316396238393420976977774365128753049
6370428697188866423198987913422469596607
44733173373298978007894810662305664993
59637471565663313909331929714352966358
6731846866774463985941776707079947313
8279183489147449225874559815647353827443
4626437992267751438129323217291011244999
6886844656999467361691327425492322846543
59637086636971338337382493892641291027
11988851836817984312859388597097247398
1748292938045110941447308984219958935695
40617907723704983061006902329677771742
576819538723793472045578149398384412908
517154119129372882842055841999556192
251929769188199473144275953286420458977
5515493615061974919984054284260318845830
43697881517380441006071858881326461234
7719883055734113829878807136311226681030
4697525323957468673713751933870657434664
33387807604505817053598003187354767706
56165218348041411689605912231847081027
280352808183283143838468782444523832955
222935001962116646449667348263362897209
915211007934836017318141870347333216244
5011304546256244545763666533899172383090
454789084957897089409715976065461226215
5068792856772313970613612536599185136219
17558488148232974641355688270586915381
354165987081622488277610178690023941837
4575424318767322913431661432553983036998
7627245580319851681030001720535894574677
1050978663591381845091098781611090473013
623288876064256666813141694015386057003
9037233486715327410777745865597078946125
73839007732262329394881987558639573392
972811674677997658524792673781511971294
4544147630735581644979463532939084057472
994099226867482737163315000703680141545
157530604099956782549470873497970068246
18775904156974595012400433401945819509
43681331904116783598293740226985287698
2471540394964339516148551927857582148709
6534621329545934304054652163386702408
9296126380122137148417522503387459997580
1189449445691348812893416738674994259899
3470662777483003612755976482241213353862
75497009694408448323552826633577998026
28758261193530882868306361646085391655
308190711999794767063569529199488299067
3299338357993695524474869574904988669200
99457388464159756118182146218881633478
50077167996426447305576348412954883686
2824204425264883206077906916606853373178
8723122840144544724959115032914266368633
473477155216291980442687596952517453863
4589481751951163533890818444262941232477
351810059058252206344709712781542998570
564843258414656043967579578600183332718
120619316786577888288696181018559734951
83617491007866304990707795473960509691
33969081663911559076160149443621108766
2239480226050016696773033116123862964
192689865038936773949421363965078961776
835792719966751152038024506890265556174
201359147503676522849786868818822304718
960579092264392857366081382753863518
95123565270110710557083505888028671053
72061147295289374342035876403555154527
106950841046202286907218263369833149353
622665098530509860067189820421966577075
5948496318774894685815958898829162803156
9230770691993718184940531113537126683463
5829572164268565493963661303949289092832
149216154417561891078472771351296090995
139998742078403935746935815229821579908
98979316548982510576061117894214611039
188999712974635385913291513772259317653
6232424464297120611467655404728275878366
2743591821025277849382438996660631724609
2778747987862351211510704385576222969292
2295164777751152773091966962758033171632
3381255332556746847765357080132435448789
9122306874079941929297807034257610823233
65646192917329022664038370087552383226
2600463388405085337014425773866395184188
231112236826695741497771719379679353513
453434364421748974507965967026242861259
236744283341661933565525839297939213881
63397131105908055884963254445090477726
1927976792372473610122142999594768153362
342269343262288828875535847212947852690
53297658669215526941447672681343573
4056561032166683754031383069657071318153
5053379067353554857333922271701285829791
9581155012309694683966729376925617252365
7074948313856906223558487025916587493075
8702494390577233902344685089183769859818
6631939239441780097576287400858859668
79733055102241980729758549686956912118
734851565867620127517994610935123925168
4963329796329201960065231382426441228062
84899756593554005076180887506551518967
307494664524364742015807946457641807272
7730155648781784219681339187736630458855
291135374485938492453016643464953866853
417951951954845517057329717296095194580
3816806755175379965431326244202187825545
775486825227969135735706938775162958317
33716325987246782922696705853553896460
24854714923975108839244450555666813290
759346554458092557836447903934778424197
5823715716188310338888095859306812445247
2355200396207277547268816235315589055713
2288286419615414769674262313673396748
923972065547812395296016878074552008130
7931116741729032606320181102490542411178
626198608983510543932013745577877837598
523213426793114270037589200367923906579
5315645159876634307199456868156020682
1133677550456286576094039366639850015783
54134113523196235625692860439787672675
275931224099813398923387235186463588
465966776434923297615986311602506910
73878994795780767986353846691535387120
77003491425664683668407853248284146149
91655246927413265208516893817141359307
2655969512257460756091303544735311408543
68224001765860559625301061358610505370
8794485734829948919826503129088502322
4551636651371336399534061095829552349696
7466978673884715563650316786151335149129
7872331454869893404940254649393228558599
477988927461585796727167611279665147
6341999411251298312289573672389154517010
460822547073333815020438485619554228981
8549605941298517234656884958520170565368
40675171683813796578953412098226152210
668139022072302872899372802111691206485
5323980116903721615360606513325721563461
687797095531775188816729330475729282539
551646773791468866149079621946782795501
37455892944625522506759564237877708204
673948101899584732139468760851233076293
235246323721447633601983199953904164265
482394821619964291693807644535678106224
7253892279044558680849634577157260298437
365152332482977412024801440786624902783
79612813174706563452129192895723545
797886070657305885311424270481923009209
303578739839736389311120546053205481785
4821647576451243955034199492670892689736
8044674138135367288220612364233022626251
26205946910039473763406837386822919599
620850119496158890536531523327052962351
14647859171080128844584334776357418026
8242822324567433803150819962496751012997
862312796874686518859971364953688835537
717458951278351043701113534468459553371
7360792315491333392830179741997278339
5429574263994895800995489013500032061812
305158148778020679604760729045163752
411846544701190492297932687329245404119
351842302504189860758192680749756436618
10511692274813528251478448362013127548
706811656535492510052861813107511925348
40487754985227647074431810615044688162
2504514398451122134624656114508076683016
900959242114656654862732535866534258227
4329601639833561392566299398237229703865
433564162755115762899782728311060517143
6955957655164810201014587854697979837435
10120875957305499336195934818484841732
5636872360901905425081093770794976442715
8522342763509244733937735694584732204659
99109656240269296237632214422057749009
111789964546151348674575117574499853254
6206771594374411498019187515890796922392
4585252196543980211730388547230118216641
97709297780526919367350708852548467690
7233805625463072517809116143253009909
181856221051759862949148391042139737168
5362602159856121744030263145816310046775
552654043418287158726976457918147062491
9874902486159733522337266504968082498836
98078185521205979164559306476918327881
120535317352908946124147115884237701761
2475611092972448933244779487505592208045
452531041273251079529770501985935266502
44477476447857443344362516541249495004
39069674251842994413524369268514468155
9929140863135628108432176268463157749158
507782755836675167898067762938315472468
220818519509833392701331982012243685793
12440575232570524843662470945227893320
3623522749756779995818444700159489379280
9616177430569585135114995550165572485059
628060943437721836083791180508964434333
303277604592064951342094761895291873505
161975574944358157879757829396879643545
856942755983804562866842691307428223033
21568273642535885944994133551892062171
446570353781241164639275116374886235810
278739876342128646476388988774540657315
4914152377025501840839486645900563301433
35392010255253423690755178388241592067
5705745898173885882092418194294822922934
955386239249573254813551542585497984668
9239209897945783633664753701959831876566
35661189300068113831712349832170707613
149537596579321877848913632109776206422
774570039637155600950074578190846721933
9619794515825707336143033096487619157784
14575559452819533885994342744359338095
9906324772814775589140797709137730795476
3400715651089314592633474095569910077780
51288345629371344194552930373937224586
89372415356748152098201911376643505816
205631001864516478338321481714465877162
61142081351704419827531758722387191900
3545132282738607495113762808368649828142
1838558131672188270129752260565020518847
62815203317215231004015842367059934756
730742586346696704453686624178131789701
3679447155408016253626789618418216819560
860734635779509326251687266604550722992
99682734664635879368723170944227318934
53061714301770091074375514140869908515
6191286079321239351548973814347934571723
39503404215227191391627868332782175102
117715663223360256316284775586422475677
156831962835754507617727958133864473459
804220502687985421543053534293733655795
5116374982886215137348677264227363279760
7584696119981424852433817224364906411
4298222832181533334255169340493261273325
389641043072943840206997051847169634
858652023277948552538912384992635301
92389348950363916897377406703799152854
646336556050294552474397929190218523610
635366889505887336088417080920266675749
620858631347218622742222364798696054482
1472379364509984813363247149212892095505
944666063288698593815004136578628129383
871189536174876552779902324506345067886
665479972949213390685309872692036045698
7388821183266558513230082968644436429173
323995741697859440187701675112550109398
5031985355207614123542389004122459723074
4334606754159994114619061553516621956964
2703466843093310897023599963768072923408
6842336226246155827719302123482338355372
801574418496831228794114036859454819294
801606038142478933865418061951212468402
97181899937320469137259291677574681126
58083489650191016755736336777723313057
2179560616753015615573329372667202069
58551624292724809922337584235797664591
65851777961900818986313801761220741113
6458394536084360545878416003595745571044
9041979175245513251459364543475793446
5603817898122384461937319163839732666
242883307918377409793783015561249828560
4180856246036794852046098683561979824142
9928812226679429131991104538075126188
3386276654012200312159172959307553705754
376189376473353032561015682941452831341
2353817591244091339462846932917340627178
287867869394228903624725396921932626977
540398193108449624338736259399741923363
644429391253436737102337335309513989071
14858807766113642969472295215846925972
886626268492103041106544945789466171757
2724912749136825731474058452974189545531
853972680938494679060249512622116021600
69385836595206555463676377453320381031
5084815434492319740352694346961228932
6826638883386859872521233460394178729220
3101299978787044159547661741297994973954
1361940135832081673078841341504625656728
2124979565709881729158122325639747717706
203976321959895233101448670988762163031
663356134062773867432783806545677086490
5297146299286360808075141461812127609351
289714436079147313154179632282638682062
1775555677031984478848792597689637692450
97263321594535436426068661573423632819
129476142162179421096361760857056887965
9436150291081697973941766745403255553863
3962879657449497246938909831152498983831
7086535950831561343967902810232368536255
792972505185459717744462735301941359920
813488424352641367530363201758386213817
82932038872960914156545850135528831340
115581668054613416056205844873824405164
698198424617222708535224925396449535420
370437162797102597396184927392797489691
913177215516136778565445386350555511646
723357934948780025438306157284137645164
47622991315377058396694454169441574749
7418122754426486483348275894833655634662
7969287447719014875187584630498515499041
2213204168974626300756635603353250452968
6807858542091509161353287942227414057934
1495695642044247214491232301164252714123
89478564646611244216036992361119487703
3440228764576906227798596718218880919081
602847195405406571578197138860481085153
856248358982532885593581172586710867
1635109469857680743198424513513796626418
8087876386628023339680585063886657781094
252083202492582710917780445287449306379
7117524432376783548921369978220324364
76483157982776435677654996524374570218
6028665765748606998074822238741226763428
68634686249360990039993718997573515146
5666424956142385889524382452993438917700
18951092782084576696374663566535563738
297761306726935974141147142029848788723
66932255528145033278590655940543637292
6936811895778060828019523429322275074
8655633829737886609639719189394063324040
872450445527627577263492720993749096917
7295483663942696845617445007347080699700
11837708700435792506441340066298935832
3350351251678708257322983130958956545
8854793894668880403256667332420570925904
637531081213770253413492404599637256603
785390213178881544201842818374517415440
8141812134128335295934015366232897388172
8784355572153161869859955683692191934666
729179489088866398172359429939734252193
98979464537963688251581682624778507264
957372712283818457963938135693523867
4618431641419952711693451284845185559750
5463676249289633965111967618376416366628
122651518840776643539776640318479847139
966474659879709290788262845010986118057
8166360126596147245292746198774943263258
3738875051292795988142049227112729006589
72447670778061546901439550638484511889
2301604238602473974489016012985067893321
3644854468594327536554063204583879534369
86545105382175373119262565470082443683
6040159530817542862996857136919334333093
3563986084892123537224768480860176395
454423285427243924981954871576381367560
538839328067258818316789516983726829055
313132136789192442707614825119843127611
746783919767933680010872439792376875240
1403754659708700885589411756374436232869
475171366273766753976016859883551165501
485969481516752510773828312470171623377
860810019900345510604926256419508212273
46305000937645584915612753750296584353
129664427595137539749217596789474027402
60479132071155380776338505949476604367
8620248825205027592026528304589876201788
325045384058979899512019412637009242922
179486208547941114507573990038276042099
71613722153968857532429954380981448622
717464710975222908669608113202359893316
418482316283983510386137931115612664238
634457983437011943837184678489050398000
2135165236203822743787171840581196055250
146989156245826592377453244359220
6651452384182361667420806399928186381027
78031277933257830988512493193567240785
357069067748624125136555198789649691104
37187677937554731923871473653566261373
855157127965321812373156472425910682406
128372542857085443497854035307434388743
2864275396789776778956085123699191469516
370856914423863272502448823160815555496
18793306821391322251980862857244203763
1748564237156723324655458614548810604640
7117374486308785723286917032960567152871
6136135964966572456482023187595648648989
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]