Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

vb.net - Open workbook and select worksheet from winform

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.

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.

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.

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

front page

Welcome Page enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

That should get you started


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...