一個(gè)用組件動(dòng)態(tài)創(chuàng)建Excel文件的實(shí)例
在精華區(qū)中有一篇關(guān)于在ASP中動(dòng)態(tài)創(chuàng)建的Excel文章, 但實(shí)際上我們會(huì)發(fā)現(xiàn)如果我們?cè)贏SP中用Set MyExcelChart = server.CreateObject("Excel.Sheet")是行不通的. 這樣做的話會(huì)出現(xiàn)如下的錯(cuò)誤信息: 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關(guān)于此出錯(cuò)信息的詳細(xì)內(nèi)容你可以看: http://msdn.microsoft.com/workshop/server/components/outproc.asp 所以, 要想在服務(wù)器自動(dòng)生成Excel文件還是必須通過(guò)組件來(lái)實(shí)現(xiàn)(個(gè)人意見(jiàn),如果你有更好的方法請(qǐng)告訴我:-)). 設(shè)計(jì)環(huán)境:VB6.0 運(yùn)行環(huán)境:NT4.0(sp5)+IIS4.0+MTS 1.新建一個(gè)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注冊(cè)p_excel.dll 6.ASP文件代碼并在IIS中設(shè)置要生成excel文件的虛擬目錄對(duì)用戶有寫(xiě)的權(quán)限. excel.asp <% set myExcel=server.createobject("p_excel.c_excel") myExcel.CreateExcel set myExcel=nothing %> 7.運(yùn)行excel.asp,在相關(guān)目錄下我們就可以找到生成的Excel文件.
改進(jìn)的建議: 1.在p_excel.dll中增加(range,value)的屬性就可以利用從數(shù)據(jù)庫(kù)中查詢返回的記錄動(dòng)態(tài)生成Excel文檔. 2.增加Email功能自動(dòng)將生成的Excel文件發(fā)送給相關(guān)用戶.
如果你還有其他的建議請(qǐng)告訴我:-)
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!