many debugs
All checks were successful
Publish NuGet packages / publish (push) Successful in 28s

This commit is contained in:
melekhin
2026-06-19 15:06:40 +07:00
parent 08b39b7bfe
commit e373d4108a
24 changed files with 1569 additions and 632 deletions

View File

@@ -5,9 +5,9 @@
/// </summary>
internal sealed class ExcelRow : IRow
{
private readonly ExcelWriter _writer;
private readonly ExcelSheet _sheet;
private uint _rowIndex;
readonly ExcelWriter _writer;
readonly ExcelSheet _sheet;
uint _rowIndex;
internal ExcelRow(ExcelWriter writer, ExcelSheet sheet, uint rowIndex)
{
@@ -47,7 +47,7 @@ internal sealed class ExcelRow : IRow
}
// Вспомогательные методы
private static Row? GetRowElementInternal(ExcelSheet sheet, uint rowIndex)
static Row? GetRowElementInternal(ExcelSheet sheet, uint rowIndex)
{
var sheetData = sheet.GetSheetData();
foreach (var row in sheetData.Elements<Row>())
@@ -55,7 +55,7 @@ internal sealed class ExcelRow : IRow
return null;
}
private static Row GetOrCreateRowElementInternal(ExcelSheet sheet, uint rowIndex)
static Row GetOrCreateRowElementInternal(ExcelSheet sheet, uint rowIndex)
{
var existing = GetRowElementInternal(sheet, rowIndex);
if (existing != null) return existing;
@@ -66,7 +66,7 @@ internal sealed class ExcelRow : IRow
return newRow;
}
private static void InsertRowElementInternal(SheetData sheetData, Row row, uint rowIndex)
static void InsertRowElementInternal(SheetData sheetData, Row row, uint rowIndex)
{
bool inserted = false;
foreach (var existing in sheetData.Elements<Row>().ToList())
@@ -136,25 +136,157 @@ internal sealed class ExcelRow : IRow
}
}
}
private void ApplyStyleToRowCells(CellStyle style)
{
var sheetData = _sheet.GetSheetData();
var rowElement = FindRowElement(sheetData, _rowIndex);
if (rowElement == null) return;
// Применяем стиль ко всем существующим ячейкам
foreach (var cellElement in rowElement.Elements<Cell>())
{
var colIndex = GetColumnIndex(cellElement.CellReference?.Value);
if (colIndex > 0)
{
var cell = new ExcelCell(_writer, _sheet, _rowIndex, colIndex);
// Применяем стиль через Set, чтобы объединить с существующим
cell.Set(style);
}
}
}
static uint GetColumnIndex(string? cellReference)
{
if (string.IsNullOrEmpty(cellReference)) return 0;
int i = 0;
while (i < cellReference!.Length && char.IsLetter(cellReference[i])) i++;
if (i == 0) return 0;
string colPart = cellReference.Substring(0, i);
return CellAddressHelper.ColumnLetterToIndex(colPart);
}
CellStyle? GetRowStyle(Row rowElement)
{
if (rowElement.StyleIndex?.Value is not uint styleIndex)
return null;
return _writer.GetCellStyle(styleIndex);
}
/// <inheritdoc />
public IRow SetNumberFormat(NumberFormatPattern format)
public IRow Set(NumberFormatPattern format)
{
if (format == null) return this;
_writer.ThrowIfDisposed();
lock (_writer._syncLock)
{
// Находим все ячейки в этой строке и устанавливаем формат
var sheetData = _sheet.GetSheetData();
var rowElement = FindRowElement(sheetData, _rowIndex);
if (rowElement == null) return this;
int formatIndex = GetOrCreateNumberFormatId(format);
foreach (var cell in rowElement.Elements<Cell>())
cell.StyleIndex = (uint)formatIndex;
var rowElement = GetOrCreateRowElement();
var currentStyle = GetRowStyle(rowElement) ?? new CellStyle();
if (currentStyle.TryMerge(format, out var newStyle))
{
ApplyStyleToRow(newStyle);
ApplyStyleToRowCells(newStyle);
}
return this;
}
return this;
}
/// <inheritdoc />
public IRow Set(CellAlign align)
{
_writer.ThrowIfDisposed();
lock (_writer._syncLock)
{
var rowElement = GetOrCreateRowElement();
var currentStyle = GetRowStyle(rowElement) ?? new CellStyle();
if (currentStyle.TryMerge(align, out var newStyle))
{
ApplyStyleToRow(newStyle);
ApplyStyleToRowCells(newStyle);
}
return this;
}
}
/// <inheritdoc />
public IRow Set(CellBorder border)
{
_writer.ThrowIfDisposed();
lock (_writer._syncLock)
{
var rowElement = GetOrCreateRowElement();
var currentStyle = GetRowStyle(rowElement) ?? new CellStyle();
if (currentStyle.TryMerge(border, out var newStyle))
{
ApplyStyleToRow(newStyle);
ApplyStyleToRowCells(newStyle);
}
return this;
}
}
/// <inheritdoc />
public IRow Set(CellFill fill)
{
_writer.ThrowIfDisposed();
lock (_writer._syncLock)
{
var rowElement = GetOrCreateRowElement();
var currentStyle = GetRowStyle(rowElement) ?? new CellStyle();
if (currentStyle.TryMerge(fill, out var newStyle))
{
ApplyStyleToRow(newStyle);
ApplyStyleToRowCells(newStyle);
}
return this;
}
}
/// <inheritdoc />
public IRow Set(CellFont font)
{
_writer.ThrowIfDisposed();
lock (_writer._syncLock)
{
var rowElement = GetOrCreateRowElement();
var currentStyle = GetRowStyle(rowElement) ?? new CellStyle();
if (currentStyle.TryMerge(font, out var newStyle))
{
ApplyStyleToRow(newStyle);
ApplyStyleToRowCells(newStyle);
}
return this;
}
}
/// <inheritdoc />
public IRow Set(CellStyle style)
{
if (style == null) return this;
_writer.ThrowIfDisposed();
lock (_writer._syncLock)
{
var rowElement = GetOrCreateRowElement();
var currentStyle = GetRowStyle(rowElement) ?? new CellStyle();
if (currentStyle.TryMerge(style, out var newStyle))
{
ApplyStyleToRow(newStyle);
ApplyStyleToRowCells(newStyle);
}
return this;
}
}
void ApplyStyleToRow(CellStyle style)
{
var rowElement = GetOrCreateRowElement();
int styleIndex = _writer.GetOrCreateStyleId(style);
rowElement.StyleIndex = (uint)styleIndex;
rowElement.CustomFormat = true;
}
/// <inheritdoc />
public ICell Cell(uint col) => new ExcelCell(_writer, _sheet, _rowIndex, col);
@@ -176,7 +308,7 @@ internal sealed class ExcelRow : IRow
public IRow Cell(uint col, string value, NumberFormatPattern? format = null)
{
Cell(col).Set(value, format); return this;
Cell(col).SetFormula(value, format); return this;
}
public IRow Cell(uint col, DateTime value, NumberFormatPattern? format = null)
@@ -226,7 +358,7 @@ internal sealed class ExcelRow : IRow
public IRow Cell(string col, string value, NumberFormatPattern? format = null)
{
Cell(col).Set(value, format); return this;
Cell(col).SetFormula(value, format); return this;
}
public IRow Cell(string col, DateTime value, NumberFormatPattern? format = null)
@@ -347,7 +479,7 @@ internal sealed class ExcelRow : IRow
// Вспомогательные методы
private Row GetOrCreateRowElement()
Row GetOrCreateRowElement()
{
var sheetData = _sheet.GetSheetData();
var existing = FindRowElement(sheetData, _rowIndex);
@@ -357,7 +489,7 @@ internal sealed class ExcelRow : IRow
return newRow;
}
private static Row? FindRowElement(SheetData sheetData, uint rowIndex)
static Row? FindRowElement(SheetData sheetData, uint rowIndex)
{
// Поиск по атрибуту RowIndex. В Open XML строки могут идти не по порядку.
foreach (var row in sheetData.Elements<Row>())
@@ -368,7 +500,7 @@ internal sealed class ExcelRow : IRow
return null;
}
private static void InsertRowElement(SheetData sheetData, Row row, uint rowIndex)
static void InsertRowElement(SheetData sheetData, Row row, uint rowIndex)
{
// Вставка с сохранением сортировки по RowIndex
bool inserted = false;
@@ -385,7 +517,7 @@ internal sealed class ExcelRow : IRow
sheetData.Append(row);
}
private int GetOrCreateNumberFormatId(NumberFormatPattern format)
int GetOrCreateNumberFormatId(NumberFormatPattern format)
{
// Создаём стиль, содержащий только числовой формат, и возвращаем его индекс
return _writer.GetOrCreateCellFormatId(numberFormat: format);