8using System.Collections.Generic;
30 private string filename;
31 private List<Worksheet> worksheets;
34 private IPassword workbookProtectionPassword;
35 private bool lockWindowsIfProtected;
36 private bool lockStructureIfProtected;
37 private int selectedWorksheet;
39 private readonly List<string> mruColors =
new List<string>();
40 internal bool importInProgress;
49 internal AuxiliaryData AuxiliaryData {
get;
private set; }
56 get {
return shortener; }
65 get {
return currentWorksheet; }
76 get {
return filename; }
77 set { filename = value; }
85 get {
return lockStructureIfProtected; }
93 get {
return lockWindowsIfProtected; }
101 get {
return workbookMetadata; }
102 set { workbookMetadata = value; }
110 get {
return selectedWorksheet; }
130 get {
return worksheets; }
188 this.filename = filename;
198 public Workbook(
string filename,
string sheetName,
bool sanitizeSheetName)
201 this.filename = filename;
202 if (sanitizeSheetName)
222 if (color !=
null && color.Length == 6)
224 color =
"FF" + color;
259 throw new WorksheetException(
"The worksheet with the name '" + name +
"' already exists.");
262 int number = GetNextWorksheetId();
264 currentWorksheet = newWs;
265 worksheets.Add(newWs);
266 shortener.SetCurrentWorksheetInternal(currentWorksheet);
278 if (sanitizeSheetName)
309 if (sanitizeSheetName)
312 worksheet.SheetName = name;
316 if (
string.IsNullOrEmpty(worksheet.
SheetName))
320 for (
int i = 0; i < worksheets.Count; i++)
322 if (worksheets[i].SheetName == worksheet.
SheetName)
328 worksheet.SheetID = GetNextWorksheetId();
329 currentWorksheet = worksheet;
330 worksheets.Add(worksheet);
331 worksheet.WorkbookReference =
this;
342 Worksheet worksheetToRemove = worksheets.FirstOrDefault(w => w.SheetName == name);
343 if (worksheetToRemove ==
null)
345 throw new WorksheetException(
"The worksheet with the name '" + name +
"' does not exist.");
347 int index = worksheets.IndexOf(worksheetToRemove);
348 bool resetCurrentWorksheet = worksheetToRemove == currentWorksheet;
361 if (index < 0 || index >= worksheets.Count)
365 bool resetCurrentWorksheet = worksheets[index] == currentWorksheet;
374 internal void ResolveMergedCells()
376 foreach (
Worksheet worksheet
in worksheets)
378 worksheet.ResolveMergedCells();
391 shortener.SetCurrentWorksheetInternal(currentWorksheet);
392 return currentWorksheet;
404 shortener.SetCurrentWorksheetInternal(currentWorksheet);
405 return currentWorksheet;
415 int index = worksheets.IndexOf(worksheet);
418 throw new WorksheetException(
"The passed worksheet object is not in the worksheet collection.");
420 currentWorksheet = worksheets[index];
421 shortener.SetCurrentWorksheetInternal(worksheet);
431 int index = worksheets.FindIndex(w => w.SheetName == name);
434 throw new WorksheetException(
"No worksheet with the name '" + name +
"' was found in this workbook.");
436 selectedWorksheet = index;
448 if (worksheetIndex < 0 || worksheetIndex > worksheets.Count - 1)
450 throw new RangeException(
"The worksheet index " + worksheetIndex +
" is out of range");
452 selectedWorksheet = worksheetIndex;
453 ValidateWorksheets();
464 selectedWorksheet = worksheets.IndexOf(worksheet);
465 if (selectedWorksheet < 0)
467 throw new WorksheetException(
"The passed worksheet object is not in the worksheet collection.");
469 ValidateWorksheets();
480 int index = worksheets.FindIndex(w => w.SheetName == name);
483 throw new WorksheetException(
"No worksheet with the name '" + name +
"' was found in this workbook.");
485 return worksheets[index];
496 if (index < 0 || index > worksheets.Count - 1)
498 throw new RangeException(
"The worksheet index " + index +
" is out of range");
500 return worksheets[index];
512 lockWindowsIfProtected = protectWindows;
513 lockStructureIfProtected = protectStructure;
514 workbookProtectionPassword.SetPassword(password);
515 if (!protectWindows && !protectStructure)
536 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
550 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
563 return CopyWorksheetTo(sourceWorksheet, newWorksheetName,
this, sanitizeSheetName);
575 public Worksheet CopyWorksheetTo(
string sourceWorksheetName,
string newWorksheetName, Workbook targetWorkbook,
bool sanitizeSheetName =
true)
578 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
590 public Worksheet CopyWorksheetTo(
int sourceWorksheetIndex,
string newWorksheetName, Workbook targetWorkbook,
bool sanitizeSheetName =
true)
593 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
608 if (targetWorkbook ==
null)
612 if (sourceWorksheet ==
null)
633 internal void ValidateWorksheets()
635 if (importInProgress)
640 int worksheetCount = worksheets.Count;
641 if (worksheetCount == 0)
645 for (
int i = 0; i < worksheetCount; i++)
649 if (i == selectedWorksheet)
651 throw new WorksheetException(
"The worksheet with the index " + selectedWorksheet +
" cannot be set as selected, since it is set hidden");
664 worksheets.RemoveAt(index);
665 if (worksheets.Count > 0)
667 for (
int i = 0; i < worksheets.Count; i++)
669 worksheets[i].SheetID = i + 1;
671 if (resetCurrentWorksheet)
673 currentWorksheet = worksheets[worksheets.Count - 1];
675 if (selectedWorksheet == index || selectedWorksheet > worksheets.Count - 1)
677 selectedWorksheet = worksheets.Count - 1;
682 currentWorksheet =
null;
683 selectedWorksheet = 0;
685 ValidateWorksheets();
692 private int GetNextWorksheetId()
694 if (worksheets.Count == 0)
698 return worksheets.Max(w => w.SheetID) + 1;
706 worksheets =
new List<Worksheet>();
707 workbookMetadata =
new Metadata();
708 shortener =
new Shortener(
this);
709 workbookProtectionPassword =
new LegacyPassword(LegacyPassword.PasswordType.WorkbookProtection);
710 AuxiliaryData =
new AuxiliaryData();
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 static methods to parse string values to specific types or to print object as languag...
static string ToUpper(string input)
Transforms a string to upper case with null check and invariant culture.
Class providing general validator methods.
static void ValidateColor(string hexCode, bool useAlpha, bool allowEmpty=false)
Validates the passed string, whether it is a valid RGB or ARGB value that can be used for Fills or Fo...
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.
IReadOnlyList< string > GetMruColors()
Gets the MRU color list.
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.
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.
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...