根据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 等同于&#34; F94&#34;在这种情况下。
为什么一大块工作正常,那么最后一小部分不能正常工作?
答案 0 :(得分:0)
问题是(上面的代码中不明显)该工作表有一些初步行用作标题信息。循环时必须考虑这些因素。所以这个:
for (int r = range.FirstRow; r < range.RowCount; r++)
......需要成为这个:
for (int r = range.FirstRow; r < range.RowCount + FUDGE_FACTOR; r++`)
FUDGE_FACTOR是&#34; real&#34;之前使用的行数。数据开始。