9using System.Collections.Generic;
12using System.Text.RegularExpressions;
26 public class Worksheet
53#pragma warning disable CA1805
55#pragma warning restore CA1805
59#pragma warning disable CA1805
61#pragma warning restore CA1805
65#pragma warning disable CA1805
67#pragma warning restore CA1805
79#pragma warning disable CA1805
81#pragma warning restore CA1805
89#pragma warning disable CA1805
91#pragma warning restore CA1805
183 #region privateFields
184 private Style activeStyle;
185 private Range? autoFilterRange;
186 private readonly Dictionary<CellKey, Cell> cells;
187 private readonly StringKeyedCellView cellsStringView;
188 private readonly Dictionary<int, Column> columns;
189 private string sheetName;
190 private int currentRowNumber;
191 private int currentColumnNumber;
192 private float defaultRowHeight;
193 private float defaultColumnWidth;
194 private readonly Dictionary<int, float> rowHeights;
195 private readonly Dictionary<int, bool> hiddenRows;
196 private readonly Dictionary<string, Range> mergedCells;
197 private readonly List<SheetProtectionValue> sheetProtectionValues;
198 private bool useActiveStyle;
201 private IPassword sheetProtectionPassword;
202 private List<Range> selectedCells;
203 private bool? freezeSplitPanes;
204 private float? paneSplitLeftWidth;
205 private float? paneSplitTopHeight;
206 private Address? paneSplitTopLeftCell;
207 private Address? paneSplitAddress;
211 private Dictionary<SheetViewType, int> zoomFactor;
220 get {
return autoFilterRange; }
227 public IReadOnlyDictionary<string, Cell>
Cells
229 get {
return cellsStringView; }
238 get {
return cells.Values; }
246 get {
return columns; }
260 get {
return defaultColumnWidth; }
267 defaultColumnWidth = value;
277 get {
return defaultRowHeight; }
284 defaultRowHeight = value;
294 get {
return hiddenRows; }
302 get {
return rowHeights; }
310 get {
return mergedCells; }
318 get {
return selectedCells; }
331 throw new FormatException(
"The ID " + value +
" is invalid. Worksheet IDs must be >0");
342 get {
return sheetName; }
352 get {
return sheetProtectionPassword; }
353 internal set { sheetProtectionPassword = value; }
361 get {
return sheetProtectionValues; }
374 get {
return workbookReference; }
377 workbookReference = value;
380 workbookReference.ValidateWorksheets();
392 get {
return hidden; }
396 if (value && workbookReference !=
null)
398 workbookReference.ValidateWorksheets();
413 get {
return paneSplitTopHeight; }
426 get {
return paneSplitLeftWidth; }
435 get {
return freezeSplitPanes; }
445 get {
return paneSplitTopLeftCell; }
457 get {
return paneSplitAddress; }
467 get {
return activePane; }
475 get {
return activeStyle; }
522 return zoomFactor[viewType];
548 cells =
new Dictionary<CellKey, Cell>(1000);
549 cellsStringView =
new StringKeyedCellView(cells);
550 currentRowNumber = 0;
551 currentColumnNumber = 0;
554 rowHeights =
new Dictionary<int, float>();
555 mergedCells =
new Dictionary<string, Range>();
556 selectedCells =
new List<Range>();
557 sheetProtectionValues =
new List<SheetProtectionValue>();
558 hiddenRows =
new Dictionary<int, bool>();
559 columns =
new Dictionary<int, Column>();
561 workbookReference =
null;
563 zoomFactor =
new Dictionary<SheetViewType, int>
594 workbookReference = reference;
599 #region methods_AddNextCell
611 AddNextCell(CastValue(value, currentColumnNumber, currentRowNumber),
true,
null);
627 AddNextCell(CastValue(value, currentColumnNumber, currentRowNumber),
true, style);
643 if (style !=
null || (activeStyle !=
null && useActiveStyle))
645 if (cell.
CellStyle ==
null && useActiveStyle)
649 else if (cell.
CellStyle ==
null && style !=
null)
653 else if (cell.
CellStyle !=
null && useActiveStyle)
656 mixedStyle.
Append(activeStyle);
659 else if (cell.
CellStyle !=
null && style !=
null)
671 currentColumnNumber++;
686 currentColumnNumber = cell.ColumnNumber + 1;
692 currentRowNumber = cell.RowNumber + 1;
708 private static Cell CastValue(
object value,
int column,
int row)
711 if (value !=
null && value.GetType() == typeof(Cell))
714 c.CellAddress2 =
new Address(column, row);
718 c =
new Cell(value, Cell.CellType.Default, column, row);
726 #region methods_AddCell
738 public void AddCell(
object value,
int columnNumber,
int rowNumber)
740 AddNextCell(CastValue(value, columnNumber, rowNumber),
false,
null);
755 public void AddCell(
object value,
int columnNumber,
int rowNumber,
Style style)
757 AddNextCell(CastValue(value, columnNumber, rowNumber),
false, style);
771 public void AddCell(
object value,
string address)
796 AddCell(value, column, row, style);
801 #region methods_AddCellFormula
889 #region methods_AddCellRange
903 AddCellRangeInternal(values, startAddress, endAddress,
null);
920 AddCellRangeInternal(values, startAddress, endAddress, style);
933 public void AddCellRange(IReadOnlyList<object> values,
string cellRange)
998 private void AddCellRangeInternal<T>(IReadOnlyList<T> values,
Address startAddress,
Address endAddress,
Style style)
1002 throw new RangeException(
"The passed value list cannot be null");
1004 List<Address> addresses =
Cell.
GetCellRange(startAddress, endAddress) as List<Address>;
1005 if (values.Count != addresses.Count)
1007 throw new RangeException(
"The number of passed values (" + values.Count +
") differs from the number of cells within the range (" + addresses.Count +
")");
1009 List<Cell> list = Cell.ConvertArray(values) as List<Cell>;
1010 int len = values.Count;
1011 for (
int i = 0; i < len; i++)
1013 list[i].RowNumber = addresses[i].Row;
1014 list[i].ColumnNumber = addresses[i].Column;
1020 #region methods_RemoveCell
1030 return cells.Remove(
new CellKey(columnNumber, rowNumber));
1049 #region methods_setStyle
1061 foreach (
Address address
in addresses)
1063 if (cells.TryGetValue(
new CellKey(address.
Column, address.
Row), out
Cell existing))
1067 existing.RemoveStyle();
1071 existing.SetStyle(style);
1132 throw new FormatException(
"The passed address'" + addressExpression +
"' is neither a cell address, nor a range");
1138 #region boundaryFunctions
1147 return GetBoundaryNumber(
false,
true);
1158 return GetBoundaryDataNumber(
false,
true,
true);
1169 return GetBoundaryNumber(
true,
true);
1180 return GetBoundaryDataNumber(
true,
true,
true);
1192 return GetBoundaryNumber(
false,
false);
1203 return GetBoundaryDataNumber(
false,
false,
true);
1215 return GetBoundaryNumber(
true,
false);
1227 return GetBoundaryDataNumber(
true,
false,
true);
1242 if (lastRow < 0 || lastColumn < 0)
1246 return new Address(lastColumn, lastRow);
1260 if (lastRow < 0 || lastColumn < 0)
1264 return new Address(lastColumn, lastRow);
1277 if (firstRow < 0 || firstColumn < 0)
1281 return new Address(firstColumn, firstRow);
1294 if (firstRow < 0 || firstColumn < 0)
1298 return new Address(firstColumn, firstRow);
1308 private int GetBoundaryDataNumber(
bool row,
bool min,
bool ignoreEmpty)
1310 if (cells.Count == 0)
1318 return cells.Values.Min(x => x.RowNumber);
1322 return cells.Values.Max(x => x.RowNumber);
1326 return cells.Values.Min(x => x.ColumnNumber);
1330 return cells.Values.Max(x => x.ColumnNumber);
1333 List<Cell> nonEmptyCells = cells.Values.Where(x => x.Value !=
null && x.Value.ToString() !=
string.Empty).ToList();
1334 if (nonEmptyCells.Count == 0)
1340 return nonEmptyCells.Min(x => x.RowNumber);
1344 return nonEmptyCells.Max(x => x.RowNumber);
1348 return nonEmptyCells.Min(x => x.ColumnNumber);
1352 return nonEmptyCells.Max(x => x.ColumnNumber);
1362 private int GetBoundaryNumber(
bool row,
bool min)
1364 int cellBoundary = GetBoundaryDataNumber(row, min,
false);
1367 int heightBoundary = -1;
1368 if (rowHeights.Count > 0)
1372 int hiddenBoundary = -1;
1373 if (hiddenRows.Count > 0)
1377 return min ? GetMinRow(cellBoundary, heightBoundary, hiddenBoundary) : GetMaxRow(cellBoundary, heightBoundary, hiddenBoundary);
1381 int columnDefBoundary = -1;
1382 if (columns.Count > 0)
1384 columnDefBoundary = min ?
Columns.Min(x => x.Key) :
Columns.Max(x => x.Key);
1388 return cellBoundary >= 0 && cellBoundary < columnDefBoundary ? cellBoundary : columnDefBoundary;
1392 return cellBoundary >= 0 && cellBoundary > columnDefBoundary ? cellBoundary : columnDefBoundary;
1404 private static int GetMaxRow(
int cellBoundary,
int heightBoundary,
int hiddenBoundary)
1407 if (cellBoundary >= 0)
1409 highest = cellBoundary;
1411 if (heightBoundary >= 0 && heightBoundary > highest)
1413 highest = heightBoundary;
1415 if (hiddenBoundary >= 0 && hiddenBoundary > highest)
1417 highest = hiddenBoundary;
1429 private static int GetMinRow(
int cellBoundary,
int heightBoundary,
int hiddenBoundary)
1431 int lowest =
int.MaxValue;
1432 if (cellBoundary >= 0)
1434 lowest = cellBoundary;
1436 if (heightBoundary >= 0 && heightBoundary < lowest)
1438 lowest = heightBoundary;
1440 if (hiddenBoundary >= 0 && hiddenBoundary < lowest)
1442 lowest = hiddenBoundary;
1444 return lowest == int.MaxValue ? -1 : lowest;
1448 #region Insert-Search-Replace
1461 var upperRow = this.
GetRow(rowNumber);
1464 var cellsToChange = cells.Values.Where(c => c.
CellAddress2.
Row > rowNumber).ToList();
1467 List<Cell> newCells =
new List<Cell>();
1468 foreach (
Cell cell
in cellsToChange)
1478 newCells.Add(newCell);
1479 cells.Remove(
new CellKey(col, row));
1483 foreach (
Cell cell
in upperRow)
1485 for (
int i = 0; i < numberOfNewRows; i++)
1493 cells[
new CellKey(newAddress.
Column, newAddress.
Row)] = newCell;
1498 foreach (
Cell newCell
in newCells)
1514 var leftColumn = this.
GetColumn(columnNumber);
1515 var cellsToChange = cells.Values.Where(c => c.
CellAddress2.
Column > columnNumber).ToList();
1517 List<Cell> newCells =
new List<Cell>();
1518 foreach (
Cell cell
in cellsToChange)
1528 newCells.Add(newCell);
1529 cells.Remove(
new CellKey(col, row));
1533 foreach (
Cell cell
in leftColumn)
1535 for (
int i = 0; i < numberOfNewColumns; i++)
1543 cells[
new CellKey(newAddress.
Column, newAddress.
Row)] = newCell;
1548 foreach (
Cell newCell
in newCells)
1561 return cells.Values.FirstOrDefault(c => Equals(c.
Value, searchValue));
1572 return cells.Values.FirstOrDefault(c => c !=
null && (c.
Value ==
null || predicate(c)));
1582 return cells.Values.Where(c => Equals(c.
Value, searchValue)).ToList();
1595 foreach (var cell
in foundCells)
1597 cell.Value = newValue;
1604 #region common_methods
1613 if (!sheetProtectionValues.Contains(typeOfProtection))
1619 sheetProtectionValues.Add(typeOfProtection);
1631 SetColumnHiddenState(columnNumber,
true);
1642 SetColumnHiddenState(columnNumber,
true);
1652 SetRowHiddenState(rowNumber,
true);
1660 useActiveStyle =
false;
1672 if (!cells.TryGetValue(
new CellKey(address.
Column, address.
Row), out
Cell cell))
1700 return cells.ContainsKey(
new CellKey(address.
Column, address.
Row));
1712 public bool HasCell(
int columnNumber,
int rowNumber)
1724 if (columns.TryGetValue(columnNumber, out var value) && !value.HasAutoFilter)
1726 columns.Remove(columnNumber);
1728 else if (columns.TryGetValue(columnNumber, out var value2))
1730 value2.IsHidden =
false;
1740 public IReadOnlyList<Cell>
GetRow(
int rowNumber)
1742 List<Cell> list =
new List<Cell>();
1743 foreach (
Cell cell
in cells.Values)
1750 list.Sort((c1, c2) => (c1.ColumnNumber.CompareTo(c2.ColumnNumber)));
1773 List<Cell> list =
new List<Cell>();
1774 foreach (
Cell cell
in cells.Values)
1781 list.Sort((c1, c2) => (c1.RowNumber.CompareTo(c2.RowNumber)));
1791 return currentColumnNumber;
1800 return currentRowNumber;
1808 currentColumnNumber++;
1809 currentRowNumber = 0;
1821 currentColumnNumber += numberOfColumns;
1822 if (!keepRowPosition)
1824 currentRowNumber = 0;
1835 currentColumnNumber = 0;
1845 public void GoToNextRow(
int numberOfRows,
bool keepColumnPosition =
false)
1847 currentRowNumber += numberOfRows;
1848 if (!keepColumnPosition)
1850 currentColumnNumber = 0;
1888 string key = startAddress +
":" + endAddress;
1889 Range value =
new Range(startAddress, endAddress);
1891 foreach (KeyValuePair<string, Range> item
in mergedCells)
1893 if (item.Value.ResolveEnclosedAddresses().Intersect(result).Any())
1895 throw new RangeException(
"The passed range: " + value.ToString() +
" contains cells that are already in the defined merge range: " + item.Key);
1898 mergedCells.Add(key, value);
1905 internal void RecalculateAutoFilter()
1907 if (autoFilterRange ==
null)
1920 for (
int i = start; i <= end; i++)
1922 if (!columns.TryGetValue(i, out var value))
1926 HasAutoFilter =
true
1932 value.HasAutoFilter =
true;
1935 autoFilterRange =
new Range(start, 0, end, endRow);
1941 internal void RecalculateColumns()
1943 List<int> columnsToDelete =
new List<int>();
1944 foreach (KeyValuePair<int, Column> col
in columns)
1946 if (!col.Value.HasAutoFilter && !col.Value.IsHidden && Comparators.CompareDimensions(col.Value.Width,
DefaultWorksheetColumnWidth) == 0 && col.Value.DefaultColumnStyle ==
null)
1948 columnsToDelete.Add(col.Key);
1951 foreach (
int index
in columnsToDelete)
1953 columns.Remove(index);
1962 internal void ResolveMergedCells()
1964 Style mergeStyle = BasicStyles.MergeCellStyle;
1966 foreach (KeyValuePair<string, Range> range
in MergedCells)
1969 List<Address> addresses = Cell.GetCellRange(range.Value.StartAddress, range.Value.EndAddress) as List<Address>;
1970 foreach (Address address
in addresses)
1972 if (!cells.TryGetValue(
new CellKey(address.
Column, address.
Row), out cell))
1976 DataType = Cell.CellType.Empty,
1977 RowNumber = address.
Row,
1978 ColumnNumber = address.Column
1980 AddCell(cell, cell.ColumnNumber, cell.RowNumber);
1984 cell.DataType = Cell.CellType.Empty;
1985 if (cell.CellStyle ==
null)
1987 cell.SetStyle(mergeStyle);
1991 Style mixedMergeStyle = cell.CellStyle;
1994 cell.SetStyle(mixedMergeStyle);
2007 autoFilterRange =
null;
2017 SetColumnHiddenState(columnNumber,
false);
2028 SetColumnHiddenState(columnNumber,
false);
2038 SetRowHiddenState(rowNumber,
false);
2049 if (range ==
null || !mergedCells.ContainsKey(range))
2051 throw new RangeException(
"The cell range " + range +
" was not found in the list of merged cell ranges");
2055 foreach (
Address address
in addresses)
2057 if (cells.TryGetValue(
new CellKey(address.
Column, address.
Row), out
Cell cell))
2063 cell.ResolveCellType();
2066 mergedCells.Remove(range);
2075 if (rowHeights.ContainsKey(rowNumber))
2077 rowHeights.Remove(rowNumber);
2087 if (sheetProtectionValues.Contains(value))
2089 sheetProtectionValues.Remove(value);
2101 useActiveStyle =
false;
2105 useActiveStyle =
true;
2107 activeStyle = style;
2120 if (endColumn < startColumn)
2139 RecalculateAutoFilter();
2140 RecalculateColumns();
2149 private void SetColumnHiddenState(
int columnNumber,
bool state)
2152 if (columns.TryGetValue(columnNumber, out var value))
2154 value.IsHidden = state;
2162 columns.Add(columnNumber, c);
2164 if (!columns[columnNumber].IsHidden && Comparators.CompareDimensions(columns[columnNumber].Width,
DefaultWorksheetColumnWidth) == 0 && !columns[columnNumber].HasAutoFilter)
2166 columns.Remove(columnNumber);
2195 if (columns.TryGetValue(columnNumber, out var value))
2197 value.Width = width;
2205 columns.Add(columnNumber, c);
2231 if (this.columns.TryGetValue(columnNumber, out var value))
2233 return value.SetDefaultColumnStyle(style);
2239 this.columns.Add(columnNumber, c);
2278 currentColumnNumber = columnNumber;
2289 currentRowNumber = rowNumber;
2317 Range? resolved = ParseRange(rangeOrAddress);
2318 if (resolved !=
null)
2338 selectedCells.Clear();
2357 Range? resolved = ParseRange(rangeOrAddress);
2358 if (resolved !=
null)
2389 if (
string.IsNullOrEmpty(password))
2391 sheetProtectionPassword.UnsetPassword();
2396 sheetProtectionPassword.SetPassword(password);
2414 if (rowHeights.ContainsKey(rowNumber))
2416 rowHeights[rowNumber] = height;
2420 rowHeights.Add(rowNumber, height);
2430 private void SetRowHiddenState(
int rowNumber,
bool state)
2433 if (hiddenRows.ContainsKey(rowNumber))
2437 hiddenRows[rowNumber] =
true;
2441 hiddenRows.Remove(rowNumber);
2446 hiddenRows.Add(rowNumber,
true);
2457 if (
string.IsNullOrEmpty(name))
2465 Regex regex =
new Regex(
@"[\[\]\*\?/\\]");
2466 Match match = regex.Match(name);
2467 if (match.Captures.Count > 0)
2469 throw new FormatException(
@"the worksheet name must not contain the characters [ ] * ? / \ ");
2501 SetSplit(
null, topPaneHeight, topLeftCell, activePane);
2514 SetSplit(
null, numberOfRowsFromTop, freeze, topLeftCell, activePane);
2525 SetSplit(leftPaneWidth,
null, topLeftCell, activePane);
2539 SetSplit(numberOfColumnsFromLeft,
null, freeze, topLeftCell, activePane);
2557 if (numberOfColumnsFromLeft !=
null && topLeftCell.
Column < numberOfColumnsFromLeft.Value)
2560 " is not valid for a frozen, vertical split with the split pane column number " + numberOfColumnsFromLeft.Value);
2562 if (numberOfRowsFromTop !=
null && topLeftCell.
Row < numberOfRowsFromTop.Value)
2565 " is not valid for a frozen, horizontal split height the split pane row number " + numberOfRowsFromTop.Value);
2568 this.paneSplitLeftWidth =
null;
2569 this.paneSplitTopHeight =
null;
2570 this.freezeSplitPanes = freeze;
2571 int row = numberOfRowsFromTop !=
null ? numberOfRowsFromTop.Value : 0;
2572 int column = numberOfColumnsFromLeft !=
null ? numberOfColumnsFromLeft.Value : 0;
2573 this.paneSplitAddress =
new Address(column, row);
2574 this.paneSplitTopLeftCell = topLeftCell;
2575 this.activePane = activePane;
2589 this.paneSplitLeftWidth = leftPaneWidth;
2590 this.paneSplitTopHeight = topPaneHeight;
2591 this.freezeSplitPanes =
null;
2592 this.paneSplitAddress =
null;
2593 this.paneSplitTopLeftCell = topLeftCell;
2594 this.activePane = activePane;
2602 this.paneSplitLeftWidth =
null;
2603 this.paneSplitTopHeight =
null;
2604 this.freezeSplitPanes =
null;
2605 this.paneSplitAddress =
null;
2606 this.paneSplitTopLeftCell =
null;
2607 this.activePane =
null;
2622 foreach (
Cell cell
in this.cells.Values)
2626 copy.activePane = this.activePane;
2627 copy.activeStyle = this.activeStyle;
2628 if (this.autoFilterRange.HasValue)
2630 copy.autoFilterRange = this.autoFilterRange.Value.Copy();
2632 foreach (KeyValuePair<int, Column> column
in this.columns)
2634 copy.columns.Add(column.Key, column.Value.Copy());
2636 copy.CurrentCellDirection = this.CurrentCellDirection;
2637 copy.currentColumnNumber = this.currentColumnNumber;
2638 copy.currentRowNumber = this.currentRowNumber;
2639 copy.defaultColumnWidth = this.defaultColumnWidth;
2640 copy.defaultRowHeight = this.defaultRowHeight;
2641 copy.freezeSplitPanes = this.freezeSplitPanes;
2642 copy.hidden = this.hidden;
2643 foreach (KeyValuePair<int, bool> row
in this.hiddenRows)
2645 copy.hiddenRows.Add(row.Key, row.Value);
2647 foreach (KeyValuePair<string, Range> cell
in this.mergedCells)
2649 copy.mergedCells.Add(cell.Key, cell.
Value.Copy());
2651 if (this.paneSplitAddress.HasValue)
2653 copy.paneSplitAddress = this.paneSplitAddress.Value.Copy();
2655 copy.paneSplitLeftWidth = this.paneSplitLeftWidth;
2656 copy.paneSplitTopHeight = this.paneSplitTopHeight;
2657 if (this.paneSplitTopLeftCell.HasValue)
2659 copy.paneSplitTopLeftCell = this.paneSplitTopLeftCell.Value.Copy();
2661 foreach (KeyValuePair<int, float> row
in this.rowHeights)
2663 copy.rowHeights.Add(row.Key, row.Value);
2665 foreach (
Range range
in selectedCells)
2667 copy.AddSelectedCells(range);
2669 copy.sheetProtectionPassword.CopyFrom(this.sheetProtectionPassword);
2672 copy.sheetProtectionValues.Add(value);
2674 copy.useActiveStyle = this.useActiveStyle;
2675 copy.UseSheetProtection = this.UseSheetProtection;
2676 copy.ShowGridLines = this.ShowGridLines;
2677 copy.ShowRowColumnHeaders = this.ShowRowColumnHeaders;
2678 copy.ShowRuler = this.ShowRuler;
2679 copy.ViewType = this.ViewType;
2680 copy.zoomFactor.Clear();
2681 foreach (KeyValuePair<SheetViewType, int> zoomFactor
in this.zoomFactor)
2683 copy.SetZoomFactor(zoomFactor.Key, zoomFactor.Value);
2701 if (this.zoomFactor.ContainsKey(sheetViewType))
2703 this.zoomFactor[sheetViewType] = zoomFactor;
2707 this.zoomFactor.Add(sheetViewType, zoomFactor);
2713 #region static_methods
2723 if (
string.IsNullOrEmpty(input))
2738 for (
int i = 0; i < len; i++)
2741 if (c ==
'[' || c ==
']' || c ==
'*' || c ==
'?' || c ==
'\\' || c ==
'/')
2746 return GetUnusedWorksheetName(sb.ToString(), workbook);
2754 private static Range? ParseRange(
string rangeOrAddress)
2756 if (
string.IsNullOrEmpty(rangeOrAddress))
2761 if (rangeOrAddress.Contains(
":"))
2767 Address address = Cell.ResolveCellCoordinate(rangeOrAddress);
2768 range =
new Range(address, address);
2782 private static string GetUnusedWorksheetName(
string name, Workbook workbook)
2784 if (workbook ==
null)
2786 throw new WorksheetException(
"The workbook reference is null");
2788 if (!WorksheetExists(name, workbook))
2790 Regex regex =
new Regex(
@"^(.*?)(\d{1,31})$");
2791 Match match = regex.Match(name);
2792 string prefix = name;
2794 if (match.Groups.Count > 1)
2796 prefix = match.Groups[1].Value;
2797 _ =
int.TryParse(match.Groups[2].Value, out number);
2802 string numberString = ParserUtils.ToString(number);
2806 prefix = prefix.Substring(0, endIndex);
2808 string newName = prefix + numberString;
2809 if (!WorksheetExists(newName, workbook))
2821 private static bool WorksheetExists(
string name, Workbook workbook)
2824 for (
int i = 0; i < len; i++)
2826 if (workbook.
Worksheets[i].SheetName == name)
2866 internal static WorksheetPane? GetWorksheetPaneEnum(
string pane)
2873 case "bottomLeft": output =
WorksheetPane.BottomLeft;
break;
2874 case "bottomRight": output =
WorksheetPane.BottomRight;
break;
2884 internal static SheetViewType GetSheetViewTypeEnum(
string viewType)
2889 case "pageBreakPreview": output =
SheetViewType.PageBreakPreview;
break;
2890 case "pageLayout": output =
SheetViewType.PageLayout;
break;
Class representing a cell of a worksheet.
static Address ResolveCellCoordinate(string address)
Gets the column and row number (zero based) of a cell by the address.
CellType
Enum defines the basic data types of a cell.
static void ValidateRowNumber(int row)
Validates the passed (zero-based) row number. An exception will be thrown if the row is invalid.
Address CellAddress2
Gets or sets the combined cell Address as Address object.
static string ResolveCellAddress(int column, int row, AddressType type=AddressType.Default)
Gets the address of a cell by the column and row number (zero based).
static AddressScope GetAddressScope(string addressExpression)
Gets the scope of the passed address (string expression). Scope means either single cell address or r...
Style CellStyle
Gets the assigned style of the cell.
static Range ResolveCellRange(string range)
Resolves a cell range from the format like A1:B3 or AAD556:AAD1000.
int RowNumber
Gets or sets the number of the row (zero-based).
static void ValidateColumnNumber(int column)
Validates the passed (zero-based) column number. An exception will be thrown if the column is invalid...
static int ResolveColumn(string columnAddress)
Gets the column number from the column address (A - XFD).
CellType DataType
Gets or sets the type of the cell.
AddressScope
Enum to define the scope of a passed address string (used in static context).
int ColumnNumber
Gets or sets the number of the column (zero-based).
Style SetStyle(Style style, bool unmanaged=false)
Sets the style of the cell.
static IEnumerable< Address > GetCellRange(string range)
Gets a list of cell addresses from a cell range (format A1:B3 or AAD556:AAD1000).
object Value
Gets or sets the value of the cell (generic object type). When setting a value, the DataType is autom...
Class representing a column of a worksheet.
Style SetDefaultColumnStyle(Style defaultColumnStyle, bool unmanaged=false)
Sets the default style of the column.
Class for exceptions regarding range incidents (e.g. out-of-range).
Class for exceptions regarding worksheet incidents.
Class implementing a legacy password, based on the proprietary hashing algorithm of Excel.
PasswordType
Target type of the password.
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.
bool Equals(AbstractStyle other)
Method to compare two objects for sorting purpose.
Factory class with the most important predefined styles.
static Style MergeCellStyle
Gets the style used when merging cells.
bool ForceApplyAlignment
Gets or sets whether the applyAlignment property (used to merge cells) will be defined in the XF entr...
Class representing a Style with sub classes within a style sheet. An instance of this class is only a...
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
CellXf CurrentCellXf
Gets or sets the current CellXf object of the style.
Style Append(AbstractStyle styleToAppend)
Appends the specified style parts to the current one. The parts can be instances of sub-classes like ...
General data utils class with static methods.
static IReadOnlyList< Range > MergeRange(List< Range > givenRanges, Range newRange, RangeMergeStrategy strategy=RangeMergeStrategy.MergeColumns)
Merges a range with a list of given ranges. If there is no intersection between the list and the new ...
static IReadOnlyList< Range > SubtractRange(List< Range > givenRanges, Range rangeToRemove, RangeMergeStrategy strategy=RangeMergeStrategy.MergeColumns)
Subtracts a range form a list of given ranges. If the range to be removed does not intersect any of t...
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 representing a workbook.
List< Worksheet > Worksheets
Gets the list of worksheets in the workbook.
Dictionary< int, bool > HiddenRows
Gets the hidden rows as dictionary with the zero-based row number as key and a boolean as value....
Cell FirstCellByValue(object searchValue)
Searches for the first occurrence of the value.
int GetFirstDataRowNumber()
Gets the first existing row number with data in the current worksheet (zero-based).
static readonly int MaxWorksheetNameLength
Maximum number of characters a worksheet name can have.
void SetStyle(Range cellRange, Style style)
Sets the passed style on the passed cell range. If cells are already existing, the style will be adde...
float DefaultColumnWidth
Gets or sets the default column width.
void AddCellFormula(string formula, string address, Style style)
Adds a cell formula as string to the defined cell address.
WorksheetPane
Enum to define the pane position or active pane in a slip worksheet.
@ TopRight
The pane is located in the top right of the split worksheet.
@ BottomRight
The pane is located in the bottom right of the split worksheet.
@ BottomLeft
The pane is located in the bottom left of the split worksheet.
@ TopLeft
The pane is located in the top left of the split worksheet.
void SetHorizontalSplit(float topPaneHeight, Address topLeftCell, WorksheetPane? activePane)
Sets the horizontal split of the worksheet into two panes. The measurement in characters cannot be us...
float DefaultRowHeight
Gets or sets the default Row height.
int ZoomFactor
Gets or sets the zoom factor of the ViewType of the current worksheet. If AutoZoomFactor,...
void GoToNextRow()
Moves the current position to the next row (use for a new line).
int SheetID
Gets or sets the internal ID of the worksheet.
void RemoveHiddenRow(int rowNumber)
Sets a previously defined, hidden row as visible again.
CellDirection
Enum to define the direction when using AddNextCell method.
@ RowToRow
The next cell will be on the same column (A1,A2,A3...).
@ ColumnToColumn
The next cell will be on the same row (A1,B1,C1...).
@ Disabled
The address of the next cell will be not changed when adding a cell (for manual definition of cell ad...
int GetLastColumnNumber()
Gets the last existing column number in the current worksheet (zero-based).
WorksheetPane? ActivePane
Gets the active Pane is splitting is applied. The value is nullable. If null, no splitting was defin...
bool ShowRuler
Gets or sets whether a ruler is displayed over the column headers. This value only applies if ViewTyp...
int GetCurrentColumnNumber()
Gets the current column number (zero based).
void ClearSelectedCells()
Removes all cell selections of this worksheet.
bool UseSheetProtection
Gets or sets whether the worksheet is protected. If true, protection is enabled.
void InsertRow(int rowNumber, int numberOfNewRows)
Inserts 'count' rows below the specified 'rowNumber'. Existing cells are moved down by the number of ...
void SetSheetProtectionPassword(string password)
Sets or removes the password for worksheet protection. If set, UseSheetProtection will be also set to...
Dictionary< SheetViewType, int > ZoomFactors
Gets all defined zoom factors per SheetViewType of the current worksheet. Use SetZoomFactor(SheetView...
SheetViewType
Enum to define how a worksheet is displayed in the spreadsheet application (Excel).
@ PageBreakPreview
The worksheet is displayed with indicators where the page would break if it were printed.
@ PageLayout
The worksheet is displayed like it would be printed.
@ Normal
The worksheet is displayed without pagination (default).
void GoToNextColumn()
Moves the current position to the next column.
void AddNextCellFormula(string formula, Style style)
Adds a formula as string to the next cell position.
void SetRowHeight(int rowNumber, float height)
Sets the height of the passed row number (zero-based).
Worksheet(string name)
Constructor with worksheet name.
int GetFirstColumnNumber()
Gets the first existing column number in the current worksheet (zero-based).
void AddHiddenColumn(int columnNumber)
Sets the defined column as hidden.
CellDirection CurrentCellDirection
Gets or sets the direction when using AddNextCell method.
Cell GetCell(Address address)
Gets the cell of the specified address.
IReadOnlyList< Cell > GetRow(int rowNumber)
Gets a row as list of cell objects.
int ReplaceCellValue(object oldValue, object newValue)
Replaces all occurrences of 'oldValue' with 'newValue' and returns the number of replacements.
static readonly int MinColumnNumber
Minimum column number (zero-based) as constant.
void AddSelectedCells(string rangeOrAddress)
Adds a range or cell address to the selected cells on this worksheet.
Worksheet Copy()
Creates a (dereferenced) deep copy of this worksheet.
void SetCurrentCellAddress(int columnNumber, int rowNumber)
Set the current cell address.
string MergeCells(string cellRange)
Merges the defined cell range.
bool ShowGridLines
Gets or sets whether grid lines are visible on the current worksheet. Default is true.
IReadOnlyList< Cell > GetColumn(int columnNumber)
Gets a column as list of cell objects.
Address? GetLastDataCellAddress()
Gets the last existing cell with data in the current worksheet (bottom right).
void AddSelectedCells(Address startAddress, Address endAddress)
Adds a range to the selected cells on this worksheet.
void InsertColumn(int columnNumber, int numberOfNewColumns)
Inserts 'count' columns right of the specified 'columnNumber'. Existing cells are moved to the right ...
SheetProtectionValue
Enum to define the possible protection types when protecting a worksheet.
@ PivotTables
If selected, the user can use pivot tables if the worksheets is protected.
@ FormatCells
If selected, the user can format cells if the worksheets is protected.
@ InsertHyperlinks
If selected, the user can insert hyper links if the worksheets is protected.
@ InsertColumns
If selected, the user can insert columns if the worksheets is protected.
@ Sort
If selected, the user can sort cells if the worksheets is protected.
@ DeleteColumns
If selected, the user can delete columns if the worksheets is protected.
@ Scenarios
If selected, the user can edit scenarios if the worksheets is protected.
@ DeleteRows
If selected, the user can delete rows if the worksheets is protected.
@ FormatColumns
If selected, the user can format columns if the worksheets is protected.
@ InsertRows
If selected, the user can insert rows if the worksheets is protected.
@ FormatRows
If selected, the user can format rows if the worksheets is protected.
@ AutoFilter
If selected, the user can use auto filters if the worksheets is protected.
@ Objects
If selected, the user can edit objects if the worksheets is protected.
@ SelectUnlockedCells
If selected, the user can select unlocked cells if the worksheets is protected.
@ SelectLockedCells
If selected, the user can select locked cells if the worksheets is protected.
Style SetColumnDefaultStyle(int columnNumber, Style style)
Sets the default column style of the passed column number (zero-based).
Worksheet(string name, int id, Workbook reference)
Constructor with name and sheet ID.
void SetColumnWidth(string columnAddress, float width)
Sets the width of the passed column address.
void SetSplit(float? leftPaneWidth, float? topPaneHeight, Address topLeftCell, WorksheetPane? activePane)
Sets the horizontal and vertical split of the worksheet into four panes. The measurement in character...
Address? GetFirstCellAddress()
Gets the first existing cell in the current worksheet (bottom right).
void AddCellRange(IReadOnlyList< object > values, Range cellRange, Style style)
Adds a list of object values to a defined cell range. If the type of the particular value does not ma...
void AddHiddenColumn(string columnAddress)
Sets the defined column as hidden.
IReadOnlyList< Cell > GetColumn(string columnAddress)
Gets a column as list of cell objects.
void SetStyle(string addressExpression, Style style)
Sets the passed style on the passed address expression. Such an expression may be a single cell or a ...
static readonly float DefaultWorksheetColumnWidth
Default column width as constant.
static readonly float DefaultWorksheetRowHeight
Default row height as constant.
Address? GetLastCellAddress()
Gets the last existing cell in the current worksheet (bottom right).
bool HasCell(int columnNumber, int rowNumber)
Gets whether the specified address exists in the worksheet. Existing means that a value was stored at...
static readonly int MaxRowNumber
Maximum row number (zero-based) as constant.
int GetLastDataRowNumber()
Gets the last existing row number with data in the current worksheet (zero-based).
void ResetSplit()
Resets splitting of the worksheet into panes, as well as their freezing.
void SetCurrentCellAddress(string address)
Set the current cell address.
void RemoveSelectedCells(Address address)
Removes the given address from the selected cell ranges of this worksheet, if existing.
Address? PaneSplitTopLeftCell
Gets the Top Left cell address of the bottom right pane if applicable and splitting is applied....
void ClearActiveStyle()
Clears the active style of the worksheet. All later added calls will contain no style unless another ...
void SetHorizontalSplit(int numberOfRowsFromTop, bool freeze, Address topLeftCell, WorksheetPane? activePane)
Sets the horizontal split of the worksheet into two panes. The measurement in rows can be used to spl...
void RemoveAutoFilter()
Removes auto filters from the worksheet.
void GoToNextRow(int numberOfRows, bool keepColumnPosition=false)
Moves the current position to the next row with the number of cells to move (use for a new line).
string MergeCells(Range cellRange)
Merges the defined cell range.
bool RemoveCell(int columnNumber, int rowNumber)
Removes a previous inserted cell at the defined address.
void AddSelectedCells(Address address)
Adds a single cell address to the selected cells on this worksheet.
void SetSplit(int? numberOfColumnsFromLeft, int? numberOfRowsFromTop, bool freeze, Address topLeftCell, WorksheetPane? activePane)
Sets the horizontal and vertical split of the worksheet into four panes. The measurement in rows and ...
Style SetColumnDefaultStyle(string columnAddress, Style style)
Sets the default column style of the passed column address.
void RemoveSelectedCells(String rangeOrAddress)
Removes the given range or cell address from the selected cell ranges of this worksheet,...
IEnumerable< Cell > CellValues
Gets all cells of the worksheet as an enumerable sequence. Preferred over Cells in performance-critic...
void AddCellRange(IReadOnlyList< object > values, Range cellRange)
Adds a list of object values to a defined cell range. If the type of the particular value does not ma...
void AddAllowedActionOnSheetProtection(SheetProtectionValue typeOfProtection)
Method to add allowed actions if the worksheet is protected. If one or more values are added,...
void SetAutoFilter(string range)
Sets the column auto filter within the defined column range.
static readonly int MaxColumnNumber
Maximum column number (zero-based) as constant.
void AddCellFormula(string formula, string address)
Adds a cell formula as string to the defined cell address.
void SetVerticalSplit(float leftPaneWidth, Address topLeftCell, WorksheetPane? activePane)
Sets the vertical split of the worksheet into two panes. The measurement in characters cannot be used...
void AddCell(object value, int columnNumber, int rowNumber)
Adds an object to the defined cell address. If the type of the value does not match with one of the s...
void SetCurrentColumnNumber(int columnNumber)
Sets the current column number (zero based).
Address? GetFirstDataCellAddress()
Gets the first existing cell with data in the current worksheet (bottom right).
void SetVerticalSplit(int numberOfColumnsFromLeft, bool freeze, Address topLeftCell, WorksheetPane? activePane)
Sets the vertical split of the worksheet into two panes. The measurement in columns can be used to sp...
SheetViewType ViewType
Gets or sets how the current worksheet is displayed in the spreadsheet application (Excel).
string SheetName
Gets or sets the name of the worksheet.
void AddCellFormula(string formula, int columnNumber, int rowNumber)
Adds a cell formula as string to the defined cell address.
bool HasCell(Address address)
Gets whether the specified address exists in the worksheet. Existing means that a value was stored at...
Workbook WorkbookReference
Gets or sets the Reference to the parent Workbook.
const int AutoZoomFactor
Automatic zoom factor of a worksheet.
float? PaneSplitTopHeight
Gets the height of the upper, horizontal split pane, measured from the top of the window....
Address? PaneSplitAddress
Gets the split address for frozen panes or if pane split was defined in number of columns and / or ro...
Cell FirstOrDefaultCell(Func< Cell, bool > predicate)
Searches for the first occurrence of the expression. Example: var cell = worksheet....
Worksheet()
Default Constructor.
void RemoveSelectedCells(Range range)
Removes the given range from the selected cell ranges of this worksheet, if existing....
List< Range > SelectedCells
Gets the cell ranges of selected cells of this worksheet. Returns ans empty list if no cells are sele...
void SetSheetName(string name)
Validates and sets the worksheet name.
int GetLastDataColumnNumber()
Gets the last existing column number with data in the current worksheet (zero-based).
string MergeCells(Address startAddress, Address endAddress)
Merges the defined cell range.
void AddCellRange(IReadOnlyList< object > values, Address startAddress, Address endAddress, Style style)
Adds a list of object values to a defined cell range. If the type of the particular value does not ma...
int GetFirstRowNumber()
Gets the first existing row number in the current worksheet (zero-based).
void SetActiveStyle(Style style)
Sets the active style of the worksheet. This style will be assigned to all later added cells.
int GetFirstDataColumnNumber()
Gets the first existing column number with data in the current worksheet (zero-based).
List< SheetProtectionValue > SheetProtectionValues
Gets the list of SheetProtectionValues. These values define the allowed actions if the worksheet is p...
void SetZoomFactor(SheetViewType sheetViewType, int zoomFactor)
Sets a zoom factor for a given SheetViewType. If AutoZoomFactor, the zoom factor is set to automatic.
static string SanitizeWorksheetName(string input, Workbook workbook)
Sanitizes a worksheet name.
void SetStyle(Address address, Style style)
Sets the passed style on the passed (singular) cell address. If the cell is already existing,...
void AddHiddenRow(int rowNumber)
Sets the defined row as hidden.
void AddCell(object value, string address)
Adds an object to the defined cell address. If the type of the value does not match with one of the s...
static readonly float MinColumnWidth
Minimum column width as constant.
void RemoveMergedCells(string range)
Removes the defined merged cell range.
static readonly int MinRowNumber
Minimum row number (zero-based) as constant.
void AddNextCell(object value, Style style)
Adds an object to the next cell position. If the type of the value does not match with one of the sup...
Dictionary< int, float > RowHeights
Gets defined row heights as dictionary with the zero-based row number as key and the height (float fr...
void AddCell(object value, string address, Style style)
Adds an object to the defined cell address. If the type of the value does not match with one of the s...
void AddCellRange(IReadOnlyList< object > values, Address startAddress, Address endAddress)
Adds a list of object values to a defined cell range. If the type of the particular value does not ma...
bool ShowRowColumnHeaders
Gets or sets whether the column and row headers are visible on the current worksheet....
virtual IPassword SheetProtectionPassword
Password instance of the worksheet protection. If a password was set, the pain text representation an...
const int maxZoomFactor
Maximum zoom factor of a worksheet.
void SetColumnWidth(int columnNumber, float width)
Sets the width of the passed column number (zero-based).
static readonly float MaxColumnWidth
Maximum column width as constant.
Dictionary< string, Range > MergedCells
Gets the merged cells (only references) as dictionary with the cell address as key and the range obje...
void SetSheetName(string name, bool sanitize)
Sets the name of the worksheet.
bool? FreezeSplitPanes
Gets whether split panes are frozen. The value is nullable. If null, no freezing is applied....
void AddSelectedCells(Range range)
Adds a range to the selected cells on this worksheet.
IReadOnlyDictionary< string, Cell > Cells
Gets the cells of the worksheet as read-only dictionary with the cell address string as key and the c...
void AddCellRange(IReadOnlyList< object > values, string cellRange, Style style)
Adds a list of object values to a defined cell range. If the type of the particular value does not ma...
void AddCellRange(IReadOnlyList< object > values, string cellRange)
Adds a list of object values to a defined cell range. If the type of the particular value does not ma...
void AddNextCell(object value)
Adds an object to the next cell position. If the type of the value does not match with one of the sup...
void RemoveHiddenColumn(int columnNumber)
Sets a previously defined, hidden column as visible again.
void RemoveHiddenColumn(string columnAddress)
Sets a previously defined, hidden column as visible again.
Range? AutoFilterRange
Gets the range of the auto-filter. Wrapped to Nullable to provide null as value. If null,...
const int MinZoomFactor
Minimum zoom factor of a worksheet. If set to this value, the zoom is set to automatic.
bool RemoveCell(string address)
Removes a previous inserted cell at the defined address.
int GetLastRowNumber()
Gets the last existing row number in the current worksheet (zero-based).
void ResetColumn(int columnNumber)
Resets the defined column, if existing. The corresponding instance will be removed from Columns.
void SetCurrentRowNumber(int rowNumber)
Sets the current row number (zero based).
void AddCell(object value, int columnNumber, int rowNumber, Style style)
Adds an object to the defined cell address. If the type of the value does not match with one of the s...
void AddNextCellFormula(string formula)
Adds a formula as string to the next cell position.
void RemoveAllowedActionOnSheetProtection(SheetProtectionValue value)
Removes an allowed action on the current worksheet or its cells.
int GetCurrentRowNumber()
Gets the current row number (zero based).
static readonly float MinRowHeight
Minimum row height as constant.
List< Cell > CellsByValue(object searchValue)
Searches for cells that contain the specified value and returns a list of these cells.
Style ActiveStyle
Gets the active Style of the worksheet. If null, no style is defined as active.
static readonly float MaxRowHeight
Maximum row height as constant.
void AddCellFormula(string formula, int columnNumber, int rowNumber, Style style)
Adds a cell formula as string to the defined cell address.
void RemoveRowHeight(int rowNumber)
Removes the defined, non-standard row height.
void SetStyle(Address startAddress, Address endAddress, Style style)
Sets the passed style on the passed cell range, derived from a start and end address....
void SetAutoFilter(int startColumn, int endColumn)
Sets the column auto filter within the defined column range.
void GoToNextColumn(int numberOfColumns, bool keepRowPosition=false)
Moves the current position to the next column with the number of cells to move.
bool Hidden
gets or sets whether the worksheet is hidden. If true, the worksheet is not listed in the worksheet t...
void RemoveSelectedCells(Address startAddress, Address endAddress)
Removes the given range from the selected cell ranges of this worksheet, if existing.
Dictionary< int, Column > Columns
Gets all columns with non-standard properties, like auto filter applied or a special width as diction...
float? PaneSplitLeftWidth
Gets the width of the left, vertical split pane, measured from the left of the window....
Cell GetCell(int columnNumber, int rowNumber)
Gets the cell of the specified column and row number (zero-based).
Interface to represent a protection password, either for workbooks or worksheets. The implementations...
Struct representing the cell address as column and row (zero based).
int Row
Row number (zero based).
string GetAddress()
Returns the combined Address.
int Column
Column number (zero based).
Struct representing a cell range with a start and end address.
IReadOnlyList< Address > ResolveEnclosedAddresses()
Gets a list of all addresses between the start and end address.
Address StartAddress
Start address of the range.
Address EndAddress
End address of the range.