top
Loading...
一個用組件動態創建Excel文件的實例
在精華區中有一篇關于在ASP中動態創建的Excel文章, 但實際上我們會發現如果我們在ASP中用Set MyExcelChart =
server.CreateObject("Excel.Sheet")是行不通的. 這樣做的話會出現如下的錯誤信息:
Only InProc server components should be used. If you want to use LocalServer components, you must set the
AspAllowOutOfProcComponents metabase setting. Please consult the help file for important considerations
關于此出錯信息的詳細內容你可以看:
http://msdn.microsoft.com/workshop/server/components/outproc.asp
所以, 要想在服務器自動生成Excel文件還是必須通過組件來實現(個人意見,如果你有更好的方法請告訴我:-)).
設計環境:VB6.0
運行環境:NT4.0(sp5)+IIS4.0+MTS
1.新建一個DLL工程.工程名為p_excel,類名為c_excel
2.在"project"->"references"中選中"Microsoft Excel 9 Object Library".
3.代碼
Option Explicit

Dim oExcel As Excel.Application
Dim oSheet As Excel.Worksheet
Dim oTitle As Excel.Range

Public Sub CreateExcel()
Set oExcel = New Excel.Application
oExcel.Visible = False
oExcel.Workbooks.Add
Set oSheet = oExcel.Workbooks(1).Worksheets("Sheet1")
oSheet.Activate
Set oTitle = oSheet.Range("A1")
oTitle.Value = "Excel Title"
oTitle.Font.Bold = -1
oTitle.Font.Size = 18
oTitle.Font.Name = "Arial"
oSheet.SaveAs "allen.xls"
oExcel.Quit
Set oExcel = Nothing
End Sub
4.編譯生成p_excel.dll
5.使用MTS注冊p_excel.dll
6.ASP文件代碼并在IIS中設置要生成excel文件的虛擬目錄對用戶有寫的權限.
excel.asp
<%
set myExcel=server.createobject("p_excel.c_excel")
myExcel.CreateExcel
set myExcel=nothing
%>
7.運行excel.asp,在相關目錄下我們就可以找到生成的Excel文件.

改進的建議:
1.在p_excel.dll中增加(range,value)的屬性就可以利用從數據庫中查詢返回的記錄動態生成Excel文檔.
2.增加Email功能自動將生成的Excel文件發送給相關用戶.

如果你還有其他的建議請告訴我:-)

北斗有巢氏 有巢氏北斗