[英]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.