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 關注
- 1290 瀏覽
添加回答
舉報