簡體   English   中英

Apache Excel 中的 POI 顏色單元使用 Java

[英]Apache POI color cells in Excel using Java

我正在嘗試為我的 Excel 的標題行提供背景顏色,但以下代碼不起作用。 有人可以幫忙嗎。

編輯 1 下面是完整的代碼供參考。 標題文本會覆蓋背景顏色,即使我將顏色設置為 LIGHT_BLUE,Excel 的背景也是黑色。 在此處輸入圖像描述

XSSFWorkbook 工作簿 = new XSSFWorkbook();

    XSSFCellStyle style = workbook.createCellStyle();
    style.setFillBackgroundColor(IndexedColors.LIGHT_BLUE.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    Font font = workbook.createFont();
    font.setColor(IndexedColors.WHITE.getIndex());
    style.setFont(font);

    XSSFSheet spreadsheet = workbook.createSheet("WIP E-MCAD Development");
    Row row = spreadsheet.createRow(0);
    Cell cell = row.createCell(1);
    cell.setCellValue("Javatpoint");
    cell.setCellStyle(style);
    //style=row.getRowStyle();

    Row rowhead = spreadsheet.createRow(1); //header

    Font headerFont = workbook.createFont();
    ((XSSFFont) headerFont).setBold(true);

    rowhead.setRowStyle(style);

    rowhead.createCell(0).setCellValue("Wire");
    rowhead.createCell(1).setCellValue("Device From");
    rowhead.createCell(2).setCellValue("Pin From");
    rowhead.createCell(3).setCellValue("Device To");
    rowhead.createCell(4).setCellValue("Pin To");
    rowhead.createCell(5).setCellValue("Wire Level");
    rowhead.createCell(6).setCellValue("HRN Level Alias");
    rowhead.createCell(7).setCellValue("Option Code");
    rowhead.createCell(8).setCellValue("Feature Description");
    rowhead.createCell(9).setCellValue("Option Code Status Message");
    rowhead.createCell(10).setCellValue("CAN LIN Status");



    for (int i = 1; i < list.size(); i++) {
        SoniResponse data = list.get(i);

        row = spreadsheet.createRow(i + 1);

        row.createCell(0).setCellValue(data.getWire());
        row.createCell(1).setCellValue(data.getDevice_From());
        row.createCell(2).setCellValue(data.getPin_From());
        row.createCell(3).setCellValue(data.getDevice_To());
        row.createCell(4).setCellValue(data.getPin_To());
        row.createCell(5).setCellValue(data.getWire_Level());
        row.createCell(6).setCellValue(data.getHrn_Level_Alias());
        row.createCell(7).setCellValue(data.getOption_Code());
        row.createCell(8).setCellValue(data.getFeature_Description());
        row.createCell(9).setCellValue(data.getOption_Code_Status_Message());
        row.createCell(10).setCellValue(data.getCAN_LIN_Status());

    }

您首先需要在樣式上設置填充圖案,如下所示:

XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

請注意原始代碼中的XSSFCellStyleCellStyle :只有XSSFCellStyle允許使用FillPatternType枚舉設置樣式。

然后你還需要實際設置 header 行的樣式:

rowhead.setRowStyle(style)

https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFRow.html#setRowStyle-org.apache.poi.ss.usermodel.CellStyle-

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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