【问题标题】:Poi Excel Unable to apply background ColorPoi Excel 无法应用背景颜色
【发布时间】:2018-11-22 00:10:55
【问题描述】:

我正在使用POI 生成excel。我有一个场景就像我想将背景应用于表格中的单元格行。但我无法做到。我已经给出了下面的代码。请告诉我的代码中的错误是什么,或者我正在做的代码是正确的..

代码

    System.out.println("Called");
    try {

        XSSFWorkbook xb = new XSSFWorkbook();
        XSSFSheet sheet = xb.createSheet(sheetName);
        XSSFCellStyle cellStyle = xb.createCellStyle();
        XSSFFont font = xb.createFont();
        int rowIdx = 10;
        short cellIdx = 1;

                        CommonUtils.getGlobalVariable("GLOBAL.M_COMP_CODE"));
        int rowNo = 0;
        // Row 1
        XSSFCellStyle boldCellStyle = xb.createCellStyle();
        XSSFFont boldFont = xb.createFont();
        XSSFRow row1 = sheet.createRow(rowNo);
        XSSFCell cell1 = row1.createCell(1);
        boldFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        boldFont.setColor(new XSSFColor(Color.RED));
        boldCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        boldCellStyle.setFont(boldFont);
        cell1.setCellValue(headingName);
        sheet.addMergedRegion(CellRangeAddress.valueOf("B1:I1"));
        cell1.setCellStyle(boldCellStyle);
        XSSFCellStyle cellStyleAmount = xb.createCellStyle();
        cellStyleAmount.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
        rowNo++;
        // Row 2
        if ((String) addrDetails.get("COMP_NAME") != null) {
            XSSFRow row2 = sheet.createRow(rowNo);
            XSSFCell compNamecell = row2.createCell(1);
            compNamecell.setCellStyle(cellStyle);
            compNamecell.setCellValue("Company");
            sheet.addMergedRegion(CellRangeAddress.valueOf("B2:B2"));
            compNamecell = row2.createCell(2);
            compNamecell
                    .setCellValue((String) addrDetails.get("COMP_NAME"));
            sheet.addMergedRegion(CellRangeAddress.valueOf("C2:G2"));
            compNamecell.setCellStyle(cellStyle);
        }
        rowNo++;
        // Row 3
        if ((String) addrDetails.get("ADDR_NAME") != null) {
            XSSFRow row3 = sheet.createRow(rowNo);
            XSSFCell cell = row3.createCell(1);
            cell = row3.createCell(1);
            cell.setCellStyle(cellStyle);
            cell.setCellValue("Address ");
            sheet.addMergedRegion(CellRangeAddress.valueOf("B3:B3"));
            cell = row3.createCell(2);
            cell.setCellValue((String) addrDetails.get("ADDR_NAME"));
            sheet.addMergedRegion(CellRangeAddress.valueOf("C3:G3"));
            cell.setCellStyle(cellStyle);
        }
        rowNo++;
        if (addrDetails.get("COMP_ADD_1") != null) {
            XSSFRow row4 = sheet.createRow(rowNo);
            XSSFCell cell = row4.createCell(1);
            cell = row4.createCell(1);
            cell.setCellStyle(cellStyle);
            cell.setCellValue("");
            sheet.addMergedRegion(CellRangeAddress.valueOf("B4:B4"));
            cell = row4.createCell(2);
            cell.setCellValue((String) addrDetails.get("COMP_ADD_1"));
            sheet.addMergedRegion(CellRangeAddress.valueOf("C4:G4"));
            cell.setCellStyle(cellStyle);
        }
        rowNo++;
        if (addrDetails.get("COMP_ADD_2") != null) {
            XSSFRow row5 = sheet.createRow(rowNo);
            XSSFCell cell = row5.createCell(1);
            cell = row5.createCell(1);
            cell.setCellStyle(cellStyle);
            cell.setCellValue("");
            sheet.addMergedRegion(CellRangeAddress.valueOf("B5:B5"));
            cell = row5.createCell(2);
            cell.setCellValue((String) addrDetails.get("COMP_ADD_2"));
            sheet.addMergedRegion(CellRangeAddress.valueOf("C5:G5"));
            cell.setCellStyle(cellStyle);
        }
        rowNo++;
        if (addrDetails.get("COMP_ADD_3") != null) {
            XSSFRow row6 = sheet.createRow(rowNo);
            XSSFCell cell = row6.createCell(1);
            cell = row6.createCell(1);
            cell.setCellStyle(cellStyle);
            cell.setCellValue("");
            sheet.addMergedRegion(CellRangeAddress.valueOf("B6:B6"));
            cell = row6.createCell(2);
            cell.setCellValue((String) addrDetails.get("COMP_ADD_3"));
            sheet.addMergedRegion(CellRangeAddress.valueOf("C6:G6"));
            cell.setCellStyle(cellStyle);
        }
        rowNo++;
        if (addrDetails.get("CONTACT") != null) {
            XSSFRow row7 = sheet.createRow(rowNo);
            XSSFCell cell = row7.createCell(1);
            cell = row7.createCell(1);
            cell.setCellStyle(cellStyle);
            cell.setCellValue("Contact");
            sheet.addMergedRegion(CellRangeAddress.valueOf("B7:B7"));
            cell = row7.createCell(2);
            cell.setCellValue((String) addrDetails.get("CONTACT"));
            sheet.addMergedRegion(CellRangeAddress.valueOf("C7:G7"));
            cell.setCellStyle(cellStyle);
        }
        rowNo++;

        String pattern = "###,###.##";
        DecimalFormat decimalFormat = new DecimalFormat(pattern);
        // Row 8
        XSSFRow row8 = sheet.createRow(rowNo);
        XSSFCell cell = row8.createCell(1);
        cell = row8.createCell(0);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("Document No :");
        cell = row8.createCell(1);
        cell.setCellValue(fhdocNo);
        cell.setCellStyle(cellStyle);
        cell = row8.createCell(2);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("From Date :");
        cell = row8.createCell(3);
        cell.setCellValue(fromDate);
        cell.setCellStyle(cellStyle);
        cell = row8.createCell(4);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("To Date :");
        cell = row8.createCell(5);
        cell.setCellValue(toDate);
        cell.setCellStyle(cellStyle);
        cell = row8.createCell(6);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("Period Type :");
        cell = row8.createCell(7);
        cell.setCellValue(period);
        cell.setCellStyle(cellStyle);
        rowNo++;
        // Table Header
        XSSFRow row9 = sheet.createRow(rowNo);
        XSSFCell cell9 = row9.createCell(1);
        XSSFCellStyle headerCellStyleAmount = xb.createCellStyle();
        headerCellStyleAmount.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        XSSFFont minusIndicatorfont = xb.createFont();
        minusIndicatorfont.setColor(new XSSFColor(Color.red));
        headerCellStyleAmount.setFont(minusIndicatorfont);
        XSSFRow xssfHeader = sheet.createRow(rowIdx);
        XSSFColor color1 = new XSSFColor(Color.gray);
        XSSFCellStyle headCellStyle;
        headCellStyle = (XSSFCellStyle) cell9.getCellStyle();
        headCellStyle.setFillBackgroundColor(new XSSFColor(new Color(216,
                216, 216)));
        cell9 = xssfHeader.createCell(1);
        cell9.setCellStyle(cellStyle);
        cell9.setCellValue("");
        for (int i = 0; i < listSize; i++) {
            XSSFCell xssfCell = xssfHeader.createCell(cellIdx++);
            xssfCell.setCellValue(monthList.get(i).getSD_LABEL());
            headCellStyle.setFillForegroundColor(color1);
            headCellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
            xssfCell.setCellStyle(headCellStyle);
        }
        rowIdx++;
        xb.setSheetName(0, sheetName);
        /*
         * HttpServletResponse response = (HttpServletResponse)
         * externalContext .getResponse(); OutputStream os =
         * response.getOutputStream(); wb.write(os);
         * response.setHeader("Content-Disposition",
         * "attachment; filename=\"" + fileName + ".xls");
         * response.getOutputStream().flush();
         * response.getOutputStream().close();
         * FacesContext.getCurrentInstance().responseComplete();
         */
        ExternalContext externalContext = FacesContext.getCurrentInstance()
                .getExternalContext();
        writeExcelToResponse(externalContext, xb, fileName);
    } catch (Exception e) {
        e.printStackTrace();
    }

