|
#1
| ||||
| ||||
| [轉貼] [.NET]以 ASP.NET 建立 MS-Excel 檔 1. add Reference "COM object - Microsoft Excel 10.0 Object Library" in your project. 2. Run dcomcnfg.exe in DOS mode. set the security for DCOM objects - in Security tab - Lauch and Activation permisson - Access Permissin - Configuration permisson add the following users: IWAN_xxxxxxxxxx IUSR_xxxxxxxxxx ASPNET VS Developer 3. 加入以下程式 [code] 代碼: Imports System.Runtime.InteropServices.Marshal
Imports System.Diagnostics
Imports System.Data
Imports System.IO
Imports Microsoft
..
..
..
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oExcel As New Excel.Application
Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
Dim oCells As Excel.Range
Dim sFile As String, sTemplate As String
Dim WBcnt As Integer
Dim ObjFS
ObjFS = Server.CreateObject("Scripting.FileSystemObject")
GC.Collect()
GC.WaitForPendingFinalizers()
oExcel.Visible = False : oExcel.DisplayAlerts = False
oBooks = oExcel.Workbooks
oBook = oBooks.Add
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Excel.Worksheet)
oCells = oSheet.Cells
WBcnt = oBook.Worksheets.Count
With oExcel
.Range("A1").Select()
.ActiveCell.FormulaR1C1 = "Hello Excel"
With .Selection.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
End With
'Dim TFName As String = Server.MapPath("my" & Session.SessionID & ".xls")
oBook.SaveAs(Server.MapPath("my" & Session.SessionID & ".xls"))
oBook.Close()
oExcel.Quit()
ReleaseComObject(oCells)
ReleaseComObject(oSheet)
ReleaseComObject(oSheets) : ReleaseComObject(oBook)
ReleaseComObject(oBooks) : ReleaseComObject(oExcel)
oExcel = Nothing : oBooks = Nothing : oBook = Nothing
oSheets = Nothing : oSheet = Nothing : oCells = Nothing
System.GC.Collect()
End With
End Sub
**轉貼出處:藍色小舖** =================== 此文章於 2005-12-05 10:15 PM 被 刀狂劍痴 編輯. |