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
9646483290441412164183425256768638953158
48268194704618612237860213387679086255
540347375345282663347632535537822489889
9073898036495818629432199194556862326452
8291937673773844440927069462875546558338
78741922335476801226239994458564999424
50487992936671734064105045382174474454
8432796815184000338712064416851817573142
1833272033907574414295179744920247757690
276852872200120737113671075949031032280
452829396915579837804275637347976249200
3213539169186110326526731686364779245672
597186703855435345457431158539674287023
432986272132882428088395136891866295843
133698434564977446711023631395345553130
27863938883927656438587123063038922341
3458689224816072279769783177238132536808
6659142525363981814647529718197534995907
4672175411057558773347229209375067267160
2785430082846414257188971194284365905513
1128365939493336202093677019848743067498
3990813870279451271674372442694125258034
465853876974638661137512918467925300
6679288569582852795257875587270431487055
659723797281505677859691831175177796928
3845746156804446598932061361314726157865
7892265578516161928411238517884169936
137452277325878018941496310054825928554
4422631736344406383577207781190937803
6825727668059240955561906317233981219042
599965442085231232982877897731152519638
700521946436310739243489695671041091632
205997322331824797857262642472127004227
67414494669681008946794595396480327587
96227249197319977873562747115665012461
987664649524342537318165358177988537038
3062923522724187582816529594940669984
2997287177324246849339975155353081302413
36951535669550894430281440514363051090
6876731424848542839848149079496937091848
77403181900774934672919228150599578810
912672665336347481325946757475371466220
9634783576499079523137799292852444259423
1595208714468426242080396785238321143480
9030665359326285419069181319204955534707
3351270148331275845382364431906437946
20717996426372130075279552437361129689
36849962795135487427967655870927493362
227648398483216498572942615572538074594
21711423801479807963369375452061964694
1903664098848818154077883476544904246
547384142693897028656286307655306577077
965249114572212583221956222597025486451
5819459245339065484989438941742246045908
294187716278889830148990284180624399302
74397035061704444946393649179059331301
9996185267798033639636866045361865674089
608253075584320741875345748259743439547
214129459689631628191544495579170781060
4849444759866098605769997577923234944141
95499520200283267529402493897848973300
922723646996624242986368269753294953683
96979000453424798740549475725288363973
188535165532562583458997465695319112563
59209199778023595488574190395152739002
226597091913392970386346645958156485304
5175126868198465326467317463672396321
649235239768071823678324843401493158628
33809614867761651063592233024163683299
34606786312387969912819598328347583288
6047761348912655842341182546996798193367
56153714902399818486112831338854964299
1970647797342438240033655732241473776577
768720268405203121496459464399809658146
121373156909257144930764908898784686733
82558309534299362123838213555545232940
2268793002886236528039252691968775035
973487196676153709861938062630483377831
64391133296349579129164101870698338980
66993379149118115097739664976263631922
953519855576156491703759631395918365078
17325167822030793311707474125051813844
80831032291369131554736837916586049324
6571675812706563715195351333563261974225
154247669168941739386129078240026166633
8375519581553692660330315154457262185587
230819083908231183560157967306460843115
1807827695467848190517349592805423856552
591890779382866542414640961781284087846
7194675734297307416155258253916550656876
77001922991836729939562449975163248684
9591815296355865138856522107698843709685
69575748369165476158301715695578048
155694504234740912004537206866860235696
265861564713918349268123005918336738709
3410254298050324991982659515705094104
658869806484304813297946199887294573705
7514234813734641834410444217254228471692
5829505139107777813144954793752968218856
9964839759591748732629354851661537528368
9914170695109092600685616188222821518171
5943728433527258926090748814338097363098
65355588948553718833479391783328334383
437147595920256234875487399779029820195
93564221123816549352312367836498332342
8093690568105376139791468238954799334120
762835475716417386814873868115034801470
22003809494425207095489985712462268184
6703194117122545441311605764628232254241
937377027744328574730878972856542455066
400758326987799295757918601688496189550
893787343993708319096108314452236682319
29538724881533645829347962585971684709
5033462383042343568228141198975573591468
419350114473569912383079681721784778960
6334724092626725855613543625491519406831
49114688326861047224346067954071965629
15536197489561112185422959723956822194
3957121936378661975737265144506517036087
966890443382181873441906337491924471556
3299338076193369465157195864345873793765
116165406338495424117265477199093543769
464520983804308554949958656768237713350
4999549630019263297017943169397991221510
4781156837258128185240321629859224609193
24094197982937308626759873777429363
9364577953133006664872980076471979768
673036121312485031659274262326977770
2867725334443586187827851555411976332401
1293444278508943442833625916771178703247
4175254955688116847760012567745117166089
248295133905086833250804821776078713373
7304710224858877183364764923518111855
631758793283307484989383007330271733125
290979171321622440226301506430611033042
984286661775144388290801584828630112252
8815646760084475955448766111188912243457
46775106428131143093411156547159701420
497763329775952838754761392837386109869
8639531155104831376074411591966265384272
7455739420695978182794802769551048345356
46399435736517868025587240703753636837
310222899042970247723208563716774481959
476286342412926843477919162457935436189
892614264057754663018339204512727159607
638122605010378444383141328326980593536
9897845979516991794530045425150330511549
67870474789428121997183674546866507
117149275273538223669815180260275101
4607959397309122190584126526255248773238
4147816835359181265941440586999521936
910664821357454477003557518416283172029
440867922620226596343429530926453066086
2587940188955103634572036312374690885638
224541888901164484896903084152127785831
52407620867584954711512295153933767013
9215660147036161593582817640668886494846
2312258662734421929403461374657912511
1992815185703644462743722233146915838956
1009628165776496893043859181684819114351
90301080858944446299361777694390349680
5396193092411290562797546260905611899443
454346975041216919796102002505118937827
1193368941562482569787201326875537998936
555946144904852495995932879702973182702
5323607554619892413593749243917142053176
28269822131639318316936284943673762641
1742199583365892561967899807329288134300
247207840539954857998029236281848808804
52256637622631767034312759463454998865
45119298758398215339172418723762959982
6057757429827477512786025353854195401772
5418720219848170473756637871109273678514
8571210558422538608647306713971931962387
1497843464424717151968304915994782318632
850023797229727301570366567215374933335
21062632733843995899385107206975312791
6258725940514156128211245967168856493987
5953627541087611935485672086783475072981
295782423407219551127087147253888018899
483775904672385891823263633144398798401
763385234862329983326680185227054801
587310419258077858172884999864099073165
6997531460026936123756649535401622142367
901947689270371690318414826369792468453
5820457838959012617461188517393351209884
872126725898133255375055954191664067887
9102386059755045831790022554591729428807
70639574462968084956908749166739772495
6544441885756754350333041084128750979192
429181633658255791093329992943459039718
3011734556250884751688183398082114721
6763493284404165447556369919917738423740
791485454886528934738078082559773894304
3171730469724868747055714042295139246892
580394517173848426935587654322551021244
591877772524902270179468184313897167599
653954451525973825980576192736193822203
5576739263997649989132914882313306571
3220503557638133924747209657750946192300
4424756677767978225169786463797687334
8738420314802086175043354190599885047148
490478002723685315958842925674079693130
6458271490758333376065699962341690083900
4753722756538967978593874880930834428023
560917551642781398937365099808536747475
8327422575246150218453402035316620143469
668458421672547161646993485734907552
196521209044550580362734485430561707808
654536067875457761648576888171212426278
2941827979041058791819704816799990108082
4191326836289470879415816495663872612873
71008727188418860627868870767892136306
3593151440677073111292861275379426640
8318666876322547836680563504499762595064
3671390787395974126846113888144266506146
30384868852484827204493183143877931599
1113163795508244840846832839624011817609
58467367592136013814054681954147126701
1401120648756864236312649581794219750
182582428231334772949942175950272548744
3541217356290964726797984375977265459
310040965761100488373147261537062992472
8668589522142204470280642046387727432266
4137633927897501756528429186297315602547
2672908135208936727675827376328726733273
6502348751076422711675078245176363011308
2866575142871865498724794546864940413683
679465052896213786158877564339768809172
8582763117323386335664911301843917439827
228924851147732930216494259878587025763
852699044912825589643453573678840612578
6470826279916992121086045325836012346705
6513626963573496281921425811439959908823
6095634296646154453137544378921010997072
8010961594385209313942864724175882776919
2559380317074425541889493630104472624613
9821988885965036226527266968990292938921
25585305891065682783120288896897414438
548198031247653690724407434733962588245
45684915534887460741237604555977316713
843081768684194589364854856973158382781
3078437772902344999164047377673252414
638093105120854993487237350392145753184
5168920520261543318878745863911447427
398976175687286664704684365728273906206
3469419635392877180182989305289043618796
5601190791727429563649228781613287447254
226467841715757789374096984649749846197
179335958154893715958969526521703548
775435231011529187506969107234899479264
529307714102080673162568174720997952182
1243653057805538637072884351995729992502
14448758550688174745879966739592201815
829524097420973591611566782222011998256
9109214822941373564520805801641446387224
7297692392984873812736907539686818402213
670760036572994703152329813996882401718
676856975264411152882007375499549082091
2390569481519081892560424286068778154
6221971070225357234373296594575761421486
1519268826147443915413985615162467953450
69538313306936781208437420713781851302
8294927796811715919316604421538298631992
476798389891562325529408005869416526647
779617181761595429995078636999528914605
9630922494757456740544508916886628813064
335467146095339569617404289085016571219
200866345480612876329561659294441464361
1514534219806895624924071457784043813451
7797686111003406641144098483223915252625
71948853516726741998994468657691222908
3918104749351416291725968781447484552401
3572194457559671227881624931712552074671
2620839197696708901654059311236433847148
993560568787286736395184478835288278795
3019720561437972421470363277566096878693
4082532315247346717366867032381379503172
65211219226785485476786261821830837770
719079416953991454185596335817338661638
1711534399553562001811873587351736229
33448190163171678486006620821019093464
63897615816021476923404627750789186640
2044276523821814165829322698895757697
21145483626485645064664249921819243788
6521758910954413784047063663990936279109
7541334665355754252849453159221305799
76937978467873929657770730565151013544
214889746398957055755522746327946901784
6717723385718128258757915650828753777478
2100751013401866627146956736960096102147
8227063662891376594155428415202401360
2827329352331531230222435638944463098193
4353876620609762111148513634866336292215
82672433330453045430130816124547506890
2532322012714489409616173047363028586244
9070795951324398507314644206219499604
6042920198154412168892902776981667395823
598352128928378596519914141835592635815
5376423988887924808446351106193215669884
4399475951383828396083552996106578516239
894851568371605042776823300632526872727
97303168494928550733487036940180925538
6578269074271439998712150642868284420
545834907236236453928328867260581944429
7617729375527815212412872429101399806057
1204797269217850638912592845746893237354
460875415122470887126509057708169928114
633555211714795987143833618662719669855
211773977597875511319186297416626138163
454034076472195481868117991213192301268
2756824867603592032678773937876303019
926646982074670790163985780391373973521
9753449179175346879163927551818365289656
252145002183774254626428509978589844524
257228147128245280121131233597944666399
902117827165474994228761682101227585376
219190213526592387177384661826894215391
529068618126472413015648690598872705845
782178484773255854667854076169278778989
266693039125943975659385481992605913464
9131325622413650438737999535714254878478
6513390444733779123823361873774520892729
4468129542135759727150525147394085251970
4927659235622341415198702915350850874313
571429416024790746713799063877527819820
9252634910267343580824638257172560885209
49449448833793549008278930274878245745
909541628761323571118434524881637514788
197764065482620060674681193877634751338
4992975409453646543161366653066618464
6210750137962818449987384215321623349982
1599198636214661386867403460177836751425
1421264712152154629698757183437538807
162849511122533952589461880549761271176
5495678763135285993518985606708816259645
469631547476151258913258721939148522099
95494671913218918956413150604477303130
108797325001395684710574562242521614475
8176382681393568507462728477993731067
65379185263788394486927643019921166874
693750821077555647618312351599776249136
6197814294723132495244905599652980387432
8289109177765769759473584734624997609446
85826424163428433932173685741171842250
8503301324855823917081110134722795748
252693828186290413908390295255499021802
57674346482176445888952489993187311801
356328631320527244011826183747577801643
5580231725795298555966012427841847778846
7128785140108890652389986266200392679928
3164526281063845261745624223812195823576
80294291499275576962455239771562076428
704094177946631688844854810592355557677
2839322054741153476472276719239885514284
23541428563273417056896724341863045757
712370244740617860488820996166051395463
6625255546025507827831796223797234068802
6757831331577749100868051679729687805925
3071725181309220805720847010762545436729
7545644369429093253791358163708215127
3160485785428803985387494506363487609820
25985124527377564182817738776168718237
49552459166438043838317620816843934661
330444656891653746070112929449144276850
35045190473288889341647013934791017243
839151346554699946102317510974291476280
1504179771478773954335573516987739551893
6712656973233347304180036545288749288416
71042043826582982819275559989992201625
3228535034092449487183486451507673024169
9512813431693510522435948331783944602396
759547571264708317662437710158859435075
281014029474737333311045504608956587974
318786805713741945515944907833731725469
9952817631938234659327613893336978798625
3364716681824138014682810647676716692
27855865694666464471853305758197467488
434992693032807594762658533349377302032
8766406760991752259456172434256340105107
216938535928620422081041880991989052181
4849831893132861902274366426253668739625
59488384748745695513258865552881396623
3287678752706399560998938990321311323206
521987376142304078005032852379223455770
260972461216845165645334979579652838877
8481412769404791502114773046648462399817
5564159032904091721682705302828782683488
7408987424624366512615763635486962418
30106101380793420743103734206654943062
140251233848495676585735825437841158574
2218753416009614687578828577769333547599
5124343280405365292010727736288537638925
78414331476511372809301270133533684232
6634234586328080122545492649363758365349
17036264642220580798753506717435535060
622966283378277482727580463096510722393
2225249456684772833702360086675809942
909629845683319188385959983480576230
190932209540651455317573225725398353305
3647565775763903122668832674946575363881
9238372049479476986665568882475442822286
7128272468544370992847477525865365851566
6670880487908047748538188324879658987304
598650091572522262986043066376272687788
3238686512925446226896898258359082415415
442770807698829639031562716238630346310
268563079501089172941715027456013229869
7109774646879916038822292857628387472
390614793583271444289874925393640103297
81443614006488418543059870111964069441
880779496885707486948252031365953823654
9585633635376757639229547041944259148457
2597753393791040151240946005733443624156
364965540196194557243054555262081299459
504340858437419519731581951627544552467
8610110418052810805222401021887842721981
23334618643335603488066344443363272506
16813916284227771494598558823758252506
704458532136279796849656306843297733179
380587821152609772559964785783449557
17316708628111118252089397154068032836
997639264377318242545513456203673838272
775714366677217446918369569247088487494
3854963075513775186721593495521168295969
59511048891596062825891348550878433599
5198256654766488317554027160317264426613
73183251412582135990729129430590558248
254151376745175293558681860382985656407
2799656782798921785740243901367896116218
1203307759783298233841892903809486813562
7832464860432951437781557843190596399896
4578861361284465579153549379582097914006
269363871979454617728684158849799528928
94658375546620975535876316041367097075
39009647487468159132261490377159156838
807093939002622493787000878813025669643
80509933331965736015143182983398607114
843569388830695619051757215921641331
22392014524919509016401148375594048
2985309889995776157552258533810719079289
3194338395866442560434451153596537366623
193968952174057845013203360587457459185
6345664664884323537850489863264820578084
6989537927368953880225737099918042222476
670351856603939938312277445979657298400
280583148960585033116790235742493089560
346065626714272760935209903247015456943
678663982645561289688945466485227608382
8145276874058989981589565579310568312646
169014343887832876888190267868661898844
741329277362429677593612476274830934355
230798409896941871334541912469827784576
3063787630726474859422878390370419666816
667195459716984310929736151645821589186
1627230270339485754642959371283685538821
3306817120381053844944399014156085574832
75124208246191025782119791484088534989
3798227525045592953391941463456828136723
275516451064666877259491849524693897272
451399438511825854662935926275413984691
180186362762778630401129896550194117634
41264897882454547775531165963587316446
14646353341433711356551067330118142126
1619352042647792125018226213385552121
7580455892203997832848187216100737672568
7748869437256922547372063904412290966046
719542549949355475548981478888988756632
6961378748257380599186776293390465
616144777610427489352143977797481663664
699383363932512254344076963064589964594
6193553610674457536716505274991755904644
154226154856744196233429417369775975439
411554977861610243299998107317635018422
5738905352318828734682403643742776544791
792791299616911676519823378110056536232
504679051327268060172347957886104562983
834397136187775763037404816824070643269
19025879245792430048738374167345343988
3629497176468050884534706985960827348045
370349863757137338462014805394490495998
8703838250572918702285431334903436215886
8412814783666065156179917850322554974084
2369239748277689451162507580014253266
66963177866296187371765836922948615451
6468436611234382639322992892865855578578
123452203369417582738486771387554471828
4108683216477415140542146457968277625899
42428578817986421149703083846773877029
625147835735297680603707684040202653
1978713960644535221939113081619546784577
57765582265063387777125312755532853092
4593478124653686909426885169712850957799
33953692327114561215503810714286672443
127398101658584479018256292176324767583
331286053560720462055944623572523331598
950773679879742267567057954435353909507
1569117674048094151384695718750316486599
235622124251672592899752455277143514585
71249839175255324785787937157214557666
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]