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
9146947433715992473319228171551455249969
6904329214303141961569035723585433326418
36807948238455023529010272214018352799
9258896490727111872846131633351089352223
176430132980905252079110434362931435689
702231583069908159658994679777921551549
11303573935761065516939184459299639378
8513348932562883808890462005325343335431
5221364497041950461629401284738959681693
419853058661845452024046211190766969969
867868328337652176189127846398837049896
12103636216798756982514815391851834595
60473259395193328169410295251475105986
2591845596306983373285783661197778768485
846154378866930825234583287476966225316
99723966295120125559116446322082766374
2605857686701541828168024872568161907763
47485653233976067657314651640008469269
456561606609816697553589217244113631685
719429517083984140671371785382813221000
4418961262646772624684179001488180584121
615350922053821227882286184264210187106
6984361030848963399659498478807115114184
774821815120291310322525429192511027994
6141906092762344515528579337976767972288
785548757510490388539376692808579352288
5367862044216020551197165794855274506
7575342693968420117752991717657278052309
867585218670638369395457399612420943072
227862136015250581783555791253631876875
69869409238616659606906326952312554621
3367284284475258878565074423312773264454
4196645769131620776840835846947118811812
3724506155289428160534157643923893955699
30127888764250750425950299782108103076
7585165678943692256053552930269555952545
178637210694104735590127391551729779896
343953668908641663269264475458145284414
98915160865258311598263327649374478686
2890351233648051143298883731193766661887
145462743886270482528101473802556769039
1800703892033290540124810140586283989
603213859497125920662331272158692835347
467556014289570724046059609845624569127
103327336685176799590681739573149247575
5914124924936613355695341572431488364749
98167180275443937137206481284749088416
5030456693372767698398566693658813996
2372665521084031524238621141553925904725
278692677501675333187132177584862033994
5401102421455976289451788663963585733398
539049896066163740795228358986448954386
820150175993292844343477124377097933937
5739594976262446889496335176728583255878
599544039360198251314845288506312234574
3066342262577022041436694894788746054
42165677770353965168490794222369065404
411237462631261131659647877236713126132
2622588352576878880683692784203256575413
5366782963683864823596079456419820985
3372116263455121953276709008290029478979
6320557277509397852047214536313730001205
6474723459403256912291801950523081768381
1700783154039063131218825702580197939072
181720617763413696548758968150548923510
313623153693952535357925418265215295527
8281325245358526275194238818306392903979
674168945722878518803860676292451659193
986580962445640754064278148286269280
24303940302262825568043593259688663962
9795206690499472012450455601752429413
4250916023999392495344301991474287615
5421409559547466690118623707405743155625
6980592462512244739141051250586298733062
9850693720608114061799954259473316322
326299772184245306379978480540172009419
824285708885644229661329093470726831994
6145984830469990144528109945666979504312
948311084428390142110262509415760604261
124949355635423492615294554426053124051
425717074982641523671345298236546823495
7252276836845815103295103046513743344935
86365322923373028634017732924127365878
8630146080259378478177578362295864632372
7245519163893422808923618296775834988929
146774095275975620166279491638793687634
3868369829235781875716774388140922621808
250281896883492198637289371529381031001
693064376534502945823178225615829164629
38148330831176643936833466570663814790
833435568909508926736660665174676335479
429637325355851585653552850374078957718
43055739484497752970721172672134717264
6824546639181660730115163767176997078851
7625435130368563120423369982158819796988
3774885248653664904627392059128720897473
471961294014091378155864294929349901997
481718527424862211351009658082618758493
5726925916086309487226635205804496833954
519544399234289495269749414252781401600
5767166499531677321974464714059635075
15622672592825573832243472114636773361
7263624967781875335012351979989628825127
63259219702685554483044211289696927924
558784837266709151575601627370011521965
492548607529276214682695209391522027810
852466128297653364910087722229591602049
155650467788738992522155324271443894137
133367672740498331593955546474379911598
8378332562366777936250916224727733363426
13533832797526110374376109355593057160
9376418349882493522623746327585551554294
6672776744937663915269412105379568252
107096288406466771661793707719065388990
10513057756734953072571736520911157918
8909801080749376479179643735663344151519
5679112376302428420864701977227365514108
652811661758172873456162895746915484698
889496862062912120142646737132932714177
45284099437759144961876819325875580458
7933222119801162432514959099942782934892
557347598241654598018444413949063301515
1571718370936507966955998864132986730
56111223928300218045107307455371322850
8840786885887747524216846013894238998619
2842865633942524846163627924621887148937
6849567166389541892313836213399389037764
9159682241008707513359357241110536856980
628773743770945442178565734660275536518
21187992372118089835162772579654798355
1098687030714349600029257942736658288407
282514719765501960455243521348873226946
819387078140138217058517711977339717861
277982353187769829442428220997430731224
8558923914879935613713764401112319604114
7935637098148726649522053264274466758132
5393838263731104197811974661583583125123
286622719273638112992061785353008163640
66895799657471625828752435772596587972
163085211654242253714235283849967919637
5966998534615323901764867497147842893142
929596147893570399238445454564451186455
6536378185131071705978814703906798178072
64091905904294474983243226492367782308
318130913788567147591314353398912384033
225219079779743429619525391427738393335
33752403882819936132611422599926545846
739395059701897197729977752726251562216
4668430658367422206217331471831389912130
976474351116598270941508410597954344040
3109723495382634625277551107885879616376
592661366819862420563032563363988719362
157716039871707636915619467750854498371
13318804510903044215406146977525399634
6428541028251747319842663932385622883091
663173645671860910126991169419962042104
63236607637520451550975855665351294140
440119666698570304046894199492395413501
487936124727628748804981956416771308741
4979711738829682572641746898222020598254
621453169416542547186237541138727911936
790517177477790892565889211951874588351
1077939875577173766562183797940016108853
1818640973766345538918814933658735482903
202883663571797283827900434456575299518
475472347104949137648303765555453442967
2205207961384662438385919257857986783612
9459120971081626866047645518514370906271
235362091158447386607797380588096073685
654931884663235164958335046841683793276
5427326460722234258011746939507323959038
30907962440541581069236257108868489385
5235567443226089318779792151832045717286
7677276519516904667440397458606410818223
76271198337584715191204217501343838238
116444902778437671263433314580076472336
44911078886440695856844206688830997324
998770586768891633111522431646585108190
337442036462830756776168567645194992661
144967182687820254685988568733079361991
26871576377689471673262981909537413508
434462878843252060972677412930738618722
174282704256244858194809230455212077908
86191380633958639568845622765812729255
926371141742782102510527688110920756020
48835118830236433019665618228044502997
2151805018677433389605235364211786043
722871886749984226460303794258229403550
9978589952102766555251554545783073567800
571574487767117491631974391597495316944
959131881718804687712177866238236688
487079583617687943321989429488341279338
3794375793779665322776262139491965979336
471669422011680440141562325192677763446
9795546335033660254616549475899532166199
29734997376364779309580039793795499624
7024478828332388818057153408657799886034
3117477123159469304627376932536160376018
975531972780171891047347137596328869164
5216827051621992429746971947156633678540
325447557468820644392553250781295517147
734593809778654897294318851443212357
173778578571407072799758434471839173386
324061096686158220206285442412131614211
377610358279874516411848352914079628229
696157376392606816545962510893743813175
193461767082557648153629890540660882259
7986989099269027729691725955952961915768
3457603098934520997290751512767179556668
54238689888627264959491514244517451979
6414962686993608708988073651491452836187
8118556048249549651683609087769235043981
514991856099622232640811569794640661339
5131835883299578919372452470379353398507
1401767274699910533242466841371429231801
422748111658181350260565837926120462598
571284305339820560127390881665478202999
1267571049476434605485499980910788988737
847181434863333908451135026273614036458
3941364074603681573699911606908569618043
3947447154011407248341913200871073687395
188164609083351259722880599054841091316
7004657533394481748286074933740585196684
9436311480353023953955533183860262271144
14611215927476211265854584172275716126
372597716133283123110571189913530613482
60344201558791024908596257676988189935
677685146887459019165664775419769807410
47736345485941943017226282398116868923
4121400348047697479059538037509681969209
77343172226752181748015260630433929382
8296716578001831325140265335961564867785
1502762292533995789370257052609259102654
4454290045863324630965503406255321398375
660484843652625112737243104273351661165
3292347410394137594235606446870355551882
40508275784995042908520943029199389495
751440204612822867081199370495425347646
699250403848408690787648858901672443376
260177199456424874816469935842191186861
4375141329911539289614957502516660328896
2566674532372816665178021606253788388163
208802041067932896456318051709563809238
707416466880272230871012035455529062575
810717596187709380484323196358478212814
77262752257892739313560929678476657727
73364439599938198535855949778456283089
147139316397336942459115122906560249145
9857211854206167825367969575309920275412
372329206883362605313223569462714271186
51782317277550353120007040858251031228
880052327082391839567114908747484156
336221658072843195631132856330745626055
2785904765978256587129689821763166564
85148904819171273339006898458619589447
34169543757684564526199246118040144766
4282918261860557245435818275137913622
6171664246602488314734957572151129994
6701842351416540201077249777892372470
5833656580882507903976025148934850012253
324352826511423312285493864266634452524
952864644474465937441374527557356504195
8518195559735142169353639897494139993478
836994841626959827025170906830129887477
505422478893878664751771797753796396680
2016508418014364548456342377403227746160
4310905899949746497450568031961586021195
86869722449379263164327674450563119498
175986619258503063751662430157067890512
2738635848596548285333012151519757794860
8049245532879771662753098881389175406060
77704113313149869393712300278185762375
184673226931483934112003405482082297946
3717100923027270556938658577498376853464
68862776363682247475003617273935212344
6245947780009289557720653020724289245170
953758872792276622930373868539060997084
8959495899817443201489857178705919125761
8090783846379659586836245532470171815640
958650474701597820922757896556285905856
15071706399895094866170143568648016054
4730130080639750828635601571651165114626
90614289414881799194842867429416994759
219213704860303281912356366856413663398
60950786141639993598850649016247064494
2573107588200688967171849652597325710
629961921543061373684713742179770333503
1355215477057246741119535933109781658729
4201642411538105647130515273245124353788
153811661832988289023876487323929184785
1696424093252768456457543731305586621006
667259611365860557843817102974646337624
8632554374715855324775281846799846897336
5458459669996495178192171361364071851205
2999424984665807402514488348888463176510
484469295643575187110926642676148889914
408485010571335534511121045656074444402
299620381231924013658168858492389964095
9467233962701329245652842299909059155552
2848540678928161638550936053858266858398
53102606803548295455841781469199591735
903286594768911977846914652746279323923
17332766493768294022617725819317753952
613597499518926860514368199348353899132
6213122197822744786092914367154164603343
480155168659112736856634463033773807335
302214464580848679855735232891610462651
157947952936653881969332851431308697722
6850290665343238026804984361498249508
332942322737111222649845642328714282277
759392789529373855780663740667169833916
6602839152089604589390632406784714877007
6793607521255087398691892427430944876395
654531683107627948739062476924351137339
9212320487654444808634803225489973774334
633211953794091313731905195077122412
2190794764939802455937298725372578651240
125434974749634681237415417555579743302
316743452097930987925763535752097215762
2539480612889666463034471886902642101156
51229335807650364132852156025213879419
5152127944224728814373806305779876575069
355441392270186753453768656373831725167
967532040671973090681560761954763511
4389294437786941662335725913226155671
17113899619966111559741262192352015333
140317285662543566986465281287523773615
8298787690495685970245371904294733006511
1806610954848084574932367590432526675
58582974450025150638908021614533047447
5872947738069054318169224368796112498072
14275636921183475938821829767065524865
6273422092856044406147335817452269261150
318863859548486740666706752160634298446
50014852413181132989809732430943592119
895613166341405071963293703650769306137
877083995770644089954570798436879696870
8420093877407963726511410480018896741
7452987310505516162456268603718535189895
289089679003612254608206268324874345176
876315492984457266717824761533430818629
808279468699168278261008810332772249128
80709291597594142734443337978267435718
4163158946096206151623161267898318663009
6677614214363186624169683578127284744526
885980055940434778044384450707450301072
7007247017615033919461942519996839954644
5462321784015879910137384621938690857646
3243905331588397642540468372286725804194
32223371352893869449761421098283956965
19623486557946465917704663692267036533
9088164284836842911734281655476558702
1638857179516034852836655576742780616117
90775032453629616410496453614381366015
3746841499513542366575069102889563582923
4744328238603023151519893371998472769477
9609804262965680966945177577496255119261
7971293432378915557512515150479557703
7170245848873652361541513729969888424
77947762748448129132795605941439212952
832912256667858156654556798477207149
835472466315459366343822735114434173387
23063141875269040493364517545966536382
9832777566139913224856114361905836048768
6577414488546315066129201320066931699
875391291042832719271983727766087201773
6815815354907567399695692970824998069851
711447665550936791576662725208916296524
122656645639617567846708682179037265710
8499212526176493069420562454093019743
26706971852527982823286609347391106924
971096211540174248036114398464972794643
8596536384202312182243902626378492887380
431232281011145505689968024484376786715
3869597652259769657889302327178922749834
142982307495663672805368977245222682281
639772277615223015807654380026666286656
166156594831145089141835507559186963908
5890196884902715183358591074249550763621
3589109505352347784531757897639847190
29183393239160696669485559775275659968
3700378586313859828860175449926256566493
474848543653720554517571423299318703623
488937327745466137854022913613328616352
1858658148034971571943263215169316955465
7370208781551910239737156301116818554975
5081921664049844334972541131999353344768
983644865522468442845665133959870759400
824385517085103885633061171343331849880
379598302068481964557806420337591577588
558169898602265564651035758359517629419
16283827286505336065172330694390753815
9172693249986277347642846508516146236115
613773782441313772768561014292459921332
812438273565759133303044970958668699202
815148666201738377548196626800924071611
8134850252812542910694099811617364387807
2742419579830681286313822459320556717
4693283463364613844237559847896187267187
283931685225350664813519547611147795241
540235234021486839929706446455172196399
2651995116804249953262895496237412341635
9008700079464158893917912095072862922
7134636919155595514593186398129249202369
2872118756435547254113243971267555818976
3307111345559266227159549049101252816
847111616751171748927264865960629856140
616567849235955471371154841390767482923
3054311527027939309752731763880739541335
286031297078894851050007977408440337745
6430618884429791147391625240433067459
1042959441145429811315892500709917137962
388143004822113353461295547371837711508
455745421218925111706081684735016642484
9630662458746491127361983316576340931405
1269385110454786272761451210114533601379
8269247458931404256326703442178019189205
1059513439052159840824111047695393741391
6496169665186595345981116100344477605318
17289578820869153412851819643212367718
8698620020985063396398006128293837707902
605639622297562262392327790394431096283
6906831340264407750120941188503986685969
70882550468210027998059889297967595390
56359122752083546681959221585517502934
4217118030719792288560972482601932435994
53751425922027447310106356517269493702
4451935051906053411188088449595413998291
3788896415879531912328001311452498836046
1450244244193758746035389970507124005783
591082719372361832013702709999779909926
4758712420276545531948897263980129577000
4579987726829650964422851687269625213851
472636556137469444717419702248968462882
8684171868573942685242944472257991205083
845146938159270417739430727341282728
317190636597290419141255381719571819
1709436497263252740398373566565487049234
1412551232996878625956408519488781176987
91459980516931991269711265380833146375
1739113688778336589444678443459247789117
679124654972901968166603485548687191637
673619729737203648007964521675749899535
518626053057783487355610876271356569789
740149929772408679272137585234568555422
6105670745654403975248634166332762408592
2382117988056453769440545215281849677497
54861652253928287564904638933584391620
3813170122716722963508683944492237415
957073851078303770818487366890299261142
9092883615941918419242904403428577665507
188408410365175659844863500925133176270
312365237649834969057363163233019181441
2363295251442217824140844534902867479077
3531478239422042362761155716642915157131
4301451434812431241587702881818326549143
5246911111784706817367131449433584205594
2014706924582907618849239400214344719143
85897740732317298634601750666554165699
649658261388506775085296846611070386051
975649407657786969715073818715826203
11153707982097257305899223556278558719
6598867957879026859362284904725315949002
2493319812099442240877407648364214891792
1505746815988756997830606819981638116596
7581280513719207475270817122993185975031
120983499139434272247676230711990979174
804976767758332249794345046319658092400
8532609521378273782020156190483575516177
2065180511303278669969692783486122615585
7374146340765639323848247812877544888262
49163328309326536216444432600276959894
9858922039046492871486817388633513706495
77109609497260181551348011417082048578
1096733722549411827620018841358028453017
8451314561858693078826945261090517401
92045692430888323596352271299345224470
9576242550209453200864965042975830419932
463242567718328342021082513321840837762
758566669887146763355172599300621858089
356199608918769995993609236642272185367
5155818458809312696786713708521874126
2792143246475481228593823734677699172280
6703472647819351129322849049107271854013
56233464207890558379412044792719778572
7664115587197034199677147624304990174678
649484015032510992783792799205544598716
4141133967569552162857284260924617366829
54188018802517591634935206111385917013
33412557714785323714139587823036400239
9240377510223669101479538147974411103069
7013462856048815176881863277293942045645
1054375365146675120175362794820358764031
993240833434333323079491462336758082334
888761106572616678446719675778993721423
4914158833863198474627235737233244221443
26048272957212484747559741153675768569
258373305123111122266795623062372709553
947072739999013664973244891332320797076
9318963185239389937664504256116451601111
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]