Я пытаюсь сохранить электронную таблицу Excel. Код работает отлично, когда я запускаю приложение через Visual Studio (localhost: 18928), но когда я пытаюсь опубликовать приложение на локальном хосте и запускаю его через IIS (локальный хост), я получаю следующую ошибку:Пытается сэкономить Excel - Исключение из HRESULT: 0x800A03EC
Исключение из HRESULT: 0x800A03EC Описание: Необработанное исключение произошло во время выполнения текущего веб-запроса. Просмотрите трассировку стека для получения дополнительной информации об ошибке и ее возникновении в коде.
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
Общая погрешность может быть искажена с помощью «excel error 1004». Запуск Excel на веб-сервере - очень плохая идея. –
У меня такая же проблема. Вы решили проблему? –
@HansPassant Каково ваше предложение вместо запуска Excel на веб-сервере? –