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
6893165476615445902853293695219154812
957852023995085459555464336518194437
88136199315747345845306911441384499749
23338047154693179942741584217742785739
719020866480397777182293652963561762681
565341412406032988181201935868439777356
400564404651106178406006794385268312281
50804496545706464795377627919023956947
88112444997652332637902216858127116227
5766345987371872562763781496513749129484
7051731265082827149452019410529551489187
4873520123611008144559927901230716063972
4586296033741638765769054809359714374799
237421228892421558658235324412396309223
8634770727702303391238538138554690104418
783282663971262896626570469994059325333
436054771425719489892308488251440219641
454015496734631652382325799823239351693
92258551719990599163196161454289773898
251053296878679951986714948684220391187
529095564430593285799554620148550527267
667267537909596307752508848561653758279
1449947299347848374097824535840167919065
4482373441365890300799329004523194491067
1748643279482226792225827386542507286
764738065037806321739268542115350401044
4097509093224233594127199196959663453598
16883052177055393022457726428736271086
8030841978589801537762769740333554022247
244617381716886647504834172944897619589
3497413086164683943065961522508132631777
4510345029248561280695159516184576043974
5860544275393133445645496126711446643292
2618758084516999357322236140327371776
877959625548200522890211166804438317418
20160429514167461570092466889648282362
99987546798835634903863984561539643947
469190403807152135127349676619898274
673974357185226617416168850255320678269
7921957628794352933770909727353214353783
6893895534241492370057266122119858886784
2199561678506029438888144355864494616656
160432286652342284059957327697710086425
46691114725626323532501976118888742964
5407611660542602962493455921352721358771
975849221774285765875454423988067999
821720268684488034456713210101312456461
42287783319824895931906052579839752742
428739258511390607531622412387683351378
66134409958620748553178336823961185554
890265847879698133751802359984379619099
5037686318248233972490719664397469394423
256095938610373789115962666926641059877
630423088795379683726371620453923772753
7680951158262724996192818499405992402750
87706553173895970258624600337053848459
24194368465260735680372302803220903976
436131712238978438796167477626517185483
3710689870755130483356791760389280379162
1847529495449336264015164736502663072653
1144731658990745232902966714861061813
282231815954463139534539632611899555964
38948464749935888044593055873193166737
851797298781215036335557424149344774712
6418892218477652733386088729738792640
9579267775181654854227210872288838471
11431163600295774628434779744844929254
955289308066406728237518017994816434211
815686997600443710648260766222241735584
7168037900249947004408668115868439558
936238212414751141385045753551322304374
430062013858463214633857881873890617203
158771466819399085514859218203968535496
3234110553785884626283856271587086187513
2052599137642671386775854129585253484
860849762849414871989171422629582495317
700916536012852558692427022674152799506
112393166181609275772651441736534061116
785364485521226143110775697536750677261
43169658854638044867649571817847546297
8402271912443566225541859732399229646
31113880134942587300360867780512347146
25541200313353992263603855454963972916
3436136250736112077807351280872206495
306252459298927227868718165594461188775
8953509939701669429443963940776332898301
2696309015183594037670167229411476725
4958632757579819769416967580944862599351
9454759365625921567524242096837139912
873755775920846494772468503745059144216
961485764861906423246832350650789665014
33767734736200657251162280411623004701
540633009945720837465983350885098866582
84439304334663365533582189488517631450
2987770036294748330354919976496199932
500795873361843905522222319967853981410
731284441405199275789836209297342905204
12772209556383489261849563291180535848
11071828486930288454765880388505856308
7516152331164257506214133105637465273472
6915624678567355391838442025294537882403
86842063484687136382389895622322069816
8217740451403402152110769831368492732525
844381851818481966177779909252291049454
808059639315364655243673688549513985284
8567201558436719660213556315245632897710
3785243095159035912430965359745919644491
731522909739971724477423133423549284376
359098905281423832605468501912170775732
895366642014974415598963844914733687164
844136098718287822968210122670316602
8694453242122146213768223388785120207
1041235037249329260378844045902023098505
2357243010863456749742255152938667638331
19922918331561564958367863931293821719
935445958197791036503824622354517615897
58231423644474417219243849412613752
88035351882228332721794892184290541091
667441227108389532368068158188212955568
8447387431687016904013032054212836838067
6361684629421589142688389487418593778094
856683134264400704953669065383895189437
317514062297896057283765551442197997655
9042289877241294809958748548139077181747
586054998323523628394523320724836109429
80313433428530339159944254596165923448
159326254464738895110388670870814708743
451743037676788660113219307729124807
3194790347536994163874976821549710604589
70838843451990652237495116542259629451
218566889134287940543144302929986964054
398511662580229446019094837196379186115
885056636785405036844666711492720668330
8105433542844786349285081000780085167070
2227123909861998551836174414866432
633345301378775817481372541374280485468
1189246768665294733425556362329177609
754282861351468299045441632897489294484
52170992262306463654076727654724746577
64565436704447371618336634262781904334
3087439377211807106386457573338408954
517090853111511445148628628464564061773
2369743418712660642934521756067116528
9716388039369717944544496921979588515353
719015087065574519268593131956364874539
4471363355752756664394207032451661351806
8432646347273831768419194340399090987599
392471762989958568888811788156113179183
8171501898016686886713488653461521833358
2900232190622555103490047586164328219358
76089693106260164097389103304091185838
593410568647713353416696965526278816273
9039342733917977656725905387298388393793
1268366622738283707142817346116041664702
795830416762717863225697635909361939828
510382311091737413915297055358933755789
645638221656852068999320587527098267270
1565970820677299578566649914199918015828
650241952464342175485290367963337213
18449571369497362175657161256314839214
771664289434742626936922737674447689688
7145655556597138839792048929205910863848
48818314754273883680960657778238693820
992297001783326810239163960638357224456
1468848679149871600366884910193514067268
3708185447113214680752251714131619786034
3262590244033633629677576050862959184194
947896199687386658969741056973481394
9380738131758530351593787139420291219548
703212395111640135958864872136840257767
21966848266747605540959331989603492843
9855768251983858605145257393475374478360
7831213061076717528445362030432437613039
5316866362954769944874289531764434745333
4546100627621251500492637221345629842
604588118290682378742826754469455207467
17123334305477482274979008317687598959
129856847682889618025649242871526006391
990540032142910121797847272192378987848
434063537507315569716361953278472727413
1923935367453569392719704638247589725798
26519670101329856892705373013096766580
5747607017673893563221705075612407525
950488615242501924067198165741159218
594876647439721995534767280322604269695
8503532195948383766365418904682599898928
8520277733511411392728645801496364247353
8871305819836734919084818439720666579798
15206678690815430009736748052266359034
598530102061242917865109517279319432172
636786757355141324588732822916751288328
713647790229617206390387194572619957687
713677414687916683173121348485745199087
114126019657849216962849481744363568100
6446274522364271489114351345640618765307
9947529223289790100562513002886999981540
54807127950158730095144982690730984604
5913526476641063955765085050844247009013
97794067236532931921851129240052004197
1700350369319269121509198676208199518
2874343819941413271337337169117382587752
2708114242657376885736143845207995918192
1718552442566567597971492850374496141
9675423883708841975752915951575595438792
878125119908565779011616308749637713334
98996664530661771507380822914091221425
60799784120989298282855143067880687382
521292385268378455894136769113142476805
4009356541608911567252048889590659253854
87568383690145737845781549897099065900
859638408564168542121491566695199099506
7318442013385029242427163398865021344784
2090801075852726900487079441964231631241
5177761063534571122583947490262827135622
31629178238871667779879978330686101568
7845991415963669283569046369962394332330
5902729633301215713715378051864616828809
92315020380041812254391363455602593
177350897013500736884906582577253701165
26943834912228963478224361348445074780
4866913357084027731040756633467420887578
735270459839605347416768689376855814563
669361375583716835362156194693360374318
572454844285402420374816622738160667
16409876135349014266787720167940308007
9802117868134109216357286819812332619233
4951838889036510137645633204972892289955
8713360335551851868669991492119767797021
121440529025350737693101174185988903600
8993279382429092518032753173873462318453
99644482455471546620701682269034677680
512984697389831077437393282132151681501
9224831717612722634279537777528614256682
494331787586965879736243862266391371673
789429976762944896042533847701320364408
6337582178903248506830388126102275873540
342459775366657957352012142921279309058
705811937442136861759838910354047889710
6895824693705337996439367631275467989624
2255514590185509249998231553612126762660
5042987927378975926278052344297151477774
9371636672553315578980102836166885385427
19178911819640309446477022996892765734
7688384433985092653142201562276813638790
983220653192200395657091528032556740
63924614995035284147269546718361019173
7052807263246728643459881931226632649278
723652669812134339748462979795444197428
3182236324681256266575231928112923331692
528133467083462360303361968381222955474
1979767562775076173353520398157918380
9755455676326531982486275705415692908352
478265562855547714861414482601644512743
9844432186337576349645183806894719618386
337194029331256878644478596222754205916
762153524468764135419566259283624495476
465247583240478081097942743821310740
58427401584679566498965722398938831019
329499164617530816402195725720368759222
547827033976299219628776163934294132042
1315475297779008467265969881143582126
252349833899411957227516848944389717165
15665487725823150785752187999929796701
4228386769303271795999175289514172171485
878470743374681395198283328478385341874
2063231396836207801119953413521528913546
997312644141953595917468942648216616397
478590613631288366877074297125434725802
8342732815986625963078955305702475296268
114178026648249003963464488846924821
2107142274663604253488867033947125725449
3719513087561045969014317304130226439402
266622087393554589978292724437830726468
782893898729921169127467395983896603417
4333699035144277590596423558566796353053
502550447393580640223838411861341392852
855463534523357825096509521354097217827
2146700920092213304297114160357332549044
8901247691503193127932912292556656686551
297974381492058929056603182382864099975
388884778579639433435624303478653206654
285262779289300079305193236981908317326
95304849310922658414751139310579539408
782566851737599515072323902498595106818
7005399455527585469063033184811467306403
62349517529291148382987984497537929854
393292018032526244854756135875957809403
324193208353537917522272216829022973155
59968270100452117039567286573050511280
973470106527682218514836202547041027442
739889712536925336928541563115713036425
6953562943134152749344742865622182576797
25522279140538929334734610802138978661
490012963977569664795548529745551845746
549584705831302061792219186440828578781
3237451118296260877112002647128263105065
9493194422542124664829427904261877187037
49627036935082741720314616190934092209
7310753762903136439339436775883592691883
892517367977789318239326518849467574571
25871279903046464716164639587546848619
948095149433299192911567487374681522299
7169188666949458465252153523346388810
8282171048194772939668027405792298739657
56047127046483247257598506584575428039
81768697772478187590401024978363208176
4682821053599783336091236493936343447911
7077995177855639389446879289678326873
71088722369246645864673914113399978452
5291813263346360976129727816127574626037
5361885676913156067231411107784366595
758328853295118028506919563660613226
5347671533236321381713888114134922
92521347953986173282558737076541697
785594619511571134359265169422689438980
388132235282757114943322088164740652088
1822752550704707415321544789147541044691
7075683736055246187027992914527342299
100561998953680725774517255251205917370
544163068461349833840992332210349393555
759294099082564329139869141830273968087
596190585060508158167639490792220037295
97839669417928948059904602461095584189
928066677901877369689422499427333772740
7766783985722413334912088945313020179214
9915116839338234147299273872466361215872
22852214326346783826285110413256127170
706699780110351534173752131997556481
6812574826172203670570225668375825764342
4534287129483664359186297640354075281268
2329660860175052849382641186480788426876
7406626210979125109214213618128194065006
11398726923422730117674598552589861377
43637111149978849760478072626534034576
7548410670494835335937624599606813259216
6319265788827090821629078593184351784051
938724021658892540352805732949242026166
5789431170947054860413981070108555554216
1225322657142322441378192988796234545894
77381862100870911899000680380213144447
44106804477527690747227563632689675582
8357465964044577230640797966748394269139
2540558092079775128043818021181373559907
784755244045721471694669698517363541131
9800172686697746720975855834846697563336
393779386911550951935399138388931269640
22083593480108637399074966531101862414
6992898649306216345904160768742892344
93281711777389320844254700518348447
6767535172774157572588903628557151266627
7340651912236918456325167304960492086613
4383453954436919747877421982727352101
230729125279706396077124164544395692380
7771836185852819166891756832746643526
239769377970108694228012667377620393529
40463362754587006636374284109942292361
913839459986192718028987208812409569387
82791682543535522967306630545116173878
70049193174743039388419740312353958337
2133442133928701135238187970757368726609
629354772286632501369689787539018909
2229433516914582251075735980413734736213
31829246722706337788101104770225241927
2840323270181080127047393732918191165312
91507312746313934791133344358007113822
789326773497696687716729065253193516952
1871708877728284738063866141453167414
489639436576813996263594164789870484348
33752214901434035717508484979536514018
316130045403170552915272605857558632949
50363397462369199993519813917479559624
630774581052873860871441079649521325226
34679370719839357667112240185582676949
27476863818893643303504636404066524058
6503702033597348694567669373756384204957
391019696202335545227825158116615766300
290911848392004583449434325719397214681
9817584529218075126275089681479778508960
364817208549603536029613665362991376787
1905738210574284123025993320911482497654
238772438755329641809656565353018599503
5287625817217021174237829413384946026907
1742131789613487776676184313182539548705
6042133562536797281084243958199893746232
448840086460334712777841502771131338282
36698967601196613382854592248433323724
296837832713409062847124858972456136291
123715672167269523133703547171938393167
285583209329573498552006794345671154177
3784277171548424454331164044810881067534
892283209633244997929264335894722921068
64659381249759095748865817232481662
8671920696045147792923656344974660102199
646663026669667292493405360528333965466
397635991092680155564894517487141918333
3006422017733822448827919142276516347993
7721902341548041853835137302309411945
86045714788629749298376608529245189273
561290555583890516076925324027574107840
70276085165370910364100835632124008169
415050388041250434275338236993420144600
985617261099181742778315776544463949505
913148212618723033239309359717989002850
841937798860507290582936223054478278342
8702117787175346567664489248402518096012
3753746542031507791180058411789357655837
1229652349554544941496905002609260707682
65421175249277165038010287833094083766
9593788957174917401799588323929113928029
1916670529569112382959877377492570162798
18954360503591657619983859189777306415
3825997965268109119637826479496912112391
7894150238264337932193416256372218373498
8342878812368164905931975734514721597773
84308543999264851491520992855589894809
62046822745611554093264717544096164496
7049157520638398294271828441181383962908
7814468046932956133363799728726711209551
50149061664198881065086179355536167416
8739446836314913506750074545896946263031
8881918668014603514752145478584232055606
2971213525523412681923235041137593326
9984482237985242523218842456612245553741
402817583477908462051971734886545939
96667854881514066142443983283017313465
733184132528802560044544746895488824718
3757681210094137601964226418237672641718
17494575621632112319662257294043471108
7167264281258504732880057021704062616865
87146069560928431788098788591423308022
684451951732943668390123053181652809105
9103933138184934444231964555752093983727
3140311398333583849212721106671083522545
1451333138737467678046658235146370027327
33485278500762227235616399976213944356
7945317598872662171257871448153941351891
74956079835421559459048226532295249245
440435422034588832991748953935850599123
763427061273293829721555228975612793583
581315565664542855608961884150326800958
2429100643415157337311216103814979703662
9557220292583861787960708584993330618402
9954343289329949145298926728245544358031
884269877631199461227903128692412911129
4397123452191152427363498800828124368773
89895284208774385477771459688674168673
82286380827957234988257555898470465467
2725116537756825982240514931204637954
370491180404558304125197484102832961046
32250655099496153445579709826490881240
8004645225692986612678359628496010923662
55489296294072273731623085478740957988
414635517526848421097866512637039211303
508360736565963553763591189968398521498
7412728266066378326387094299126631669558
2837260624769428568910128974660625448948
782311903666882071578851587844815569837
6049910743659744460992181756949912564822
2875099374047333847625331849083828125
808031046284347841666738107726153008029
705272394610420562082471265223645463199
3044773959787280204983195736787374216957
36014960905371546816325647666568884505
124154675923415853049448864411685325
7326324596063012235411278272602821599399
161422754721894066343786166149297408389
845273163324263197873693154683066564514
590523293888023857123174411761092518292
72678067334444462069903522616327495247
45355754505818725401999389552994944125
4741931269041658156583942814444926161310
6981438150804430751426561378629579285160
3934273430064289556439515484752110634218
91968580519499497337859971114951782271
757062401384579330246868872180085097246
278313428691076193896348206574536609
3712470922931415321266613595187798628494
82286784785109577498951439572450609003
6007900771013029390115714613428851574591
432779875984006870978933408226853348647
1076661975819460408426112269592024353276
695017609864634217168470309856793231001
2193950247428594161419244385306277022718
35928798228685293162941751939899701766
84954237846470951600939820285102101895
314565529983145994998725905599328219634
6789367178191148065493744912061178811
31343134678397965816314187480597502149
9417293860265471621052125763292468476944
1466314376753812305396983941716799204999
827748035452138564980336493676540436123
4932240430119154693529075722399549189113
4305551793259601949634764053436333178531
829840435377124870321014003868275428261
28822454304554561155609931843693182555
82803976786986313111094410813430258663
3021541453305181111643332132735714263421
80872730821116608180283092164315900858
5166235438791590740289426543183664532
2748206291768683517314834889938963407
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]