[]
A workbook is a spreadsheet document comprising one or more worksheets. A collection of workbooks creates a workbook set. By default, a workbook set contains a single workbook and a workbook contains one worksheet (Sheet1). Spread for WPF provides all the essential properties and methods needed to create a workbook and perform complex operations on data present in a spreadsheet.
You can perform the following actions in a workbook.
You can get the workbook using the Workbook property of the GcSpreadSheet class.
Refer to the following example code to access a workbook.
C#
// Access spreadSheet1 component as the workbook.
GrapeCity.Spreadsheet.IWorkbook workbook = spreadSheet1.Workbook;
VB
' Access spreadSheet1 component as the workbook.
Dim workbook As GrapeCity.Spreadsheet.IWorkbook = spreadSheet1.Workbook
You can create one or more new workbooks within a workbook set using the Add method of the IWorkbooks interface.
Refer to the following example code to add a new workbook.
C#
// Create a new workbook in the same workbook set.
GrapeCity.Spreadsheet.IWorkbook workbook = spreadSheet1.Workbook.WorkbookSet.Workbooks.Add("Book2");
VB
'Create a new workbook in the same workbook set.
Dim workbook As GrapeCity.Spreadsheet.IWorkbook = spreadSheet1.Workbook.WorkbookSet.Workbooks.Add("Book2")
Once a workbook is created, you can open it to make any changes. To open an existing Excel workbook, you can use either Open method of IWorkbooks interface or OpenExcel method of the GcSpreadSheet class.
When the Open method is used, it opens an existing workbook as a new workbook in the workbook set. However, it does not appear in the GcSpreadSheet control and GcSpreadSheet will show the content of its old workbook. Usually, this method is helpful when working with external references. For example, use a formula to refer to a cell range of another workbook.
On the other hand, the OpenExcel method opens an existing workbook in the GcSpreadSheet control. This method replaces the current workbook in the GcSpreadsheet control with the newly opened workbook.
Refer to the following example code to open an existing workbook.
C#
// Open a workbook in a workbook set.
GrapeCity.Spreadsheet.IWorkbook workbook1 = spreadSheet1.Workbook.WorkbookSet.Workbooks.Open(@"D:\TestFile.xlsx");
// Open a workbook in GcSpreadSheet control.
spreadSheet1.OpenExcel(@"D:\TestFile.xlsx");
VB
' Open a workbook in a workbook set.
Dim workbook2 As GrapeCity.Spreadsheet.IWorkbook = spreadSheet1.Workbook.WorkbookSet.Workbooks.Open("D:\TestFile.xlsx")
' Open a workbook in GcSpreadSheet control.
spreadSheet1.OpenExcel("D:\TestFile.xlsx")
You can save the changes made in the workbook by using the SaveAs method of the IWorkbook interface.
Refer to the following example code to save your workbook.
C#
// Save the workbook.
spreadSheet1.Workbook.ActiveSheet.Cells[0, 0].Value = 123;
spreadSheet1.Workbook.ActiveSheet.Cells[0, 1].Value = 456;
spreadSheet1.Workbook.ActiveSheet.Cells[0, 2].Value = 789;
spreadSheet1.Workbook.SaveAs(@"SaveWorkbook.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook);
VB
' Save the workbook.
spreadSheet1.Workbook.ActiveSheet.Cells(0, 0).Value = 123
spreadSheet1.Workbook.ActiveSheet.Cells(0, 1).Value = 456
spreadSheet1.Workbook.ActiveSheet.Cells(0, 2).Value = 789
spreadSheet1.Workbook.SaveAs("SaveWorkbook.xlsx", GrapeCity.Spreadsheet.IO.FileFormat.OpenXMLWorkbook)
For more information about saving the workbook to different file formats, see Save Files.
Once a workbook is created, you can protect it from any modifications so that other users can't perform certain operations on it. To restrict editing of a workbook, use the Protect method of the IWorkbook interface.
!type=note
Note: This function is only supported in the Excel I/O scenarios.
C#
// Protect workbook.
spreadSheet1.Workbook.Protect(GrapeCity.Spreadsheet.WorkbookLocks.All, "password");
VB
' Protect workbook.
spreadSheet1.Workbook.Protect(GrapeCity.Spreadsheet.WorkbookLocks.All, "password")
Document properties contain information about a workbook, such as title, subject, author, subject, etc. Spread for WPF allows you to manage these properties within the workbook.
To customize the document properties, use the BuiltinDocumentProperties property of the IWorkbook interface. This property accepts the BuiltinDocumentProperties enumeration as a parameter to access all document properties, which can then be modified to meet your requirements.
Refer to the following example code to set the author name for a workbook.
C#
// Set document properties.
spreadSheet1.Workbook.BuiltinDocumentProperties[GrapeCity.Core.BuiltinDocumentProperties.Company].Text = "AuthorName";
VB
'Set document properties.
spreadSheet1.Workbook.BuiltinDocumentProperties(GrapeCity.Core.BuiltinDocumentProperties.Company).Text = "AuthorName"
You can change the default theme of a workbook to enhance the visual appearance of the worksheets. For example, when you apply a theme to a workbook, the styles of font, cell color, and sheet may change depending on the modified theme.
The ApplyTheme method of the IWorkbook interface is used to apply a theme to the current workbook. The easiest way to apply a custom theme is to pass a theme file. A theme file can be an XLSX or an theme file with extension *.THMX.
Refer to the following example code to apply the Damask theme to the workbook.
C#
// Apply a theme to the workbook.
spreadSheet1.Workbook.ApplyTheme("Damask.thmx");
VB
' Apply a theme to the workbook.
spreadSheet1.Workbook.ApplyTheme("Damask.thmx")