如何从WindowsForm项目中显示/打开/启动ExcelWorkbook项目(Sheet1)? [英] How to show/open/start ExcelWorkbook project(Sheet1) from WindowsForm project?
问题描述
如标题所示,让我说一个解决方案中有两个项目。我有三个,最后一个只是一个例子。一个项目是调用ExcelWorkbook
( EWB
)
As shown in title, lets say that I have two projects in one solution. I have three, the last one is just for an example. One project is ExcelWorkbook
called (EWB
)
img src =https://i.stack.imgur.com/EDnk8.jpgalt =enter image description here>
另一个是 WindowsForm
调用( Core
)。我想从 Core
显示/打开/启动这个 EWB
。我添加了第一个参考:
and another one is WindowsForm
called (Core
). I would like to show/open/start this EWB
from Core
. I've added references to the first one:
,我可以看到该工作簿的工作表。它不是那么容易,因为当你从另一个项目显示表单。只需要注意, EWB.Sheet1
不包含一个构造函数,它采用 0
参数,其构造函数有两个参数。
and I can see sheets of that specific workbook. It's not so easy as it is like when you're showing form from another project. Just for note, EWB.Sheet1
does not contain a constructor that takes 0
arguments, its constructor has two argument, ...
以下是这些参数:
- Microsoft.Office .Tools.Excel.Factory 工厂
- IServiceProvider serviceProvider
- Microsoft.Office.Tools.Excel.Factory factory
- IServiceProvider serviceProvider
对于这个项目,它表示如下:
For this project its says as following:
我想在按钮点击事件上打开该工作簿,但不知道如何:
I would like to open that workbook on button click event but I don't know how to:
- 开始
- 显示
- 打开
- 或...
- start it
- show it
- open it
- or ...
任何想法如何完成?任何帮助将不胜感激!
Any idea how this could be done? Any help will be appreciated!
我如何从另一个项目打开表单,这是我打开 EWB
。
This how I open form from another project, and this is the way I would like to open that EWB
.
using WindowsFormsApplication1;
private void CoreBtn_Click(object sender, EventArgs e)
{
Form1 newForm = new Form1();
newForm.Show();
}
推荐答案
添加引用 Microsoft.Office.Interop.Excel
和使用Excel = Microsoft.Office.Interop.Excel;
这里有一些简单的代码让你开始:
Here is some simple code to get you started:
Excel.Application xlApp = new Excel.Application(); // create new Excel application
xlApp.Visible = true; // application becomes visible
xlApp.Workbooks.Open(@"C:\workbook.xlsx"); // open the workbook from file path
xlApp.Quit(); // close the application when you are done
有关更多信息,请参阅 Excel对象模型查看此处。
And for more information the Excel Object Model look here.
这篇关于如何从WindowsForm项目中显示/打开/启动ExcelWorkbook项目(Sheet1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!