asp操作excel 技術總結


目錄
一、  環境配置
二、  asp對excel 的基本操作
三、  asp操作excel 生成數據表
四、  asp操作excel 生成chart圖
五、  服務器端excel 文件瀏覽、下載、刪除方案
六、  附錄
正文
一、  環境配置
服務器端的環境配置從參考資料上看,微軟系列的配置應該都行,即:
1.win9x+pws+office
2.win2000 professional+pws+office
3.win2000 server+iis+office
目前筆者測試成功的環境是后二者。office的版本沒有特殊要求,考慮到客戶機配置的不確定性和下兼容特性,建議服務器端office版本不要太高,以防止客戶機下載后無法正確顯示。
服務器端環境配置還有兩個偶然的發現是:
1.  筆者開發機器上原來裝有金山的wps2002,結果excel 對象創建始終出現問題,卸載wps2002后,錯誤消失。
2.  筆者開發asp代碼喜歡用frontpage,結果發現如果frontpage打開(服務器端),對象創建出現不穩定現象,時而成功時而不成功。擴展考察后發現,office系列的軟件其他 如果在服務器端運行,則excel 對象的創建很難成功。
服務器端還必須要設置的一點是com組件的操作權限。在命令行鍵入“dcomcnfg”,則進入com組件配置界面,選擇microsoft excel 后點擊屬性按鈕,將三個單選項一律選擇自定義,編輯中將everyone加入所有權限。保存完畢后重新啟動服務器。
客戶端的環境配置沒發現什么特別講究的地方,只要裝有office和ie即可,版本通用的好象都可以。

二、  asp對excel 的基本操作
1、  建立excel 對象
set objexcel app = createobject("excel .application")
objexcel app.displayalerts = false    不顯示警告
objexcel app.application.visible = false    不顯示界面
2、  新建excel 文件
objexcel app.workbooks.add
set objexcel book = objexcel app.activeworkbook
set objexcel sheets = objexcel book.worksheets
set objexcel sheet = objexcel book.sheets(1)
3、  讀取已有excel 文件
straddr = server.mappath(".")
objexcel app.workbooks.open(straddr & "\templet\table.xls")
set objexcel book = objexcel app.activeworkbook
set objexcel sheets = objexcel book.worksheets
set objexcel sheet = objexcel book.sheets(1)
4、  另存excel 文件
objexcel book.saveas straddr & "\temp\table.xls"
5、  保存excel 文件
objexcel book.save    (筆者測試時保存成功,頁面報錯。)
6、  退出excel 操作
objexcel app.quit  一定要退出
set objexcel app = nothing

三、  asp操作excel 生成數據表
1、  在一個范圍內插入數據
objexcel sheet.range("b3:k3").value = array("67", "87", "5", "9", "7", "45", "45", "54", "54", "10")
2、  在一個單元格內插入數據
objexcel sheet.cells(3,1).value="internet explorer"
3、  選中一個范圍
4、  單元格左邊畫粗線條
5、  單元格右邊畫粗線條
6、  單元格上邊畫粗線條
7、  單元格下邊畫粗線條
8、  單元格設定背景色
9、  合並單元格
10、  插入行
11、  插入列

四、  asp操作excel 生成chart圖
1、  創建chart圖
objexcel app.charts.add
2、  設定chart圖種類
objexcel app.activechart.charttype = 97
注:二維折線圖,4;二維餅圖,5;二維柱形圖,51
3、  設定chart圖標題
objexcel app.activechart.hastitle = true
objexcel app.activechart.charttitle.text = "a test chart"
4、  通過表格數據設定圖形
objexcel app.activechart.setsourcedata objexcel sheet.range("a1:k5"),1
5、  直接設定圖形數據(推薦)
objexcel app.activechart.seriescollection.newseries
objexcel app.activechart.seriescollection(1).name = "=""333"""
objexcel app.activechart.seriescollection(1).values = "="
6、  綁定chart圖
objexcel app.activechart.location 1
7、  顯示數據表
objexcel app.activechart.hasdatatable = true
8、  顯示圖例
objexcel app.activechart.datatable.showlegendkey = true

