简体   繁体   English

未定义在 vb.net 应用程序中创建 Excel 电子表格

[英]Creating a excel spreadsheet in vb.net Application not defined

I am trying to create an Office 365 excel spreadsheet using vb.net.我正在尝试使用 vb.net 创建 Office 365 excel 电子表格。 I have added the following reference.我添加了以下参考。 Microsoft,Office 16.0 Object Library to the project. Microsoft、Office 16.0 对象库到项目。 Anything with Excel.用 Excel 做任何事。 Errors as not defined.未定义的错误。

What's missing?缺少了什么?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim objApp As Excel.Application
    Dim objBook As Excel._Workbook

    Dim objBooks As Excel.Workbooks
    Dim objSheets As Excel.Sheets
    Dim objSheet As Excel._Worksheet
    Dim range As Excel.Range

    ' Create a new instance of Excel and start a new workbook.
    objApp = New Excel.Application()
    objBooks = objApp.Workbooks
    objBook = objBooks.Add
    objSheets = objBook.Worksheets
    objSheet = objSheets(1)

You have two options to resolve your issue:您有两种选择来解决您的问题:

Option 1 : Add Imports statement:选项 1 :添加导入语句:

Imports Excel = Microsoft.Office.Interop.Excel

Option 2 : Specify the namespace选项 2 :指定命名空间

Dim objApp As Microsoft.Office.Interop.Excel.Application
Dim objBook As Microsoft.Office.Interop.Excel._Workbook

Try the following:请尝试以下操作:

Add a Module to your project (name: HelperExcel.vb)将模块添加到您的项目(名称:HelperExcel.vb)

HelperExcel.vb HelperExcel.vb

'Add Reference
'Project => Add Reference => COM => Microsoft Excel 16.0 Object Library

Imports Excel = Microsoft.Office.Interop.Excel

Module HelperExcel

    Public Sub CreateExcelWorkbook(filename As String)
        Dim oMissing As Object = System.Reflection.Missing.Value

        Dim excelApp As Excel.Application = Nothing
        Dim excelWorkbook As Excel.Workbook = Nothing
        Dim excelWorksheet As Excel.Worksheet = Nothing

        Try
            'create New instance
            excelApp = New Excel.Application()

            'suppress displaying alerts (such as prompting to overwrite existing file)
            excelApp.DisplayAlerts = False

            'set Excel visability
            excelApp.Visible = True

            'disable user control while modifying the Excel Workbook
            'to prevent user interference
            'only necessary if Excel application Visibility property = true
            'excelApp.UserControl = false

            'add workbook
            excelWorkbook = excelApp.Workbooks.Add()

            'add a worksheet And set the value to the New worksheet
            excelWorksheet = excelWorkbook.Sheets.Add()

            'indices are 1-based in Excel; A1 = Cells(1,1)
            'excelWorksheet.Cells(1, "A") = "Product Code"
            'excelWorksheet.Cells(1, "B") = "Product Description"
            excelWorksheet.Cells(1, 1) = "Product Code"
            excelWorksheet.Cells(1, 2) = "Product Description"


            If excelApp IsNot Nothing AndAlso excelWorkbook IsNot Nothing Then
                'save Workbook - if file exists, overwrite it
                excelWorkbook.SaveAs(filename, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value)
            End If

        Catch ex As Exception
            Debug.WriteLine("Error (CreateExcelWorkbook): " & ex.Message)
            Throw ex
        Finally
            If excelWorkbook IsNot Nothing Then
                excelWorkbook.Close()
                excelWorksheet = Nothing
                excelWorkbook = Nothing
            End If

            If excelApp IsNot Nothing Then
                excelApp.Quit()

                'release all resources
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp)
            End If
        End Try
    End Sub
End Module

Usage :用法

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRun.Click
    
    'write file to Documents folder (ex: C:\Users\<username>\Documents\TestExcel.xlsx)
    Dim filename As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TestExcel.xlsx")
    HelperExcel.CreateExcelWorkbook(filename)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM