Je veux faire ceci: QuestionComment changer le style de bordure de plusieurs cellules à la fois. NPOI
Mais faites-le en utilisant NPOI. Y a-t-il un moyen de fournir les 4 "points" du carré et de changer sa bordure pour ce que je veux?
Je veux faire ceci: QuestionComment changer le style de bordure de plusieurs cellules à la fois. NPOI
Mais faites-le en utilisant NPOI. Y a-t-il un moyen de fournir les 4 "points" du carré et de changer sa bordure pour ce que je veux?
Non, à moins que vous ne le programmiez vous-même. En outre, les styles de bordure dans NPOI (2.1.3.1) sont buggés, essentiellement lorsque votre ICellStyle a le même style de bordure qu'un autre ICellStyle (par exemple, deux cellules ont une bordure supérieure noire) et que vous en changez un, le changement est propagé aux deux styles (par exemple, une fois que vous ajoutez une bordure supérieure gauche à une cellule, elle est également ajoutée à une autre cellule).
J'ai créé la demande de traction, mais cela devrait fonctionner pour l'instant (méfiez-vous de 64 000 limite de styles cellulaires):
public void CreateBorder(ISheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn, BorderStyle borderStyle)
{
// top line
for (int column = firstColumn + 1; column < lastColumn; column++)
{
ICell topCell = GetCell(sheet, firstRow, column);
ICellStyle topStyle = CreateCellStyle(topCell);
using (new CellBorderLock(topStyle))
{
topStyle.BorderTop = borderStyle;
}
topCell.CellStyle = topStyle;
}
// top left corner
ICell topLeftCell = GetCell(sheet, firstRow, firstColumn);
ICellStyle topLeftStyle = CreateCellStyle(topLeftCell);
using (new CellBorderLock(topLeftStyle))
{
topLeftStyle.BorderTop = borderStyle;
topLeftStyle.BorderLeft = borderStyle;
}
topLeftCell.CellStyle = topLeftStyle;
// top right corner
ICell topRightCell = GetCell(sheet, firstRow, lastColumn);
ICellStyle topRightStyle = CreateCellStyle(topRightCell);
using (new CellBorderLock(topRightStyle))
{
topRightStyle.BorderTop = borderStyle;
topRightStyle.BorderRight = borderStyle;
}
topRightCell.CellStyle = topRightStyle;
// left line
for (int row = firstRow + 1; row < lastRow; row++)
{
ICell leftCell = GetCell(sheet, row, firstColumn);
ICellStyle leftStyle = CreateCellStyle(leftCell);
using (new CellBorderLock(leftStyle))
{
leftStyle.BorderLeft = borderStyle;
}
leftCell.CellStyle = leftStyle;
}
// right line
for (int row = firstRow + 1; row < lastRow; row++)
{
ICell rightCell = GetCell(sheet, row, lastColumn);
ICellStyle rightStyle = CreateCellStyle(rightCell);
using (new CellBorderLock(rightStyle))
{
rightStyle.BorderRight = borderStyle;
}
rightCell.CellStyle = rightStyle;
}
// bottom line
for (int column = firstColumn + 1; column < lastColumn; column++)
{
ICell bottomCell = GetCell(sheet, lastRow, column);
ICellStyle bottomStyle = CreateCellStyle(bottomCell);
using (new CellBorderLock(bottomStyle))
{
bottomStyle.BorderBottom = borderStyle;
}
bottomCell.CellStyle = bottomStyle;
}
// bottom left corner
ICell bottomLeftCell = GetCell(sheet, lastRow, firstColumn);
ICellStyle bottomLeftStyle = CreateCellStyle(bottomLeftCell);
using (new CellBorderLock(bottomLeftStyle))
{
bottomLeftStyle.BorderBottom = borderStyle;
bottomLeftStyle.BorderLeft = borderStyle;
}
bottomLeftCell.CellStyle = bottomLeftStyle;
// bottom right corner
ICell bottomRightCell = GetCell(sheet, lastRow, lastColumn);
ICellStyle bottomRightStyle = CreateCellStyle(bottomRightCell);
using (new CellBorderLock(bottomRightStyle))
{
bottomRightStyle.BorderBottom = borderStyle;
bottomRightStyle.BorderRight = borderStyle;
}
bottomRightCell.CellStyle = bottomRightStyle;
}
private ICellStyle CreateCellStyle(ICell cell)
{
var style = cell.Sheet.Workbook.CreateCellStyle();
style.CloneStyleFrom(cell.CellStyle);
return style;
}
private ICell GetCell(ISheet sheet, int row, int column)
{
IRow r = sheet.GetRow(row) ?? sheet.CreateRow(row);
return r.GetCell(column) ?? r.CreateCell(column);
}
/// <summary>
/// Make a border style of <see cref="ICellStyle"/> unique for duration of a lock
/// so it doesn't propagate changes of border to other cell styles.
/// </summary>
public sealed class CellBorderLock : IDisposable
{
private readonly ICellStyle style;
public CellBorderLock(ICellStyle style)
{
this.style = style;
style.BorderDiagonalLineStyle = BorderStyle.Thin;
style.BorderDiagonal = BorderDiagonal.Forward;
}
public void Dispose()
{
style.BorderDiagonalLineStyle = BorderStyle.None;
style.BorderDiagonal = BorderDiagonal.None;
}
}
code
est CC0.