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
33383288915613635967782996981346811468
3558338669232396572862491472306157887975
8315805816889603705656329049348834389224
4664324198647531371021801439923274973766
869336226916448754478631316138248292491
829310891993306886682828991218157216753
3848459317200336328731579766819073447
8398591325805595720852665993772526211749
8367226620083227804893903989948241908045
2273325887918840532423814038671489926638
3728511767282256426874747932975571811229
39432545841991745737575238222272461
3109909051776573381219024677861431746754
8894224998153958519586197170324070668623
170817721144634850339423841627973987074
2406373274568687625116867432719864801009
505716914339998896462934145342089281752
1725862327596426803252719473979770476927
388516563915997520911038881997779147144
650192768349650567703048580448352372994
36043242271297610463562837285386924165
3878782589934933355151449511172251877159
1405983918805441526618042691849896847419
619593072080845769328518567780488523
801435367472885708311591750211860911451
728196277992561333136113862434591515
282495249362909898721516032241979002142
923371782839224398488518403341465216739
735112069843632536987777731436738289828
80576332104858668674369865792431885559
585715128540454276245752314097256614862
326792316457994413177682130127426947857
9179856895526304941148878451931645986072
40503300875676698435834968864169033215
80027334782024753219406242596072193937
4927936219583336457848729805704393994340
66975523279505925820402795642514657618
422511851883292198084514204229415687553
28988308505796155862729280889039029901
594594951898667587236841492935587177793
8210576884718544682154148669562699927276
33620563456931764628092314651447505701
685899264991110014702056922928462823470
304140084822808235759450203092827032940
546858733459706764333036098102047043922
7011882365733274308369498878824628347370
88345646927721298645741176127905095154
5121851772255838755445510691683925433
17945545726712966062626354016181406337
2677397677867240524219699755409049559738
3874632969575468352548704276266770289086
3123515198525900108369808926764823549428
671935637002336362007601961459874319814
305715694514195459740656002159830228997
624065827596460991839465694750618622605
931730721615144642556462818879225805501
50286855396739056763967653318750542994
6333983975995629748784649727849047547
5517916355072980724292789767954193782799
39544837702232197924452088621354623753
685728852749040479069862036547024165088
652246108444486027035095325222371368373
1006551547067945388159417190479989877840
29995545449597058888137914554332357401
389261454706128396931338270390590659557
84086670161655732099243814128717829085
617637885075009844296223193638843457044
54398255662783839069806597587753689803
2707317216995376364827947112263676523494
250239253813519125544546435113666113522
6154153217327995522249743920788981197592
797582903017477469886659238097006935714
9503573690377223396421868639154365886013
994662455385842635128716943212643742221
253065506696704881982143130186962617918
3608589729417754552092803371643034931
309571212651353164348244599964252001519
9021247538658583399478898938897327354818
8911299932383819442251209347133687814040
2703214745384420873612829233431286065232
1988350830316297915634353462328980586471
5936957442761355124073239990622456682654
5169238626722653464185749196588670058336
722071652648790380039767145256271274793
9781531070285482376095182306352528789799
3962939640669565482475786180279776264788
27832478956867150613865223957148963866
760562963512898663628505174546388915433
1375529792621679353343877868875767812
9133687289936207112851467813512878894071
7608196872275435548647786248746810148
8008384710204361834190161157817373135152
3935104783069243720938104766536343236199
1677720489243341831249444610935974777305
866293996413209428940881277994436626
3260493454279422961671493853681553506
75585305968181276116938271466665044632
804688137932601518422498905417758737418
208711359268376368121177437741044911049
902096222529935124672769389496081874644
822303727575630828040273365446438686
4667183569294131787935052476901570865844
504799851198524958460750923717724
5531737070279126144187157224425714002046
3054165933704159801440028318264391176054
670191645625169688033109750191761123054
57708370719729129921551735993687218668
3455241688866136244453108187492777538
2803650551446837696117872659499155643185
67119703234822195012243106256604617606
631446312229346684266551746718619664998
205751488062634466950653172596019262693
63894376308613855631605138784243042438
57676865120565806664565278058835842983
465552771513911228427205120181713092222
86093820216289541633500917146464615466
6117594734476164317339891138171250271609
678735953097160827594204079145060386585
1655440887902748428760979021360273421124
3587258030347344456192175657588217276582
22167107873677171425496141788057726985
716514341781604866143355904186927236043
30911570533271103675509760851762893130
7795319371222816798592294007211622514815
144829112294982962398121831194247276200
9753606318928597795859363075285028318181
3479811894641968612263389547874042088668
4434787733004663715940059927757864531705
27029551184720213712485492947236637481
1713101472533367954585717913817925259554
272673428876738739321793372810095416397
714938415449096223291081540439355080
251133195217829321179568653820624176194
9675193995073325509089441420301760903278
131822231015396258214753820647885573344
716498531617754761835436352959275388494
4266965114014687337723067675935598168073
9875681458603635877929633500587465994411
55305138815238949810156655686277002799
67143686464741925946196597494931441213
2910330380254734790988089465486885609329
5958276122654191487597421730681975962248
7744131951937644610723046342378399473800
5670487461015084267069015012464716477684
9281173625584855370019283910390030653188
167792934582467792756790796554335323634
340593518282509623482534486472277648600
1101332627308087193221775242669167496081
283866835210219297713555818127964163136
7875781285323676405525716093521826799478
635844765526553469296617695187051085334
94735518312877614414895430431469820960
348018928069779897226751248473844797361
719128167312644717118766753747685892949
9703255791454329976420404810404325776728
2332312662527093568384677798723298822
52965885814002518838795134363857265857
19417923381874686192570910255971396109
8473140277636931177338147193326028297633
243261449969750656090971451802341211555
3578511714983726761254591184155659216983
4356695512548276460623803448484333953397
17947507088561097958166859675037776944
9738321944832448676920909557867715178082
541491042653584141597659270883517795117
134574532400244054011993276803438222920
259866182308343784269267856673965056400
27006778550713682881071827701945977887
982374511734491459614886105112512135004
7890463126983571788432799590427170177564
9127432925034792464875354270372765034090
3178260247985239439876237247947785403947
923292541815285220948725842525910233926
8773672641562236512831293129321866782526
9479743368399355370499025066229917626594
2924649499675190828993731784897237655335
761761412206932108330941718387029378567
7581126540747474304169955266351552861309
724544727827920525967184106655761414190
665057768155657219114947675415598335666
5426328718938375853330537860655496817302
40672974853200895268223108182529088127
3710176874224215507013759752481551742406
590473748746379083947264590444441053562
6466137366639941220697997586474589262635
183391505443732453046621499029563443414
6609142091246229605025271259830123284176
369054208985958326473006539688423669175
81491945163209770014861651383492085508
5787480242667112101077904820317156876643
71453466217833096634495396744558511905
9883668874088228693589736418588955892502
752918644278788863199892528501472749775
463318615210231386873126478716271862021
371324011874676030817359446469927502116
9321919915883319742923947183517144663979
58529813374883833021889123744861758426
7760900982783897864530761349298774232776
72044888146638469865117956312219805547
468572056478488943950121328568489576464
4187750546298594195129192917444714973733
390873534065873369835494800941314289842
4260288586362698112285035555966710335210
9788878053398839424392068648505854229410
673781131925509241554140795183099886034
7088145028295900539389116274959856323129
9889306077818139683822625810103727904419
76172627690662928177305977218075823
810435581297094219965911035302531056367
453282266524866174191617696873253936386
8814186077788543412071772537100858142227
9128995264839648690877196144662717559898
90106124555943804773735251279219783485
5736303030487464111380663678475149582967
9856557392228496912577681974585047361666
4222404881946823849327242271119744362624
426661229924239454853916306439208575088
87555028640326352003698918642090528304
15985349997258174826792075827419275169
987386367129642327916129297669238997062
1095194486187531742946760484102736526
977988127124722243998067124978027504948
58295766637099166929332645427151143967
975157173014591058661882690396881202205
2889205086537472564143568245114864954761
6301203926906694856311586224412881479118
5350620261589651278065172838761935968015
38578739581837918349793640031257006326
4052254528409865108330326080674546793332
6221216592171548731727504884353683519649
6644759943402448490885603305790127264781
1909264837005654996712224111822845989903
4325468596681550396729018566961312327957
8227613928097967519567254932660860031131
367858991878275898158487497096110694208
4255499753893407741299602767903548081844
1128686224924228377772328745978142395305
3151277790143740539557387086403631569
4663406074825024366585796825576498578062
3323809280404661679225349404680458306
9180579914414321954974357623154875703760
3691145089023399898665956711220885199328
437956205194135789647074940476847649219
958051414838564115556615180520314997156
447661358050266995587256949521942153665
4824520987725542182293508063611866669540
443815122342147558272925316025669752898
3668113877346576265970579466438770842
391828402206382764479336207696103953827
2577232269609840412142013801386291928671
56628886406441169775668571147454121170
714199213487044961627964061885477892090
2868292232534310949539726136248324386
922212491403494215557732018235441851065
294020353627987137272583976854343182
21436322507585162568656464540205331553
874389908214240084612663313835118304
3172178394315861488025501508292565003859
980284103002152136399186060680593421450
354627786059392680761929046730896281483
377021393737328013385724288221016459690
9657628841912275799546826166633727874448
799444488809769803670999851976022056881
3372355393795135259245478502217744586
2209920670632833154515579854108139315945
203264714943763384419540367635777997831
7185367450347645153693697961185630496617
7388848740256204406789511863771181411530
270542865927317980742858707993035064402
4263276377736622645724115435789463909590
195131399057848370208705423151237540912
61517337113480984874713628586094626039
9274526662768725369837176146942661447620
4259173437334287259083682578647029252642
36648087667875011292728071820957175524
1363837915987283630032119678708488892796
173655912262786440629988410663495616676
912376505756507584728434699365255542062
25949154385248326391506992662692665949
4868180957832533348086505771878229143544
8138508807440631697364080673354778838
815539478444892218811872910086402944412
7105373630642679767884427858713198726074
28977278923110129730443950334975991332
85524430221071509342450254725857823339
1758859346251019535610969201626433129201
37035650644785281248116374314059242440
198872836767844911562087277910493935636
36882889999735485534463701550860046980
26039778431829151595271586171930586554
9632981992214591772611921211528743798141
610246035259582180745636619481774889430
4575971892702209415557329108549994297317
6094769120576006517500489428676902019
758465817005685582409437403810666039882
354876337826394467339354495451045132252
3093391913739680117257645260893870425997
6202484599023475268022835706748562159430
58737472900814968774324844441686744708
8260741846187109568386776079411474801080
4800597750488496627621185158530215396234
138548186743829134647317854163740016840
5093572598677610589241716167615833427762
7081906223395045756271973930384226894099
8667800754084805327526929546413651816895
176254528627421429237951867761528497185
8822830169461989692540804179305501272
8861222159633407579713295542217616471333
4676355521101163825761528176446329721377
74855359387917235621088260819598011952
4189481610194354157793843571187114758
3843833598561298305957716594450287389085
406934878288380880316852690477012802172
373418165893133289268038488810720597241
331073509321107341359468406463052106783
506266004727967456507032512836551115074
9324811963846996548261173114562379395145
628689661028726449158841467758611189619
65773711866273969515678324738625793973
4658337658135769622945348818195767566656
990121763981347776694252415249051426539
515833436717427971030611050272644066634
1885785130741208704828211488949722628764
7696131871301695439542689280916024663485
484366950423949652128222654523663666443
5787523355342312984567896304784780276156
381634993627106421859748861428972034307
163580458553496064542051365927059771378
909767859643513551659628178984264692951
87411858553796963562764932243639843209
13203184612654245323136631978556136746
1410219946104835580014575743389872867502
70476541497198619283721620578772265390
3382720579318463493166652693461455902
1221737724462098450617615006545842946891
97968123991488288479440067923246174748
9516956489901181962814768064436832998729
527128296769790275588768925961935640891
4534913175356004925360159235369469573775
738545744203318769049436850920060899044
675963062044777564898281598370588237507
206687274395529541052085942465347942032
1686260426168182983244361565096264715
89909102884103415253440283269422608395
546017604445245955213294187304355829198
6624307775584484767934166092146288185626
6197601883358184915569092963865262591177
2706972074293489204485862392141438863239
9365870791392430227390825999884022111293
5030567595441723137551108565176962956309
212668539737923347462577513719689299243
618074115414427796112875481310846304757
975487223988120541935271543240076666043
2125570050001419897524205663211791164655
652043599774685938808462757293190098400
189994704804891870563551382708155422601
871018392978415010865992568358112473326
6167227696544659757091014131935833762875
5845226673033057316696907431860164723599
3433198292476197763175609569596130982672
644691334747237070564625458492288117868
1405310585108558985762267639943674107950
17531724364780050223896524629719957428
82428354317546894398953329627001325409
8167531940524009661306065886042399023
7092852276385061138975711716435851843447
206835124920187029552711123499677984968
894537512616277439309524145923296781343
1115857654210022442267679679356025465
490919614208066207217812717262461631332
4491413196527887189757234836226396281704
608950676188196431817588108692928878750
307933887787845946601187888583255723360
7639248982382942734431466412681445648747
594873102333608134885463609794095663157
352650978325714410182304414107093988878
9967145167787902265311302870944072818867
276583922188443377535683014353033017600
6604929948897656184273354443929291651926
34116285174235735058073265955260259615
694988714122743189551529295974394492522
92827589223875098371769189362024724943
5681171880246782958574350536642323954
94777368767056822838468927498939365366
9135749872704778898460603227191351185491
8723958221387922251830067467456873
18532767278590597392434425337488383623
13215345975862143025771916548028066648
215683623553219435276053788262984407
76417537197543054653993170649808765464
7251783036951744271186563763735422638537
860123838181944359205645682674999101388
3254681040664192290012054625138951911513
8370459920165234815322359100495444162056
25799678621345834733317271134653661227
6872307841372032350472103500822670688602
3341128798552332247452361519265416908960
10456371051175756624689107213897892628
482818883566485802233092433430090266832
443673106587656261278843458224249809577
6311185398931120034595825386825381167
1612808905320940899001016243738964975
442054858462762983674519407980596017632
7969356832097253940295271044113263987997
471717845484794182521934787675882487967
5002899394512788121243531314859847593
4391538687427355294638199184303224155746
698452979224663443675419926209751541564
881625102594910393908785851272341628
99275328929361320567222898832222422339
1113913033797925248354766990468989391991
10995936431751956957858869637723122737
1437379449965768381382314623345015971271
67418200391885638653783652874541412396
9493738665137817887673217436594533181
48087582440525997053974236231378002597
4793792113486673929558172174877649505245
73228010861484769647291178624628235746
8523981382898525396011812566855299233948
7485223286703850946897676387903494113313
1835277088646217800167184475839756267699
9226886689444953196405650790484679870
549658336282806532842057799512574020502
4727150448602401812384266769124750911844
1089731540012123307175561032379837203118
33495299551339324266922743329379055069
546740518081665744758388343168389647270
6045833537927836482536263254666798844034
5886856534975659225977358370452482947349
772107465591220517076881727525276587
4643780185216946913257489711543516774
94703445724725117803739169995094806405
9656124544209157735492877377535752681
5104338856921862164469251972471329758613
89874876414217528850622949904928621248
1835318174180055082906773489777881212
336730211901841061059856969168282020
3428291821353567522034805167112317130
164436051751377232736834521485127947809
8632489558617378304338953839127252786210
6948478329271584986831921144467378824357
9096441788641870425737487819403491273367
2278947594615231576732638728477954628
68364996496864669686776624294219335005
937078967645944890887841608098472370123
26537512784946423942166697543993324034
733180014112577565847197433157191738
802145234578165017479865376446942771284
704025647570973290971164429731449437789
166283647973269976212488166746359268350
7680946258501444475870426100392787424198
65395698665566883949224608117266561529
2417697868891256322660344863287532124426
330953821005735306114144145562864727142
6080495624865859185163331074212766113816
832442054343805267229621871584454419149
2320379731261573815187919213167895702260
111656712817135798635243055628218295183
1372391930461926388598336912310178596330
8000582376249755798470076395438595513562
273916915627135078796687566156966349146
191616074429908627839281103309226327166
4514873995635345760657210496417959895
6041523549152001288152929568688848309
2938794111958863245912343979152717518991
5589957097686451200149216688831539731045
741398973140571251316493194488499614
77424259143540089849767690578176934996
757960178972150598776984202954559718697
238132938460433549140825143795119879857
192738086752202362022618203782574101729
369849889198798524292047845385274742131
3161728581567137134390687735664141798027
990574827019599921369846611406827947640
38701706047431529662984247387769593528
1826638733979501205199752237990939333707
509928335165629366376291853847273476475
30059808724310194925066127369970325808
24533843913183882035772585472421269291
8371701699981054727225383036609440843101
678771682627591490743186175488944853228
73103609745787817372278247997511855672
59332070897631305718282287626582512029
616017182107670665605542315937353486078
9089844736786029104242169836566352593109
5243507226368194273099256846133575304031
806390524063250819378805967453240897384
4802277477416122607962887765995620783877
31344318822835324070719960967156795994
6713477753745008409255925950539065699967
3857339043914539623457543083523367562148
95552630437517182424133721554734627756
3274363339561457459184352339385223135
736673226599373297423045047140233894411
7768773191018080389797036294900518684496
618491153039493518414823328255701886833
9712728875789505205698155037870822463847
587971983057695324714482982232649845117
11806380326694048798634828437382658347
673286168899939212369022652976860328032
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]