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
7003346515684819712617865307891735979669
173789001331798999478964697161768021620
6687655620031403714888458527785896009291
23472506627369415018551355148051986510
9249572863178414417272595639766583243694
842747611417231025033653415983598242458
2997497213317959405253419031236370561101
1117539557075616877721026324227521596190
83251027466670965397121490925881072527
42023387856973904123136145734483469394
42893921759464627805702996850788439245
1415227022386630665184971033306664068725
9643790929514073175368167844587134217746
6829817936167770718391136211197134582377
516620695093977166853455407733484089301
4259961978917496667938745761916966052119
92922908554298674029762333359662253549
72459183170241234941176263176694434264
6584811570686127122301210446944692479
769515758662843568199898627439162641527
526074996996102986783407781931878354643
49053468459234852614711044393836115632
720323941525630534766985212834361041458
855130414833343507240551735657440657887
205838567753763316232933414621124721679
425958921648533900224635480918382466096
293266508195348714335687261298346445896
8066521410991057128672147748125506246
7743233181516777361441109285986825183595
1559587690273473267781982054626227529350
1645880439724374281938515332916354644249
7750765956865349391330013795967119829
324912348646373911483629091976616093199
3351505561955648709849533925943254024830
801758867594818711898892821182716673369
6064420181707329948533185095795382293439
36441079801614524239434632788215306334
9536145150005628851054387186478818410000
559973092879399083926186985993341285065
4683690550345465652764592823458029132237
149085145751128575055971610650988314444
3428151734222733487625251698990838104
4274390139227798001323546968642276357
70742658320136546282336684263684823372
960872697184499741384463693619875434460
2849608087545628993535636338773899915758
11297857881171541853429773191281159859
2776211279208340294616017377997058872504
646942063432823309016686672993553906220
940813211034530148642039903467630813991
27334150664808059494089608166382050424
6955134723544044659783295821237152022423
870099362673930268556747033971755325039
9454722855326845907956967619228887733017
7668437938617734211815624126858876595948
5870156584014287384427887539735711426174
553015193957960954911974735924436205711
4390518324993480205138610753861458191
94401787434621442547005121869569104606
750210621560806255889804192456189232
9041336128112495292950341484239755657630
45563273549840621603952183686997274159
5056799144263293809999881525533118717
8273857643061313622950068795757800432
5596100008326865161650401791368184541166
5869747922491315828974961413049130949
408795982386926512778187390093935931888
7661986856515644480969201032330816829627
5925915570947910555566576814810573333305
489027695671232856653733177943044803255
8425194843517950973053635944104454265834
5509709553448861194642678789376643305851
7907692632954202479324598318375786915946
6986665161487215333770265030127216438428
76219176996149735986953794542563464980
6962507997959410553380311836394041677211
13519288820942331175383226325245368420
4527336788844041780643131610354843384842
680697505011485564127062888510113384563
1473777018516406711750865221034812549
473774646849117675001939866245733831270
403914093229255229398014325365785030683
749966004162842259814761184715104524695
502218278170514887993504172631749013787
90875210641395895628105492863538884925
3937201480499468306876282904160072396204
867251504727232638687896484813215069106
4159350859585177899039086420689383519072
87372258550888257041263942132511113079
9730221861918669161169874528150784571332
72377334647085235638706928195606137280
187359748564280671739722309605188475943
2705613170964911347879441944946039287705
23445837975121866121043644723029709187
6705954745984545529383704294548574906072
9887984851906877745831945025260667754125
860889171388248563527975453685589271115
896567263331530811021528675341111669285
975267859129752649091436804264013331667
2514322603091662426748382365179144
27711711514110447734317731629812021914
894394748070733649908346614858192777650
867588707036610578636002503531084906655
74654019951327164916300648705421916778
1737752763494578581556517034881290195088
94685540480820036139048510308543799926
92563162685717775549176365645331991153
57645660325996867569536957273266803595
975820543346345857398397052904665271722
93961031887588867394176890576465562886
92361032785863499783199683651638953405
463088641084191690398316827373234228999
638841731041398867101362746568689621984
8414998091957974975686332428462556496042
767840762214937527274046738824093768984
74786031340926327177313477974437284991
54564646678588732645198838281727019628
59774500290023891012642221013494824344
466630655427387726201726271591339141358
554384105035242410347206588161179252130
9876694681763688504616896375605366981798
910950957461983185804080893906330701147
333431664795093843594948697784091558545
904235805542128077034387683913328923853
484235251966536797792214582154888801089
99145367444739599097597769741584959264
792585862619707554477897248609252666514
116280321195610456914776590385195984169
508551025105646332809877275362726425
1589881254478034905911801983941735839060
4220181841621192256116586122928292572726
88945467976939480213095574690550746451
524552172898586152908851188554794018468
7480113839487399575056501318965465125498
28188961296181112730869110288329059440
18517127826522141281682896280889456329
9297537346725623193031281162516762429967
578781982899166849032878344283923321408
146165431834425586059576384658547319443
7158416037068336917730757812850567408478
11885475652895360624291708681638087308
14651719442992541741821405549764169355
7388351252655910191939785707735782869139
18191595370864872495695117719776631862
462784793936639725192524130854060481836
16451646791529550284061272423366419643
44346224193647663305576859580869751331
65765946807161610312785414510979234081
78132466231819216543951706489919016420
264538905572619086897155627943689595800
1702280323667730386294414280823283993421
6794168327525876317745084019303750886707
8558749919082606529279312042336688656
866826263661348718959175158067814616659
19335261116613808177579912429071068631
315063677832369737624982809297101731603
9583905274487012248286104428867525614910
38839121302075323076286922696022014473
728917655486130821121294636665647253550
76204481468710606383666634858205583353
936939206596291264086184213601997079931
682968476388315022552580721890497608643
1052260496095840151443098425498820781835
3604403893678514433094027408118436467759
1267155989377249323439669303631587977187
856899228785430439794662262149857885522
4541261045032458415949557653597518746415
51367010672569192656818197561225533128
869147661819188831880018207618568326061
602713116057628457135986940575628701380
974935582773812580924897894481747904538
740771662380868597556500369402117333432
13463118849125417897226150456789033897
2812920845028532591698698936909038751033
95585842232479748317286470405138922015
5126994976231867378938855269385312076711
706460727699001668141868255638588286862
812163777286406784628296283348887661643
255524627930930252850063069592033224
969010431047992634148339673292768532916
6960869653468873238960456333728612706174
9501869666942882509338877471778451323625
8552663931373390356681379028686536729332
498885475276278878985862486499787645708
12291885280056592211325551823250104368
915066309236621597379973926891747805192
654093412144939093945144161493649035566
3202297590983881944276909482658920602838
4569242039918799231186648422316747752579
999724332561651437073696797146027792299
948246776652639927556496151164438736079
51712790459757585141349601363548912078
4932593358343153754598968167103919767823
6162304241182246182047772568255658406753
8937702988991651797429391338689464987554
9943266379073116740179312900515693512788
9434434693715013882752918661914494161574
43929688700558641449017286615926026440
9321442624607189967736048918282918470
3024842378673530604852062677746727768420
301890386282366255389498205134772689157
4014778015597057853506762999375187871
840332043424982522295476596662753186539
1961530864713199624371446695986958827033
2329111979632923383924443222461341753457
761292499079207344824044569970789207178
9353296015549939220631025413244916646300
20387490163676416787642109764792622255
60439186809046466413039436849794016552
6558586587304101240596499550396018151347
307728958904504997366400856820839026719
42496592625632362093296794848872881782
342268565771765814631312481593594322275
69286053449168722058110285921417444415
8086685479829971363962159859259568912070
423398149876943937183319150340878874397
5315572386677032497213099521569653856
85063142507713895795211696336677826240
9454389469895222168593112798306850438978
7616858892735104807582628962441719545245
4733357465342975962894124313036646823
1096162774368662967849176421964762057429
763153461989262969980497338739197658470
8122426355125350156031547653602323491253
9374530912207066362831177075407686086058
3029317789184492291728546215443190617051
528780588595811793992381952948430773513
815364958894808687634663408318182405941
6260807374372365193918280647643098910
3940633880345348280027219708843232693546
91396454850167422141786425648382292778
5842453188341444816430919965940331895647
484414688705954374234764384351761552563
26293646793131176394627311286076694877
7496670641011991387116972233533775967492
5637496646982010627127978702184974504157
259750758867303710075623732191472305610
540329924197946111114009362848475446807
6277752960576651031866949962152657506
77442523693017321432658118080121097972
92199532794915461643914761720698547114
40839898852361460611006058149857997593
96171084569470098603814194097946532656
588282778347432711532512860913242767928
844090816409966558649972238331630912
976081354159465969016408369355040655991
6415903449795891477643461856516034598285
742652063942202480387243146989773993911
462864860957789815333912736987294755
1608456359594832472684426874719461673906
39861785821656503524394060789840457172
99717345778279807307023530339710297523
197119969085301141603684521349781175173
872072392386181895635731192864625352722
258426696982258601133511966134529011746
7345720723456886754096639529839444866250
2711724971041031571354929124290792732405
5584577948147619725144949537635576595
17142742835657148820136236163254893426
264029956963456819849852391112835411861
9688716187165344937827964124785469559944
6756454668158211304760419658700969396207
444170179955297620284476653632871363826
206406030291597142133799907196232415535
9663770766734176809061686573479837295877
4649748067916951173376431813441434208598
664448614864766234689574302281673378141
1412623350887576828096146266482936361363
8428347363434507174720386809965520196182
46787228216798696895036207940073775503
8552340012622866593443616432517695898478
8361542366248638581216355418487948778626
8886102685731505481125489832152152481227
123859728391505176695158846084237976
953916223626818149121661152636528981850
47538473612225541796530335914419804009
951174283732411330636119698642354656958
51556193282852568053200180616441273667
955357287026204180025641161694463276292
9861687149057060667585367814836953278613
2815347632923377380524571752094584249
713213788065899680394435561433019374914
107148519171288286123782919337475597333
9950734088087305396796058001169734136662
382325266028906634026119872449833908424
1133960994580778685864781742614310
257035996018945875246932712933138706764
23844058386030859561705653017989179399
7818286283491246955674069585298087823854
10514227969445082213293403417798634502
242172916005102862074597617779952644298
429917079732696614925572901361982065545
87938060617346865858595634390320825071
9315986310292987286499452111671984852663
7518618640102862255950303899797438355535
102352593719889699772701457388455472192
65995064033578985373694539619842341040
9268173392718280107830559554446758593527
794215322457250319687052511269249697172
865631633942114403663888454928789216725
85591750138747624925349050178972811064
5793470999656142200439954914645656579485
56165317160067155420836326608217429480
401550828891850644693093439772873922702
4868356719605569612029955314272249329
884664727986729143172262433163129786163
2689506961944683917449322913766651331635
9739789216252126477332194279351114212214
930039516687269123388441896327225717448
6975405968557880112196611318385346381432
7212140594597290891944703379715750494542
54269341353075331405509712046397399302
4421416560894159789789145661705948297284
499568010834477472342018632331550184909
9269454317585061925210314826293344182610
469361841449159467977068914511691829356
1309301060275495319083607959485611759560
343470796485904964669322709102872243886
5170250859076316631378129331661926877
111990256634717920116157782365015341378
3817629652581736923496108642750814217814
462571036202726923883167791408223996389
8862979035099660680548993355245624224145
7345974513942708499543962527653335507783
3014345979227468656582901362827959391887
3279334141187221709410409759099306289
7162577476306533479995357558285332523939
73629234378765391588239969330467111075
4920530170192760403718096840489622787040
8273311620959859976142399583388814185
972658807501149810594134619818763365684
99463226494942787838872838933984189430
5231208258193248210941475992399074568949
1376737848066958098039326407025234103
431116907827518022832082893796932462889
825463737348919288253043501863773969
716965133112058361576651305121468454195
106417835281789627399652546375764273815
3913651844135365659310576889263262313404
183960618034218282376296568557718432013
223351492877558997648558529153326702
3433867280166964185568964663478962813370
1989331347845548344247385788632981454824
1661398537276653934117734128196274619565
91409826794545861524150761473517924810
968298252771709579528492774672145265465
33497030966119123074867095969573204962
899800198962524567642804916939227537911
930112510332924446059726966429915421666
5062913166819550577519774689579153027583
9078102562611925722277084225655068408236
2302409195072288169184632635226634258123
608522836514757019401931764259492971664
8626875162047882614423797733224313808782
16993888708664934279835742130605496393
740847574407340355914619703310988235701
28661038161816299416431241683987306482
280054051351748543114983107720054239412
4756700349072411444875421100640135015548
937685979624812746583843572514247424707
627547007030792998547535081698050122216
7732309142799101347567372264324439635464
99095748605055142904825180193921218440
3145724215562018541862185971036712860
138664441712352957974366630596652048588
940372767857123677185043076020212421
50686092049336437193503437524028523750
649724751349999401547319997567247352437
308156413674937806956638405253776212858
7075836839499673933060537946347196671
452813873854980255045022125570378767632
2064460354281216323874785866545117568
53453132133937613260451145769293431223
1972653359711342755853298699876217313
8383850195422475606770239198549549114476
749070543271468787152312438029155743586
279743227089344486466523177726521279205
8027474563159640843339711927668165876955
186275771919584167519382020551222717
27384621296596555786994002583469567470
1850470589872835801655762686703085131170
2688384826849195607551337970709686583812
508135626679555757209504355339492943663
812124209581727724425812986595725417337
598829633939867873384989688586649072311
835151752861713183413585332256714057824
850050733080999056734550498651407681291
79022760957897425926101332298054014423
5498848210817019920911719041664361466569
29064155162226524916991187471000683854
18377578176645847731002087720973311040
698393531254712795266798928824893738963
386799787391594319118585442091933178184
54607578766132242415783612712373438787
936735044557760884969720465627148623589
535289667593346155423858998208755466015
64678016995995022105410419338238219933
1392996654491759339299455248872592402221
910397359529554163543851485644136305989
50405392280590392047108359233154196212
130144944398917951941436122621944004179
983910852331856586838725107799496631350
1859958015964738154044991620448644085878
5810152073581734307035801846345359755232
64891446199716528371718254722061013768
964323266999151264939858256580135637860
7412120267606618827696145361165379814557
1358945590535491666156911908970745889077
942914801508402347260005256629123956634
319993982921375432138539290393352387779
564875556724970530768433875613523861
92151554453681425901439994099328098
5523564256743962884564258264781454989044
43235553264102841988822618479329332483
7274922419379845282459137462788086631388
191290302813146563723595216519670902703
13426193009215656006682604824268792010
48787302211518542462203712358383816693
962543632803919334481841599198223124
24768894836719194481632742067588128396
6482587697249916138623112798908671024077
67584136616568748886496211998337365782
4464717731947555873688117707597392594436
314518195418959833622216456453925995821
8354138037943993821794208276257699376976
4963852034492168070515000633610044823
83352935160414343864310539318422476635
584018991215181883663096205831683744448
822469194185184940252224217181409430391
908012547733778598613929134842280013401
795197734116456879763626481146041369319
9639173799218331343117580818874929890
7025133334233783278759995873272262661859
368723819405668484541440308254064787344
2091614746384992658941281305797467135609
52764929362159618302517784746480028088
8818905669327198421838473328305469741052
67876606365024482874376352892290027714
680148406700998765535133462580845315350
825438437735114353137706793381400346
138360032601954162125274553043528264545
3190255989779791341263947449573219933605
9826336093791727783091824330778934133646
405165331674259721740829491473771458306
8770857447136024586620858610658379026894
324128855097436871862067392230280605153
4355492834496119513483097529909372856569
4313133728413638890633382788153967066666
192898035783434948259881066451009684
76293171851627875762981184722325959907
9287548735719182886629685393702475014593
36925353640348361552418917545472141221
4167795010714356888528121656336242391771
834754313033574254313011930763026651213
99937531782618111588191835685444082122
55537516128957526493291780268039556123
6915246753043381928531571255333485259346
39541075004850530946945315538818536343
555261057909802238233134826365039913909
562194824417135721356259402699972691620
15437593757361911284772659285183766455
6339570893637533473095601382730315462596
9027574928196902862973053958720322444812
1960865088385938483165496056356374231
628853336425649487828145848637085688168
227642625696218484273269810117431493312
8793762667809950698436712285284152764631
310910642282188252018477473170947795176
5753748945352562498067972556383228768647
26282684673447946019139758669662081249
88607246734155418328073080559848748000
877851835905990077456172865808735137261
50781408340065726744552360627545111972
18795824088117649053061774868087636953
93454529114220934365028402204715329505
76498065472420436944061645449949766948
77435694680586949977155031805038151603
3776440554058895463147724184385938872600
3552156896018178552517039278969324429624
284152792939720126706563449593394834474
5338885336123533841952942200395460246253
335819791734492039048374788365192196389
586767887771451551920827866433168511636
3652991771623127999522184194953691095
39281827611744565214719396559780985486
9584565885583400640921538031434665291
1302907089877750874757287507220571012788
9540672845046985795083576265776912782663
797445744547951990095816961982086566369
461249347553400551841812866024953173
41032687157490645191474170731879476814
39017386686571408464100700578640462804
4439916998026490768767104966262938726451
39344940716193919709725525472745612993
4499313424358185708667326783177847981243
458791965571104655242054543475958161470
6443663215532282189888929629265930243608
469552963701545551596228873831072196444
399169372230192051621375874952835903288
4716530825518480764939444305617972466064
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]