vb.net 如何从 Visual Basic .NET 2010 自动化 Microsoft Excel 2010
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9546726/
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
How to automate Microsoft Excel 2010 from Visual Basic .NET 2010
提问by Naad Dyr
i have my database in VB2010 and i want to push 2 tables from that database to MS Excel 2010. I came across http://support.microsoft.com/kb/301982but even after following the steps i'm getting an error: Type 'Excel.Application' is not defined.
我在 VB2010 中有我的数据库,我想将该数据库中的 2 个表推送到 MS Excel 2010。我遇到了http://support.microsoft.com/kb/301982,但即使按照以下步骤操作,我仍然收到错误消息:未定义类型“Excel.Application”。
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Excel
Imports System.Data
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
'' etc...
End Sub
End Class
回答by Nadeer Madampat Tanalur
Try adding reference from Menu: Project >> Add Reference >> .NET Tab
尝试从菜单添加引用:项目 >> 添加引用 >> .NET 选项卡
And add these:
并添加这些:
Microsoft.Office.infoPath.Excel
Microsoft.Office.Tools.Excel
回答by Hans Passant
Imports Microsoft.Office.Interop.Excel
That means that the type name you use is just plain Application, not Excel.Application. That's going to cause trouble though, that will be an ambiguous type name in a Winforms or WPF application, they also have a common type named Application. Which is why you so commonly see Excel.Application in sample code. With Excelbeing a namespace alias. Which you create like this:
这意味着您使用的类型名称只是普通的Application,而不是 Excel.Application 。但这会引起麻烦,这在 Winforms 或 WPF 应用程序中将是一个模棱两可的类型名称,它们也有一个名为 Application 的通用类型。这就是为什么您在示例代码中如此常见地看到 Excel.Application。与Excel的是一个命名空间别名。您像这样创建:
Imports Excel = Microsoft.Office.Interop.Excel
Now you can use Excel.Applicationwithout trouble.
现在您可以毫无困难地使用Excel.Application。
VB.NET also permits this:
VB.NET 也允许这样做:
Imports Microsoft.Office.Interop
But that doesn't work in C#, the namespace alias creates more portable code. Not sure if that's important at all.
但这在 C# 中不起作用,命名空间别名创建了更可移植的代码。不确定这是否重要。
回答by Tahbaza
Use of the direct Excel and other office automation libraries requires the full product to be installed on the machine running your code. Look into use NPOIor other Excel automation library to accomplish the same without having to install the office suite.
直接使用 Excel 和其他办公自动化库需要在运行代码的机器上安装完整的产品。考虑使用NPOI或其他 Excel 自动化库来完成相同的操作,而无需安装办公套件。
Installing Office on a server is usually not advisable, and even if you're running in an end client desktop environment you'd have to worry about the version of Office installed and other client configuration variables of the installation to get a successful execution every time.
在服务器上安装 Office 通常是不可取的,即使您在终端客户端桌面环境中运行,您也必须担心安装的 Office 版本和安装的其他客户端配置变量,以便每次都能成功执行.
If you insist on the path you've chosen initially, you'll need to capture a reference to either a currently running Excel application instance (GetObject) or create one for yourself to use (CreateObject), like so:
如果您坚持最初选择的路径,则需要捕获对当前运行的 Excel 应用程序实例 (GetObject) 的引用或创建一个供自己使用 (CreateObject),如下所示:
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True
回答by Check-Kay Wong
Here is an article. It summarises the use of Excel from VS2010. There is an exemple too create in VS2010. the sample works
这是一篇文章。它总结了 VS2010 中 Excel 的使用。在 VS2010 中也有一个例子。示例作品
http://checktechno.blogspot.com/2013/01/all-you-need-to-know-with-visual-basic.html
http://checktechno.blogspot.com/2013/01/all-you-need-to-know-with-visual-basic.html
You also need to make sure the computer have Excel correctly installed. Do you have Excel 2010 Starter or the regular version?
您还需要确保计算机正确安装了 Excel。你有 Excel 2010 Starter 还是普通版?
As Tahbaza said, if the CreateObject fails, that means that something is wrong in that PC.
正如 Tahbaza 所说,如果 CreateObject 失败,则意味着该 PC 出现问题。