Class Workbook
- Namespace
- FileFormat.Cells
- Assembly
- FileFormat.Cells.dll
Represents an Excel workbook with methods for creating, modifying, and saving content.
public class Workbook : IDisposable
- Inheritance
-
Workbook
- Implements
- Inherited Members
Constructors
Workbook()
Default constructor to create a new workbook.
public Workbook()
Workbook(string)
Overloaded constructor to open an existing workbook from a file.
public Workbook(string filePath)
Parameters
filePathstring
Fields
spreadsheetDocument
protected SpreadsheetDocument spreadsheetDocument
Field Value
stylesPart
protected WorkbookStylesPart stylesPart
Field Value
workbookpart
protected WorkbookPart workbookpart
Field Value
worksheetPart
protected WorksheetPart worksheetPart
Field Value
Properties
BuiltinDocumentProperties
Get or set built-in document properties of the workbook.
public BuiltInDocumentProperties BuiltinDocumentProperties { get; set; }
Property Value
DefaultStyleId
Get the ID of the default style.
public uint DefaultStyleId { get; }
Property Value
Worksheets
public List<Worksheet> Worksheets { get; }
Property Value
Methods
AddSheet(string)
Add a new worksheet to the workbook.
public Worksheet AddSheet(string sheetName)
Parameters
sheetNamestring
Returns
CopySheet(string, string)
Copies an existing sheet within the workbook to a new sheet.
public bool CopySheet(string sourceSheetName, string newSheetName)
Parameters
sourceSheetNamestringThe name of the sheet to be copied.
newSheetNamestringThe name of the new sheet to be created.
Returns
- bool
Returns
trueif the sheet is successfully copied; otherwise,false.
CreateStyle(string, double, string)
Create a custom style for the workbook.
public uint CreateStyle(string fontName, double fontSize, string hexColor)
Parameters
Returns
Dispose()
This method releases unmanaged resources.
public void Dispose()
Dispose(bool)
Releases the unmanaged resources and optionally releases the managed resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingbool
GetHiddenSheets()
Retrieves a list of hidden or very hidden sheets from the workbook.
public List<Tuple<string, string>> GetHiddenSheets()
Returns
- List<Tuple<string, string>>
A list of tuples where each tuple contains the sheet ID and sheet name for hidden sheets.
Exceptions
- InvalidOperationException
Thrown when the workbook part is not initialized.
- ArgumentNullException
Thrown when a sheet's ID or Name is null.
RemoveSheet(string)
Remove a worksheet from the workbook.
public bool RemoveSheet(string sheetName)
Parameters
sheetNamestring
Returns
RenameSheet(string, string)
Renames an existing sheet within the workbook.
public bool RenameSheet(string existingSheetName, string newSheetName)
Parameters
existingSheetNamestringThe current name of the sheet to be renamed.
newSheetNamestringThe new name for the sheet.
Returns
- bool
Returns
trueif the sheet is successfully renamed; otherwise,false.
ReorderSheets(string, int)
Reorders a sheet within the workbook to a new position.
public void ReorderSheets(string sheetName, int newPosition)
Parameters
sheetNamestringThe name of the sheet to be reordered.
newPositionintThe new position (index) where the sheet should be moved.
Save()
Save the workbook using the original file path.
public void Save()
Save(Stream)
Save the workbook to a given stream.
public void Save(Stream stream)
Parameters
streamStream
Save(string)
Save the workbook to a specified file path.
public void Save(string filePath)
Parameters
filePathstring
SetSheetVisibility(string, SheetVisibility)
Sets the visibility of a sheet within the workbook.
public void SetSheetVisibility(string sheetName, SheetVisibility visibility)
Parameters
sheetNamestringThe name of the sheet whose visibility is to be set.
visibilitySheetVisibilityThe visibility state to be applied to the sheet (Visible, Hidden, or VeryHidden).
Exceptions
- ArgumentException
Thrown when the sheet is not found in the workbook.
UpdateDefaultStyle(string, double, string)
Update the default style of the workbook.
public void UpdateDefaultStyle(string newFontName, double newFontSize, string hexColor)