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
74892785153919822251686936589372669832
42342710616436071304777030829351931767
8266451452839930911520692800754481247949
3917743447288242893798414790182071964121
3887659645296690112485676516307679117580
279167926551869171664529602626026288009
4227720578696567495470097617932247674688
319069885019851257916916945189696283987
807941532698683285918516433736980621688
805147925762275752563970964349579835
303419228192876220960853372681685916850
645471632056666433802412644834994604518
5583802838053981671119502253121613286
97624006841817143941995197228846183040
48554881231780922983802955528018023475
414133019770331852851062457571644467977
1559353968179729105180075927298157171084
3861792918692983563299875733826003776
891751287249724619185917303687633608335
41922518601351443257130868561237931793
2357977031897432482750828290928986476286
356476581680932740744437242815764316380
40046885382879339676961522140759954607
109141697697797748982239138809084239361
3027174484742754806749018733537691787
5661279850534861616962681756804675187082
386545428321771627826276102939764273836
519580364831585661684790598717719952029
6358458263355372653541295780446548738045
4664862383605932397840086073608438302513
4577978214119693699985713731891342691476
518930273185141932461946224624607866562
3081134465062367309741495863408038745198
546423228307343043669663957356794224216
70064567437239172021281110774296246339
3383823253485912807192518043738124129133
954666802040146212695575457909832488461
359597566753679955479451623399257957516
707986539531164895271582904877879544706
1328969318388624798516851162876034116610
8137312138954936616722931870697564864023
69183448695898851322246565068746207167
590221938753425472182854301135224578460
8608398461181553460579156815319764353216
8793569147662012769149731187461856526293
91870143795126927913382501802929229714
4807338350626021982672521355250256459973
786296789249769219104571435582651341889
1915690351135255699040987239801551104114
6162563573911170534426767622622055125211
93864040404842478983318913232266056921
8142987693127868600324925580839342016655
5288255330724687945933834588549344084286
932989404281867846110406967413649333738
9646946280529771230467029161185485091987
4829678687981973301278369202693610912543
801139535904222012454642781961366459665
2146353608384731119182102632829944552
6377514654115575247287245095106917614371
557066726455732311149156512542780105151
2424165856795381433710723279807327805032
2512387269909061343318087156830683814787
6123144537741639448984206332230699998001
9000190188422547300320413734591751433885
4321616722809449899426489053470244995533
4800338467402672180753191249216089197327
4547681836183255470139058082507223935
974210162921889591591366013991543645718
910814661415496832644411478853319318180
9791577661847159348152644926925036684
27976318646667014552253574843130856494
7716362633702619532992795029312255777660
936052204664916949181975589027110344337
159384677707131578414354127149939701292
4346301855698999111050053433769572922676
69136045321960598074813917878715001112
8035468273658818317362117365013656923
254540846095236568897601280555313359691
470616689912312259180438306661171264168
7835619164105686832029559445928528905517
815452664037167259947357459970197698627
1110518143793129628747918434466718145290
536884873829400818684667727095273619085
410431672682521938930926540915979482982
3960551056951744753944967713571255783791
8337287443232700987568751352819110578432
531399514290606451651715075846881068025
6244323882815650477924563502812479169784
385174831925298466638793420522523606819
1834929527936184741868087021277495876317
1640683827776278383795082018890052834444
8741475563603721170926395188507271922606
91091403482983046282939521549141629737
53842065254173719769791671442855989100
5155532368221586672225665987153768425338
8818368152277052266982792642193302962
621689891853540422682318893429053299778
2868909699794116170379685914447631544549
90148076386162382468161614736171954826
190154967746903796535513696968090735382
63586222315615046452244574397657015281
697926844613982429138923203445924352329
696253185046487422939697803445456325855
790462952776819763751944156878979558656
40775427621999693307656412772629007088
198155313842424105755842563650752764128
3095141053539543710524405715331782838945
912237243008499010228323584653817902034
5916169517321226125345594437502626688626
828121031343734404849473036450880216847
700625238884690562648756919569144259172
795667887155416536073899243773053987953
1981598549167210411646213039169264589160
9548406548798766923257580936063182822
8436925872078965580977145421289031727
782663855209121943016962177650854632970
330232535410325649179404308755392516983
6683710281916518035891681927697362714
55732637404218543509305422853254436039
919527782029838349538052222595417649146
7594313559491251104027702205135512273788
4328350313809361122236695957722349559739
327393555847209421069429304640150232728
481044567965900934762162278451532348539
208222245331080759039652963189471793862
594761556941130285454645699782678662986
69866739555580746744800814580568015685
2551593719917061882272545193658273935781
5625667432441350198821175485844436069028
4345184291293598134875278451273132598047
6363118345109072277328113804800756628826
94385751531336549785835611164296532991
43506001841912910734708278735127826140
416163022330547971479474556502790556919
9502700538631474131410917539743560771623
5396423323135946763419142567615385587
587221367772522181618909319477569599272
689063945680367478380784792284009255
63575375439752253987693985093099492514
269322894785509170675809856241682685613
930432861804470383558887424490821286831
364298363731011238488483677675457616
5689967857283169122294412826965320498763
79204550770017963924203324892626635176
506418735827711267279344342819543129979
7793772546064444382921589066423118387129
3717921318533289713311277401565492141
63076602725670551437925254969721880394
103472975000619459822407126926711519561
725057916290657135146262874861416955790
5655383234467129130418513462972669043717
557450214682410984649115397834833265842
634966579333930241380459363882262294341
861298682479930746413611594707160058070
8718850867167232190473433473482718739545
878954197456394557222510404974431283434
3590139673547888897362555218234332728338
2228343727966732951217067025768188126559
532646936975705871966215307441395712761
3873470366972916609324068375747268259975
9747263318398567618320558691429217044724
66834301824616061264073424424196245224
551868861483944456775606696527196051467
60229097638090295028012957960717076325
201070884669363377425739682429499644337
525115821048674890628294174409756545316
29399184644931037665261601223482061147
8977920372102173518639039213729116602
1990374884055129114878053743853420588877
612575515386794960279893493369968714242
9483487298728189559944589203501286867108
883495443714168624592608275283656440805
4549270263691762744973271390263389074472
2946307074036829775252031032390862873852
57679018742497294215044956663767032278
66324567267324222395521497529747726614
132182057800929744285527481699351853624
221699111263690431861383708129082748136
7741257814643780453812608244760997579944
450473548219253685853811281865465087531
198496792069287691777951452645392954120
941876255903339255294590412462094425736
8097507667698798422139135490823159127495
248598165270597667245279721988388793982
19176694843640324597628696695828737356
7292490059128553642587131291769636635891
7577835198493065427043882802972576799901
99894672645983858644909948760291363224
906742037787527752329972426212414224458
4963423671384894460240711977426820602524
207142767201818872986156603728719308014
807332392731348423771541179432516616810
97397319952177912411793391470270506966
733950018156762822929846537723694812253
8503379439357059872782258388286056321
3381887064031791984198474963401747104504
2219556974273324729423457507406816511873
799264012515210827216351665396218767782
6487839486054981952791685614376839525093
7204178411536137498428683865713243853706
5382406817798131191490084605867485438846
1701593621379911741074831880513248924128
919559934415527447996876548554146379335
36051553141663731799448844817085564441
5025657441830255941020486752333067376
1289306550571060944298435334988972408845
9808505813188666239344246599733038257002
8188149099406916154030075781628915636267
7524786136653012183615118544253472197
5162163956505866628661287295534863013474
144778133018996140965584639872860634474
913512366189293470857490272758773103916
5945401333843865744094464874443191373
8057358050258801588580647911774833506217
25916044587932776357723445972539242082
804460634117309484992643687500720605613
4641991046399020456940134508724338075135
40753010868448435652496557977711276184
732662674883563261788279466537950175932
3338218354649661392140742968566540969006
798978017411448487117086881702473512714
2450508075606215537249185727140878477162
94993392414140053034524487445715075736
571686295883138130809356120591488028170
24754902594506799616626939284125048328
442826264157884870585517791660460021282
41177434723610558189918533295413476927
84463835845957599186545151553257701665
5638131979803478728039275620683341127815
9942770455678915425644752442374691984910
5577790574041544430584849152226392300
1800412322277264564589724835566716178337
893180843938722232640409920560251795285
6744596686386186490421122658028898144
1198279757729311962434566551451100298
637668007735458145889135844420472707452
67622366960779061951057358955623086681
6040745591156067918929461188249463665365
260723526792605069773174410155845281300
2049299294248420751162072491294685703431
898888816949919100319846312330362835629
490762024979317451734288817865113775290
45255739621048786891254617348210567003
4444838268843246754643331670313270298703
28014229442323214742796437889930788616
522337267717314340949807874964064649759
43916138511949378311506091315434098165
1355484638989697677436251870296177804844
731385527991553230479295991848195652865
79512795100880385505038867216764945127
6797934961118149854488374654863343931
8409600535892212738436588105180021712913
672067528759990038116424353562269007690
8384872887987821309166530544384037701
90216631396200610582634794919822514730
549073072846678512026359001879450618374
2494561669278285836688056140925865408140
4898906186084038303059051557195724723376
565783526086603817775011312478501265767
970374513916478651878485620265649156972
123110354121360695350642301150548616663
270465551452757931471864109101297934658
33257329655017837249022824967362245798
900983328440712217036662718471903605353
42555874962076668396258513165894301617
56556373653199477876048333761914347743
4856979651082417395356346993691931437076
17251451621430182892223566345801905771
7024529422069024060701825620022989356
369658423752458102165279369102475136700
6219199048534659509945047827924880647
609881491257335989672450678574019756643
712977245873994798395294675518611583116
569875474686268952677871308296526927067
4933773247253214737660102831364493011728
7478388191195588498667969595672684504070
72622137454007144175425307288076691725
21348495339157271471202695582953467431
430454737224917364877486570921341437873
6783809967236217796721706300849732903
419347893075806468983721690377167629822
830731418071964460457848744756335806284
38496802669396434120176493502450496671
221540294716512149028209203797224437271
39153455126396299730331009518356007247
666884731710135513225564893776319239254
783614812484735101512649131867487637603
7027591941143525377739821299317452487702
9506161432348657494414448512999505607
409165004907191519388959448902628157663
3634747396368122021965659733541324866
29699659656145617767856049376992867808
9819949745331739703792983166267166916
3887544766943360141885245545363619427
9624514939609352963222955306878052737329
599038773468131150667867763837678882074
2369316631102939933842046757789085559
9817790633462105681671438982712860544160
523065905589274578053684872938346994277
460478822153661794513199645820226579837
881112848492018230745114837928684868525
904323868591121563236618158922966844664
965648357146568421879770872675602778
27864191976564784595308213818951601757
928068163165773873017086314334881287949
561550179110444793511139489666823284323
909379045471679545397936122161458217975
1965145544372743988165822711433127406863
5129841245336801100332153202739556869985
36766568826362293421987469666275477179
784669872612502864028846422988666682803
162953637886699789396662461973165158567
435848591425682966910348378985288196
357526303796273769014460891797647654979
8280307891274016988872612107919039905485
725760323668770434758139163677222243610
256293418108357330687493584566045846963
1474755830503711478345693031486519618
6547530929575597420739577342306647639946
27723706980466715665508594436718679253
48879211977692662080526933835099549031
1505419884388747483560675995757130763475
4845604841821814499189701069900284876001
130938657223220724043176649714868803
6026169230962644304929541469186742961499
875416935619313469282736735534592447784
27543645853231025477524279832049852000
93536853200281119559195089480374761287
5719489444916483252246047306236540782941
72442356206997547570649266862497631525
3761774673057168803014476237445473043172
5010955914669012844996797306246114554114
6755990661891455995823100216191232484
811174242249480684070795139794011051037
6788539226738224489641418064412998483911
758466717795494983342087435715043375006
450610199556510828382728963237250678426
859130132652867105548025228257566398436
6179737928332627307888609419386678196199
8098657965952397904334151253551053632213
80450287910964720621490237739710325196
3788822932786206210999721372593164398403
7508940480176217400010368563650386437586
487778762610957366313035664254541989470
586668928457993946584138906541258160987
978171358955005982399925226642982966604
6053484771766989755522214803487391195408
253562556659858728405873808824127301145
571518774933903473826905560281065387289
8571906876469128119637717850514037647547
475836354318339634245042739412364498053
41842590543526843983759250648367862249
77346713200464155353849938005056745171
267981422933297211701190544900533696957
7504145835117702513924696990255212893181
72528269995095219980123465596123684505
8059737992371755576521299659275386879
746280853694378511961145033058684122
9895840722892768543563801448295690447561
22367256406646568357544019571441569155
7846178490947126871924373918107377144742
558681399703700995807378225020555068518
707093350483661937310522577365423496761
60025443391854342878707793755999813953
52851629158231723868981695889658162468
932820756305420217375191998956240697379
9141921167413658301253589234522181055698
68734388826842345803748798016583103381
2220161723009651636797667074139649265675
52892892508567835593535460012381798020
4549532758918907868660852682198925997914
9512722076716111112326603914972627975687
9158386258339109949261789464484585606797
3067392580641173808322003848826288047934
6779894466313269846962543188803344648
67657272420352854904672494491905406920
1672135577563282778460671500471446673797
8101536870304154919134852326582299743478
692969598479644637259731626092528418
9367698322176385750089135356408303659
8998839761038745410440035014553382703974
4642363375595673650646605427738194913785
552022845354883056135154126820557974728
8907233836381938106827283032209687017801
1436514216604214655382587618581066608665
971530415978828991643538559666837113186
2491623676054261690168964674688736539233
43716023921468336140140577539799073
197588674364385163077287544900939092501
4387997253553161326015684950366845351967
410065077780568380617825832045461486733
7112442818483421554071019102755428002306
222087129481267329746953973305086972921
686666005259706181168010436034592807112
699433280763007973952182273250379439722
973353075821212211321829841459296686733
56183908089343342759684487289643715087
2726782372184231891424353206833787091426
876244864177992586986404927226761492687
85329897345715517655602391734429351331
81078178526287126543264505298463794505
78898689086139993978513826862827481851
79231034106275591526189672824141156361
2371471426865861810764718464175034038346
312398666467927326205376128074324771433
495334526259470958607000167951674677314
1223773343497599674348607746236551501140
5359284714141375941126197837450146515376
54453053768113887753229163081018544147
85792577100312215146410298724796019608
81027061331660902026492244827685046109
490124055437885661057727653535270311049
31782141225413187568589820549442158478
5524529896969098729636703702765066746624
4603547242253716849433131258814137112600
3171636967172628886045272356960490274963
1703554474097752865550483834181374574514
493764282907586239022400743112195824845
254951674629224087936639908559960026375
9284349367879772233271184566297569921735
84742560842342358912703225151223982165
157432374248776447549885745795245919379
6349648093698273192843559758545952705
1246256480212806140378572797743694834057
595588553066689214753878725564949295343
28389428914840554495879994262889167704
110182893690655524033677090722677404839
8560828471418547337213169328618245725974
514642912026804151279078805122758584368
7777662018964316346711101692912485299412
3615336487901668977292676787620859761
97486590487799461135447712325973969184
4957195583099468401159069489162164986486
857478144422385313877274167454415406546
3059594013204747201870558347788991128899
7181114263365646971216701393169869068391
659487817149729455587034652818544943189
466174902712760142438462368038599468332
308762854533318197258482084926974314
79767832375569642808396422809522464395
3736943796793029212537042044992360272781
7582688544925666835681183272029281562
171126948091698163033726726215150465876
2443412834715046131456376560558193546170
8218778072104112370239943370832330031126
1795215459353798384787253268966912091935
506347643922411994985968679426886294750
621549862187879848447704896567368416094
8892744845760726588184045467070871025
702484876651506452345004412372649408356
1528194262956972107716149697529239443041
307252189336734961239448319113430238497
9358962280933121743337629195508843532300
9647764678603576697336041740161999963
4904412812155505467297625211204671351302
186756525968129290455201782975193396026
8974743710565958612173253519353770989341
309081815925940121845449837617666415
17422696175366830429878973440917768274
64385476144495157843288532257038089689
75085974860547167175233320136557773647
7031403612688751607373398369477951262
4864290992302389766020606089306254772546
97091810966179729287352594646689692274
405182592579510920957636713313984744704
56857791389120268287229828679998303095
1617276127611461887712258689505970037965
355086776123696159466926949379137119664
2970479556177701952671631632759494646295
337466935914762170616632909180371154397
316714809463449432543032444567794803323
929775652256209479510172579839197211196
5888847930516189998839784612988085658403
526591963273032612270437893530554513212
2127661457419784946187546040958347683806
771924555565965418565494107411117297559
650558835220450233323008623380917997725
4096438788805460428762449644683428047222
12918603673154673441242060115618516656
335372395934358292177846801294263097128
730665695255028349658663908658515317949
651963076745842337811530327467799685820
991964482103188951096079749893499956
558863702255595023321936724300661548653
96209312354576286969454133352541609991
870910080501799479946294191268325231291
904688134753584710152400206589196817
2882163491168769252448188836220662482393
6555184139343425529289803606378135566146
300261315716774560657373930848937143355
7878950155127236257678049077625288753485
819780386202782882183540833110893759258
7780513276885650456083268004739489481232
736791586333515893461922271017608045
956768941386161190334692835771356334615
9886222681876986802125953284506218378025
17862992713968961163948428611774517242
190599041282709480698551484273297915954
7573433667888839215714922227237611104741
567485682520298997229109220609684424718
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]