五、  服務器端excel 文件瀏覽、下載、刪除方案
瀏覽的解決方法 很多,“location.href=”,“navigate”,“response.redirect”都可以實現,建議用客戶端的方法,原因是給服務器更多的時間生成excel 文件。
下載的實現要麻煩一些。用網上現成的服務器端下載組件或自己定制開發一個組件是比較好的方案。另外一種方法是在客戶端操作excel 組件,由客戶端操作服務器端excel 文件另存至客戶端。這種方法要求客戶端開放不安全activex控件的操作權限,考慮到通知每個客戶將服務器設置為可信站點的麻煩程度建議還是用第一個方法比較省事。
刪除方案由三部分組成:
a:  同一用戶生成的excel 文件用同一個文件名,文件名可用用戶id號或sessionid號等可確信不重復字符串組成。這樣新文件生成時自動覆蓋上一文件。
b:  在global.asa文件中設置session_onend事件激發時,刪除這個用戶的excel 暫存文件。
c:  在global.asa文件中設置application_onstart事件激發時,刪除暫存目錄下的所有文件。
注:建議目錄結構 \src 代碼目錄 \templet 模板目錄 \temp 暫存目錄

六、  附錄
出錯時excel 出現的死進程出現是一件很頭疼的事情。在每個文件前加上“on error resume next”將有助於改善這種情況,因為它會不管文件是否產生錯誤都堅持執行到“application.quit”,保證每次程序執行完不留下死進程。

