试图保存Excel – 从HRESULTexception:0x800A03EC

我正在尝试保存一个Excel电子表格。 当我通过Visual Studio(localhost:18928)运行应用程序时,代码工作正常,但是当我尝试将应用程序发布到本地主机并通过IIS(本地主机)运行时,出现以下错误:


来自HRESULT的exception:0x800A03EC说明:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪,了解有关错误的更多信息以及源代码的来源。

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的debugging模式下运行时,它是完美的。 这是我的代码…


 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 

根据您所看到的错误和您的描述,问题是为IIS用户提供足够的权限来运行Excel。 在debugging模式下从Visual Studio运行时,它全部在您的用户标识下。 从本地IIS安装运行时,它使用IIS用户帐户。 该帐户默认情况下不具有运行Excel的权限。 因此,您需要为该用户提供足够的Excel执行权限。 这是允许写入文件夹的权限设置。 在你的问题,这听起来不像你已经采取了这一步。

由于您在本地运行,您可以login到IIS用户帐户,看看是否可以以这种方式login时运行Excel。 这也将有写用户特定的registry项,第一次运行设置等,也可能会导致您的问题的影响。

当然,如果您打算将这个应用程序部署到实际的Web服务器,事情可能会更复杂,因为networkingpipe理员不喜欢给公共系统授予应用程序运行权限。

我最喜欢的方法就像你所描述的那样简单,就是在SQL Server SSIS包中创build数据导出,然后从我的网站调用这个包。 如果不需要特殊的格式,这往往是更清洁(对于pipe理员),并且比直接自动化Excel更容易出错。