我目前正在开发一个使用VS2012和VB.Net实现VSTO的Excel项目。我有一个基本工作簿,其中有一个默认工作表,它根据下面的代码复制ADD命令。下面的代码工作正常,但我看不到如何访问这个添加的表。任何人都可以建议我如何引用这张新表吗?
I am currently developing a Excel project with VS2012 and VB.Net implementing VSTO. I have a base workbook which has a default sheet which from an ADD command ic copied as per the code below. The code below works fine but I cannot see how to access this added sheet. Can anyone suggest how I reference this new sheet?
Dim noSheets As Integer noSheets = Globals.ThisWorkbook.Worksheets.Count Globals.Sheet2.Copy(after:=Globals.ThisWorkbook.Sheets(noSheets)) Globals.Sheet2.Visible = Excel.XlSheetVisibility.xlSheetVisible任何指针赞赏 Phil
Any pointers appreciated Phil
推荐答案我的避风港没有使用VSTO,但是使用VBA,你可以通过工作表列表进行迭代。我相信VSTO必须有类似的东西。 VSTO有工作表吗?添加一个快速循环,检查工作表名称,并查看调用的内容。 I haven't used VSTO, but with VBA you can itterate through the list of sheets. I'm sure VSTO must have something similar. Does VSTO have a count of worksheets? Add a quick loop, check the worksheets name, and see what is is called.
您可以使用它来获取名称的工作表: You can use this to get the worksheet by name: private Excel.Worksheet GetWorksheetByName(string name) { foreach (Excel.Worksheet worksheet in this.Worksheets) { if (worksheet.Name == name) { return worksheet; } } throw new ArgumentException(); } private void ActivateWorksheetByName(string name) { GetWorksheetByName(name).Activate(); }
Source [ ^ ] 奇怪的是,工作表无法使用Copy()函数返回对创建的工作表的引用,重要的是要注意创建的工作表是Microsoft.Office.Interop.Excel.Worksheet而不是Micorosoft.Office.Tools.Excel.Worksheet。我认为应该有一种方法可以通过索引获得它,但我找不到它所以也许这个答案没有帮助......
Source[^] It is odd that the worksheet doesn't have a way to return a reference to the created worksheet with the Copy() function, and it is important to note that the created worksheet is a Microsoft.Office.Interop.Excel.Worksheet and not a Micorosoft.Office.Tools.Excel.Worksheet. I think there should be a way to get it by index, but I can't find it so maybe this answer isn't helpful...
带上一粒盐作为我没有VSTO,但根据我的理解,它是标准办公室互操作的扩展包装。 复制工作表后,新创建的工作表成为活动工作表;因此你应该能够用它来创建一个引用。 对于标准的interop,语法是: Take this with a grain of salt as I do not have VSTO, but based on my understanding it is an extended wrapper around the standard office interop. After copying a worksheet, the newly created worksheet becomes the activeworksheet; therefore you should be able to use that to create a reference. For standard interop the syntax would be: Dim newcopy As Excel.Worksheet = DirectCast(app.ActiveSheet, Excel.Worksheet)
其中app是对Excel应用程序的引用。 我相信VSTO语法将是:
where "app" is a reference to the Excel application. I believe the VSTO syntax would be:
Dim newcopy As Excel.Worksheet = DirectCast(Me.Application.ActiveSheet, Excel.Worksheet)
更多推荐
VS2012 excel应用程序
发布评论