使用Excel 2010,Visual Studio Express 2013.
我在Visual Studio for Microsoft Excel 14.0中添加了参考对象库
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
super.draw(rect)
let top = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.bounds.width, height: 4))
UIColor.white.setFill()
top.fill()
let bottom = UIBezierPath(rect: CGRect(x: 0, y: self.bounds.height - 4, width: self.bounds.width, height: 4))
bottom.fill()
}
然而,它给了我以下错误:
未定义“Excel.Application”类型。
类型'Excel.Workbook'未定义。
未定义类型“Excel.Worksheet”。
类型'Excel.Range'未定义。
如果我使用的是excel版本的错误参考库,请说明如何将正确的对象库添加到列表中。
答案 0 :(得分:4)
您导入了错误的命名空间。
更改
([0-9]+)
到
Imports Microsoft.Office.Core
如果您还没有(将15.0替换为您的版本),则需要添加对 Microsoft Excel 15.0对象库的引用
而不是后期绑定,您可以使用正确的类型
Imports Microsoft.Office.Interop
最后,要正确清理Excel引用,因为它是一个COM对象,请将它放在代码中,以完成对象(例如,在关闭表单时)。对您创建的每个COM对象执行此操作。
' using the fully-qualified class name as an example.
' Excel.Application would work just fine because Microsoft.Office.Interop is imported
Dim oXL As Microsoft.Office.Interop.Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
' Start Excel and get Application object.
oXL = New Excel.Application()
' note that you have proper intellisense now because oXl is no longer just an Object.
oXl.Visible = True
答案 1 :(得分:0)
想出来。之前的代码来自https://support.microsoft.com/en-us/kb/301982。但是,当我以下面的方式编辑代码时,错误消失了。
Imports Microsoft.Office.Core
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oXl As Object
Dim oWB As Object
'Dim oXL As Excel.Application
'Dim oWB As Excel.Workbook
'Dim oSheet As Excel.Worksheet
'Dim oRng As Excel.Range
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True
End Sub
End Class