評論
# re: asp操作excel 技術總結
這個程序一共由三個文件構成,第一個文件的文件名為:toexcel .asp是主文件,內容如下:
<%
'前面是和來鏈接到其他數據庫 ,請自行書寫相關語句,此處略過
sql="select * from [tx_use]" '這里是要輸出excel 的查詢語句,如 "sesect * form cai where 性別='女'"
filename="excel .xls" ' 要輸出的excel 文件的文件名, 你只要改以上兩句就行了,其它的都不要改.
'你只要修改以上兩變量就行了.其它的我都做好了.
call toexcel (filename,sql)
set conn=nothing
function readtext(filename) '這是一個用於讀出文件的函數
set adf=server.createobject("adodb.stream")
with adf
.type=2
.lineseparator=10
.open
.loadfromfile (server.mappath(filename))
.charset="gb2312"
.position=2
readtext=.readtext
.cancel()
.close()
end with
set ads=nothing
end function
sub savetext(filename,data) '這是一個用於寫文件的函數
set fs= createobject("scripting.filesystemobject")
set ts=fs.createtextfile(server.mappath(filename),true)
ts.writeline(data)
ts.close
set ts=nothing
set fs=nothing
end sub
sub toexcel (filename,sql) '這是一個根據sql語句綜合 和filename生成excel 文件
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,3
toexcel lr="<table width='100%'><tr >"
set myfield=rs.fields
dim fieldname(50)
for i=0 to myfield.count-1
toexcel lr=toexcel lr&"<td class="xl24">"&myfield(i).name&"</td>"
fieldname(i)=myfield(i).name
if myfield(i).type=135 then datename=datename&myfield(i).name&","
next
toexcel lr=toexcel lr&"</tr>"
do while not rs.eof
toexcel lr=toexcel lr&"<tr>"
for i=0 to myfield.count-1
if instr(datename,fieldname(i)&",")<>0 then
if not isnull(rs(fieldname(i))) then
toexcel lr=toexcel lr&"<td class="xl25" ><p align='left'>"&formatdatetime(rs(fieldname(i)),2)&"</p></td>"
else
toexcel lr=toexcel lr&"<td class="xl25" ><p align='left'> </p></td>"
end if
else
toexcel lr=toexcel lr&"<td class="xl24" >"&rs(fieldname(i))&"</td>"
end if
next
toexcel lr=toexcel lr&"</tr>"
rs.movenext
loop
toexcel lr=toexcel lr&"</table>"
tou=readtext("tou.txt")
di=readtext("di.txt")
toexcel lr=tou&toexcel lr&di
call savetext(filename,toexcel lr)
end sub
%>
<html>
<head>
<meta http-equiv="refresh" content="3;url=<%=filename%>">
<meta http-equiv="content-language" content="en-us">
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>正在生成exlce文件</title>
</head>
<body>
正在生成exlce文件....
</body>
</html>
**************第二個文件名為:di.txt 內容如下:
<table x:str border=0 cellpadding=0 cellspacing=0 width=288 style='border-collapse:
collapse;table-layout:fixed;width:216pt'>
<![if supportmisalignedcolumns]>
<tr height=0 style='display:none'>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
</tr>
<![endif]>
</table>
************第三個文件的文件名為:tou.txt 內容如下:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel "
xmlns="http://www.w3.org/tr/rec-html40">
<head>
<meta http-equiv=content-type content="text/html; charset=gb2312">
<meta name=progid content=excel .sheet>
<meta name=generator content="microsoft excel 9">
<link rel=file-list href="./222.files/filelist.xml">
<link rel=edit-time-data href="./222.files/editdata.mso">
<link rel=ole-object-data href="./222.files/oledata.mso">
<!--[if gte mso 9]><xml>
<documentproperties>
<author>xky</author>
<lastauthor>xky</lastauthor>
<created>2002-05-27t17:51:00z</created>
<lastsaved>2002-06-22t10:03:03z</lastsaved>
<company>zydn</company>
<version>9.2812</version>
</documentproperties>
<officedocumentsettings>
<downloadcomponents/>
<locationofcomponents href="file:///e:/msowc.cab"/>
</officedocumentsettings>
</xml><![endif]-->
<style>
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:general;
text-align:general;
vertical-align:bottom;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋體;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:常規;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋體;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:general;
text-align:general;
vertical-align:bottom;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
border:.5pt solid windowtext;}
.xl25
{mso-style-parent:style0;
mso-number-format:"long date";
text-align:left;
border:.5pt solid windowtext;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋體;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>
<!--[if gte mso 9]><xml>
<x:excel workbook>
<x:excel worksheets>
<x:excel worksheet>
<x:name>sheet1</x:name>
<x:worksheetoptions>
<x:defaultrowheight>225</x:defaultrowheight>
<x:print>
<x:validprinterinfo/>
<x:papersizeindex>9</x:papersizeindex>
<x:horizontalresolution>-3</x:horizontalresolution>
<x:verticalresolution>0</x:verticalresolution>
</x:print>
<x:selected/>
<x:panes>
<x:pane>
<x:number>3</x:number>
<x:activerow>24</x:activerow>
<x:activecol>5</x:activecol>
</x:pane>
</x:panes>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excel worksheet>
<x:excel worksheet>
<x:name>sheet2</x:name>
<x:worksheetoptions>
<x:defaultrowheight>225</x:defaultrowheight>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excel worksheet>
<x:excel worksheet>
<x:name>sheet3</x:name>
<x:worksheetoptions>
<x:defaultrowheight>225</x:defaultrowheight>
<x:protectcontents>false</x:protectcontents>
<x:protectobjects>false</x:protectobjects>
<x:protectscenarios>false</x:protectscenarios>
</x:worksheetoptions>
</x:excel worksheet>
</x:excel worksheets>
<x:windowheight>6600</x:windowheight>
<x:windowwidth>12000</x:windowwidth>
<x:windowtopx>0</x:windowtopx>
<x:windowtopy>1395</x:windowtopy>
<x:protectstructure>false</x:protectstructure>
<x:protectwindows>false</x:protectwindows>
</x:excel workbook>
</xml><![endif]-->
</head>
<body link=blue vlink=purple>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM