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
3060999099519071451179398821534846658453
8095226367696030811782117706618933953
446642206879975819127981869645427185467
8709171163482596121846461672454833116886
429178407988244942174644790747655256535
1874495070354633485713081276225571598412
4945963784955764841086746422912022605599
46951858421525117339337749141087976809
69162451584610689563946247139388785688
4459687295943133403215421784335532374947
698982774223905762339509245132420621931
8535573416735609976637528771052991956
756235709910849451139170150789246289374
481253308664278583794881903887010016908
487936045515192988497276235884648034034
80634743766895066508510585454623328277
397501540761646417455864009506070411233
936767043637760636283954683975243225062
36198699825592396611377585051017478339
17821458795345214827348960286195039184
4843621974369890842657997575620187312035
2061357122859986895512234176107888037153
2315647112907166246542803442927692744086
289245711942646945601235892480895815562
60255597420488625087851425641011332666
9219261650437925636291565503924388847240
332381131704482569544313493388622534463
7915504943733303522160693439438464636396
6697495057977358799920595632928477749831
70555018181970317848633311494261065502
8085871373297286236367178684355245189470
882599065379199265089619372889031811856
8874472665073662919682428748598981316573
868712927455970551045364379554867675579
5383456798939733192014257593496569777914
64312663732729422715108354635543544337
986282987886191243232528760342837761422
416258386138578970611876772666187235328
431232067010675637416836718779647454729
25446459966015587337601251194777829065
55132833906590129261635422551885697314
44115508588435606413935473432098788110
5534185234962414706596072199839364799301
4051955164545380759871955832705162741176
745486302388358179645088755076597563592
3974671430631938036540741119074101974
270956472544865928952176274308281356876
1923145671282022706659388842895189477
1878912189601040852568527061457645885256
8451140026933504456839828006711929148553
908251308048134701819565226234428433748
2316757612678049711418214932019812674
294265808042123240342352774845036465437
5864385929131903429655797861696142898
8460795393725655962921292567420514022966
40326095301414702565885100318067417240
251393698806295821990238739722643325294
690314654992911341613324742773642177869
823067649101305947494079554730412726581
23165447863325039102459303856432267682
85712670316973169953257924632538178626
162497206460263785473384422661413936482
643768235110248165458188234287452152022
9462287165505896389769957130525069848010
9378906338028613168638527180795717312123
507941154131573188240946096727242450
562267976521446818681132379348931525821
1322383029394196640973247085263990652191
287125586283370971981778617592050106327
71882946658087604369467455862433899418
553841089578910657345583416257534800
7348479182751503434972861099153973643353
360485145940954534795382642917224524658
81362041182202887761094378074608009310
26271195154987830727939795432467625954
7570606286237696137741571739677275157509
45577243375375416947965484435155529712
329660656180393732383079746492621977586
8058472317949353499892687555919327486867
31951887730690454229479820396210878843
9449258679243108149344713657421676958803
38412935668187712571926545919690375435
5173980496360671928790163763847017228
1947209241447671061741708367787559881
3989235210267573338045707752634932636498
97869363606389449842983291647984445831
7047673585058515537946673297793462487559
591958099255235252735728254671358167096
5696361682618702122756204631435034731602
2701480574802404495818109948264450676591
349227569117967813684834361482568495278
765655493525339962233153156374350421635
770846657344435839422897624766378876590
2431925219281045813764995619516996219580
313166368962976138970688294577516909852
5911440124962902532685598464191493754
945281594353728361519631131707349172434
2694303757502121399174764945433455386715
334685351278970660226158300705913065167
25447028127662402484943783534614083919
6779193820869433713365120238278850310
3452533337168232448639625568851453948428
7634583630107858567090023230476386963467
4259833548611070777283971305883289008686
9104469292146935533695862866489872082152
1508414324094401769427912061488225359723
40933317329733155818926376896104589011
421820069696137448902628400521249507329
5201253613908761490515548354276214629042
26015911999198053819036360545975381085
3535780458858107591653716608988471988763
5268490284601332433892549287980017563222
3570589037252065414824233572102100006674
63589848748545368848135584690775545526
193946457707539925894683775539441224117
6733140884097577475815176208727538584516
1562932657416302811861396797536311374264
3462406850999445555625689898995620668991
2850487096641740932321153231225959004770
989479738612908677806764535676165112216
413890287165763743373444862952393291358
1356956651054428237471461012537614131700
243195325231836291329512969818350258386
2076163933649332827151656263887418461045
20291296253886297661082719372261412537
7029253445775890245578530643054925684
14439547818445182468279476234447697422
52977707514696780789169844372214434205
3292798587262226147610482114870137519437
7328303188783013292772905500447435112360
992328163090599573832932469243263174603
409795503851471146545394721652985521922
1260936283762685733433396329189566391870
64196661747116739462478636053414116799
3325689642575768163620945058572249665055
1376297884474281966678436469364161314607
811183307930959541985413469238458035243
959039947763538134111476640984037645340
57184386878727989224052475724431625830
8724624719479053391286675270716944344447
6405878027958094704687746224170177857425
1424178277815754712470888615810677639
5173870211241422705425868579267525253
7525758444411525115130283416777717244580
9241972147606848364149713856617263282176
194272043458192874337974493953724904946
859196045926520170891343549379872318241
840581694488398847478336777555116976000
26168084998982444659413825259251475406
798927795458762245856893932473912872812
22397458591404748885828949483092133818
25137437720965164294605345940063122758
764313591566814851543229348532577683115
5221326760611366421471626035140081044067
4173423411203005321271129312478736154597
898435798366474532678146780532914749740
5379921068712226827794267749923163531882
48403048668100777510145761783394168182
232782149617444994260462801761026551041
9958689543849511576925092264472715948545
73437279133142149586474304344776278234
422998597946919843115613753368552208563
896483733956691543773772176613396944523
9958810150228491890110884567139926638575
3931576650488883793913303661584111629523
2672695110674653196841459189618357346581
2615305198864471128572213343441019337837
6221829237153979657935224480942929384560
162359964772950198699805780758621874177
63379538952974651173855145489520567539
590850381532386712151649835543738061571
6979506952296507187320031143326187425683
5653519582059640289732743056724255483374
35986546020356034813734143969198299732
5964863449847237419733483079415678867292
155226087566319081306578611779510313982
8282933620236017137084434579374305229
908971246819326225013994477188955192533
187448852188412229354506047987285265615
475374599118444731329628215509980492
4373125174203947930616223219104285671291
174072707899826051742206460335278425631
8935623821558269941717934552179184568
6238496357306614930942978411244177774769
82961363827442532075760387952697127688
669266119932624551032767973424793865731
455433804997283725344676192877698428929
4430326137549782229640909947610836319053
6558130445599753487567441100538851698368
744897284811222208872564203226974738281
6133249817448822930969376544755215084115
6479171116691865005596881263228337143
8160152397264149874085275690946696308699
936545183944428437727708431666723205042
63511815543235646729705325069960278493
1993481555465080327081112470275043181775
133540429194348639089685027564349688730
377413527940590379169498170827063499698
1349537331701365220790127387632960285199
41883067594092406824767225321661343672
5238143197282711962336435178887610901659
5679464346629474623887733152476544398
7710882287945614847180689654802820344941
315185294048150482234374784810905204612
124941139808644827232636139198738039158
3160804991757330615430541652724819327
6672670275932814925343772523775466214369
2337869131703868554389938086463469028672
491833605145717335098700248238169503174
73195319761104727466348595409633329220
894732146734481888801796208972472076
19398175721363334348915578662939941187
7002520989133203123684755227610311949488
840846019741794661992789204216185409981
39727462707875305779903774811395115195
95332767785100100851413633300149141724
14796575144210668299134726775436842422
1987120023975647477571682024364449901212
42734815964223068739689596770793881896
5359943599419121841261868887185659933519
3854642748061727204053519627951836831760
956697924163539299226085097369081116430
6327187191256387822595801028605695562158
1777682349413289931384267070256496557092
8284182490188735358764347570648609267
270711748881150818625231339519662298686
2842737181954351794196882954367788533
3548349782981210364323593821062753918
39261482780204626825471747305260992591
537918404394395898778456607548568336991
4129195933589724728298871227122688917652
189284339872663224983891965310265589862
56684615578894688410872023735485845927
3950338357038730141171769318788863170
9593935027122789253661142099739919803622
741028504173806682586143695188620052525
23486352662127202969751634016398518328
6277973571858645883926769311285531619096
4987500996541272996258723012879319435
867517130457544505590257851779239588761
89448510453129209955641258343524813754
265563429259215783046190112590089217833
2081697315563029483398954476001571801
813148044836238780975327991370956835980
70913431875126829789467438819024394226
1153784561296158381437392066095689682
137823827664386279512593574410185563671
28275914702749441395946770563742987178
478350626684582972017376775196192379081
4971893738554836829210206394419424796356
667576666354619480589221740938620861699
82766998448319874447169784843121137925
91479383481084261468793559817352378123
3487561580499850451993756592401572534795
98987595818464551320755576259073685298
72525077509600250789498041123877802096
22636498334373314436502695538263595556
684448917063652421138048912434613359380
758461553835580330776956493768781788780
608187067402802349037004895255152314748
3365206317171639846443645774561383559
4119910313185492390072807158786231563
9284930328162354215486637168954393232
3756909219132797487827839917897138546702
4297140433884310349071107917418539619609
9151128067225508247215474632795917684313
7440203870488394752515168321385167652082
460027445277289845069592850725447458982
733940416422259753652123627597684342641
6058138910154420552544836263489675944618
2024618630926087742550746298284596929695
5552160442789906582926159984580033458261
38519581470376277482571827417785114565
933748423329747575513297298413855095
243028883962274892897452168576249835654
1496242285181859742966887143892117395548
6663561837316773328955935624207984161297
48374024212749118095070599526980586464
160162589940175503881305513558395968716
2339734173048457373267381439363015521457
9000905782042685647451459502366759457858
940352336551199579290443289949983739648
75128449729369621199017560246926288383
2460859692738988779151866044408023124990
2838151741822119310488646636220387573905
7555858179625693156531853112735629305315
22699164368075874225337782272186514010
51028661931803881016016919233299313870
5556361281731287516661386570167443534
23175433907263427182439543296674067472
3164411425211748755671626739352091716611
2688657531184626128940396145531015472648
430712595494501123712547425135112680976
827581302603845416111131293150314354567
686754516244916958769022848531168463725
9801614450228157980073284571630122948562
668892133743436569821379971195059116514
897439044260843347636425036432592708805
6520425584241105804621705906684563488
208356684833427837155547917172358737151
7920234544018972491488535008521749851704
870552587322750542245973123306795824992
8523831119001424374881708915618071452
6323889283325000798840217011617782984334
592288595025977293794251162469664794231
1142994052476826444142324417128482252944
231751065614449644051639962525792314815
3760761482382408169331209473274076765035
364828412900287658098253522129772094833
7209564429282585429944247092143628822416
4413670580518090736046527730923889183390
755165689194367839992083149938251949232
3947744948151169525437215057160045252411
564445859944751117528637265641743553986
2297675457841840406057749072865358052840
596360567453118910839689318878525045845
186156137333314100873531247068419631
953787999124560944488529794951537742490
82684654803577152218827012563444899541
183481933220472383126811041426672467498
591017538811654589555461155763985115729
170159433934484616282487185434931456895
693145275785552523754437733724335847034
467346829514009851647709494238050378240
452116936936450522064305734466331671473
757536199344995057868771873737444622653
64176154993657024811489837621411903304
3408131973622696738338679190830567915117
8063422996198337368969277479817739187565
549014876899292818626256153496078184962
4123926378854739966931061592311910937018
3612512974074740366749901328206722722154
5951425695436522809092511181213463425323
465850913238076467736491559257169206145
8871733425455166477744196884150533919869
14485466936097355681918264785728133264
4119810625905725333284267178111645304975
81512475796646357064548583235325235748
940163725081547562210159597397513218369
444227495543443510190777745736693597984
2638931230142368876997553652285294716056
815537395723477059866666186015889333605
1433208223843473312697594594978154618436
8740839031588435835870191136898980824007
6723742327306571980595673595378421535799
41643453529472867679903236783926639704
459063084238848254013078896542538259214
69918015896409197149128344733421825816
107681351056246420886354513714061880494
377184289629530414398534883755698737360
85251603396299336761782637984998841705
881010821641531146590622738483315699702
249644655920282480347153848976116849486
38776090584941402369548745461587077653
7431465556163036450634193307772435109
1581581263282813384495452876406487524217
43656354648458172956681208625910225420
51782867408955549219693917223896832493
62649711147939835869295396845568295332
52303298115526794646657641664149166803
687273579314317760807608184285129234897
91018469965165121529946451264232565845
466196985052254792333038433055834188236
95138861701730143562595303622829053614
941172134796874944129292845122716845966
998621082675484039664966608014691645114
4554976735805237428478317031126891445655
729579257679269864585204117532880112997
4583340033294316272049828486286544769219
5844122062045154918729865789577992116926
8189418297699921569290028168567823249
277288103429728498132279780484367906324
357888872932568748365043660371153068622
59413689148267681128358713882783423337
84335588817988062524264760448864523622
742864276177389976253913403578388967059
7391954289536056451524703957174029537855
6573515164413710892238952230747760023908
14244747007148454129093828560459987049
5904747169976826887957289796848789228866
9175911140583990974856073744646499582782
4468263150796747267877065231260599113191
743373167583574502794797051736284749012
95073495331197947409314830500616125511
321081724917785268664864478282008898785
3367880946173019477643891621700043954822
93576680811192454222806185857072496845
4477792650771548159811313176856845198180
32254665034167957529330406251268022425
68602853382543554472739625439015988236
38007346735584584738501179509825784723
3348453983063141968757781400193570954325
678458935633871822903746475244093457244
3013564040386399834837378572761254331440
562887192543940976117603618584836998934
41675655887572912431294473565078662483
692424535983022515589658500120313727323
55115020750650989085521494779429707742
3851752968993899429392837861544831399
12339648136016771135682395212007446964
16352149224654112266682454945546122041
64869201701523994824589612631619475947
7029205681005696242842125260499698324
7319914153114277224878093438307324272515
524461599118897602919447238133411914626
1021640275135914566884917211459594364348
588417053593220143739294126254535405347
585686330801449338771743554341616319854
359874124034450270015055304358119228344
9842915831325922424611841297855428429192
74661124324575480677836964852197283226
8692365669286175190279675741638565999320
270758862588481622368766278688731546483
2076459420649869447293234744569749652277
781185466984450675865822356771538511662
45774278311183839143538945292239312643
3506493242549081864069013864830523912240
441862648114282543249722755276051516322
80435546148953949978670415881597165313
380818097572815891820099824320045922834
700778515746511372931690329032316878354
461652159394281742292563357375910326619
4019224648328245275357772029478914234
6229782896543466887641265052401511619006
9026875834449311955821354036494522958055
65971941651527646766632209205863449155
406546261035333689238068956153664461840
73212805380911367352888856621478736402
6240115793274220435596333860585546169421
5684487164903515728576052370637939523
9344797680284372975119955462881548957
8475458856685544318360825790952257524173
631080402593353986308398762330102247748
39272632393280873860299074636477751181
8269292998516514348425579655257628979372
285954932458657220122434185465331187991
160948004008471736338052530867210623510
3925760693519473502242034126901827968249
234636060751879518125051620116567941408
7813780877539108387784418131983117995139
2942594339913998586978014266393466762239
998062643612398493589722141802031442315
8345498910954471919837858720693861723344
18801745203708666216174291823477076673
48392792814122589761299688491568634284
5232212367935292903045751467935979685719
173929661283933970367765450483719387990
9156264172763801713530667652973897658080
855915695180477819979726384525413561575
36363092235876294368046627887391438390
3036475074918118213989015059263342486794
2426960986714620733992985636631528465591
9094752826503178394723246333742075913467
8375941388704468151927416015240036429565
8591154324140516975665544043160003773
1525909697479175698833190894794686540
20975893908118526255899351785589585450
636441703687526970218177296104788581696
5952682180708846235591787709163141652000
880199167823240762394677674237239871507
3655913153464808469937422565753513877383
319332032962750600799572556212521184531
29026081495416912620342717883636018653
759966446437576043169129935053015652192
952212627040196691216027116570075207314
74976738838461057079506745267432511872
471979238228638153761588026680846662096
4144107171569375700244033132552771354015
790697646706440352568039793143858493435
386279215832776262869534158764467962924
4442494283652113563451695252812615511283
8055422358419497725442737072863021942941
1591796278793817487830835524846729032027
324067196516941426811351252892389067029
8005194922317390775125952219533415589
895718222609322501371288685590214213803
1834626519367329234390529719763347823410
30455624669246592517993098548770475501
553747393596506637986184154275585405822
6909410381561127896716994211200451053193
7467587573705734632862172872642737292617
781534101357427213864614252584097582
2789163257983994298343519102349646486224
8607109465325873193029399923410520616403
9739689559837921535115532591582957178209
917443952048406406159092132979072153568
13476796274665879478340895937234826967
870936498955182283514667460720282040699
224970725047148390542353575590858783636
3957228279829817518937323523067895056
2339444664161349539963504112396772011867
71433295127617341520233807760123991790
973436398238824951285640490496869227448
2967723082794996964964516513427267219
1836700094998861531182445802149287075631
51546384411953329119796679920568626853
668672891309135449118628366569355616
7152797884594880119077778642621634526135
41468939684727931164573242290883182213
5975351593613708753023502105997796747069
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]