ExcelTechnology
Workbook.Open Method and How to Open Workbook in C#
As is known, Excel object includes four common classes, Application, Workbook, Worksheet and Range. Each class has lots of methods, properties and events.
Workbook object is used to represent a Microsoft Excel workbook, which is a member of the Workbooks collection. All Workbook objects open in Microsoft Excel is contained in Workbooks collection. And there are three properties which can return a Workbook object: Workbooks Property, ActiveWorkbook property and ThisWorkbook property.
Besides property, there are lots of events of Workbook object. Workbook.Open is one of them, which occurs when the workbook is opened. This article introduces something about Workbook.Open Event and how to open Workbook in C#.
The namespace of Workbook.Open event is Microsoft.Office.Tools.Excel and assembly is Microsoft.Office.Tools.Excel which is in Microsoft.office.tools.excel.dll. In C#, the syntax of Workbook.Open event is public event WorkbookEvents_OpenEventHandler Open.
The following example by using C# shows the Open event handler, which maximizes the Microsoft Office Excel application window when the current workbook is opened.
private void WorkbookOpen()
{
this.Open +=
new Excel.WorkbookEvents_OpenEventHandler(
ThisWorkbook_Open);
}
void ThisWorkbook_Open()
{
this.Application.WindowState = Excel.XlWindowState.xlMaximized;
}
How to use C# to open Excel Workbook
If we want to open an Excel workbook in C#, it is necessary to use the Open methods of the Workbooks collection, which makes it possible to work with all open workbooks and to open workbooks. The following code shows you the method to open the workbooks under specified path in C#.
this.Application.Workbooks.Open(@ “YourPathYopurWorkbook.xls”,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing);
