我不明白为什么会发生这种情况,首先我尝试在第一行的列标题中应用粗体文本,然后我想将标题单元格的边框设置为 MEDIUM,但是这种 MEDIUM 边框样式适用于中的所有单元格工作表。下面的相同代码中还有更多问题:
- 我的列标题(第一行)中的文本不是我想要的粗体。
- 我的列标题中的文本颜色不是我想要的红色。
这是我的代码(使用 NPOI 库处理):
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();
}
请为我修复它,我对 NPOI 很陌生,刚刚尝试使用它。您的帮助将不胜感激。谢谢。(<---我不知道为什么这个“谢谢”不能跳到下一行,即使我在输入之前输入了 Enter)