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
427759591773681173291824120651486553
739949479169719426491894694199293377343
683026363547976077407324987969637182392
413267201513760753655445339948649277179
3564210111352454592412526588025674559
595371901258755964307601381215780277549
357785114457995558573373700418629939322
1298309230002004164730812705166876077731
66124466767962098295905241429890569756
7623274569233938354776863709589235815928
459262691203789138299019599776353096641
452143627213508843041564763752512814573
8538247028102859695652364234213281962210
924134793679417569940864817507555296472
4411108168659071815591178294176758566
487678687885999264930492253638966184308
2429829190191213696795878423660338975663
968836572979991962011084180881036193688
18035654744855283514109696930377567774
847583413421439549224493021454116933939
864851061892652525447359605767174353230
4799851286291844976280693975496768495169
3175671897099934454647674640558512248347
1476213967377523595578053250572272531135
70405660372868161628536050786274053917
220450863667761195964014628393543926823
379282472089805288680807057841138084138
78927126868169521744231993381034968105
490566985657302297079324959765746233490
337112643306137769768077909098586495761
66093622309959624486680116971201001540
250358291764113339716605869375691969906
881865651168614744167283466936821089021
455453644092963907221022104975148666231
9725722432581215424127158892725073461037
8081951145521788343327153563986146062
6547200369784706273189393403785075975011
976597145218542485941264640984996869444
756519522652544726292177185703970101501
50035807699177548882634334818051372178
6695502654474118892048931036599097821659
170261695621670511781457283787056593185
5037851534164889754239023041617310532644
4611195736186096993618158366968417473
51489550984399622177118345396550725
683394701491825562677130995971794287592
213389478651307889153851197790937843922
8282899766341018428164431311857236545126
299987616038536325285253573662383151015
97477776502787941197750451112137247658
9623739881823341643730973768895354588913
101942208356250741187389984199784201867
259722016633141615449398303268171342262
4016339821616073974581590314766453570
148505256539893263494727625615029914314
938530466491713796227099303199286853825
395483932617377199158992396429055388416
6426117067027173301292878182666429903180
5875320327669084404054514700958814171384
8620408418327829683891034143806637058672
2233143762678265500539416747264287345333
14309158977144873167276411907162296293
1707937128657731212430892045359378324155
2451228817916656913146645561629160571140
3626350488952851457414812922549888949615
447297113922575396064718452728873012905
42376976709667381139492591719668639987
1295399589772453848196291594367263278761
544334375341870060603497884276594091449
851658694448464773014052705242995103441
162976005730509183801231886742877464073
4947569263019154614330607374523356364
6070281756657001370635196394389030583688
530758799328846239058320832328936385038
75363715147373259759601866865391657452
866744093757311180964599621691376169171
77313684202690958647842774528291928181
41344337887953592746978736371299066085
570882669631585795369326343832291441978
141448351915536971028353234957551626095
9174696943108631801914715866245624821927
198420278363274193762775850289366746734
68951289839965535164861802651023032136
790975916208309210631570852393842301158
9560723814463549588718462105249070638927
900195418892844071877985256872287234172
8204470082252824946393040567858263407
777764598973113823566357332976125424737
96845814965236310454870569406328263
6393434362427046997510543108995928939315
9901761550412686777773457239527390918870
13315890924116119228321130628554899932
598615217654146980110823793968659561612
93797865254020449674333124601611004804
996691767302635073328628365880271113362
4973529366595871639823363660984482112880
4728764832844863393515526852404821644856
44314765526644918547988629363805204277
99152348896111889108305721006599951512
439396891829862559318491480349068237350
8072824290178739844249921754517965218529
180446168717182032561109403317399108580
6738302616617853566021732028244647647512
20335472975892633186081265700117146764
8298817063237215666161043676304343916830
6190551829712824379963697023993159746468
8869453945329102426537314172846398023051
3087303981604244733776803196912584132382
46078253803494601114788755724379537213
1377747691249881656773427651896174626433
8896125460155583365352203372334736158154
74598139587622734953894990576479972335
575018213777776981442273307964912913169
189375246706814446948508607953828095242
66185208032124834232062019448916075498
3535730667629210174415707117358717037439
431561784653692044228166841967890639231
220156946852314506792409120597599137645
36992994665447826726021781423487862363
62322574747289988547173000518992574285
9400970067475721879149473622597479481771
8999575932568151875818929800985693664223
19828595701976173881945738152051421296
4610927269718978904143507714668521102458
150725745046225184372750868372097658
459634371066400962220258309534724965917
129367093706713583023495203343797384627
9006716686901979862302404930992555451
7461960732269600575365113780995088975841
6011540424036943592194961218821393764
177313712866319191629825328628783226339
404918837404586528433966555708591043037
8613299246142553773575155455841076415
6553893622559577678216595252702553973135
6607816563739909970481865797833155716822
450833954413107210787635877744566406
81944863411466240765487642556927642035
2037975619209893719321894591454565714492
19999365203639948111423889567679095808
3567241927804541237895337674500334862786
3884372541933813964538984951128778709007
809745171984655131071224792927648822922
868436515484102062947663629822614309
295479424622694820169391206832559704
241293688586959690172114679832596982732
8903107489269707528548502307915157746
94019053431926797776353506278558437438
697785999896416896558308200683964644187
77028414049122647152822105880951319277
5189893535978076271131066746765777419487
4665574954936662827543947966947499257
8580641212965624273171245654372271963
7956587364704397718078401038164012374
640852558645535924862438841873692475003
1157605355103257787616296311143633933553
395439014991154415139534434773862965412
30382981870364445152247158447991325043
171024936939246964965448468911811628551
9418161376965646949818507934530829807036
172650431878147812756586053992026395645
3866512923601913201262994104839994029646
4071229274384571858010131841519159843580
6926349279007284605198517066342378322117
2700702931052787970958648731362924364721
3059397313414030346218736292544854724626
1465124049744144154599228789101542273536
43494148562041835340129218977089608876
903676069735976443879228581478053225205
2262707011114950191935805053789215816928
559715102069505883974113267839349915392
6325302029363230645833822252154684237388
863945857554707311259214394508216921482
31844380583159118516711892415557731314
9448574189436105651937086316698297284545
6927737262757397198159584243602230919043
506499021811882654964049527695117402573
263342896146406746659207239448276552850
9149367087641265367476284287807779487182
675720733479264296288759802126377262586
5070930935715651157699381694129762738
107922289880176296018171147740604445363
8669156793931844150615902262238462011574
961726184002338529977442836851593487064
8725565282239644480026984852350146107028
765586223189903613965041977389895984370
721054239411616710446489324861314745615
99047556274573176572266758305318354076
238335824535720313774832672627037354586
8068751015524042837243607619167743807569
96292982763403898543927847302256356514
9278476414603878542488539263335389357213
5633929389386880329759456911217870822392
625822565071526387557215548462813886163
2154526685299562375660797472644045654212
1010230054817292362790769860610394033516
14556068640422344553243725244220607380
1775285688183291282092403905579054665008
650551288455889799034593070497082529288
1116332150521314840424525854326896645517
991060541318066608630713284402167555888
95091335622446998125453162701677726789
2764704832532518159133253299549242453872
1614368672921130316341416901218520857586
877906956368699560395501897215694354305
5975745626038789161230589185934167154
68992293318687431996444101539310805509
7293142656802824754397608369510939236385
385061045202611775983271072648031874250
985148399184434734017574062766647014182
3665546865273573726460193862756866414450
6799752883397317712265255635259050658620
520502019734911627661336481961153667
932065831124637529394044411313253129955
1590345599707658535386003835422123701027
1221374337142274501081694937243684267903
7181744449607756945644788471393996201
20765638663471132877181833157170965717
339057405449381426322147170967572207646
39501876491411122287298262329701711716
18703391609188251907561368583706442566
269550933097518586443766732526273414202
9822811294096930376676551502298118937765
8127457223204881692434063158818091969881
249442987171740816785826079595686543798
828577815867565358510615411463638318299
9920241707856520264836466601226494636
202448082089255283677194558875872818754
107434626839623721906379296430036439755
17223812488760311049847211975437204106
185520465801277093264854669716056219520
1868444458268485685611121790947560304141
8043767673118103745472515288308875563406
6417373183654122560333576340339439097371
7616242857367884502911282419712437387
4661500865262998442381264728991619086990
2694175573366940626583665193303485331899
80771403691838291942943104956361669994
385570399776935379594849575339421952760
8192852742138277911459509939652386771
7399209275456968415694233640170694328849
83978751623538278913010198912051135778
92385793733900216715685758923826629893
87585859987153218196998170132630542130
9315815265582916836286011304551337245
4222490215168962263517065799332820955610
9824761669564822106484337362693463224570
489364494419754558285331945940060981555
4311569395011848674528458074346282417698
857695623431365395751156552015577823
62896295536850584324512472205156874685
26791845322789683355515619484186955620
8976787339065182510190867121843747336059
2236701251142614723137525404785555383159
87135881310076601044759219835322347319
596948216925756210282218237526876031959
4965294088288711981117632232793060206344
991705439922568632999312021094953275
92209900741994946514515309737309750809
8236912592867990325316218767631685132243
682329285551503679563449751501528622921
11510893477706571636482971694411661491
5171274333507448154436863372921251411987
8178867699974218541038545898350631784788
6249617378897055825338341037665158006784
87722564635662411910709046843415265707
4976640953411611480537928088741729733148
66919991900215719557096599715048453732
11699023111140288185382352320715377295
6557341211307002422731881673510257836447
41704439544533972845427976491343974783
81043736334821403471938995962375267500
17601636840185272983580543384264438529
23871519924976945735602308368652515416
883338817057824941510909195883471352255
33575683313765579350582550035731412935
897578748911710184874415663181432467581
46354779203916609031953211639463347175
44702080401455215498895060538913229367
9339337746641749934757329758926529184779
722648333678816499538654358398894410
6884576734209794592505115931687087636
4248136080374009367245834831761087464607
210255335782878412601005731039738723359
8619594241603279160160661524307537235360
845023277590800086738059515025313094775
6756987724545864794634646791963873862
6635844332623104886254367959417174122765
7928969149509511944065711039943064505803
448878021641405143828505234264871166959
9555221540897629409455133923857554293665
9190695137524005863083514095359762213300
4445855674087828560227159345228248234269
1851575487069036409896487060547717003678
4218978044568988376678389080732111127404
842165482116849862623713035432443181551
4390682360444914845168947745932256082409
7175213530819944958728892308478794634
2487851511948149726876312748719269328439
47288462393999527943077656189002209139
811481716602753878350695077560532531302
834847359915195377997398729911595563632
307058586986534690448760453995793691058
571048718401994553826791619335663862922
572835908654230340960095271430229214262
430187869003895575019782647618925961757
3580952640002723418644126783706583343364
220356924587319023043009323683232970641
482991275497311880135049919275031952216
733555644904166142284555540489458148167
403791172453592189988201109135645282818
5701982458667775185181178258680586568775
8876634251427202813337583446416363191222
9729782695221582796156549464512646743267
3790772901066895815636945771022829311
7053590621948172148959253772235646668
40532122630831705485107307842486709780
965074529464178025656573736325739301735
55778316488523646922645039460951126071
75438695462278689008487455354626957298
53133368304845791614283475186595714403
911069621123708132725023183357732536838
607944945209542849082346692628324441366
9438879652357849561434861155611896431164
2743509414127902712039792828760383363284
8687744872980521552762981153594135
798815723056335735152472168964383616633
87409853234229291812987830496789472131
1018390571849959855654216375816238511
9580260443458732298083695410739597858769
94604163708724311811924487311199879987
3832974383972040707986491774900738534360
1684686867984412202326952242103180704099
3476953751643586802919839956407594174703
690019614920368692265724313708977158354
8948492176376505898746688193214036426922
7597815253837251132741153061714589402555
7137205970281041277465460815207285878
3437347285387523880766616068233058608979
57222517263732703486174574466291833501
487563458100149929583833845625496684476
6094521118328351706682477898949384241776
550025294136595570166047936842561669
6579594032046521409739231656233590386
228288741234671297335004061545524897790
543233163596251700258737129585454726
995838584419268347541345513685442866240
9303275193209516712216663975555130468937
424481301208147162379459297461842487954
776138151939858905037767837455242041003
120637994805914516410677648372727336861
6630202931594113708735216966961493575199
793431353697815334908784518903115664009
671865189243518345129438233498588688940
654087994691411928970646138422161776020
51672501588816447557061387572048752472
977426717998480863132444227084447767670
878667614531059356015384407866179417966
9817569111642077953687506300784952235958
1628382047686661723252153877894348309353
39846077749661689126968727357402631250
8063208728865414339464571635774814748623
849834986653271019479810580145032852832
8712165596668081586966972240136444463
3766548069011816900085009478831021477374
9424291982607866234732162170518963559722
942222866889233395710142680584272772355
5339460770351668676772965320195993526
5379128511655537953964495768802821391942
686428404974584912037816620155365358313
27731053757261673774374505545971034667
49702660228275765998328719227772417996
469461106046648197263819086599842696706
54339768192038639779004971536785335443
8612633610394572900627302388105895492715
8108506981626711876690465889168131593922
8758739974309054406455551487352696419785
725680266074447695321795980388387470
5612464055485349489848068509551487257592
6773867557369857797751275745888638412678
271345973273482566515177442274625955090
8443783311414789659432509426570466423426
8832539696917964479884034827104841387
805522047787737745914302321519943064605
857012828840987230895011127972997914525
9657440141628142463938967806525536317068
81925869349458169074026273821155204513
895529679042770689167438297279622851883
7270632112882958238470996954515950418146
4607317113724025488471628585301382768907
5422930541058446859833534750465149615045
76618515526744834421293886282169410987
14937756903310571322836414173725978062
20025454824130803901269015389783196733
98075669326747244704872165327903835567
579946036909311951964231536543427027794
510632995652395911185892112563799209489
525959696859456586746442314278167212724
299974261770669746338778273414921187986
7285373366085656219046476560998392873632
2114160884517018594817953263390338859103
807418715820476831703362965755014511234
7621374830303287193172429810139116173385
210963783411545620039858775631138595271
21419809897462494341537350563897111194
70701390271939446505189780243097159812
600039382768160641443502551109690171411
91278592500370572841329977183734334901
3046460895195010688057418416559866007910
750651978148149830790363082575444937101
130365079139724462146028653477962075595
190322311837401951863331375347999073846
74986362347184664285256383113444026008
8431120068947002298479304932852518472970
50363456213429275692087615314671549925
200831627732210352276775914148232107624
798193729361511823087843990127329464009
9906247844177451890530626557531587116242
58887965282482151434437212650004914733
253022916442488452904964703480698218773
88141137765956116634273195766958472385
485988715900714676616954185579944546384
27694460601504694919451628191159611893
7608669974443619853629480309562886853
2942577818714879633554853317865471809078
6338432970533416555010575712852860663390
897521761824702959940904993900750504797
949259451492275464278812186869439643835
2914429649869442748783507898833457399732
9832143832629444161939511385916538206997
7772420016255176966940116319877129582297
582287883105526114832282540402634611897
68547288715394833895401345235625908222
6294282443091604536864606345557487129212
157635737263307410888641052110575106084
3295368233923514488261482914678298948
24153821949128154624953743034192617212
3952618188048081326849772909456912465667
62688638679486486724865116731283772808
6967607061675905950353721051549892503650
7896418682706209278393702712460966343249
7843534213067030633844613457326414968102
50309844953356599668559183530922076820
77676449616494878657158510755344777488
989483770837442669154496847787015872135
5990582011111214765633151238497742155
19826087802158022599337666229189849042
54634255511126523216469339525165065667
9455987219999465888788612219462359882241
277327491393938117728724471977632739467
6895656982883980354959028008728072287778
838743878823945470533491559291964367081
6786946620896622300112897600801191858562
332442681946857318276935166282696430331
492922038924156028067553560941941254643
547732659046253767525008770588714332600
47083002944050916912535811389928108768
7710300457191135257311947752484835118062
4326582320014960248218209183676769747786
552267494517596634487169586980855831067
59523781587885187996339076092463574325
637841046907508956638901271460967437220
287688127176617556199186454645379984181
926379070623586281643657069450153048153
14482539915830714330786420193360876616
2996335815404419897986364437128201777
21359924269297749749212500277688664720
50921825947227419496824137945889043395
1453769569897944363365929543925189774181
11018996018111284179860472882472973267
91745486592644097095900342108034224671
433436729294628198089097433979176641986
3962991068516587401502297759855223110
20099337999066737847005876566753512476
8194656530046489939290436346136482988963
349223449142175197646069855877633712617
6740620981983069528436588944622665247952
2855971283011531301859818319348086965707
871925217199241336370055256102456649870
454883650919777314119400781263814468
6518489399644298296535108559924435552280
2647937248566644772477877251168287319
656898557423742417099215159011142703812
772825409552726947848674574911447137340
820498093361495707542846065963553887364
4619203095784335590028068393213622462304
954385761496360146776681874903520938
9196630251236033666377253468983874449175
309780256511478083562798258274835757441
2687105996468981819721918113108177687400
36462793699431459458818362487982471861
257570542869727099148857063809025218025
958445891698416309643282826595070284101
736236824981178991349494389589594297618
357720086462727355682539621884380262555
1713468271528311127291646676583120093072
54887498840633226968366636733656976366
7029157188404150771864968894671394573394
280142633650344682827969701277551313222
6214342648106259159977912536752560758739
4492500513026238228171123475164099838
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]