【问题标题】:Object not set to the instance of an Object ERROR Excel 2013 Automation VB.NET对象未设置为对象错误 Excel 2013 自动化 VB.NET 的实例
【发布时间】:2014-06-07 02:07:08
【问题描述】:

尝试在 VB.Net 中创建一个小型报表程序。它适用于 Excel 2003 和 2010,但在尝试创建第二个工作表时在 Excel 2013 中引发错误。下面列出了程序,我还指出了错误所在的行。它甚至没有在我添加的 Try and Catch 中捕获,它作为“未处理的异常”出现。

目标框架是3.5

已尝试将 x86、x64 和 Any CPU 作为目标 CPU

我使用 2003 和 2010 的 .dll 作为参考。

Imports Microsoft.Office.Interop.Excel

Private Sub DRpts()
    Try
        Dim strFileDirName As String = ""
        Me.Cursor = Cursors.WaitCursor


        'Morning Report Sheet 1
        Dim MornRpt As String = "SELECT DateCreated, ReportClass, SUM(QtyOrdered),          SUM(ExtendedPrice) " & _
        "FROM tmp_DailyReport GROUP BY DateCreated, ReportClass"

        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet1 As Object
        Dim oSheet2 As Object

        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.workbooks.add
        oSheet1 = oBook.Worksheets("Sheet1")

        oSheet1.Name = "Morning Report"

        'Load Headers
        oSheet1.Range("A1").Value = "Class"
        oSheet1.Range("B1").Value = "Units"
        oSheet1.Range("C1").Value = "Extended Price"

        'Formatting()
        oSheet1.Range("A1:C1").Font.Bold = True

        Dim CellLoc As String = ""
        Dim CellCntr As Integer = 2
        Dim SRptDate As String = Me.RptStart.Text
        Dim ERptDate As String = Me.RptEnd.Text

        Dim Reader1 As Odbc.OdbcDataReader
        Using connection As New Odbc.OdbcConnection("DSN=#########;")
            Dim Command As New Odbc.OdbcCommand(MornRpt, connection)
            Command.Connection.Open()
            Reader1 = Command.ExecuteReader
            While Reader1.Read()
                CellLoc = "A" & CellCntr
                oSheet1.Range(CellLoc).Value = Reader1.GetValue(1).ToString
                CellLoc = "B" & CellCntr
                oSheet1.Range(CellLoc).Value = Reader1.GetValue(2).ToString
                CellLoc = "C" & CellCntr
                oSheet1.Range(CellLoc).Value = Reader1.GetValue(3).ToString
                CellCntr = CellCntr + 1
            End While
        End Using

        Dim FormatRange As String = "C2:C" & CellCntr
        oSheet1.Range(FormatRange).NumberFormat = "#,##0.00"
        oSheet1.Columns.EntireColumn.AutoFit()
        oSheet1.Columns("A").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft

        ------>   oSheet2 = oBook.Worksheets("Sheet2")   *** BAD INDEX ERROR OCCURS HERE *** <------------------
        ------>   ' Also tried this variation --> *** oSheet2 = oBook.Worksheets("Sheet2") ***  Neither Work

        oSheet2.Name = "Summary"
        Dim OverseasSales As String = "SELECT SUM(QtyOrdered) AS Units, SUM(ExtendedPrice) AS ExtPrice " & _
        "FROM dbo.tmp_DailyReport " & _
        "WHERE (CustomerNo BETWEEN '0' AND '9)' OR " & _
        "CustomerNo BETWEEN '0' AND '9' OR " & _
        "CustomerNo = '######')" & _
        "GROUP BY DateCreated "

        'Load(Headers)
        oSheet2.Range("A1").Value = "Report Class"
        oSheet2.Range("B1").Value = "Product"
        oSheet2.Range("C1").Value = "Units"
        oSheet2.Range("D1").Value = "ExtendedPrice"

这是我收到的错误。

System.NullReferenceException: Object reference not set to an instance of an object.
   at GoldenManagement.DailyReport.DRpts()
   at GoldenManagement.DailyReport.RunDailyRpt2_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

【问题讨论】:

    标签: .net vb.net excel automation


    【解决方案1】:

    我忘记了我发布了这个。我前一段时间找到了解决方案。

    我应该使用错误发生的地方;

    oSheet2 = CType(Me.Application.Worksheets.Add(), Excel.Worksheet)
    oSheet2.Name = "Sheet2"
    

    除非指定,否则较新版本的 office 将默认为书中的一张。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-14
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2011-07-09
      相关资源
      最近更新 更多