从VB.NET创建Excel报表

时间:2015-01-08 17:00:40

标签: vb.net

我试图以不同的方式从VB.net创建Excel报告,每个报告都有自己的问题。我尝试的最新方法是使用ActiveX com。我在网上尝试了不同的代码,但最终都出现了以下错误:

由于以下错误,检索CLSID为{00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80070005拒绝访问。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))。

我在线查看了错误并尝试了所有解决方案,但我仍然在以下行中收到相同的错误:

Dim xlObject As New Excel.Application()

或不同页面中的类似行:

Dim oXL As Excel.Application =  New Microsoft.Office.Interop.Excel.Application()

程序没有移动到下一行,这意味着那里有问题。

到目前为止,我所做的事情是:

1-我添加了对Microsoft.Office.Interop.Excel和Microsoft Office 14.0对象库的引用

2-我添加了Imports Excel = Microsoft.Office.Interop.Excel 在我的代码顶部导入Microsoft.Office.Interop

3-我使用Try ... Catch ...最后或不使用它来处理程序完成后使用它们创建的对象。我为工作簿,工作表和范围对象使用了xlObject.Workbooks.Close()和xlObject.Quit()以及Marshal.FinalReleaseComObject。

4-我尝试分配工作簿名称(C:\ Contracts \ Filename.xls或C:\ Contracts \ Filename.xlsx)或让程序创建一个。我将NetWork Service的所有权限(我自己作为用户,IIs_IUSRS)授予包含filename.xls的文件夹。

5-我更改了dcomcnfg / component服务/计算机/我的电脑/ DCOM配置/ Microsoft Excel应用程序属性/安全选项卡,并添加了具有本地启动和本地激活权限和本地访问权限的网络服务帐户。经过大量的试错法后,我还为dcomcnfg / component服务/计算机/我的电脑属性添加了相同的权限。

我甚至将一些在线代码复制到我的页面并尝试了它们。我正在使用Windows Server 2008 SP 1,64位操作系统和MS Office Professional 2010版本14.0(64位),并从localhost查看该页面。

这是我尝试的最新代码:

Imports Excel = Microsoft.Office.Interop.Excel
'Imports Microsoft.Office.Interop

Public Class TestExcel5
    Inherits System.Web.UI.Page


    Private Sub btnWrite_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnWrite.Click

        Dim Testmsg As String = ""
        Try
            Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()


            If xlApp Is Nothing Then
                AlertUserMsgBox("Excel is not properly installed!!")
                Testmsg = "Excel is not properly installed!!"
                Return
            End If

            Testmsg = "Passed creating the Excel object......"

            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet
            Dim misValue As Object = System.Reflection.Missing.Value

            xlWorkBook = xlApp.Workbooks.Add(misValue)
            xlWorkSheet = xlWorkBook.Sheets("sheet1")
            xlWorkSheet.Cells(1, 1) = "Sheet 1 content"

            xlWorkBook.SaveAs("C:\Contracts\Excel5.xlsx", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
             Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
            xlWorkBook.Close(True, misValue, misValue)
            xlApp.Quit()

            releaseObject(xlWorkSheet)
            releaseObject(xlWorkBook)
            releaseObject(xlApp)

            AlertUserMsgBox("Excel file created , you can find the file C:\Excel5.xls")
        Catch ex As Exception

            AlertUserMsgBox("Error in export: " & ex.Message & "   AND Testmsg=" & Testmsg)

        End Try

    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try


    End Sub

    Public Sub AlertUserMsgBox(ByVal sMsg As String)

        Session("AddScriptName") = Session("AddScriptName") + 1

        Dim ScriptName As String = "ScriptName" & Session("AddScriptName")
        Dim ScriptStr = " alert(""" & sMsg & """); "

        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), ScriptName, ScriptStr, True)
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

这对我有用:

Private Sub btnExportToExcel_Click(sender As Object, e As EventArgs) Handles btnExportToExcel.Click

    Dim StartDate As Date = DateTimePicker1.Value
    Dim EndDate As Date = DateTimePicker2.Value
    Dim worker = selectedWorker.FullName


    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer

    xlApp = New Microsoft.Office.Interop.Excel.Application
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")


    For i = 0 To dgvAppPlatite.RowCount - 2
        For j = 0 To dgvAppPlatite.ColumnCount - 1
            For k As Integer = 1 To dgvAppPlatite.Columns.Count
                xlWorkSheet.Cells(1, k) = dgvAppPlatite.Columns(k - 1).HeaderText
                xlWorkSheet.Cells(i + 2, j + 1) = dgvAppPlatite(j, i).Value.ToString()
            Next
        Next
    Next

    'Freeze top Row .. dar inca nu-mi merge
    xlWorkSheet.Activate()
    xlWorkSheet.Application.ActiveWindow.FreezePanes = True
    Dim firstRow As Range = DirectCast(xlWorkSheet.Rows(1), Excel.Range)
    firstRow.Activate()
    firstRow.Select()
    'firstRow.AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, True)

    xlWorkSheet.PageSetup.CenterFooter = "Incasari : " & Label5.Text & " si comision : " & Label6.Text

    xlWorkSheet.SaveAs("D:\Docs\Rapoarte\" & worker & " " & StartDate & " " & EndDate & ".xlsx")
    xlWorkBook.Close()
    xlApp.Quit()

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)

    MsgBox("D:\Docs\Rapoarte\" & worker & " " & StartDate & " " & EndDate & ".xlsx")
End Sub

答案 1 :(得分:0)

我使用Open XML SDK方法创建一个Excel文件,我不会再回到使用Microsoft.Office.Interop.Excel了,因为我无法弄清楚错误。