为什么我的范围的一部分无法添加边框 (Aspose Cells)?
Why is a subsection of my range failing to add borders (Aspose Cells)?
基于 here 中的代码,我添加了以下内容:
string bottomRightRange = string.Format("F{0}", rowsUsed);
var range = locationWorksheet.Cells.CreateRange("A8", bottomRightRange);
//Setting border for each cell in the range
var style = workBook.CreateStyle();
style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
for (int r = range.FirstRow; r < range.RowCount; r++)
{
for (int c = range.FirstColumn; c < range.ColumnCount; c++)
{
Cell cell = locationWorksheet.Cells[r, c];
cell.SetStyle(style, new StyleFlag()
{
TopBorder = true,
BottomBorder = true,
LeftBorder = true,
RightBorder = true
});
}
}
//Setting outline border to range
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
大部分情况下效果很好,但请注意:
末尾的最后七行(除了最后一行的底部边框和所有这些的右边框)没有边框。为什么不呢?
注意:在这种情况下,bottomRightRange 等同于 "F94"。
为什么一大块可以正常工作,最后一小部分却不能正常工作?
问题是(从上面的代码来看并不明显)sheet 有一些初步行用作 header 信息。循环时必须考虑这些。所以这个:
for (int r = range.FirstRow; r < range.RowCount; r++)
...需要变成这样:
for (int r = range.FirstRow; r < range.RowCount + FUDGE_FACTOR; r++`)
FUDGE_FACTOR 是 "real" 数据开始前使用的行数。
基于 here 中的代码,我添加了以下内容:
string bottomRightRange = string.Format("F{0}", rowsUsed);
var range = locationWorksheet.Cells.CreateRange("A8", bottomRightRange);
//Setting border for each cell in the range
var style = workBook.CreateStyle();
style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
for (int r = range.FirstRow; r < range.RowCount; r++)
{
for (int c = range.FirstColumn; c < range.ColumnCount; c++)
{
Cell cell = locationWorksheet.Cells[r, c];
cell.SetStyle(style, new StyleFlag()
{
TopBorder = true,
BottomBorder = true,
LeftBorder = true,
RightBorder = true
});
}
}
//Setting outline border to range
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
大部分情况下效果很好,但请注意:
末尾的最后七行(除了最后一行的底部边框和所有这些的右边框)没有边框。为什么不呢?
注意:在这种情况下,bottomRightRange 等同于 "F94"。
为什么一大块可以正常工作,最后一小部分却不能正常工作?
问题是(从上面的代码来看并不明显)sheet 有一些初步行用作 header 信息。循环时必须考虑这些。所以这个:
for (int r = range.FirstRow; r < range.RowCount; r++)
...需要变成这样:
for (int r = range.FirstRow; r < range.RowCount + FUDGE_FACTOR; r++`)
FUDGE_FACTOR 是 "real" 数据开始前使用的行数。