如何一次更改几个单元格的边框样式。 NPOI

时间:2016-05-06 15:24:22

标签: c# excel npoi

我想这样做:Question

但是使用NPOI。有没有办法提供4""广场并将其边界改为我想要的任何东西?

1 个答案:

答案 0 :(得分:3)

不,除非你自己编程,否则没有。

此外,NPOI(2.1.3.1)中的边框样式是错误的,基本上当您的ICellStyle具有与其他ICellStyle相同的边框样式(例如,两个单元格具有顶部黑色边框)并且您更改其中一个时,更改会传播对于这两种样式(例如,一旦将左上边框添加到一个单元格,它也会添加到另一个单元格中。)

我已经创建了拉取请求,但现在应该可以使用(谨防64 000个单元格样式限制):

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;
    }
}

代码为CC0。