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
91483306401829058643715855436444732545
46008860683314758171524145357426504727
5972420878586141815360357493462794106852
241041566302131653864685017634028296381
115595439505870527047477892144063569163
8687144023268060227980813097157597302726
85632472965442202997333253955896213913
4552566964816954379652094194681796521422
5010662363392146852062995988227841262929
89499299968518043478650930867349143472
4492654798951269865826021430570678494
691540518931273145353914199090556613738
4691203128698517707499447451307561333564
728295119462794729932657220561667712603
233939742140808692164953247017554318783
341418067422131243879844738232876284812
428496955002271701663563980517830058975
3021312139963104866562656139108732295387
6368380419456798247046646163766287105411
56744674245110322591311443412409397227
5811057877857904854362747212982821485
1216849339889569511479554791107760168281
384417967353476556219847379329466871993
60959506904659183346156992370364592357
94171011791148920986817926197368028
8482101496976436676030012389797781237158
35179942813368874076720465111325778154
453943007249571322201672912380364042617
8980669257126643046623841423554700634
1509424821669288369334943784465929196237
232179414419411715294197317478340555424
4003652593550955744045895112222185779
24667224195152697035580841235395683640
221193845598349889788944682676686692933
969443111112724117662721249746312549493
58962511485951777857454458619077487169
16167488414247280870159478856386035024
57566779407540439627077458277363081528
6107588075308376483593918305416466007666
207168666749570547123757380709188808073
4381512596453567577110443278198976525376
703621736822840677395465516155289997890
601833436242886124825889772527596803173
132359759498388073310274737369464198055
649294231895984471422243128717621402498
945119259097857747767588322277736684284
2347957312936436697156385997105693145120
260293213727992400937384115627780027005
497158433585339134202389854450656715314
723099753595345146299469824847833518
501799378461559229191680624217177168278
681934774919676268506117531348375083417
55029311123275601563696349714796794580
9324789433911745636819916285373894291122
84119856801971654011348353311919239295
80392088166113590148141549532171928611
1045820042391814602649479245912494361076
707425192733925968056974633509077483702
166649073104215571275503683659132343580
8169913042704662473898370252168373120
1775812238865064529129459735171498564484
427745402509896748233635430364429518364
99748528546573408736902689773837353230
6710257362162039290429286345422310946923
24508822802371766598887180875534990834
97784398312110411399228117882719595559
256587453138791958385322669884880519445
654154446348575773999744823583618165500
95901921198020244275494281586900804373
88843159763496871872461246590676363313
193266520784306405439696102316389982459
3508841282997350654860918157973492099602
2939124541834725383411198941815245072679
729099135055338817349655947673990304990
23883696387395697612202108832891261227
429099961909932812348935963737826976720
2759718494874149472861856778483124772956
8786631148745434371902493489970571348
816439068890734180479777831270269761473
3949983547748546550112426088606561581894
419917799308912246846238098418682749072
959962047383156821068569481573572819737
293056692339988615203495446980829209975
7996140966269574254625425565369292111480
8802127574103011727456828637538957531
35914248641278547840385450748389522850
2691375596933968363736393085337597126472
25068961957827969852280382860511621467
77560155232436582776324516311070877956
7600262651281773494420718021462224067344
6754829492143436527368371599451213896
160684434373237337242427406949530163509
8920364258385084195140719860825483789530
3019195862029750126822986339134716199349
1697519655354476642899657444351537824946
2724931511948417466923415085523605312
2146375433653317495636473546782649484837
3149882814809694654863772794110377963208
20119324989606033873492671538918372231
62529809876352442854995299108498456684
381487901484472831117175365510519185884
5561887618115865615433779503169061507082
8959505320544111810927379694265813667362
925499254993776594525480787489933705783
87109188254713648540503982691272585582
346358394470624214959201407287113448805
7534564742133547833212799303228260408531
7904555341983815295125666422182935537643
4348740713376231541262091525177344448
339293856312170889341886479667619993610
374321789430925263543783917985546091651
417152082651657295591489648596623003
5370452994713868849029857689383992167239
54004984728113396835552429384728143975
22372159691548151776472535978472128868
697961343423820781953139507430293972776
9852537512715297806325559695952345994546
394911714604869297063023610210719431283
942873776615993385472262583485326751939
2503868421524973280392909945208447246764
914936791118100715469399096452563807590
647916819020426134251287281131782366933
3217392533704119793599735681965787793739
1336789599837474997031876822383339688751
21036544775647044829866206834313554301
98804475257053379163268113420536018
861892751962990792017808407854324706825
2481879796152277558888244344165439179942
917510421026948438297412571361836121943
681399096048397658541830850091675258763
703285772975520381512781060665364108812
469869506385573072483906421509234224846
7384768607981260312873290481077865862
37632570514328169934019862533119438571
815272305854528249506726597235072756440
48279148790698082260717750607210677127
8826519092499851350424223104349736133429
8793609224592454805722587127399450911845
78151523023829625294098692121640661137
951348013997353478366872543293606502593
50659091774341904720310132340079272271
699961959939834842052241346885174976779
363354031061276885197989996516126976
987058217632752332161663206788468163721
8118915990817290546363698176270762132780
2915208788894807827325196610185168972335
386073592673025388414678780962764001611
4289780487078038706258071221575163511345
656122317490492933017264685326727606688
625670912893713410680707766811569336039
222790558756785993448589769678170919059
5397887810142487125310509886626485054488
24725994768012456271116439585567806572
21135071512456235978373878881839389935
250258761083637793595323599737890434949
64313604754411537327833662340052175040
3865126264322720163379948888671734332049
23986639579057641331399066267599786172
858795257226305491343098774534431951545
369058206658233114161398709328596505534
810528795952228582573474394821247562033
8709756240746208742595773183380922112955
592734811405259176414678561669490843191
135648686392449243218717155939236024951
9955617348360222081184181777922332808
933673347595877386537719635856722905
2347874217539523925826917841187458534149
4310108629836389854452639406925797314908
1852398998393710145469428647989821021160
632165114394431388070676366992278242361
45767014686841687643828892347328523
979607228282349909613095464338176617823
192974859497498535592192499845499605293
42469482835439922144146677579620875123
20120092020274030547006389672326443797
5969799565246680987987272619642289121
15790061521848352229523635724720312046
3232142410735553205443786676525678707
800088031839414334906155823023637042479
3892809793230669647532944460128946966
609581610244008534945842045807237424442
325733657433853420124332547526749936050
2587829643709329784859377534840281975722
9484325248870393197736128255883339584
328140084782548249786282349243980199379
5198747376788818815760613286435991099881
79962335686316713892527985618428628622
97003074997214773058130285725194419075
49573852403990997576122162568367567747
232749598520842284275643493901791914449
3362217049104188628814684853719934022430
421056792256665120383445637116269965884
515729938190291562939978509222861684463
5885751880408178606834833106917611214242
143452236046082448290782348859014755307
85296680645261399916117210222977384985
238423049938169511046757727859529059673
10887645896975471548242667432439653092
221076809438650112919826143101227281769
4171696931044217958586161681673745902167
78671901916249927534219299388223612459
1202481383334300452257892513974453396342
800847751207821730081542564582777153905
9531144560169778998843253939965326669151
229667678425982758436157829577389626894
88872503507787391547880635386284248818
259561685931601427935412265583832723987
97565573770583334243745088194199585529
4651522277691041821196878079269522795177
661588763098710974630735265383590832953
687127179263519789225003819572816646360
7256188156984557259070573419246728671314
1904538149898368570270592845484515052989
6558136010431685127248765622263848325889
7144437346217072116499246261938362142199
40674155079462648150665164574835692571
3926663299748215282249467201480944578685
446882044066667984157287913047857463
4187717676097095901974349616692967664
6948986366009797426115184001244972085326
6909225875516014620417338622754426105566
8130311028817284658376823525250646035114
664297677222997527350457631719925823053
340910149436590837335831108054512326122
4703563771022802983328485879496245255785
1995619894114416556754201563301778263176
791051791595909403631149977593680108604
10177817574373714200111754318147176681
4544639236556203540176673016291013476668
7431290242895208420419715646722559794775
281118339443403218860273523507448389296
9723852041585370756459554126110638803858
5165961861175342846034941331779871313773
3415299086613551886946553640724222195505
402847616322627151083938016387140762177
79397959170115919174868764729050631194
678243332524572989632042256805488704639
493158358465187488461081682360669174693
266935661148696728342996934970313041958
607631075732206943951443347273298792561
264010749498319288265057950165313979774
9237178949398077627791296439225944603101
75732389992383342652448476595209942515
3890256924452018868781022223740016345464
1306658461149213434054224907994692474533
8341437869135609556690106750858647955006
5551369167493961968405291226144154034
407354511067605195336133709667819046406
26665288606448663681775312430396047091
945343162781770756286330161451410324477
41087462306435363472769416415438839393
319793848009681857456724255332269962728
77964461252704826586862971505414656140
69092846409261330858422800633775747294
115369551291087117176552564593397726043
9608762124672558924493133022277884495336
1614375954499773860457383924654629011814
6926218812457799281822121710585911814716
6527495133362909555742586491190145324417
8394401696976174929082036669479371288766
963195704924785195632092592569388426912
179571649876552082961823121601716051611
43304997544866829972451685706049414760
9910805098587296763013019074110597842624
5156423277083409819342604750529220416490
9702378214663216417730462009189693254906
32453772280553638855497079357233002916
9695688413824721737287365321844038863896
32045019980270389287671607856813585300
154495864285220063554717145146264112043
4142729849591289841230728282189683429684
918589268683135743775285136435089958430
3973475872713535592026168707599238798946
5997463320591720859192463657964691149
244738023941451923873713760287538745165
396820454614147322901271802363528881349
7496547464483873439465905229288876732095
2261126098197357100979398588506687555163
97963684145147828017769930789697512654
755023734813873796816392767356546602861
731137576099611110385131227302419896644
616973557729613973212005743552488659295
8149444662455986639268083456777575298418
2011795959009413705167006801483734805050
4020759626262983847728967026194623864633
3387300415964936741357115097302760762035
379437012562113582187015504620947643148
8088305193401262546311036546323737410
365523734146619256327701683211459729410
33672235296436598298829941763826754686
5534657979401763529210643405269367253639
7858972568282992259533347672624072481693
5727280587802304101813154036728831618804
7979474012923352667246016536223315679034
992456783401180345097525695891632512038
79053467236856704926613460243860625022
62931265901322203534933377538697472651
3881836767008606381245388510179367538061
566620549074233474275834213698265658001
949034679493161306065726943163079149046
961940092957931443169561277276698213006
8894636818569910996914719456335698964973
877811616006887445502455415122835061962
2352163492281530771298019398836696501532
8750397561143574963494331054685636856129
121132672815757664222884793936895023081
90101869724042162014132194628266418387
4859505766879107640234565790596395091009
21551905992676755331043377955120803791
833589611154514110990613630448163067283
41868960169661054896439291312983677689
8021121962598207987190429847711450431281
3298512112341336703752449923385218382992
141150956266712024784338353525037595600
318825106528535461247352304696303438
397483595457501950317517700176833923106
4737350639915882180219044330378423787577
1164596523308305843898655784468155897924
136719522683986201272398877914650894409
2499267329008567148867139338421851382383
163949083463117764632347434771623071721
1736579453744483391485149602706880502906
8075759458821581861727661453836651967413
3317401354668866128364248777320232544138
9084311149617460321478512915317146281928
9378876727131262724443216193984066635307
78122092216668737638903868187687664
4101523028061644172837909984459256119245
62654911317531519617572610616513247006
5576851574366405103312824734277375139221
2719865494895623461449382453469823009
6400618379275191810652098886418549038499
650251386933334590688744910775227927740
45577530292967018569583342118113164074
1641778341045447300657987000999711634184
1635162932814159666319539603616374751780
797333197095143482140856502279023078541
6691980455062595696731328351357983050
388351296564152730152908615886484116930
598685908132937175149994284478650936211
57596783239255281366802321877683031453
28406714408397398897571306536374249264
4510186272041957276558272103479959147768
1502392541677005515586101606636555492476
238595824924230016945227630620849488155
3580947559107188790794635145170042153206
9525756442866542460312804474918555128243
725416147632907856554224261781653453212
56810445822448117042147755406038431289
967778317369021927796413260528672907522
366081724046320141045592960959992531414
735913557286959784282495777891064127570
3839260873820455329409373523724333769
9585784864053510139468239971229969336542
850443226112135810515412495264710481206
568718105691166710496588308515524406
4604813441351677654688013729269584141398
3096537978476076503967299725537346204870
8637287673092061208366118517512750336198
6287916558827624323454056406270714752985
525542814893424816984539951970566319622
852177163887197822117192425291212151179
3957960197534899111943601801885051473415
917463564611224062738041777665326418057
4395753852538999309468533916769077159
722187489534530970952364693361085459374
127258456292717497813687972959916904283
245035893463570646192071877779977771714
1977800753764657640725884974849114858875
1769334682933749438748065476618672334075
1380416666092459544034383684897923655662
67846285791786833377200565579551171576
66327338478125255104122462852157485442
2815779943703019264356892200493162277401
5030510270158597660017449095536793098777
4631134384694650373050079924553690435546
2871976450263899729569306470805012822595
1679371625405690680739711976169861902109
37871098388893409630253833091996995690
3665366130358166216711732213307589005629
337232624965735413927736067530514755373
90353121387053042078838696621557722510
342417679407703942758177971252285872544
962772509558104051127984620167829139978
2245461430709764281185455661797779526710
415326450051998423973882615784396829389
9154381770646434903543529779041327235
6470327562856304160088854216885676098746
5222883914457870541365488984855461177411
600919878275908659245949180547875098101
423781874899701974173689098483980969086
61541042891796731327941206761699282321
571756583932677480092263874208733578681
423358652775894570310841279907582334131
519573636742624969447081172903125347351
754632309185289720991255349251539505030
2927458884343793597014062667454873193
54892363878288916432839288620631144149
221720178874934251898815586261382215436
8388296044143426927522417146160304785
494499145564519259091620867318411617184
45225366864582208155416296691539826036
6590774664655989709947366932165975821357
7035269330524979389034371196651369011
9294925361929405716796633767130181474867
5011927845057233446589515634514743046735
260110359752941778238489557945676881005
10337703333167642050657989159121754642
456711172044764269581552755960267344263
881824096141557017685256347441887357946
97672119925367115798467755763325664123
875482987198928073506170096973387830
2197999859187049268014475806726168207470
412548317023442584091635476985890064177
299834794408449252235219296779247446651
5709303984378130276139422944172270508763
633892224511549289525955968836825564432
31795902603919248134716393695061554591
299854559383930266084305475601226738006
979457772219238287392523936246857306984
95837992283261337759807362536301944569
4181908350931740261862689186320983657449
46898248647933937672817383714937654928
6171296284376217435471682630430118561956
6540975783486696917946562292391147115
397248366791790216973549931784471498
5634900139007148675166896578471621109908
5389184658244318808086687207461843319858
3202115871707955761222907498790927201436
734496061498574428436156432164115452570
6089321081318270523892358217547714888512
43745131673960816694681557761358614935
83949049613452717009087697021219738767
7610572619307000143412444248751932328301
286723534894901839440713003805397985269
9069382919875491712058681551872789486987
410271973968927117431382834627870484446
134549297999175945758167477423090564680
4846840054699163344337929988589219127
5146249295915870205996262944495478425562
1616944948675750239934523076885246805822
1824793034293358896280257043645838901322
2985843812647745999660079365524211909966
949907158621364726560131280594808336
563131338580896126969578054722948029868
542069525049815597105747662742140759
950741725716436926273693248334539589925
1373664427481274106150277363460846257400
19291624714484529522534531912530265538
9949734047870616836179336767079107299
765735114755087565910475410331196521969
5009641144408126815415795329807652149208
2356568210367997493796531927902878583008
499924539372257074773642198495662556241
24712654362928904112172941473612702793
886715213030589216349245506603739641514
1471610527477944435235388603867694454894
4070846066077720463115242645976694076568
9387942051548293748654382114443620387026
65652295909827757731736815907901484531
43522432156943598358671394602270211024
3085795122159161169131565110769619993168
7822391745675160931619972084952579179735
264742779687564521336660746289821899876
480793166783536975294813244248877598212
53543945997499363280669084524631241484
593599773713544553698808388624312774232
44521312346589273894698860655161604374
868495112634837640767002508728724691488
92513209347261787236113702118971863395
46736282633495255012910463281468094474
9239254366838609671769916756306256376900
736950130284948683392017591144398898225
436556847738802304198741440202819104661
282419072129857512854693705335542689044
870718185515369180589422345923828506942
9527208679071205779632459746565635454690
164565850686664796259864041463287937
5151281689562301946973353374301741069484
46821699892271620233609726597692035402
96037261906476296588566967632908366271
5797294282466693884455437376714247239
60733113445663811256256593330105435863
3471510848561341287999712786509449728925
948288143598655313782005214907424748808
704434964890186848093223260963196128465
5570271792179951549388853334883885024117
830169718375729629695144510511759327450
78689098804548466104258060763742098302
224486148282952113379181215837635719011
923472917754145196659454814753353204999
8638236883707766156322716113527060692232
305259118624780275112172572984931501354
58387031695815349863958119881391375437
9445396530649222372371436613517529342906
490261961472194565392614931493538383964
9966353239099182969751133466048915988
81053892127946791170196362331326512126
8212369364116136382916314107242983247013
745340259684573479535303922358861031168
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]