I don't understand why this can happen, first I tried applying a bold text to my column headers in the first row, then I want to set my header cells' borders to MEDIUM, but this MEDIUM border style is applied to all cells in the sheet. There are more problems in the same code below:
- The text in my column headers (in the first row) is not bold as I want.
- The text color in my column headers is not red as I want.
Here is my code (handling with NPOI library):
private void CreateATest(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
HSSFWorkbook wb = new HSSFWorkbook();
ISheet sheet = wb.CreateSheet("NPOI");
IRow row = sheet.CreateRow(0);
row.RowStyle = wb.CreateCellStyle();
row.RowStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
row.RowStyle.VerticalAlignment = VerticalAlignment.CENTER;
row.RowStyle.WrapText = true;
IFont font = wb.CreateFont();
font.Boldweight = 3;
font.Color = (short) ColorTranslator.ToWin32(Color.Red);
font.FontHeight = 30;
row.RowStyle.SetFont(font);
int i = 0;
foreach (string header in new string[] { "ID", "Name", "Age" })
{
row.CreateCell(i++).SetCellValue(header);
row.Cells[i - 1].CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.MEDIUM;
row.Cells[i - 1].CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.MEDIUM;
row.Cells[i - 1].CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.MEDIUM;
}
row.Cells[i - 1].CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.MEDIUM;
Random rand = new Random();
for (i = 1; i < 1000; i++)
{
IRow row1 = sheet.CreateRow(i);
for (int j = 0; j < 3; j++)
{
row1.CreateCell(j).SetCellValue(rand.Next(100));
}
}
wb.Write(fs);
fs.Close();
}
Please fix it for me, I'm very new to NPOI, have just tried using it. Your help would be highly appreciated. Thanks. (<--- I don't know why this 'Thanks' can't jump to the next line even I typed Enter before typing it)