vba 将 excel 数据导入和导出到 vb.net 时出错,“Microsoft.Office.Interop.Excel.Application 未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20973280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:14:39  来源:igfitidea点击:

Error importing and exporting excel data to vb.net, "Microsoft.Office.Interop.Excel.Application is not defined"

vb.netexcelexcel-vbavba

提问by Ahmed Faizan

I am trying to run a file I downloaded to import and export data from excel. I downloaded from from the following site:

我正在尝试运行我下载的文件以从 excel 导入和导出数据。我是从以下网站下载的:

http://www.sattsoft.com/sourcecodes/details/1/9/import-export-data-to-excel-using-vb-net.html

http://www.sattsoft.com/sourcecodes/details/1/9/import-export-data-to-excel-using-vb-net.html

When I run the file the error comes on the following lines

当我运行文件时,错误出现在以下几行

    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet

These are the errors that come on those respective lines

这些是相应行上的错误

Type "Microsoft.Office.Interop.Excel.Application is not defined"

输入“Microsoft.Office.Interop.Excel.Application 未定义”

Type "Microsoft.Office.Interop.Excel.Workbook is not defined"

输入“Microsoft.Office.Interop.Excel.Workbook 未定义”

Type "Microsoft.Office.Interop.Excel.Worksheet is not defined"

输入“Microsoft.Office.Interop.Excel.Worksheet 未定义”

What I want to do is to run the file without the errors so that I can import and export data from excel to vb.net datagridview. I am using Excel 2010 and visual studio 2010

我想要做的是在没有错误的情况下运行文件,以便我可以将数据从 excel 导入和导出到 vb.net datagridview。我正在使用 Excel 2010 和 Visual Studio 2010

How do I get around this error so I can run the file?

如何解决此错误以便我可以运行该文件?

回答by David Sdot

Add COM reference Microsoft.Office.Interop.Excel to your project, it's called Microsoft Excel XX.Y Object Library (XX.Y is version number depending of installed Excel).

将 COM 引用 Microsoft.Office.Interop.Excel 添加到您的项目中,它称为 Microsoft Excel XX.Y 对象库(XX.Y 是版本号,取决于安装的 Excel)。

回答by Yoda

I had to use this library in references from the "Extensions" section (not not under COM); had to select Microsoft.Office.Interop.Excel, version 15.0.0.0. That fixed the error. Selecting the COM reference did not resolve this message.

我不得不在“扩展”部分(不在 COM 下)的引用中使用这个库;必须选择 Microsoft.Office.Interop.Excel,版本 15.0.0.0。这修复了错误。选择 COM 引用未解决此消息。