我试图保存一个Excel电子表格。代码工作完美,当我运行通过Visual Studio(本地主机:18928)的应用程序,但是当我尝试将应用程序发布到本地主机,并通过IIS(本地主机)上运行它,我得到以下错误:试图保存Excel - 从HRESULT异常:0x800A03EC
从HRESULT
异常:0x800A03EC 描述:在当前web请求的执行过程中发生了未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。
Exception Details: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A03EC
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[COMException (0x800a03ec): Exception from HRESULT: 0x800A03EC]
Microsoft.Office.Interop.Excel._Worksheet.SaveAs(String Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local) +0
avi.Billing.WriteExcel() +6845
avi.Billing.Button5_Click(Object sender, EventArgs e) +414
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18034
我确信我的文件夹的权限是正确的,我的区域设置为美国恩,我已经确信没有与我想要的信息的问题保存。就像我所说的,当我在VS中以调试模式运行时,它完美的工作。这里是我的代码...
Private Sub WriteExcel()
Dim ConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("Pro_AVIConnectionString").ConnectionString
Dim Conn As SqlConnection = New SqlConnection(ConnStr)
Dim sql = "SELECT Transactions.id, Transactions.TransactionDate, Items.ItemCode, Customers.CustName, Transactions.Price, Transactions.Quantity, Transactions.UpdatedBy, " & _
"Invoices.Invoice, Transactions.Tax, Transactions.eLogTxID, (Transactions.Price * Transactions.Quantity) + (Transactions.Tax * Quantity) AS LineTotal " & _
"FROM Customers " & _
"INNER JOIN Transactions ON Customers.ID = Transactions.CustomerID " & _
"INNER JOIN Items ON Transactions.ItemCode = Items.id " & _
"INNER JOIN Invoices ON Transactions.InvoiceID = Invoices.ID " & _
"ORDER BY Invoices.Invoice, Transactions.TransactionDate, Transactions.ItemCode"
Dim cmd As SqlCommand = New SqlCommand(sql, Conn)
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim pth As String = "C:\Windows\SysWOW64\config\systemprofile\Desktop\Transactions.xlsx"
Dim x As Integer = 2
Dim GrandTotal As Double = 0
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US") 'doesn't help
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
Conn.Open()
xlWorkSheet.Cells(1, 1) = "Transaction Date"
xlWorkSheet.Cells(1, 2) = "Item Code"
xlWorkSheet.Cells(1, 3) = "Customer Name"
xlWorkSheet.Cells(1, 4) = "Price"
xlWorkSheet.Cells(1, 5) = "Quantity"
xlWorkSheet.Cells(1, 6) = "Updated By"
xlWorkSheet.Cells(1, 7) = "Invoice Number"
xlWorkSheet.Cells(1, 8) = "Tax"
xlWorkSheet.Cells(1, 9) = "Line Total"
Dim sdr As SqlDataReader = cmd.ExecuteReader
While sdr.Read = True
xlWorkSheet.Cells(x, 1) = "Jamie" 'sdr.Item("TransactionDate")
xlWorkSheet.Cells(x, 2) = sdr.Item("ItemCode")
xlWorkSheet.Cells(x, 3) = sdr.Item("CustName")
xlWorkSheet.Cells(x, 4) = Format(sdr.Item("Price"), "c")
xlWorkSheet.Cells(x, 5) = sdr.Item("Quantity")
xlWorkSheet.Cells(x, 6) = sdr.Item("UpdatedBy")
xlWorkSheet.Cells(x, 7) = sdr.Item("Invoice")
xlWorkSheet.Cells(x, 8) = Format(sdr.Item("Tax"), "c")
xlWorkSheet.Cells(x, 9) = Format(sdr.Item("LineTotal"), "c")
GrandTotal = GrandTotal + sdr.Item("LineTotal")
x = x + 1
End While
xlWorkSheet.Cells(x, 8) = "GRAND TOTAL"
xlWorkSheet.Cells(x, 9) = Format(GrandTotal, "c")
Conn.Close()
If FileIO.FileSystem.FileExists(pth) = True Then FileIO.FileSystem.DeleteFile(pth)
System.Threading.Thread.Sleep(3000) 'I need to pause here and show the recent change in position then continue after 3 seconds
xlWorkSheet.SaveAs(pth)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
通用基础错误可以与“错误练成1004”被一派。在Web服务器上运行Excel是一个非常糟糕的主意。 –
我有同样的问题。你解决了这个问题吗? –
@HansPassant你的建议是什么,而不是在Web服务器上运行Excel? –