【发布时间】:2017-01-08 16:19:31
【问题描述】:
我构建了一个 .net 应用程序,可以将数据保存到 Excel。当我手动运行应用程序时,程序会将数据表中的内容保存到 excel 中。问题是,当我安排任务在我的 Windows 10 PC 上运行时,我收到以下错误。
:System.Runtime.InteropServices.COMException (0x800A03EC):HRESULT 异常:0x800A03EC 在 Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs(对象文件名、对象 FileFormat、对象密码、对象 WriteResPassword、对象 ReadOnlyRecommended、对象 CreateBackup、XlSaveAsAccessMode AccessMode、对象 ConflictResolution、对象 AddToMru、对象 TextCodepage、对象 TextVisualLayout、对象本地)
Public Shared Function CreateExcel() As Microsoft.Office.Interop.Excel.ApplicationClass
For retry As Integer = 1 To 5
Try
clsScrape.SendMail(" Return New Microsoft.Office.Interop.Excel.ApplicationClass")
Return New Microsoft.Office.Interop.Excel.ApplicationClass
Exit For
Catch ex As Exception
If ex.HResult <> &H80080005 Then Throw ex
End Try
Next
Return Nothing
End Function
Public Shared Sub ExportExceltest(ByVal excel As Microsoft.Office.Interop.Excel.ApplicationClass)
Try
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Name")
Dim R As DataRow = dt.NewRow
R("Name") = "MY Name"
dt.Rows.Add(R)
Dim strFile As String = "C:\Users\CodeMonger\Documents\Development\Test" & DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss") & ".xlsx"
' Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
clsScrape.SendMail("The datatable was built and this is Right before save")
wSheet.Columns.AutoFit()
wBook.SaveAs(strFile)
wBook.Close()
Catch ex As Exception
clsScrape.SendMail("Here is the Issue :" & ex.ToString)
End Try
End Sub
【问题讨论】:
标签: excel vb.net taskscheduler