【问题讨论】:

    标签: java excel apache-poi xssf


    【解决方案1】:

    如果你使用这个 sn-p 它将起作用:

    XSSFCellStyle style = workbook.createCellStyle();
    XSSFColor myColor = new XSSFColor(Color.RED);
    style.setFillForegroundColor(myColor);
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    

    我可以给你这个link,其中描述了如何使用 XSSF。 主要变化是从 Workbook 中创建一个新的XSSFCellStyle 并设置填充模式。

    【讨论】:

    • 谢谢它的工作..我可以给我的自定义颜色...十六进制代码
    • 使用new Color(0xFF0000); 而不是Color.Red
    • 上面的代码适用于每一行...我应该如何将它应用到特定的行
    • 将样式设置为特定的单元格。
    • 我已经更新了完整的代码..如果我错了请告诉我
    【解决方案2】:

    Apache POI 4.0.0 答案。如何设置 Excel 单元格的背景颜色:

    //create workbook/sheet/row/cell
    
    IndexedColorMap colorMap = outputWorkbook.getStylesSource().getIndexedColors();
    java.awt.Color pigeonBlue = new java.awt.Color(189, 215, 238);
    XSSFColor myBackgroundColor = new XSSFColor(pigeonBlue, colorMap);
    XSSFCellStyle myCellStyle = outputWorkbook.createCellStyle();
    myCellStyle.setFillForegroundColor(myBackgroundColor);
    myCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    outputCell.setCellStyle(myCellStyle);
    outputCell.setCellValue("Hi There");
    
    // write & close workbook
    

    您的单元格应如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2013-03-14
      • 2016-10-04
      相关资源
      最近更新 更多