var g_styleID = "ruizhi"; var g_logID = "200"; var g_comDesc = "Desc"; document.write ('

MS Excel ODBC



MS Excel odbc "Driver={Microsoft Excel Driver (*.xls)};"&"DBQ=" & Server.MapPath("test.xls"),是唯讀的,只能取用;如果要建立或更新,則必需使用server物件Excel.Application

Excel是辦公室最常用的軟體,2012年的總統大選,網友傳給我一個總統大選各組候選人,各縣市得票結果給我,vote2012.xls,我利用微軟提供的Excel ODBC
"Driver={Microsoft Excel Driver (*.xls)};"&"DBQ=" & Server.MapPath("test.xls")
輸出資料,製作了個FusionChartsFree的3D長條圖_VoteMSColumn3D.xml(長條圖顯示各組候選人在各縣市的得票百分比)

VoteMSColumn3D.xml
復制內容到剪貼板程序代碼 歌詞或引言或程序代碼

<graph xaxisname='Month' formatNumberScale='0' yaxisname='得票數' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='5' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='5' decimalPrecision='2' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='2012第13屆總統副總統選舉結果-得票百分比' subcaption='揭曉時間:101.01.12'>
    <categories font='Arial' fontSize='12' fontColor='000000'>
    <category name='合計' hoverText='合計'/>
    <category name='臺北市' hoverText='臺北市'/>
    <category name='新北市' hoverText='新北市'/>
    <category name='臺中市' hoverText='臺中市'/>
    <category name='臺南市' hoverText='臺南市'/>
    <category name='高雄市' hoverText='高雄市'/>
    <category name='宜蘭縣' hoverText='宜蘭縣'/>
    <category name='桃園縣' hoverText='桃園縣'/>
    <category name='新竹縣' hoverText='新竹縣'/>
    <category name='苗栗縣' hoverText='苗栗縣'/>
    <category name='彰化縣' hoverText='彰化縣'/>
    <category name='南投縣' hoverText='南投縣'/>
    <category name='雲林縣' hoverText='雲林縣'/>
    <category name='嘉義縣' hoverText='嘉義縣'/>
    <category name='屏東縣' hoverText='屏東縣'/>
    <category name='臺東縣' hoverText='臺東縣'/>
    <category name='花蓮縣' hoverText='花蓮縣'/>
    <category name='澎湖縣' hoverText='澎湖縣'/>
    <category name='基隆市' hoverText='基隆市'/>
    <category name='新竹市' hoverText='新竹市'/>
    <category name='嘉義市' hoverText='嘉義市'/>
    <category name='金門縣' hoverText='金門縣'/>
    <category name='連江縣' hoverText='連江縣'/>
  </categories>
  <dataset seriesname='蔡英文蘇嘉全' color='AFD8F8'>
    <set value='45.6300646121232' />
    <set value='39.5434122874253' />
    <set value='43.4571508303023' />
    <set value='44.6801395563162' />
    <set value='57.7220338487086' />
    <set value='53.424742058499' />
    <set value='52.5277492771197' />
    <set value='39.852656109885' />
    <set value='30.9305603904362' />
    <set value='33.1817154393255' />
    <set value='46.4930821393416' />
    <set value='42.3664227244876' />
    <set value='55.8103592967312' />
    <set value='58.5793468120204' />
    <set value='55.13223970539' />
    <set value='30.5030441886575' />
    <set value='25.9408705530148' />
    <set value='45.654281810569' />
    <set value='36.7680427378471' />
    <set value='39.4891207966714' />
    <set value='51.042664750346' />
    <set value='8.21688669291541' />
    <set value='8.03228285933897' />
  </dataset>
  <dataset seriesname='馬英九吳敦義' color='F6BD0F'>
    <set value='51.6023784090598' />
    <set value='57.8737233054782' />
    <set value='53.7277015716675' />
    <set value='52.1581199394378' />
    <set value='39.8029576470502' />
    <set value='44.1876657504017' />
    <set value='44.8869819357647' />
    <set value='57.2005555824039' />
    <set value='65.7610025608592' />
    <set value='63.8467183344119' />
    <set value='50.5807721754348' />
    <set value='54.6298527397024' />
    <set value='41.6714882171731' />
    <set value='39.0434285751179' />
    <set value='42.9276359172576' />
    <set value='66.4728487581353' />
    <set value='70.2968305338453' />
    <set value='49.7575917845652' />
    <set value='59.2885960007209' />
    <set value='57.434690675943' />
    <set value='46.2678324284041' />
    <set value='89.2354409531897' />
    <set value='86.6064565718678' />
  </dataset>
  <dataset seriesname='宋楚瑜林瑞雄' color='8BBA00'>
    <set value='2.76755697881694' />
    <set value='2.58286440709652' />
    <set value='2.81514759803027' />
    <set value='3.16174050424593' />
    <set value='2.47500850424115' />
    <set value='2.38759219109932' />
    <set value='2.58526878711563' />
    <set value='2.94678830771103' />
    <set value='3.30843704870458' />
    <set value='2.97156622626261' />
    <set value='2.92614568522367' />
    <set value='3.00372453580993' />
    <set value='2.51815248609569' />
    <set value='2.37722461286168' />
    <set value='1.94012437735237' />
    <set value='3.02410705320712' />
    <set value='3.76229891313994' />
    <set value='4.58812640486579' />
    <set value='3.94336126143196' />
    <set value='3.07618852738558' />
    <set value='2.68950282124987' />
    <set value='2.54767235389485' />
    <set value='5.36126056879324' />
  </dataset>
</graph>


make_VoteMSColumn3D.asp
復制內容到剪貼板程序代碼 歌詞或引言或程序代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Language" content="UTF-8" />
    <title>2012總統大選MSColumn3D</title>
</head>
<%
Body="<graph xaxisname='Month' formatNumberScale='0' yaxisname='得票數' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='5' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='5' decimalPrecision='2' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='2012第13屆總統副總統選舉結果-得票百分比' subcaption='揭曉時間:101.01.12'>"&vbCrlf
Body=Body&"    <categories font='Arial' fontSize='12' fontColor='000000'>"&vbCrlf
Set fs = CreateObject("Scripting.FileSystemObject")
Set conn = Server.CreateObject("ADODB.Connection")  
conn.open "Driver={Microsoft Excel Driver (*.xls)};"&"DBQ=" & Server.MapPath("vote2012.xls")  
sSQL = "Select * FROM [2012$]"
set Rs = Server.CreateObject("Adodb.Recordset")  
Rs.Open sSQL,conn
do until rs.eof
  Body=Body&"    <category name='"&rs("縣市")&"' hoverText='"&rs("縣市")&"'/>"&vbCrlf
   rs.movenext
loop  
Rs.close

Body=Body&"  </categories>"&vbCrlf
Body=Body&"  <dataset seriesname='蔡英文蘇嘉全' color='AFD8F8'>"&vbCrlf
set Rs = Server.CreateObject("Adodb.Recordset")  
Rs.Open sSQL,conn
do until rs.eof
  Body=Body&"    <set value='"&rs("蔡英文")&"' />"&vbCrlf
   rs.movenext
loop  
Rs.close

Body=Body&"  </dataset>"&vbCrlf
Body=Body&"  <dataset seriesname='馬英九吳敦義' color='F6BD0F'>"&vbCrlf
set Rs = Server.CreateObject("Adodb.Recordset")  
Rs.Open sSQL,conn
do until rs.eof
  Body=Body&"    <set value='"&rs("馬英九")&"' />"&vbCrlf
   rs.movenext
loop  
Rs.close

Body=Body&"  </dataset>"&vbCrlf
Body=Body&"  <dataset seriesname='宋楚瑜林瑞雄' color='8BBA00'>"&vbCrlf
set Rs = Server.CreateObject("Adodb.Recordset")  
Rs.Open sSQL,conn
do until rs.eof
  Body=Body&"    <set value='"&rs("宋楚瑜")&"' />"&vbCrlf
   rs.movenext
loop  
Rs.close
Body=Body&"  </dataset>"&vbCrlf
Body=Body&"</graph>"&vbCrlf
filepath="data/VoteMSColumn3D.xml"
SaveToFile Body,filepath,"utf-8"
'newfilepath=Server.Mappath(filepath)
'Set fout=fs.CreateTextFile(newfilepath)
'fout.WriteLine Body
Response.write "寫入 VoteMSColumn3D.xml 成功<br>"
Response.write "<a href=VoteMSColumn3D.asp>瀏覽</a><br>"
Response.End

Function SaveToFile(strBody,File,cSet)
    Dim objStream
    Set objStream = Server.CreateObject("ADODB.Stream")
    With objStream
        .Type = 2
        .Open
        .Charset   = cSet
        .Position  = objStream.Size
        .WriteText = strBody
        .SaveToFile Server.MapPath(File),2
        .Close
    End With
    Set objStream = Nothing
End Function
%>


實作測試

[本日志由 CHOME 於 2012-05-09 06:48 PM 更新]
上一篇: Peter Paul and Mary三重唱
下一篇: 使用server物件Excel.Application來存取(建立)excel檔案
文章來自: 本站原創
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相關日志:
評論: 0 | 引用: 0 | 查看次數: -
發表評論
暱 稱:
密 碼: 游客發言不需要密碼.
郵 箱: 郵件地址支持Gravatar頭像,郵箱地址不會公開.
網 址: 輸入網址便於回訪.
內 容:
驗證碼:
選 項:
雖然發表評論不用注冊,但是為了保護您的發言權,建議您注冊帳號.
字數限制 1000 字 | UBB代碼 開啟 | [img]標簽 開啟