NanoXLSX.Core 3.2.1
Loading...
Searching...
No Matches
Workbook.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System.Collections.Generic;
9using System.Linq;
10using NanoXLSX.Colors;
15using NanoXLSX.Themes;
16using NanoXLSX.Utils;
17
18namespace NanoXLSX
19{
24 public class Workbook
25 {
26 static Workbook()
27 {
29 }
30
31 #region privateFields
32 private string filename;
33 private List<Worksheet> worksheets;
34 private Worksheet currentWorksheet;
35 private Metadata workbookMetadata;
36 private IPassword workbookProtectionPassword;
37 private bool lockWindowsIfProtected;
38 private bool lockStructureIfProtected;
39 private int selectedWorksheet;
40 private Shortener shortener;
41 private readonly List<Color> mruColors = new List<Color>();
42 private readonly List<DefinedName> definedNames = new List<DefinedName>();
43 internal bool importInProgress; // Used by NanoXLSX.Reader
44 #endregion
45
46 #region properties
47
52 internal AuxiliaryData AuxiliaryData { get; private set; }
53
58 {
59 get { return shortener; }
60 }
61
62
67 {
68 get { return currentWorksheet; }
69 }
70
77 public string Filename
78 {
79 get { return filename; }
80 set { filename = value; }
81 }
82
87 {
88 get { return lockStructureIfProtected; }
89 }
90
95 {
96 get { return lockWindowsIfProtected; }
97 }
98
103 {
104 get { return workbookMetadata; }
105 set { workbookMetadata = value; }
106 }
107
112 {
113 get { return selectedWorksheet; }
114 }
115
119 public bool UseWorkbookProtection { get; set; }
120
126 public virtual IPassword WorkbookProtectionPassword { get { return workbookProtectionPassword; } internal set => workbookProtectionPassword = value; }
127
131 public List<Worksheet> Worksheets
132 {
133 get { return worksheets; }
134 }
135
136
141 public bool Hidden { get; set; }
142
146 public Theme WorkbookTheme { get; set; } = Theme.GetDefaultTheme();
147
151 internal FeatureSet Features { get; } = new FeatureSet() { };
152
153 #endregion
154
155 #region constructors
159 public Workbook()
160 {
161 Init();
162 }
163
168 public Workbook(bool createWorkSheet)
169 {
170 Init();
171 if (createWorkSheet)
172 {
173 AddWorksheet("Sheet1");
174 }
175 }
176
181 public Workbook(string sheetName)
182 {
183 Init();
184 AddWorksheet(sheetName, true);
185 }
186
192 public Workbook(string filename, string sheetName)
193 {
194 Init();
195 this.filename = filename;
196 AddWorksheet(sheetName, true);
197 }
198
205 public Workbook(string filename, string sheetName, bool sanitizeSheetName)
206 {
207 Init();
208 this.filename = filename;
209 if (sanitizeSheetName)
210 {
212 }
213 else
214 {
215 AddWorksheet(sheetName);
216 }
217 }
218
219 #endregion
220
221 #region methods
222
227 public void AddMruColor(string color)
228 {
230 mruColors.Add(Color.CreateRgb(color));
231 }
232
237 public void AddMruColor(Color color)
238 {
239 mruColors.Add(color);
240 }
241
246 public IReadOnlyList<Color> GetMruColors()
247 {
248 return mruColors;
249 }
250
254 public void ClearMruColors()
255 {
256 mruColors.Clear();
257 }
258
259 #region definedNames
260
272 public DefinedName AddDefinedNameCell(string name, Worksheet worksheet, string cellAddress, Worksheet localWorksheet = null, string comment = null)
273 {
274 Address address = new Address(cellAddress);
275 return AddDefinedNameCell(name, worksheet, address, localWorksheet, comment);
276 }
277
290 public DefinedName AddDefinedNameCell(string name, Worksheet worksheet, int column, int row, Worksheet localWorksheet = null, string comment = null)
291 {
292 Address address = new Address(column, row);
293 return AddDefinedNameCell(name, worksheet, address, localWorksheet, comment);
294 }
295
307 public DefinedName AddDefinedNameCell(string name, Worksheet worksheet, Address cellAddress, Worksheet localWorksheet = null, string comment = null)
308 {
309 if (worksheet == null)
310 {
311 throw new WorksheetException("A defined name to a cell must have a worksheet");
312 }
313 if (cellAddress == null)
314 {
315 throw new WorksheetException("The cell address pointing to a defined name cannot be null");
316 }
317 return AddDefinedName(name, DefinedName.NameType.Cell, cellAddress, worksheet, localWorksheet, comment);
318 }
319
332 public DefinedName AddDefinedNameRange(string name, Worksheet worksheet, string rangeAddress, Worksheet localWorksheet = null, string comment = null)
333 {
334 Range range = new Range(rangeAddress);
335 return AddDefinedNameRange(name, worksheet, range, localWorksheet, comment);
336 }
337
349 public DefinedName AddDefinedNameRange(string name, Worksheet worksheet, Address startAddress, Address endAddress, Worksheet localWorksheet = null, string comment = null)
350 {
351 Range range = new Range(startAddress, endAddress);
352 return AddDefinedNameRange(name, worksheet, range, localWorksheet, comment);
353 }
354
368 public DefinedName AddDefinedNameRange(string name, Worksheet worksheet, int startColumn, int startRow, int endColumn, int endRow, Worksheet localWorksheet = null, string comment = null)
369 {
370 Range range = new Range(startColumn, startRow, endColumn, endRow);
371 return AddDefinedNameRange(name, worksheet, range, localWorksheet, comment);
372 }
373
384 public DefinedName AddDefinedNameRange(string name, Worksheet worksheet, Range rangeAddress, Worksheet localWorksheet = null, string comment = null)
385 {
386 if (worksheet == null)
387 {
388 throw new WorksheetException("A defined name to a cell must have a worksheet");
389 }
390 return AddDefinedName(name, DefinedName.NameType.Range, rangeAddress, worksheet, localWorksheet, comment);
391 }
392
404 public DefinedName AddDefinedNameConstant(string name, object value, Worksheet localWorksheet = null, string comment = null)
405 {
406 if (value == null)
407 {
408 throw new WorksheetException("A constant value pointing to a defined name cannot be nul");
409 }
410 return AddDefinedName(name, DefinedName.NameType.Constant, value, null, localWorksheet, comment);
411 }
412
426 public DefinedName AddDefinedNameFormula(string name, string formula, Worksheet localWorksheet = null, string comment = null)
427 {
428 if (string.IsNullOrWhiteSpace(formula))
429 {
430 throw new WorksheetException("A formula value pointing to a defined name cannot be null or empty");
431 }
432 // Note: Added strings like '[0]' will out-of-the-box throw an exception on saving a workbook
433 return AddDefinedName(name, DefinedName.NameType.Formula, formula, null, localWorksheet, comment);
434 }
435
446 internal DefinedName AddDefinedName(string name, DefinedName.NameType type, object value, Worksheet targetWorksheet, Worksheet localWorksheet, string comment)
447 {
448 // Validation is implemented in DefinedName class
449 DefinedName definedName = new DefinedName(this, type, name, value, targetWorksheet, localWorksheet, comment);
450 AddDefinedName(definedName);
451 return definedName;
452 }
453
458 internal void AddDefinedName(DefinedName definedName)
459 {
460 definedNames.Add(definedName);
461 }
462
470 public bool RemoveDefinedName(string name, Worksheet localSheet = null)
471 {
472 return RemoveDefinedName(name, true, localSheet);
473 }
474
483 internal bool RemoveDefinedName(string name, bool invalidate, Worksheet localSheet = null)
484 {
485 int index = FindDefinedNameIndex(name, localSheet);
486 if (index < 0)
487 {
488 return false;
489 }
490 DefinedName definedName = definedNames[index];
491 if (invalidate)
492 {
493 InvalidateDefinedNameReferences(definedName);
494 }
495 definedName.Features.Remove(Features); // Decrease counter of defined name features
496 definedNames.RemoveAt(index);
497 return true;
498 }
499
504 public void ClearDefinedNames()
505 {
506 for (int i = definedNames.Count - 1; i >= 0; i--)
507 {
508 DefinedName definedName = definedNames[i];
509 InvalidateDefinedNameReferences(definedName);
510 definedName.Features.Remove(Features); // Decrease counter of defined name features
511 definedNames.RemoveAt(i);
512 }
513 }
514
521 public DefinedName GetDefinedName(string name, Worksheet localSheet = null)
522 {
523 int index = FindDefinedNameIndex(name, localSheet);
524 return index < 0 ? null : definedNames[index];
525 }
526
531 public IReadOnlyList<DefinedName> GetDefinedNames()
532 {
533 return definedNames;
534 }
535
542 internal int FindDefinedNameIndex(string name, Worksheet localSheet)
543 {
544 for (int i = 0; i < definedNames.Count; i++)
545 {
546 DefinedName candidate = definedNames[i];
547 if (string.Equals(candidate.Name, name, System.StringComparison.OrdinalIgnoreCase)
548 && ReferenceEquals(candidate.LocalSheet, localSheet))
549 {
550 return i;
551 }
552 }
553 return -1;
554 }
555
556 #endregion
557
564 public void AddWorksheet(string name)
565 {
566 foreach (Worksheet item in worksheets)
567 {
568 if (item.SheetName == name)
569 {
570 throw new WorksheetException("The worksheet with the name '" + name + "' already exists.");
571 }
572 }
573 int number = GetNextWorksheetId();
574 Worksheet newWs = new Worksheet(name, number, this);
575 currentWorksheet = newWs;
576 worksheets.Add(newWs);
577 newWs.Features.Add(Features);
578 shortener.SetCurrentWorksheetInternal(currentWorksheet);
579 }
580
588 public void AddWorksheet(string name, bool sanitizeSheetName)
589 {
590 if (sanitizeSheetName)
591 {
592 string sanitized = Worksheet.SanitizeWorksheetName(name, this);
593 AddWorksheet(sanitized);
594 }
595 else
596 {
597 AddWorksheet(name);
598 }
599 }
600
607 public void AddWorksheet(Worksheet worksheet)
608 {
609 AddWorksheet(worksheet, false);
610 }
611
619 public void AddWorksheet(Worksheet worksheet, bool sanitizeSheetName)
620 {
621 if (sanitizeSheetName)
622 {
623 string name = Worksheet.SanitizeWorksheetName(worksheet.SheetName, this);
624 worksheet.SheetName = name;
625 }
626 else
627 {
628 if (string.IsNullOrEmpty(worksheet.SheetName))
629 {
630 throw new WorksheetException("The name of the passed worksheet is null or empty.");
631 }
632 for (int i = 0; i < worksheets.Count; i++)
633 {
634 if (worksheets[i].SheetName == worksheet.SheetName)
635 {
636 throw new WorksheetException("The worksheet with the name '" + worksheet.SheetName + "' already exists.");
637 }
638 }
639 }
640 worksheet.SheetID = GetNextWorksheetId();
641 currentWorksheet = worksheet;
642 worksheets.Add(worksheet);
643 worksheet.WorkbookReference = this;
644 worksheet.Features.Add(Features);
645 }
646
653 public void RemoveWorksheet(string name)
654 {
655 Worksheet worksheetToRemove = worksheets.FirstOrDefault(w => w.SheetName == name);
656 if (worksheetToRemove == null)
657 {
658 throw new WorksheetException("The worksheet with the name '" + name + "' does not exist.");
659 }
660 int index = worksheets.IndexOf(worksheetToRemove);
661 bool resetCurrentWorksheet = worksheetToRemove == currentWorksheet;
662 RemoveWorksheet(index, resetCurrentWorksheet);
663 }
664
671
672 public void RemoveWorksheet(int index)
673 {
674 if (index < 0 || index >= worksheets.Count)
675 {
676 throw new WorksheetException("The worksheet index " + index + " is out of range");
677 }
678 bool resetCurrentWorksheet = worksheets[index] == currentWorksheet;
679 RemoveWorksheet(index, resetCurrentWorksheet);
680 }
681
687 internal void ResolveMergedCells()
688 {
689 foreach (Worksheet worksheet in worksheets)
690 {
691 worksheet.ResolveMergedCells();
692 }
693 }
694
701 public Worksheet SetCurrentWorksheet(string name)
702 {
703 currentWorksheet = GetWorksheet(name);
704 shortener.SetCurrentWorksheetInternal(currentWorksheet);
705 return currentWorksheet;
706 }
707
714 public Worksheet SetCurrentWorksheet(int worksheetIndex)
715 {
716 currentWorksheet = GetWorksheet(worksheetIndex);
717 shortener.SetCurrentWorksheetInternal(currentWorksheet);
718 return currentWorksheet;
719 }
720
726 public void SetCurrentWorksheet(Worksheet worksheet)
727 {
728 int index = worksheets.IndexOf(worksheet);
729 if (index < 0)
730 {
731 throw new WorksheetException("The passed worksheet object is not in the worksheet collection.");
732 }
733 currentWorksheet = worksheets[index];
734 shortener.SetCurrentWorksheetInternal(worksheet);
735 }
736
742 public void SetSelectedWorksheet(string name)
743 {
744 int index = worksheets.FindIndex(w => w.SheetName == name);
745 if (index < 0)
746 {
747 throw new WorksheetException("No worksheet with the name '" + name + "' was found in this workbook.");
748 }
749 selectedWorksheet = index;
750 }
751
759 public void SetSelectedWorksheet(int worksheetIndex)
760 {
761 if (worksheetIndex < 0 || worksheetIndex > worksheets.Count - 1)
762 {
763 throw new RangeException("The worksheet index " + worksheetIndex + " is out of range");
764 }
765 selectedWorksheet = worksheetIndex;
766 ValidateWorksheets();
767 }
768
775 public void SetSelectedWorksheet(Worksheet worksheet)
776 {
777 selectedWorksheet = worksheets.IndexOf(worksheet);
778 if (selectedWorksheet < 0)
779 {
780 throw new WorksheetException("The passed worksheet object is not in the worksheet collection.");
781 }
782 ValidateWorksheets();
783 }
784
791 public Worksheet GetWorksheet(string name)
792 {
793 int index = worksheets.FindIndex(w => w.SheetName == name);
794 if (index < 0)
795 {
796 throw new WorksheetException("No worksheet with the name '" + name + "' was found in this workbook.");
797 }
798 return worksheets[index];
799 }
800
807 public Worksheet GetWorksheet(int index)
808 {
809 if (index < 0 || index > worksheets.Count - 1)
810 {
811 throw new RangeException("The worksheet index " + index + " is out of range");
812 }
813 return worksheets[index];
814 }
815
823 public void SetWorkbookProtection(bool state, bool protectWindows, bool protectStructure, string password)
824 {
825 lockWindowsIfProtected = protectWindows;
826 lockStructureIfProtected = protectStructure;
827 workbookProtectionPassword.SetPassword(password);
828 if (!protectWindows && !protectStructure)
829 {
830 UseWorkbookProtection = false;
831 }
832 else
833 {
834 UseWorkbookProtection = state;
835 }
836 }
837
846 public Worksheet CopyWorksheetIntoThis(string sourceWorksheetName, string newWorksheetName, bool sanitizeSheetName = true)
847 {
848 Worksheet sourceWorksheet = GetWorksheet(sourceWorksheetName);
849 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, this, sanitizeSheetName);
850 }
851
860 public Worksheet CopyWorksheetIntoThis(int sourceWorksheetIndex, string newWorksheetName, bool sanitizeSheetName = true)
861 {
862 Worksheet sourceWorksheet = GetWorksheet(sourceWorksheetIndex);
863 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, this, sanitizeSheetName);
864 }
865
874 public Worksheet CopyWorksheetIntoThis(Worksheet sourceWorksheet, string newWorksheetName, bool sanitizeSheetName = true)
875 {
876 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, this, sanitizeSheetName);
877 }
878
888 public Worksheet CopyWorksheetTo(string sourceWorksheetName, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName = true)
889 {
890 Worksheet sourceWorksheet = GetWorksheet(sourceWorksheetName);
891 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
892 }
893
903 public Worksheet CopyWorksheetTo(int sourceWorksheetIndex, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName = true)
904 {
905 Worksheet sourceWorksheet = GetWorksheet(sourceWorksheetIndex);
906 return CopyWorksheetTo(sourceWorksheet, newWorksheetName, targetWorkbook, sanitizeSheetName);
907 }
908
909
919 public static Worksheet CopyWorksheetTo(Worksheet sourceWorksheet, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName = true)
920 {
921 if (targetWorkbook == null)
922 {
923 throw new WorksheetException("The target workbook cannot be null");
924 }
925 if (sourceWorksheet == null)
926 {
927 throw new WorksheetException("The source worksheet cannot be null");
928 }
929 Worksheet copy = sourceWorksheet.Copy();
930 copy.SetSheetName(newWorksheetName);
931 Worksheet currentWorksheet = targetWorkbook.CurrentWorksheet;
932 targetWorkbook.AddWorksheet(copy, sanitizeSheetName);
933 targetWorkbook.SetCurrentWorksheet(currentWorksheet);
934 return copy;
935 }
936
937
946 internal void ValidateWorksheets()
947 {
948 if (importInProgress)
949 {
950 // No validation during import
951 return;
952 }
953 int worksheetCount = worksheets.Count;
954 if (worksheetCount == 0)
955 {
956 throw new WorksheetException("The workbook must contain at least one worksheet");
957 }
958 for (int i = 0; i < worksheetCount; i++)
959 {
960 if (worksheets[i].Hidden)
961 {
962 if (i == selectedWorksheet)
963 {
964 throw new WorksheetException("The worksheet with the index " + selectedWorksheet + " cannot be set as selected, since it is set hidden");
965 }
966 }
967 }
968 }
969
974 private void InvalidateDefinedNameReferences(DefinedName definedName)
975 {
976 foreach (Worksheet worksheet in worksheets)
977 {
978 if (!worksheet.Features.ContainsDefinedNameReferences)
979 {
980 continue;
981 }
982 foreach (KeyValuePair<string, Cell> cell in worksheet.Cells)
983 {
984 if (cell.Value.DataType == Cell.CellType.Formula)
985 {
986 FormulaData formula = cell.Value.Formula;
987 if (formula != null && ReferenceEquals(formula.DefinedNameReference, definedName))
988 {
989 formula.DefinedNameReference = null;
990 }
991 }
992 }
993 }
994 }
995
996
1002 private void RemoveWorksheet(int index, bool resetCurrentWorksheet)
1003 {
1004 worksheets[index].Features.Remove(Features); // Remove cascading features
1005 worksheets.RemoveAt(index);
1006 if (worksheets.Count > 0)
1007 {
1008 for (int i = 0; i < worksheets.Count; i++)
1009 {
1010 worksheets[i].SheetID = i + 1;
1011 }
1012 if (resetCurrentWorksheet)
1013 {
1014 currentWorksheet = worksheets[worksheets.Count - 1];
1015 }
1016 if (selectedWorksheet == index || selectedWorksheet > worksheets.Count - 1)
1017 {
1018 selectedWorksheet = worksheets.Count - 1;
1019 }
1020 }
1021 else
1022 {
1023 currentWorksheet = null;
1024 selectedWorksheet = 0;
1025 }
1026 ValidateWorksheets();
1027 }
1028
1033 private int GetNextWorksheetId()
1034 {
1035 if (worksheets.Count == 0)
1036 {
1037 return 1;
1038 }
1039 return worksheets.Max(w => w.SheetID) + 1;
1040 }
1041
1045 private void Init()
1046 {
1047 worksheets = new List<Worksheet>();
1048 workbookMetadata = new Metadata();
1049 shortener = new Shortener(this);
1050 workbookProtectionPassword = new LegacyPassword(LegacyPassword.PasswordType.WorkbookProtection);
1051 AuxiliaryData = new AuxiliaryData();
1052 }
1053
1054
1055 #endregion
1056 }
1057}
Compound class representing a color in various representations (RGB, indexed, theme,...
Definition Color.cs:20
static Color CreateRgb(SrgbColor color)
Creates a Color from an RGB/ARGB color.
Definition Color.cs:171
Class representing a defined name within a workbook. A defined name is a descriptive text that repres...
Worksheet LocalSheet
Gets the worksheet that scopes (constraint) this defined name. If null, the defined name has workbook...
string Name
Gets the name of the defined name as it appears in the workbook (e.g. MyRange).
NameType
Enum to specify the type of the defined name.
Class for exceptions regarding range incidents (e.g. out-of-range).
Class for exceptions regarding worksheet incidents.
DefinedName DefinedNameReference
Resolved defined name, if the complete formula expression is a direct reference to exactly one define...
Class representing the metadata of a workbook.
Definition Metadata.cs:19
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.
Definition Shortener.cs:19
Class representing an Office theme.
Definition Theme.cs:17
Class providing general validator methods.
Definition Validators.cs:13
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,...
Definition Validators.cs:22
void AddWorksheet(Worksheet worksheet, bool sanitizeSheetName)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
Definition Workbook.cs:619
bool Hidden
Gets or sets whether the whole workbook is hidden.
Definition Workbook.cs:141
Workbook(string filename, string sheetName, bool sanitizeSheetName)
Constructor with filename ant the name of the first worksheet.
Definition Workbook.cs:205
void SetSelectedWorksheet(Worksheet worksheet)
Sets the selected worksheet in the output workbook.
Definition Workbook.cs:775
void ClearDefinedNames()
Removes all defined names of the workbook and removes their references from formula cells.
Definition Workbook.cs:504
void AddMruColor(string color)
Adds a color value (HEX; 6-digit RGB or 8-digit ARGB) to the MRU list.
Definition Workbook.cs:227
Workbook()
Default constructor. No initial worksheet is created. Use AddWorksheet(string) (or overloads) to add ...
Definition Workbook.cs:159
void ClearMruColors()
Clears the MRU color list.
Definition Workbook.cs:254
void SetCurrentWorksheet(Worksheet worksheet)
Sets the current worksheet.
Definition Workbook.cs:726
void SetSelectedWorksheet(int worksheetIndex)
Sets the selected worksheet in the output workbook.
Definition Workbook.cs:759
int SelectedWorksheet
Gets the selected worksheet. The selected worksheet is not the current worksheet while design time bu...
Definition Workbook.cs:112
void AddWorksheet(string name, bool sanitizeSheetName)
Adding a new Worksheet with a sanitizing option. The new worksheet will be defined as current workshe...
Definition Workbook.cs:588
void RemoveWorksheet(int index)
Removes the defined worksheet based on its index. If the worksheet is the current or selected workshe...
Definition Workbook.cs:672
Worksheet CopyWorksheetIntoThis(int sourceWorksheetIndex, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its index.
Definition Workbook.cs:860
Shortener WS
Gets the shortener object for the current worksheet.
Definition Workbook.cs:58
DefinedName AddDefinedNameCell(string name, Worksheet worksheet, int column, int row, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a single cell, to the workbook.
Definition Workbook.cs:290
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, string rangeAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
Definition Workbook.cs:332
void SetWorkbookProtection(bool state, bool protectWindows, bool protectStructure, string password)
Sets or removes the workbook protection. If protectWindows and protectStructure are both false,...
Definition Workbook.cs:823
Theme WorkbookTheme
Gets or sets the theme of the workbook. The default is defined by Theme.GetDefaultTheme....
Definition Workbook.cs:146
DefinedName GetDefinedName(string name, Worksheet localSheet=null)
Gets the defined name with the supplied name and scope.
Definition Workbook.cs:521
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, Address startAddress, Address endAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
Definition Workbook.cs:349
string Filename
Gets or sets the filename of the workbook.
Definition Workbook.cs:78
Workbook(string sheetName)
Constructor with additional parameter to create a default worksheet with the specified name....
Definition Workbook.cs:181
Workbook(bool createWorkSheet)
Constructor with additional parameter to create a default worksheet. This constructor can be used to ...
Definition Workbook.cs:168
void AddWorksheet(Worksheet worksheet)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
Definition Workbook.cs:607
IReadOnlyList< Color > GetMruColors()
Gets the MRU color list.
Definition Workbook.cs:246
Worksheet GetWorksheet(int index)
Gets a worksheet from this workbook by index.
Definition Workbook.cs:807
bool LockStructureIfProtected
Gets whether the structure are locked if workbook is protected. See also SetWorkbookProtection.
Definition Workbook.cs:87
Workbook(string filename, string sheetName)
Constructor with filename ant the name of the first worksheet.
Definition Workbook.cs:192
void SetSelectedWorksheet(string name)
Sets the selected worksheet in the output workbook.
Definition Workbook.cs:742
Worksheet SetCurrentWorksheet(int worksheetIndex)
Sets the current worksheet.
Definition Workbook.cs:714
DefinedName AddDefinedNameCell(string name, Worksheet worksheet, Address cellAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a single cell, to the workbook.
Definition Workbook.cs:307
Worksheet CopyWorksheetTo(string sourceWorksheetName, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its name into another workbook.
Definition Workbook.cs:888
void AddMruColor(Color color)
Adds a generic color value. This can be an RGB/ARGB color, Auto, Theme, Indexed or System color.
Definition Workbook.cs:237
IReadOnlyList< DefinedName > GetDefinedNames()
Gets a read-only view of all defined names in this workbook in insertion order.
Definition Workbook.cs:531
DefinedName AddDefinedNameCell(string name, Worksheet worksheet, string cellAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a single cell, to the workbook.
Definition Workbook.cs:272
Worksheet CurrentWorksheet
Gets the current worksheet.
Definition Workbook.cs:67
Worksheet CopyWorksheetTo(int sourceWorksheetIndex, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its index into another workbook.
Definition Workbook.cs:903
List< Worksheet > Worksheets
Gets the list of worksheets in the workbook.
Definition Workbook.cs:132
void AddWorksheet(string name)
Adding a new Worksheet. The new worksheet will be defined as current worksheet.
Definition Workbook.cs:564
bool RemoveDefinedName(string name, Worksheet localSheet=null)
Removes the defined name with the supplied name and scope.
Definition Workbook.cs:470
Metadata WorkbookMetadata
Meta data object of the workbook.
Definition Workbook.cs:103
bool LockWindowsIfProtected
Gets whether the windows are locked if workbook is protected. See also SetWorkbookProtection.
Definition Workbook.cs:95
bool UseWorkbookProtection
Gets or sets whether the workbook is protected.
Definition Workbook.cs:119
virtual IPassword WorkbookProtectionPassword
Password instance of the protected workbook. If a password was set, the pain text representation and ...
Definition Workbook.cs:126
Worksheet CopyWorksheetIntoThis(Worksheet sourceWorksheet, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of any workbook into the current workbook.
Definition Workbook.cs:874
Worksheet CopyWorksheetIntoThis(string sourceWorksheetName, string newWorksheetName, bool sanitizeSheetName=true)
Copies a worksheet of the current workbook by its name.
Definition Workbook.cs:846
Worksheet SetCurrentWorksheet(string name)
Sets the current worksheet.
Definition Workbook.cs:701
DefinedName AddDefinedNameFormula(string name, string formula, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a formula expression, to the workbook. Do not add a leading equal si...
Definition Workbook.cs:426
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, Range rangeAddress, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
Definition Workbook.cs:384
DefinedName AddDefinedNameRange(string name, Worksheet worksheet, int startColumn, int startRow, int endColumn, int endRow, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a cell range, to the workbook.
Definition Workbook.cs:368
void RemoveWorksheet(string name)
Removes the defined worksheet based on its name. If the worksheet is the current or selected workshee...
Definition Workbook.cs:653
DefinedName AddDefinedNameConstant(string name, object value, Worksheet localWorksheet=null, string comment=null)
Adds a defined name, pointing to a constant value, to the workbook.
Definition Workbook.cs:404
Worksheet GetWorksheet(string name)
Gets a worksheet from this workbook by name.
Definition Workbook.cs:791
static Worksheet CopyWorksheetTo(Worksheet sourceWorksheet, string newWorksheetName, Workbook targetWorkbook, bool sanitizeSheetName=true)
Copies a worksheet of any workbook into the another workbook.
Definition Workbook.cs:919
Class representing a worksheet of a workbook.
Definition Worksheet.cs:27
Worksheet Copy()
Creates a (dereferenced) deep copy of this worksheet.
string SheetName
Gets or sets the name of the worksheet.
Definition Worksheet.cs:347
void SetSheetName(string name)
Validates and sets the worksheet name.
static string SanitizeWorksheetName(string input, Workbook workbook)
Sanitizes a worksheet name.
IReadOnlyDictionary< string, Cell > Cells
Gets the cells of the worksheet as read-only dictionary with the cell address string as key and the c...
Definition Worksheet.cs:234
Interface to represent a protection password, either for workbooks or worksheets. The implementations...
Definition IPassword.cs:14
Struct representing the cell address as column and row (zero based).
Definition Address.cs:16
Struct representing a cell range with a start and end address.
Definition Range.cs:16