簡體   English   中英

帶有Java Apache POI的彩色標題單元格

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM