问题
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
)

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:

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, ...
Here are these arguments:
- 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!
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();
}
回答1:
Add a reference to Microsoft.Office.Interop.Excel
and using 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
And for more information the Excel Object Model look here.
回答2:
Excel projects are very similar to their VBA counterparts: if your project involves functionality that can only be accessed when a specific workbook is opened, you're writing the VBA code "behind" that specific workbook, in the VBA editor - in VSTO that's a "workbook" project, in Excel 2007+ it's a macro-enabled workbook (.xlsm), in earlier versions it's a workbook that runs code (macros).
However if your project involves functionality that "runs" at the application (Excel) level and is available regardless of whether a workbook is opened or not - let alone whether a specific workbook is opened, you're writing an Excel add-in. Add-ins are loaded when Excel starts and "listen" to whatever to feel like, letting you enable specific functionality when the workbook you're opening has such or such filename prefix, or when the worksheet that's activating has some specific label/name, displaying a form (WinForms) or a window (WPF), fetch, insert, update and delete database records, talk to a web service, whatever.
To open a workbook from a running add-in, the file is opened using the Excel object model (Application.Workbooks.Open()
), and the workbook in question needs not to be included in the solution. You'll need the workbook project only if that workbook needs to run macros (in which case you also have the VBA option), or if for compleness' sake you include it as a "template", for versioning purposes.
That being settled, in order to launch Excel with a specific workbook from any other WinForms app with merely a parameterless constructor and a Show()
method you'll have to wrap yourself some launcher that has a parameterless constructor and that exposes a Show()
method (then http://office.microsoft.com/en-us/excel-help/command-line-switches-for-excel-HA010158030.aspx command-line Excel doc could be handy - or reference the Excel object model as shown by Porkbutts' answer). There might be other ways, but I think command-line launch is the simplest if going through VSTO isn't an option in your standalone WinForms app.
回答3:
How about creating some assembly process in ExcelWorkbook project and checking the process is active or not in the windows form Core.
You can detect the start and stops of the processes running using System.Diagnostics.Process
回答4:
First this assumes a lot since I do not know the scope of your projects ie the code (DLL, LIBS, EXE)
I am assuming ExcelWorkbook is a Windows Form project.
Build the ExcelWorkbook Project. You should now have a Exectuable file: ExcelWorkbook.exe.
In the "Core" Project right click References and select add.
choose the browse button and locate the executable for ExcelWorkbook.exe select ok.
You should now see a reference to the ExcelWorkbook Project.
In the class/form ect you want to utilize the ExcelWorkbook project you will add the using statement
using eBook= ExcelWorkbook //or what ever vairable name you want
so now you can reference the ExcelWorkbook project
public eBook.Form1 _excelWorkbookProject;
_excelWorkbookProject = new eBook.Form1()
if it is a form then to show it
_excelWorkbookProject.ShowDialog();
// if you want it to remain the top level window.
or
_excelWorkbookProject.Show();
来源:https://stackoverflow.com/questions/14257580/how-to-show-open-start-excelworkbook-projectsheet1-from-windowsform-project