与Excel在VB.net中的工作失败,没有明显原因。

huangapple go评论68阅读模式
英文:

Work with Excel in VB.net fails without reason

问题

以下代码是关于导出到Excel文件的。
Visual Studio在图片中发送了错误。
这段代码有什么问题?

Imports Microsoft.Office.Interop.Excel
Module Module3
    Public Sub OOO()
        Dim excel As Application = New Application
        Dim workbook As Workbook = New Workbook
        Dim worksheet As Worksheet = workbook.ActiveSheet
        worksheet.Name = "Export as Excel Project"
        worksheet.Cells(1, 1) = "Value1"
        worksheet.Cells(2, 1) = "Value2"
        worksheet.SaveAs("d:\Actor.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault)
        workbook.Close()
    End Sub
End Module

与Excel在VB.net中的工作失败,没有明显原因。

英文:

below code is about to export to excel file.
Visual Studio sends the error in the picture.
What is wrong with this code?

Imports Microsoft.Office.Interop.Excel
Module Module3
    Public Sub OOO()
        Dim excel As Application = New Application
        Dim workbook As Workbook = New Workbook
        Dim worksheet As Worksheet = workbook.ActiveSheet
        worksheet.Name = "Export as Excel Project"
        worksheet.Cells(1, 1) = "Value1"
        worksheet.Cells(2, 1) = "Value2"
        worksheet.SaveAs("d:\Actor.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault)
        workbook.Close()
    End Sub
End Module

与Excel在VB.net中的工作失败,没有明显原因。

答案1

得分: 5

Your code is wrong starting by first Dim excel line. You expect that would be an Excel.Application but it is not, it is System.Windows.Application (as I understand from error window). If that line were correct, next line is trying to create an unrelated Workbook object. Instead try:

Sub Main
    Dim xl As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Dim workbook = xl.Workbooks.Add()
    Dim worksheet As Worksheet = CType(workbook.ActiveSheet, Worksheet)
    worksheet.Name = "Export as Excel Project"
    worksheet.Cells(1, 1) = "Value1"
    worksheet.Cells(2, 1) = "Value2"
    worksheet.SaveAs("d:\Actor.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault)
    workbook.Close()
    xl.Quit()
End Sub

Note that file extension is XLSX, not XLS for current versions of Excel (for a long time really, and Excel wouldn't open it without a warning if you happen to save as XLS).

英文:

Your code is wrong starting by first Dim excel line. You expect that would be an Excel.Application but it is not, it is System.Windows.Application (as I understand from error window). If that line were correct, next line is trying to create an unrelated Workbook object. Instead try:

Sub Main
	Dim xl As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
	Dim workbook = xl.Workbooks.Add()
	Dim worksheet As Worksheet = CType(workbook.ActiveSheet, Worksheet)
	worksheet.Name = "Export as Excel Project"
	worksheet.Cells(1, 1) = "Value1"
	worksheet.Cells(2, 1) = "Value2"
	worksheet.SaveAs("d:\Actor.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault)
	workbook.Close()
	xl.Quit()
End Sub

Note that file extension is XLSX, not XLS for current versions of Excel (for a long time really, and Excel wouldn't open it without a warning if you happen to save as XLS).

huangapple
  • 本文由 发表于 2023年5月21日 19:35:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定