[英]Colored Header cells with java Apache POI
我想用不同的颜色显示标题行。 到现在为止,我为Font只能使用不同的颜色,因为背景颜色无法按我希望的那样工作。 我的输出文件应为xls格式。
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
...
String[] mylmcol = {"EmplNo", "LMSurname", "LMFirstname"};
String[] myusercol = {"UserID", "USRSurname", "USRFirstname"};
String[] rolecol = {"Group", "Role"};
String[] ackcol = {"ACKValue"};
XSSFWorkbook workbook_cbk_output = new XSSFWorkbook();
Sheet sheet_cbk_output = workbook_cbk_output.createSheet("UserList");
Font LMheaderFont = workbook_cbk_output.createFont();
LMheaderFont.setFontHeightInPoints((short) 12);
LMheaderFont.setColor(IndexedColors.SEA_GREEN.getIndex());
...
XSSFCellStyle headerCellStyleACK = workbook_cbk_output.createCellStyle();
headerCellStyleACK.setFont(ACKheaderFont);
//DOES NOT WORK:
headerCellStyleACK.setFillBackgroundColor(HSSFColor.AQUA.index);
headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Row headerRow_cbk = sheet_cbk_output.createRow(0);
for (int i = 0; i < mylmcol.length; i++) {
Cell cell = headerRow_cbk.createCell(i);
cell.setCellValue(mylmcol[i]);
cell.setCellStyle(headerCellStyleLM);
}
...
FileOutputStream OUTFILE = new FileOutputStream("Myoutput.xls");
workbook_cbk_output.write(OUTFILE);
OUTFILE.close();
我尝试了命令setFillBackgroundColor
但这是忽略的。 是否有另一种解决方案为背景着色?
您可以使用XSSFColor
代替HSSFColor
,如果使用XSSF工作表/ CellStyle。 对我来说,它适用于:
XSSFCellStyle headingStyle = workbook.createCellStyle();
headingStyle.setFillForegroundColor( new XSSFColor( new java.awt.Color( 207, 207, 207 ) ) );
headingStyle.setFillPattern( CellStyle.SOLID_FOREGROUND );
并将其应用于单元格/行:
Row rowHeadingStyle = styleSheet.createRow( 0 );
rowHeadingStyle.setRowStyle( headingStyle );
这会将背景色设置为所需的值!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.