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
26065373470237161063189749952378469158
3605765099307217806734624196943133145752
8160236842464890366020635650589373641305
6002147456698690445976983944759690579979
8842245268843965700147591008224352075679
9368322680604577797675709526489925090
382510801716496545258854716491570286163
796888029661353390563641554363982283091
927963979359452792271142692464878694940
46325714277862362153090471138039370301
718351733154444487005583677819285204220
6754437884008693206042827915550646593880
3676719715661055016355935684619514199
528983777689962260452255831563915785789
9904753628661331419222025882462048096462
429080868971100018268074615242694944122
3564559393936446758094941492966172928172
7293946515083032761761829972645914857902
2732903666379173757853395600660217331
889194022578476956893296109870277968119
219060109804278914212207989554549412035
9333263941489948118095719341532854766638
576447698636005198047994495623231533380
316944608622822881005513734377175745434
97671295858729627041365353365523562708
975428201735315914345382239881294997438
1840886354954618555956778620236682041611
7128452918747596963518715244165275652
56253615641752596254241613033952407366
1541254751301557803785228056993160577252
40489519318475876413732624817576838400
5314857818187350176926759190388215955736
9143115134502228612538255941379651368111
4971472289916178121623239554581065625769
75381160263945543386772633383437976538
5231516352704859239980388900192743324851
655272042778324870796999179177266115121
4242349799996567551513457659817733796155
967876788933352942443341681778284568453
89723559912037646435621655295683636468
1954788632658420267234536489746662058096
6175134889169131487313331990122913246755
124719709314121714448216481120196704677
689490704646573345871997627686165669247
299125697521710485719142746088486516037
2992231649675317835946638765758245334957
215697271828832840687422778186334202831
49321281733897611619205571654261578032
86334103498191441854327699697095673714
8169928832644271468928257849748170464588
4296211521513659349746545817939550003284
559091767747302310672202981754668567891
2803162865549337967338059703862378074
8808977219891279200332873909669917815538
595169396554804717515005892911087117721
8121520064224143604211659219157214169139
931267551684309145231667525719327612525
976270959947561547083792388358452174586
47313219567833419969861137865217077571
608042784302790266533983978443818226700
650069515316896606395549174437155325
335075453608751405472413796306646842508
6840997471105611433998528926215422862372
215735104346266355668052788917158567861
2199835642481144661869927829708617912
4402420532367063301578443432193030102949
49777402970860948122270869546592126476
3132677927588283739715435051507075717970
1503543756428633118566746429815347912384
638766061472470889576055041845246869169
9155921495313131405696326047263955418195
7631310383501616647453581735131456001240
94011612779539766391163262223722403265
949937115405787983396309440779352496675
924961696069814271178208968527723314456
7006811490294233589256644244118206185
53633979310418688475194656301575582099
6077988457549149136650191493556673366314
57254282559223299569231659825986828325
1570550472576589413131383503100797122731
452360763622238348638872850086899979
5653440423237334731382303785052491671
39413957192477672496729413172374591417
55722272420097062522513913926164091223
969994066627215857086017428187557494881
5491570973562649946226432902291882242
43374994914637215171927013246677888205
1604432412421116353221275984616570863442
3200777915435865895067111706121199463642
784955383748223749248047992230753621742
960935315269253992442437735415317695738
27616040987455379227981840398421327414
162477267772261266564187646286099337154
3395482016647328498995616401120970334988
79765287158511812178653931782769325770
4234365625546657520932991111976643755976
962492415615554291214875521385873572868
33183202848568828982546044477288465048
9267732612636727686447601366738567867495
2272821871942301882631323545176713562064
376797418946561543132242176073553327776
904417043137829417528695959524790915277
750388664542651759012259927279666954172
57746748325313335392247649213745203068
35801822193383716310640949849044712932
49069592212086714368169706559635025712
20768251692319897562421654657519406625
685658018134515448157444369886821191983
7297278664996696388061735896164818563873
8734450066119740920361371935263031714902
218176259394449916194767987695066545451
765628791921729533182315505570978511872
945995015031288959505509625487619152822
6424420212026592110726816476983520033515
486895497683563974939955878545020391431
212958044635321973708232288040949586445
74033662797563397243792188422794387675
149469126108899724875403295386165005162
833397948199301507731332098370025127453
773351158573706359577532186895235234476
66292227873239073705164265643447128791
522581128854125099247172589911831477589
937719497445532498044624278563863792735
2775223213222321983919776695552346292280
2738615759293240358589497777328596973261
9199321214948865750972242776100423893498
1348996715489601276628426927521996999633
142140984408927350241144765673469063270
585989822995466172831121475842238275422
28135275670839726299730210951731864071
5883760454933831175434201623216076832770
796589829516656927961995463833515237357
99401951733284327449603791572615145681
99335773393941773313589470211735412014
229287594005441673536369868907158548477
95201080218714413719310792815068646705
630599186072168189299355361152444108578
210493621921337879255921127291796525562
1036345320179891790654108995883036368968
17616601320800187031845549335982989682
1459824083863793215653454904196290453768
229414754233689607969893434707882178779
256643925186134272161268362386072322223
4261741253691362923718985855187469374430
23883125551725622266389020631780996298
8014199538135136969168512520758019027961
213727674527386856674784364314123021178
1647812799379369276334885775824644403
43718725590663354046019937031897308596
9527385240997027125629739780549163797
35824289618222724852446621244313406252
33389942148337533122178069214260913803
6805473050815996913013882077734721167670
913445634417977698013807915980354894699
546543072799414415953935539860846498879
5597843468152773670814593325285924450
7801679048483859592718531793145271202
4237535940915063163825498469476216464153
95566611920853113835310344316524775783
741178932578961544205007768761902380382
473516878172482444776942789578912959773
37034324899245858555881416909953294631
417417944178964211337555982570476838683
4377382865545063859557637532669132614241
904648767921378934316679966279324416010
54819363671384441608322231124825636373
1469506153232328971549769142386129487427
168298116994293334939288153772299114965
1614933164234340691364073115654317559274
477713384596325550166773718247386459170
456314157052856986597239857832399366258
452415388492472292675198571640533161900
521298859660865423366731840462621038439
172459998289318354481511699166634456353
3573366363437628835750078852376642014147
803627471786767721185395370293374234964
7999792466787618599049912173844197302397
79613784398914912313961315515225221238
85642820252985576225017983999530431336
455628985788807235648025213117598621788
3409339126632556303423547513213167147929
5976820137165279585667048415449292910
669222582935536762765031293737427103663
88469518907686398684336391432080306054
367051295463285658514074378270184534461
455296026998727517176382665403140703883
2261411016847565784136779473241544003044
6595808263888121477752652153490111806926
695790273190251739113881669528076088992
389411870568141977026255646592567798509
8254566673629217284586223522275686595227
52326598137132114072379486642019076170
8275843518234415687869767475241449526192
926249593865482384077124791262520813494
817014511153825091113932510848613631967
8778690220413535565626736634288040096636
5695389729406483128031621161798415345667
448317055141462813621046383345048094359
740618332096914486709211603397378555241
79906471335293785543116094191352553119
95425322402682060719588151298327301691
9305273968027511574528673846731644356453
11848395770690762800719610262594591251
5638656133104227131575859761843193209
26114235777866960568952791206628915178
140515305607495249348465323995156867493
1805333411542501412233812854232367308994
700179723386279584326796882072868855
608458679823682230885032607936073674321
447398991170444367418825807234912747543
777957572530806890518463257717339709458
5809245678948429775836324993320396149353
4188876258865075261972664676455975116574
22237992775555843257976047482263223322
456111738795179473235039392106211198610
3322816398232974203425133954391495131775
539162702385140944213223499116974477133
4618144970498932563875434283263291561
7420983795622027710794085129596677923212
675759899255871665656982125838322311
9616371731321274230572986170107051408024
9767854991348981374070893013275513023375
761245346093272376685052044782618201925
17235203234795486590420999512555857483
6484944798474005194320106503273434489737
4205717263319978531198121676784877941
982078574845236046225763675767651203093
58354022930085402679345947946726516268
5237305381908298438795754055452371108348
65839555811775711941773218016217844011
798170039643446860384144229878661526798
57077084819176606953263117828749121051
96077318229237346998467871299593464608
9286225566555219852958528293943091643976
773244187559476342516054694777372866613
3061465968628691612274376382128557244537
158138982585204132344441251780779864224
2106302114125540974818082472869112504985
9685479337893496245663456215850024053430
7368140873832937681074117124502450966679
32547572136830262063209275259712313575
5253359874452075599297124969472685183263
90217049164279983829437973131578661570
841896777006541339118127881941984115367
974672983804772936943206444890077784446
8174561513146811563367607186199013597907
8879386924283824732628972166865987328938
593671866538141626102885686586919229323
5104616743974837727750535808672430288925
679188518359765615453299845680534962
370344003257237611286951734858958895042
7825383610797522869165805878194692933574
1889665735696729981775899784114844467130
2487137343009843720736027201459499252148
916344279059020481044204366727355466417
2993979965746676973681726992875450927463
891562249862466926118302294757727942103
358871024737925577144591985632369248591
99603937477699257311281629786127034488
740878777074517512762656634712455153214
787723397692172914364167292957393091697
768082321325355716131169846066922746
81231988395404285816421433949515728041
434287951512412997737566599394331013888
355991756748267215863233378970428076485
4230761619048198763450695561859562797189
650469607130276983949890939436625548982
495353137120430943869333327689001526673
889134353310101282375357419236293684288
745589659364662659216101299472619632344
3418164919735371279138049913519877504261
75442325752580225021161186873076186604
3981286786941401579189116773181517627583
235387539995275495582135465284777254159
3225797656785493576467429104385049457772
2029641417067489241716602229120251845352
51183269110668765218651095325244609886
554499254104637043297462920638096086848
8835646178367721974192901514951111092928
139662083192511308611808101521583803874
11198744867081673571835456875513365554
691992767820949534357238329842795128114
8024111453861411812869076058193591695099
4546915838317492869994841052249818981212
5520989163474712829980399634685251465661
890954664913605524655509839234674058954
56941436536891672061228435837786802695
71552561152026690899805500276276426423
979706543953853717394015105757776721393
813476856328863838699752322753264999
856712275872506546214169450755281078744
527613764790247538237667293283083288178
772874423786439541371004160825024146058
6567860449801592566480616837958246362194
2439151390293312536336501953445730342730
318721799798325210869749655663315375036
15255508868488203644809875298723036353
74011423452019507885585548124058574621
7567248531914128924955622927116060268821
744776861488777813447097937107534694311
499890388034898709578359372724013509125
302031886962337603127882614136371734948
111968115415954460005354897498491858711
7722395035403936486895328135814387066210
4747627979035856161765383437134732015
6400824184497695534339966686857670805
6879795453462391788574987005117886169559
6346355049555988509672702091501629072674
7951623363985342376557336619785830636051
662411636438163667174337222222736401242
200373989433356751147269866591913138294
762645995887909933376976746319344069477
3873474287678859035901428635262296152
7787251415817456965087193827641499324127
365139854930875952511341963828097924167
3937897943769389627689251949450653489179
8931198053636766362080023466347185267387
9245844627915009561960256373757841683786
155689665447597405393731943243070787300
252923327028443990694574259447191781050
5284607186438340879729752163395233549356
4353529234265136209411945913905865089796
1521597516177189746371392193258221914849
124958233507581252639759071684263376579
70923634703982618049040679735362967378
313949144764899390037136925829188464138
865563385389691749822417537335583869125
124038546811350011732461820815410365191
754370066229197268729952683802576348545
2965381087032949467354426410602895406481
3045482454317014807772118865579667796069
6774552546954446750413663207557338681942
812435961477414291189266296224103714
541640762750701322274118789554390537538
2216872594124356619221648009314686442852
2519780671545943383137255265675290912501
472353011673264848764827767291667922982
919028382199407531168094952313421894078
53553501462434821924485198133279244289
559318232601249636017605149145074546479
601446815511949124857138746323996251962
767780988793025464067567259571796418906
72802928259481593092502212105851804618
230174963007086102995334539436528203582
43936005275826298143718223135670437764
6690517793593305944118481462891987051935
44329204956924156693939413495350332366
9165426638034776556948262553562677554961
696088884626690293223446067489590442978
748044745706098653296348028213737914289
266812002889957461822717976166343314026
994893020765121317505517816162277999
695668795879306653929669368939476997155
1766626380652876157526934408476785186613
518258912613908422031458496905590974486
5406565576031259439099485949526995311897
331117189016851729582937641924987248657
88568327970928307373121339211032214152
62025917499440868970883559478233559324
54512892496570255211301680856725458487
911312857305256047545691625891497215142
27828581998503467482947545091744024586
478796609554825611375215377332133642044
800344272564089257798815317879867663019
309026489701930188362941471522034170219
181272178057931582148072547290354254778
57906086371586546736213055367133642012
24367897801771202460492091541928521243
4661768519176088215843142208744285116399
3253746683096843507245304883971155423287
9779706738328360114370658337311942041050
466100120297363890936875114423024106482
97372729381383131942873229633588787898
349483594695571492613408678950494434231
4301957972219953282145545436597426596986
90726749142043032907891640438646646581
426523736206362133218967160695932907788
612280587785321998884455866479185266363
635912531415021819775719794710040896650
84928345253559531282057891802473118262
68712928249617640495715307112064459477
516095875418456876531513871859181387021
76316815402392181618641247017101985536
4750829017782090921149831495362780563705
12888148706332411885186906419633881326
6324123811848182410014556025476169261018
66215756044452462162012995480865388558
9576590283553595390313477224388295695371
32958392790520392680795338866124568673
5542215224329524593616453081624452599958
7467901016724509300170678911346565167
13336641719346124267086607876025925387
45793710418366839748462691061992985937
25063792368270663468996773797175298806
3031587794906518563315749809422537778816
6393804337333371554416848689337020781540
19469325192605987171518980404446193917
41775374565833777136396754796648242771
62301578245101874721617241751294250530
204172620864574189645281421348702905
8500414134707154406474776374634385753967
90877167560538042331471797664909197799
466412201109823246834084503785077577166
12003098936880493043558746777789984649
3444887686149750466356546437264572582846
15993164981447762705767726713325414439
8036805317067762953452286528447970524836
7028824624965283605798913226402892225783
863735526735531435253406276829524897942
27137149154568372913881025936854403
95597483868740196940617022140281209928
92552527444449431142385561788965996518
5562831161625347522238566170908823938059
23018109792276886143381831351463377656
6464441057111573994472943643683047807961
526683599453154844763322625777094946070
272860478632919948881858275907949116649
908767728803274668862527803791236516449
3556474834364357442057486769696154598841
83135382866872832434237629172293592798
433767603812881132968299505601462893219
28571616840161821249927230890044346778
542246017163268064059219918398915113467
50240181068403488278075258532941863233
3805175699396439493727159175795282544476
5086955416203904326539651333922180888909
5181103287859151409993295194161477797310
1663168595412241528124858106208175347206
2763395792547665380573308853989993738014
621129772677348059273126189302323559681
151282777451631458631338381286071617233
599442216491952982223249065686170941104
1599981459617933503327063917893853815626
874916152941532810970697530719758774774
77351098378793485298884246746213767
39751880048282725546334927511656832527
4216992045498211640637713618403498215260
201417898701640247667114235102247212322
7425779163441025412949991808812379786844
58891229686183626725905740408110749098
40434484841415242140162695688767832257
678355998722316371037823046305541108088
773461231341105449668721410470190183614
3930206625941704127172578730223022139058
139830754332429746889187427216937015081
552167355695433324584903530537277881429
147852314310709263914458186250993783230
4532495750355798334873608082744851501702
41185177458279488971631421353599014309
2015165481216036455762065918474092129918
9522321512828512400430651434996440551294
473954689583939923572365092840162695859
4187213752811606768765836985167738658877
6287499358457653226133609941758875877677
71863554827790569781784596638288223990
1515274973386585815672713909355791109509
81648448642188148742571617814485832809
9225722271441070145134306105981810292326
9951329077729302714045482049882766213774
4506150130356429661070041256683641378204
877777283429448857976000857534214651536
16155632798310335671726665040082549757
960880094335457257636033977693882331300
364554514530448493926358190778043857621
2533897991987876489180052928860546064681
8251699526878512758464978919410024021257
443580669437520458348891708387261211215
473751168432843337704045373108924063718
820560734562324607116691450190032905080
2183506184517231437679032017152336348325
8620600839984021722622612721615755782
84271327267632132612003256359584313296
7574671618195670987588029887897790537444
36143932779052736954350607283186888
65941663143450431459167488561065407578
3231673714852311887928763959370296272031
4886215771151474956385128309294163538163
857030765336808113434198231836475731372
517763452233409662067952062843752697874
152715862799497787827674052592142112808
4548204820269836489924429148463107398
7860302922175042211736221999193378567
3590433268013394482762046888615277522
71907128421338461243692233985292562937
307267563416145594016028949185336835447
936035463685214318773161181785365801204
3234623987836306332940403058491349013595
1817957578603453360930272744394823024909
37668016299019636695651241952699124720
49667688462154819620614318076615177434
8409529796769143160248221095659754363477
5124984232244063583897736099798526322610
6458662412532844985910153562752486005361
441795605269204908685845959760514973340
472622572355929866163707714119655455516
33604251314974425652220777119627886608
9539275489155032694551116238301397711573
45996799419667043117704358597227365576
254738943229203716061454721105786784371
72507233712195014584123927660094397029
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]