8using System.Collections.Generic;
31 private string filename;
32 private List<Worksheet> worksheets;
35 private IPassword workbookProtectionPassword;
36 private bool lockWindowsIfProtected;
37 private bool lockStructureIfProtected;
38 private int selectedWorksheet;
40 private readonly List<Color> mruColors =
new List<Color>();
41 internal bool importInProgress;
50 internal AuxiliaryData AuxiliaryData {
get;
private set; }
57 get {
return shortener; }
66 get {
return currentWorksheet; }
77 get {
return filename; }
78 set { filename = value; }
86 get {
return lockStructureIfProtected; }
94 get {
return lockWindowsIfProtected; }
102 get {
return workbookMetadata; }
103 set { workbookMetadata = value; }
111 get {
return selectedWorksheet; }
131 get {
return worksheets; }
189 this.filename = filename;
199 public Workbook(
string filename,
string sheetName,
bool sanitizeSheetName)
202 this.filename = filename;
203 if (sanitizeSheetName)
233 mruColors.Add(color);
265 throw new WorksheetException(
"The worksheet with the name '" + name +
"' already exists.");
268 int number = GetNextWorksheetId();
270 currentWorksheet = newWs;
271 worksheets.Add(newWs);
272 shortener.SetCurrentWorksheetInternal(currentWorksheet);
284 if (sanitizeSheetName)
315 if (sanitizeSheetName)
318 worksheet.SheetName = name;
322 if (
string.IsNullOrEmpty(worksheet.
SheetName))
326 for (
int i = 0; i < worksheets.Count; i++)
328 if (worksheets[i].SheetName == worksheet.
SheetName)
334 worksheet.SheetID = GetNextWorksheetId();
335 currentWorksheet = worksheet;
336 worksheets.Add(worksheet);
337 worksheet.WorkbookReference =
this;
348 Worksheet worksheetToRemove = worksheets.FirstOrDefault(w => w.SheetName == name);
349 if (worksheetToRemove ==
null)
351 throw new WorksheetException(
"The worksheet with the name '" + name +
"' does not exist.");
353 int index = worksheets.IndexOf(worksheetToRemove);
354 bool resetCurrentWorksheet = worksheetToRemove == currentWorksheet;
367 if (index < 0 || index >= worksheets.Count)
371 bool resetCurrentWorksheet = worksheets[index] == currentWorksheet;
380 internal void ResolveMergedCells()
382 foreach (
Worksheet worksheet
in worksheets)
384 worksheet.ResolveMergedCells();
397 shortener.SetCurrentWorksheetInternal(currentWorksheet);
398 return currentWorksheet;
410 shortener.SetCurrentWorksheetInternal(currentWorksheet);
411 return currentWorksheet;
421 int index = worksheets.IndexOf(worksheet);
424 throw new WorksheetException(
"The passed worksheet object is not in the worksheet collection.");
426 currentWorksheet = worksheets[index];
427 shortener.SetCurrentWorksheetInternal(worksheet);
437 int index = worksheets.FindIndex(w => w.SheetName == name);
440 throw new WorksheetException(
"No worksheet with the name '" + name +
"' was found in this workbook.");
442 selectedWorksheet = index;
454 if (worksheetIndex < 0 || worksheetIndex > worksheets.Count - 1)
456 throw new RangeException(
"The worksheet index " + worksheetIndex +
" is out of range");
458 selectedWorksheet = worksheetIndex;
459 ValidateWorksheets();
470 selectedWorksheet = worksheets.IndexOf(worksheet);
471 if (selectedWorksheet < 0)
473 throw new WorksheetException(
"The passed worksheet object is not in the worksheet collection.");
475 ValidateWorksheets();
486 int index = worksheets.FindIndex(w => w.SheetName == name);
489 throw new WorksheetException(
"No worksheet with the name '" + name +
"' was found in this workbook.");
491 return worksheets[index];
502 if (index < 0 || index > worksheets.Count - 1)
504 throw new RangeException(
"The worksheet index " + index +
" is out of range");
506 return worksheets[index];
518 lockWindowsIfProtected = protectWindows;
519 lockStructureIfProtected = protectStructure;
520 workbookProtectionPassword.SetPassword(password);
521 if (!protectWindows && !protectStructure)
542 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
556 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
569 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
581 public Worksheet CopyWorksheetTo(
string sourceWorksheetName,
string newWorksheetName, Workbook targetWorkbook,
bool sanitizeSheetName =
true)
584 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
596 public Worksheet CopyWorksheetTo(
int sourceWorksheetIndex,
string newWorksheetName, Workbook targetWorkbook,
bool sanitizeSheetName =
true)
599 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
614 if (targetWorkbook ==
null)
618 if (sourceWorksheet ==
null)
639 internal void ValidateWorksheets()
641 if (importInProgress)
646 int worksheetCount = worksheets.Count;
647 if (worksheetCount == 0)
651 for (
int i = 0; i < worksheetCount; i++)
655 if (i == selectedWorksheet)
657 throw new WorksheetException(
"The worksheet with the index " + selectedWorksheet +
" cannot be set as selected, since it is set hidden");
670 worksheets.RemoveAt(index);
671 if (worksheets.Count > 0)
673 for (
int i = 0; i < worksheets.Count; i++)
675 worksheets[i].SheetID = i + 1;
677 if (resetCurrentWorksheet)
679 currentWorksheet = worksheets[worksheets.Count - 1];
681 if (selectedWorksheet == index || selectedWorksheet > worksheets.Count - 1)
683 selectedWorksheet = worksheets.Count - 1;
688 currentWorksheet =
null;
689 selectedWorksheet = 0;
691 ValidateWorksheets();
698 private int GetNextWorksheetId()
700 if (worksheets.Count == 0)
704 return worksheets.Max(w => w.SheetID) + 1;
712 worksheets =
new List<Worksheet>();
713 workbookMetadata =
new Metadata();
714 shortener =
new Shortener(
this);
715 workbookProtectionPassword =
new LegacyPassword(LegacyPassword.PasswordType.WorkbookProtection);
716 AuxiliaryData =
new AuxiliaryData();
Compound class representing a color in various representations (RGB, indexed, theme,...
static Color CreateRgb(SrgbColor color)
Creates an Color from an RGB/ARGB color.
Class for exceptions regarding range incidents (e.g. out-of-range).
Class for exceptions regarding worksheet incidents.
Class to register plug-in classes that extends the functionality of NanoXLSX (Core or any other packa...
static bool Initialize()
Initializes the plug-in loader process. If already initialized, the method returns without action.
Class to provide access to the current worksheet with a shortened syntax.
Class representing an Office theme.
Class providing general validator methods.
static void ValidateGenericColor(string hexCode, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills,...
void AddWorksheet(Worksheet worksheet, bool sanitizeSheetName)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
bool Hidden
Gets or sets whether the whole workbook is hidden.
Workbook(string filename, string sheetName, bool sanitizeSheetName)
Constructor with filename ant the name of the first worksheet.
void SetSelectedWorksheet(Worksheet worksheet)
Sets the selected worksheet in the output workbook.
void AddMruColor(string color)
Adds a color value (HEX; 6-digit RGB or 8-digit ARGB) to the MRU list.
Workbook()
Default constructor. No initial worksheet is created. Use AddWorksheet(string) (or overloads) to add ...
void ClearMruColors()
Clears the MRU color list.
void SetCurrentWorksheet(Worksheet worksheet)
Sets the current worksheet.
void SetSelectedWorksheet(int worksheetIndex)
Sets the selected worksheet in the output workbook.
int SelectedWorksheet
Gets the selected worksheet. The selected worksheet is not the current worksheet while design time bu...
void AddWorksheet(string name, bool sanitizeSheetName)
Adding a new Worksheet with a sanitizing option. The new worksheet will be defined as current workshe...
void RemoveWorksheet(int index)
Removes the defined worksheet based on its index. If the worksheet is the current or selected workshe...
Worksheet CopyWorksheetIntoThis(int sourceWorksheetIndex, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its index.
Shortener WS
Gets the shortener object for the current worksheet.
void SetWorkbookProtection(bool state, bool protectWindows, bool protectStructure, string password)
Sets or removes the workbook protection. If protectWindows and protectStructure are both false,...
Theme WorkbookTheme
Gets or sets the theme of the workbook. The default is defined by Theme.GetDefaultTheme....
string Filename
Gets or sets the filename of the workbook.
Workbook(string sheetName)
Constructor with additional parameter to create a default worksheet with the specified name....
Workbook(bool createWorkSheet)
Constructor with additional parameter to create a default worksheet. This constructor can be used to ...
void AddWorksheet(Worksheet worksheet)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
IReadOnlyList< Color > GetMruColors()
Gets the MRU color list.
Worksheet GetWorksheet(int index)
Gets a worksheet from this workbook by index.
bool LockStructureIfProtected
Gets whether the structure are locked if workbook is protected. See also SetWorkbookProtection.
Workbook(string filename, string sheetName)
Constructor with filename ant the name of the first worksheet.
void SetSelectedWorksheet(string name)
Sets the selected worksheet in the output workbook.
Worksheet SetCurrentWorksheet(int worksheetIndex)
Sets the current worksheet.
Worksheet CopyWorksheetTo(string sourceWorksheetName, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its name into another workbook.
void AddMruColor(Color color)
Adds a generic color value. This can be an RGB/ARGB color, Auto, Theme, Indexed or System color.
Worksheet CurrentWorksheet
Gets the current worksheet.
Worksheet CopyWorksheetTo(int sourceWorksheetIndex, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its index into another workbook.
List< Worksheet > Worksheets
Gets the list of worksheets in the workbook.
void AddWorksheet(string name)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
Metadata WorkbookMetadata
Meta data object of the workbook.
bool LockWindowsIfProtected
Gets whether the windows are locked if workbook is protected. See also SetWorkbookProtection.
bool UseWorkbookProtection
Gets or sets whether the workbook is protected.
virtual IPassword WorkbookProtectionPassword
Password instance of the protected workbook. If a password was set, the pain text representation and ...
Worksheet CopyWorksheetIntoThis(Worksheet sourceWorksheet, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of any workbook into the current workbook.
Worksheet CopyWorksheetIntoThis(string sourceWorksheetName, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its name.
Worksheet SetCurrentWorksheet(string name)
Sets the current worksheet.
void RemoveWorksheet(string name)
Removes the defined worksheet based on its name. If the worksheet is the current or selected workshee...
Worksheet GetWorksheet(string name)
Gets a worksheet from this workbook by name.
static Worksheet CopyWorksheetTo(Worksheet sourceWorksheet, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of any workbook into the another workbook.
Class representing a worksheet of a workbook.
Worksheet Copy()
Creates a (dereferenced) deep copy of this worksheet.
string SheetName
Gets or sets the name of the worksheet.
void SetSheetName(string name)
Validates and sets the worksheet name.
static string SanitizeWorksheetName(string input, Workbook workbook)
Sanitizes a worksheet name.
Interface to represent a protection password, either for workbooks or worksheets. The implementations...