[英]Creating a excel spreadsheet in vb.net Application not defined
我正在嘗試使用 vb.net 創建 Office 365 excel 電子表格。 我添加了以下參考。 Microsoft、Office 16.0 對象庫到項目。 用 Excel 做任何事。 未定義的錯誤。
缺少了什么?
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)
您有兩種選擇來解決您的問題:
選項 1 :添加導入語句:
Imports Excel = Microsoft.Office.Interop.Excel
選項 2 :指定命名空間
Dim objApp As Microsoft.Office.Interop.Excel.Application
Dim objBook As Microsoft.Office.Interop.Excel._Workbook
請嘗試以下操作:
將模塊添加到您的項目(名稱: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
用法:
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.