C# NPOI rowStyle

7.2k Views Asked by At

Ii have applied NPOI to generate the excel. So far so good except that I want to bold a row.

I have tried:

tmpRow.RowStyle = workbook.CreateCellStyle();
tmpRow.RowStyle.SetFont(boldFont);

However, nothing has changed.

While I can do it by setting it one by one:

ICellStyle boldFontCellStyle = workbook.CreateCellStyle();
IFont boldFont = workbook.CreateFont();
boldFont.IsBold = true;
boldFontCellStyle.SetFont(boldFont);
for (int p= 0; p <= 12; p++)
{
    tmpRow.GetCell(p).CellStyle = boldFontCellStyle;
}
//tmpRow.RowStyle = workbook.CreateCellStyle();
//tmpRow.RowStyle.SetFont(boldFont);

I would like to know if there is any way to set an entire row style in NPOI?

Thanks.

2

There are 2 best solutions below

0
On

Generate style, set it and then apply it to row. Something like this:

var rowStyle = workbook.CreateCellStyle();
rowStyle.SetFont(boldFont);

tmpRow.RowStyle = rowstyle;
3
On

Try this for Row styling

XSSFFont defaultFont = (XSSFFont)workbook.CreateFont();
defaultFont.FontHeightInPoints = (short)10;
defaultFont.FontName = "Arial";
defaultFont.Color = IndexedColors.Black.Index;
defaultFont.IsBold = true;

XSSFCellStyle yourCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
yourCellStyle.SetFont(defaultFont);

var row = sheet.CreateRow(0);
row.RowStyle = yourCellStyle;

Below one was for cell style

 XSSFFont yourFont = (XSSFFont)workbook.CreateFont();
    yourFont.FontHeightInPoints = (short)12;
    yourFont.FontName = "Arial";               
    yourFont.IsBold = true;
    yourFont.IsItalic = false;
    yourFont.Boldweight = 700;
    XSSFCellStyle yourStyle = (XSSFCellStyle)workbook.CreateCellStyle();
    yourStyle.SetFont(yourFont);
    for (int p= 0; p <= 12; p++)
      { 
        row.Cells[p].CellStyle = yourStyle;
      }