I've got the following code to add borders to a row:
Range _range;
_range = customerWorksheet.Cells.CreateRange("A1", "P1");
_range.SetOutlineBorders(CellBorderType.Hair, Color.Black);
...but it's not working - no borders appear on that top row (row "1"):
Why not, and how can I add these borders, as they are done for the rest of the sheet?
Here's a snippet of code for that top row, showing how one cell is added:
Cell PAItemCell = customerWorksheet.Cells[rowToPopulate, PAITEMCODE_COL];
PAItemCell.PutValue(frbdbc.PAItemCode, true);
var paiStyle = PAItemCell.GetStyle();
paiStyle.Font.Name = fontForSheets;
paiStyle.IsTextWrapped = false;
PAItemCell.SetStyle(paiStyle);
And here's how the borders are added to the data portion of the sheet (theoretically, it should work for the top row, too, without the above attempt even being necessary):
private void BorderizeDataPortionOfCustomerSheet()
{
int rowsUsed = customerWorksheet.Cells.Rows.Count;
int colsUsed = SHIPVARIANCE_COL;
string bottomRightRange = string.Format("P{0}", rowsUsed);
var range = customerWorksheet.Cells.CreateRange("A1", 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 = customerWorksheet.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);
customerWorksheet.FreezePanes(FIRST_DATA_ROW, SHORTNAME_COL, rowsUsed, colsUsed);
}
It looks like you are mixing the things. Well, if you are applying outline border to a range "A1:P1" in your code at the first place, then if you are again applying style/formattings to the cells or specifying outline borders for the range (including the first row as well) again, it would surely override the existing formatting you applied previously. So, kindly make sure you have written your code fine and both code segments don't interfere each others' formattings.
I am working as Support developer/ Evangelist at Aspose.