2013-05-22 167 views
2

我试图保存一个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 
+0

通用基础错误可以与“错误练成1004”被一派。在Web服务器上运行Excel是一个非常糟糕的主意。 –

+0

我有同样的问题。你解决了这个问题吗? –

+0

@HansPassant你的建议是什么,而不是在Web服务器上运行Excel? –

回答

3

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

既然你在本地运行,您可以登录到IIS用户帐户,看看你是否能以这种方式登录时运行Excel。这也会影响编写用户特定的注册表项,第一次运行设置等,这也可能会导致您的问题。

当然,如果你在部署该应用到实际的Web服务器的规划,事情可能会更复杂,因为网络管理员不喜欢给应用程序的运行权限,以面向公众的系统。

我对一些简单的像你所描述的首选方法是建立在SQL Server SSIS包中的数据导出和从我的网站调用这个软件包。如果不需要特殊格式,这比管理员更清洁,并且比直接自动化Excel更容易出错。

+0

其实这个链接解决了我的问题。 http://stackoverflow.com/questions/1981395/windows-7-net-excel-saveas-error-exception-from-hresult-0x800a03ec – HolidayJL

+0

是否有任何引用读取作为Excel文件从SQL导出数据和然后按照你的建议在网站上使用它? –

相关问题