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
233021095712355588929323589140053962770
7841109075782050742982476746307859141712
796174618327550807234019400545514042799
4471352683802344191798747210214831745808
579524088071738134753141322641962955793
91188119142025923179297518227345796069
821484883633934031123769120653245996298
1597123848287923417199252775614461769198
1079367711111641892433410676652802006
841730776827901550767461104602460754958
131021346910687152178432204531814662375
822417796324746930417536752475359174919
8591736418881783148510104047575663872232
2571924189117778402556386336898531967559
658945733072437514254707437750672774558
5335847556273023321641385652795498901991
152782924852870343285226973533092647182
20001187708966678678097160434214071163
24539602080767372552460913116467048688
78213212065676791196504835464073612658
270243688471737124047185225122200264
3685104366546124525046898114927345906637
44575919123387948534433977088337144755
9260822991114168458162061902585570284093
170779631856942201549451146955121442015
11121569082261459380007653393627825
2434434299817411330931914455432853389172
79672724515730444444032474800939162200
88325586870184187539086168068497756716
11241952793678027281922614534324541874
755248931799617855986013872370027666835
151081422838441502186021105950525404941
7953356986173076322293838062735454927345
8865162445917901899695631265554410342340
748922465233248013291998476193666123994
934567623999014396848974497287898414587
663240003925248369293981494667824519425
9060350876924394457275094020893028756250
651289937601354019296273384872594591304
3366363092161360597939738450990696712461
3288398963093822987568865668782691424512
5738192375867390992340045315815946768869
7490647497201860380328152173928035718240
8863509999979437247039302934749787665067
674269484553157462667496785916339813135
971280548784093642828516145358794011810
3236996641486431730798264835189676973026
551693399301199616168363204736852378455
73909118262554813979065875122291172297
763919224367806679716272471268768451617
7688371032743411824960699847602161582363
22734697188422630853966690871137662515
1236225129384462703287366923982574831709
910670329683108534954537251062155893971
365727327724862449352374857636773863613
16365914737472194435244399355234591395
5763833910717659923771122780495467401967
860041487847309875682798433887532293879
7619817316092457903344441369769994855759
7305923371269725731769116919266523701
914743915606793310918135668401931234935
484805812439740878890704758497383158929
9156347706381853085220884812583482851
468325161535201873027187489632218377420
7669504608087893785453922938296719712
8447586997406131566471945215924777454
983754608470479262527363725765464193067
308594109631761981408588569951088926573
208863922257700650479947838337841638958
7185403438685218982199442743466120022830
1491497539345619438281229614460230329903
306927393057799772226221093203991287565
4954771074045774528924076118530643584346
9376218492935760703585661448955948058
230166931614875835116364339312663168533
166321056622560412225609730357559582582
8298552052342339451583194264688982662614
533012083816921402929987866975473007491
565571997868471258068586715242564836859
4731862741664221202832343412711243764325
790046624369976312837631038710134434749
4908395363853193122469268046858219446995
5541531632681699477641667937258990539345
6961302363598668610077541758785270755234
92245693122135556264523164481979783351
944769608274570998564535682226673172419
4378458081133324325529301958226355858196
454446922751711990638987124463764964539
5865317290768371631417471686419631409845
69187556221667002245274794757115411219
492851346484257138759078840320322249772
6968471029381873599051215350943436369
34923012786668750268774571613435167703
5649694185708530386142015870314973982172
858578777322398955547858682842670793406
9982182073563327493065591105191174772216
174921219787227767925922823666197977554
880114124671691508662897537296862823329
11485418834527740852549875881589501241
6686770819156618350694235770100449184712
8749510192883401194987288069331668677462
2352660988390420290389646845912659395
7613227412324627511845860415289018433
680275231783388071187908707387320155370
4727732066686447202242981985264120397939
340837817494836579745264874136886943228
7531417262587947793085811206545842959327
1083615297574947738849113174355051676422
269518957402884733437219423312974002383
9511836204179747301332650158813474
988969172806981638778013443248095837658
555249101351857238107909247270303523485
2696453870211233620552161708369312367746
583865118781344222108438148536437957152
949130570225726619176508151507048058759
148076188623839217199353284877489763111
3837212911166779481150868080754599481709
5127823083225706225109939666316433341
658385169702392601561029712636412978138
91393548410712824616271580895413055844
61507159550664698802965839018492912309
2794842572525949749635484703444345849
68030425731779381089522147216512665157
4183739829695851895145942892178159308673
473240356076316250025486542281103210291
241397402864994461525382751569441143542
651983017158973446615849419329410677764
5980990661245005629166079630505310188710
7074371567464895118096008732252969355685
49154921089080421758788743582268015392
726320406477927612181649794638776082905
2077429728602997952490956382803445139218
968821041559964489133681651998012854516
1104876238205456884040855900271274929548
528466406969311343484600155799456495125
60563498555260227505673578674898121410
68502079914041102742140854513741740823
1431155715194946981067530527912838643
298842999828229182585621463456928523381
3019160811896689859239424857717320805
25281753496658352232938934469553158352
362150351654224920395101795134510372368
636589123273609596762776819427635058622
69333745135143343731691539253213013689
244138513811470853866396571669333528799
33632054360151587273396496669035048750
7140310038057067498788208780571927607556
9028425398097017974888952587785671109
606443252962232583653582160625314864064
2538939117033701128178597532839149
36422282944334149524884076854986212605
152964156569817010036866575846934675699
9587818770063886332318184049427930761612
33787132856995014796009121312091555198
657138593127472810230101691246125978770
778392971797859242094526709994452078312
4611918535299543417934013030507022123090
24403120365025156887596957553110593716
107141109450120294944139859625122863839
365939924668593940018593129096755979162
839072656680472691157805081580980913720
225859929285917737580417340703692539024
4870754989053577178188376236822122489540
514333078465251147941042933833251504113
66464079666211622039372575456375492882
32813506425451353622496103543864504619
935031488537516627738137958241899514679
50985521927758631042800840831558801251
299934642226217883594464883887073252776
5930135724049752349890544218270025121408
5110770459594352374467915870154029871438
676803481416491713463366380636218016633
5088208612747515312966273165766136368027
668114762514351903380034678556927712337
303431721217669158923632222547958452615
168780473684580281649842977209414381290
193624744989163251485556289380036724702
3217843734088937741666717783258377246524
2577114957317021543217577287291328912898
2920456483406839346655723185799220772905
216693072448964136076007634566664026186
5298659130125950466258561858444483034394
5461270146133587230296892885956924141272
7555736529036485721929047780660373081979
6982999223204355712259563829960124129809
8466769087596561715182076339381174447853
134463721812936429879445624889684018792
27358127700342949906950879359032852006
957828298119237347699346982601418625215
8429449418743672500449296736922527384158
586766994352685285136258122577366813
1924510783723024915390041494303194168260
541824359808993098653582764010151635612
430981672102566972485412877697958682137
5663865380289130881369549190219082656499
7131524080354983624160517815977887611356
2868834294454968295985373478120752675376
1442309570466172525564402448549884814753
19396186469793394575306629544848252269
816232133233876662324267179161946429073
941536153606212717204983187191733311330
3701113710411580184928053600995793698323
4192340676485755151124993528682745877657
7457867012601645150452289730226592767681
25725462322657926731494613819131255517
131319088211780873416759514702518637806
7926828115776608597183422358469375753365
970021385077883933550935335705719119560
3102279401780694078984288331719827517
6145306770211121793893472593670037003
577160365483102657282101567433211414115
528230561775765162342711102801577604845
2150499422049094344825851134965312839785
1431436151357909189258141713892170495760
536678903507800511965041951951571397388
2731938935429343191768367078986847321174
4148128579315411538179621064347997791200
9534470771349407761884803856579273180
618713535538711334334409012511728266358
9023613627616104554815849663892123223869
5823429258797997724452086819883669104408
289540516506552753193542394470931867519
6536912818497375946125005600367176995043
937698207899464972432327233330424638876
189081731402710416337302835522899707509
9604145639902014890793383501318043963794
34701035430986153278655125931765618914
1611745950449989635290132676179723241221
55465110190721917887941177850480765753
750638162956859762839449284412848761
355366212259811561765836897203627376243
8669237149268762560270441976104560571596
48788875516281761391468457763924373417
6770556257221493124961582138799208691
5971290887286902964217555929768665035517
626289187441950695226840600613095121297
61908211108481376733497592259668887223
39736716954246433305396520108011878194
4336318928797076913426844013398133808840
5448119738348535817999974088956489743973
5645329574723332286826663336174676637944
963952741477371521944218789260537835780
9823694055096646977182309601680884709372
6322230839275291156939997339210440302684
462865511271747125864196345195848848376
2820840891758023631998457854644791639714
323033136798695762187409410172277868850
1187684438876060467615628308309977834740
1065267348458365521353518921640196121164
99162686169114568939709570285929159255
2961139336234789809687108048853168694801
128690998820645963542397510778666774920
4910856376195299661158495380236244162761
8734796384375828266255541189976360751569
5948603147732576901971815946431997181004
9250969328051962632613179499380370197482
7399689034653499579861888131457251649341
4985196761514862216140718247160254899395
8340917553196482237335509520355572096945
348644458998697319356857290853990364209
7992597188762568169569243502391864831328
44089681929081024083974628871041182518
1600991027528790704663631325997562657439
4538167350103676323653552251717454347
3473832585963491910930533234899825904278
1278878783794897505011998644648334116882
5171170284333591998856418070383982789
66022740152341428575112310644286951339
824599491412447136964478730110174044662
510294173804221220769194256128074009974
60134274704563268089862039587039453773
630730521027359910232948414211664313981
3014859083437590547681209097680513548833
1622120248145414611173647285337835793592
8185419091396857603298391347397199861336
3394836927394324380998482490678162685618
92411875031293626373859749115911363259
36684446487381071263777982776444897680
6688433244213020377230958263225717041006
6316966357175083611188142892085275358
540883555931638602457112558684916802417
37673636194696332642641640159537903787
353028434395317193077139628529852292
6474383036922304994089394068492755889373
77007429160470214534987311879545317828
57586626143282415416736355585788237886
8250478169788491637226408237805630645047
87404592409455415514062445641441791990
36997893221843980075627192684441217973
211623574696422347491161246778874753399
662985031490164321745502229681176765285
18861901191018880773695222489971655960
417202029534755984689822314763264259
6712417689582794561632439665926107742
8218516473626760128057523670502414238125
277855213158140541152646237589650502358
7989507497075843801697144754998755064894
9192684939026561916945561623818448767793
75215416171449815995702535490516892581
96816753874712891458933964964736683263
9850986541282064288048736698880163981491
9214672325481217483242152468160657424416
72431961547168298962293087394386839627
8790185293877101913324704121499653177048
741230855963295079408796109836243695366
746751935662484406854342697107385114271
336251811068329552189445756018462901561
6124655240485704584938703048774254255506
58679803447064324256882114244883472237
66341847509778512581400913786227161769
8952494790608902509864074264957963161866
7993173129668174683395268445511770947371
32941052367451957727251769690188853252
78923226963472538571151134553442984946
2915663322982288258064989907816169578300
9540423469362496833025848430620377941017
371393283657172656173495042758243769935
2461592168837042354300562653605571410
27051053577319879027974356338790845628
60866824597688904771134159694335107783
2250202865276234104537116963351191282030
982538167578263968472817520094398562754
5810789239157063712978999882652668963933
812449759959794274185555864947725798750
799861262916378851169008242676696838137
119111245131722573025581286186805348193
83355059162169715641708714164536404650
97379732994328364362133594252113419992
4039254543866639853195011199615368341607
4880450825928132711623115761147682607372
896585659467361090998449129662521198255
4713284174767310421556275105863488371073
23842591865915893840136977759144284207
227969004402563889174437174269283452841
995833524983810751015095993669729072583
173851717143905213132339866587349871602
359997161229104488614215872494564703780
220742849021489531169674284947377086455
49652666841878932337561291981530157813
187062989013043876663951280154688235504
299140973503886249660358949126058363
2723309361471701268731739537691636977239
924315984853570351569487618748576505053
8492531842103346478080368010857684126637
6567145962248702984690753874581526807771
53492161405917727038406043978446071682
4048654479097218018148998157864274774
94463123051665682457713915921575561574
47578159970518411093783144531215572326
966169195438718847759320472081144131284
4900311321746487951569362235853278002049
596260125217009832939122673176188492180
3121209985374332353083614240904794257715
490988808230990343258384637644093312466
497222587417638313955754166182596233877
6021616867034227731765081435632072856956
875353018213472568871177393236684386607
28138494213216968439577717373711897483
1114601011729079364164913279450472822
493415677215237231192186185798491487875
7614721782084622115748587682796895357126
9080713367438144880958742543795275661775
8460837027971885199612649187933296638235
218625094532175923284708671176136655665
863357135294445689510664776235774205297
97915365892255719278245079160884399392
1107942090628860693511741054465238872134
136616019552903246871222342542898643
881255367169139269292445551260709269485
322593875343074853562588660164810763082
9785731754984198256522529605311317664311
5180724134113620221247113560278138482299
6511788038030532980294741125628653644
8791875240957413417994649582825458325828
673380019602632297943668616790743409066
761475002548266413324922860633831705924
87598818271995145622964805856982972518
288684091729738577235256090439612274971
339017874304940132387097861865053544417
4856322990535380847090152929707831939305
2274240299557160245136419775890465903839
990285197467240434418335126262027211045
9423196677184459349370059458392867062964
9810268245811677224510118287946585579852
984152271157653669336154063439325762065
43393033557265321545988199344079709830
51891528492115584911499963854661626871
1324184475253055625760809353189538467170
334074257785927680434226097138376451063
257844983533169925020122291362434238881
20287854079792134813342199774885673592
595844368046990753629381234870513168
158023546154611775010076050407158674945
2777607597667107149241807714458566002591
195492679841184115747770197150849959912
277658343937811246881358272862696918574
3794466831545109253624243758454773215
943894028443464951594657567827763143806
560972352932924634056119407835179745919
12470463154978298842081378917775689902
630676028421964580836323810904272479288
262415649866155624965275533260659543341
8716157835041177978408553255167891244
683520617854802069074215948831519211766
76226703163987595739029763756557088323
6993341567896933221359062049339264146
6229951525974386758221216480463675
3650268517703486417423876830343818388665
4998556370286305447070588272438870564280
43749095366387929047666367467588637161
823192374165941671384289471893254259480
9739931237737562729849301053135619523909
1131332395485340488838246885238839692366
88807997329111444666413295226564416793
498534174651392478570795689370951056370
3316364487347587166494752267125966326322
505323161384848768802510827084512944767
6463842450005474956023515515705181711733
3121292853415847583854795096830694116671
6412894792663978975683368657125683637101
6596235266154766649673357584357954423641
899557612464861679951388818245714568733
766595413530544277671278050374121284856
4471949568632905699537925891105555781209
2761824243023292158934084096346282918978
6417686290620769405617638927432411608
43982889527281480662719216551296873386
164139703501409659857198810455744095099
953275212904197357326783624177012822072
304581665778995447862652268183585217515
5999231757938700207720953265049691504
661230749089904411616600214050841984653
697389471148764733382465953763469622893
915635026892491348529543703581025714423
661629586005865369459167926344670116859
103297408553922163864579109663664705
511918229291431798621672546691711115
6387332037225964238363304764802088363576
652797496270687379962496595273442467429
953420308181353711934240829561733978419
254554064781663342619206429832158726841
103815848126150457264884768473645026168
32613831836854699132042843389431791329
77410043626774139557338266415913297276
240948059065996766279681184374157732999
203239113239647219299964701423274112420
43907156609043975586251425246539598301
564693711571441344353081302641898283097
54189122144464840283016755198813949033
6527607555208512183044095224513197448338
4339339671452668416059929449111659929334
855796192625179707693539752268420203709
144489476481965730810798309551785085198
4610608356697709257966793970854956773793
1113191245181744328859481966833171321007
1437906300334091269239115591642778861
261992948886439022014803879324266132575
85454742626778741794742990736928772529
9382201338459952538234969660399092917
602290154765307314577232418313893951694
58615994105539867829598407465297155317
21866804575136974615689411595716916463
862213121992972855959403191167735171481
948367499011336571145547993062105822418
2668885968552626212611455963781777928
783817812300967128686860481110242024
3624896875682818292610947183952830766253
1661500519368378219522729538443692956852
622977974446702072906241289666876934183
3849497727723625209438129162642056061
7763365570265558248490169044898018787914
8814946664211452723913419356763874643371
386734932343118489160057683756736233971
2865442283101661210672549004501871309249
50734261321748038698071323415343496453
3381766950782538309846395696120536842625
730550971394356385097058502248599042221
31861413902326392962764186318660544634
234132903650113026705174639649582307694
4590486584411084451577689354593628024241
7687436726295774923557185071343705381
8138152428745293894215388040760275685591
9502603755598965619724195721311240689033
927311892474682791303142258096037292415
1964214994946987129651206333164554455751
1988658836426368557826614893621772752552
70146422734237049081875466872211931633
887989858112117173492696471220279366431
12102514869449592485713836551015905237
5766304592434073758118376458838129136276
5006776663103297128135002273925617371976
23944006132227756920986952628647338052
6563377987946409526765092291873952062798
9848991146934669974412074543661381131965
9186494352746974797272905634466949071966
1399656270241143885838605434878593386175
1997799876135296592547435152133970261618
7084592274033886368752614899944944816623
4393777648515679822591667859381797216169
9289592139474222820159908682773153182565
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]