打开工作簿并从 winform 中选择工作表 [英] Open workbook and select worksheet from winform

查看:30
本文介绍了打开工作簿并从 winform 中选择工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个项目.一个项目是一个 windows 窗体,另一个项目是一个 Excel 工作簿项目.然后我将 Excel 工作簿项目添加到 windows 窗体项目中.

I created two projects. One project is a windows form, and the other project is an Excel workbook project. I then added the Excel workbook project to the windows form project.

窗体是一个欢迎页面.该页面有一些按钮,可以在特定工作表上打开工作簿.例如,下面的屏幕截图有一个按钮摘要工作表,当用户单击该按钮时,我想在该名称的电子表格上打开 Excel 项目.

The window form is a welcome page. That page has some buttons that are supposed to open the workbook on a specific worksheet. For example, the screenshot below has a button Summary Worksheet, when the user clicks that button, I want to open the Excel project, on a spreadsheet by that name.

如果我的欢迎页面表单是我的 Excel 工作簿项目或存储在我的硬盘驱动器上的任何其他工作簿的一部分,我会知道如何打开工作簿.但是因为它们是合并为一个的单独项目,所以我不知道如何从表单访问我的工作簿.我想我想添加对 Excel 的 winform 的引用.

If my welcome page form was part of my Excel workbook project or any other workbook stored on my hard drive, I would know how to open the workbook. But because they are separate projects combined into one, I have no idea how I can access my workbook from the form. I guess I would like to add a reference to the winform for Excel.

我还附上了我的 VS 2012 项目窗口的屏幕截图.

I also attached a screenshot of my VS 2012 project window.

欢迎页面

推荐答案

这是一个解决方案,包含两个项目.当您构建解决方案时,您将拥有 Windows 窗体的 .exe 和 Excel 工作簿的 .xlsx 和 .vsto.执行所需操作的最简单方法是使用 Excel 互操作,并且您需要知道磁盘上工作簿的路径.代码可能是这样的:

It is one solution that is holding two projects. When you build the solution then you will have the .exe for your windows form and the .xlsx and .vsto for your Excel workbook. The easiest way to do what you need is to use Excel interop, and you'll need to know the path on disk to your workbook. The code could be something along these lines:

Imports Microsoft.Office.Interop

Dim xlApp as new Excel.Application
xlApp.Visible = True
Dim xlBook as new Excel.Workbook = xlApp.Workbooks.Open("path/toyour/workbook.xlsx")
xlBook.Worksheets("Welcome Sheet").Activate

这应该会让你开始

这篇关于打开工作簿并从 winform 中选择工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