2 回答
TA贡献2016条经验 获得超9个赞
请尝试如下方法。我已经分离了字体、样式,最后使用了你的单元格样式的 for 循环分配也修复了 for 循环内部的错误,你每次只将样式分配给 Cells[0]。
HSSFFont headerFont = (HSSFFont)workbook.CreateFont();
headerFont.FontHeightInPoints = (short)12;
headerFont.FontName = "Arial";
headerFont.Color = IndexedColors.White.Index;
headerFont.IsBold = true;
headerFont.IsItalic = false;
headerFont.Boldweight = 700;
HSSFCellStyle headerStyle = (HSSFCellStyle)workbook.CreateCellStyle();
headerStyle.WrapText = true;
headerStyle.FillForegroundColor = IndexedColors.LightBlue.Index;
headerStyle.FillPattern = FillPattern.SolidForeground;
headerStyle.Alignment = HorizontalAlignment.Center;
headerStyle.VerticalAlignment = VerticalAlignment.Center;
headerStyle.BorderBottom = BorderStyle.Thin;
headerStyle.BorderTop = BorderStyle.Thin;
headerStyle.BorderLeft = BorderStyle.Thin;
headerStyle.BorderRight = BorderStyle.Thin;
headerStyle.SetFont(headerFont);
for (int c = 0; c < headerRow.Cells.Count; c++)
{
headerRow.Cells[c].CellStyle = headerStyle;
}
TA贡献1820条经验 获得超10个赞
替换这些行:
for (int c = 0; c < headerRow.Cells.Count; c++)
{
headerRow.Cells[0].CellStyle.FillForegroundColor= IndexedColors.LightBlue.Index;
headerRow.Cells[0].CellStyle.FillPattern = FillPattern.SolidForeground;
}
和:
HSSFCellStyle cellStyleBlue = (HSSFCellStyle)workbook.CreateCellStyle();
cellStyleBlue.FillForegroundColor = IndexedColors.LightBlue.Index;
cellStyleBlue.FillPattern = FillPattern.SolidForeground;
for (int c = 0; c < headerRow.Cells.Count; c++)
{
headerRow.Cells[c].CellStyle = cellStyleBlue;
}
然后,只有第一行的单元格才会应用单元格样式。
- 2 回答
- 0 关注
- 1199 浏览
添加回答
举报