如何在Apple POI中将负货币值设置为红色字体

时间:2017-03-19 00:53:32

标签: java apache-poi

如果小于0,为了使总借记栏中的负值变为红色,我该怎么做。

enter image description here

            HSSFRow myRow = null;
        final String[] tableHeader = { "Rank", "Names",
                "ID", "Industry", "LE", "# of Items Processed", "Avg Size",
                "Gross Credit $", "Gross Debit $", "Total Throughput",
                "Net Change" };

        try {
            Row header = mySheet.createRow(rowNum);
            for (int i = 0; i < tableHeader.length; i++) {
                Cell cellHeader = header.createCell(i);
                cellHeader.setCellValue(tableHeader[i]);
                cellHeader.setCellStyle(style);
            }
            for (Customer cust : customerRollList) {
                myRow = mySheet.createRow(++rowNum);
                myRow.createCell(0).setCellValue(cust.getRank());
                myRow.createCell(1).setCellValue(
                        cust.getCustomerRelationship());

                if (cust.getWcisId() != 0) {
                    myRow.createCell(2).setCellValue(cust.getWcisId());
                }

                myRow.createCell(3).setCellValue(cust.getLineOfBusiness());
                myRow.createCell(4).setCellValue(cust.getLegalEntity());

                if (cust.getNoOfItemsProcessed() != 0) {
                    myRow.createCell(5).setCellValue(
                            cust.getNoOfItemsProcessed());
                }

                if (cust.getAvgSize() != 0) {
                    myRow.createCell(6).setCellValue(cust.getAvgSize());
                }

                if (cust.getGrossCredit() != 0) {
                    myRow.createCell(7).setCellValue(cust.getGrossCredit());
                }

                if (cust.getGrossDebit() != 0) {
                    myRow.createCell(8).setCellValue(cust.getGrossDebit());
                    //myRow.createCell(8).setCellStyle(style01);
                }

                if (cust.getTotalThroughput() != 0) {
                    myRow.createCell(9).setCellValue(
                            cust.getTotalThroughput());
                }

                if (cust.getNetChange() != 0) {
                    myRow.createCell(10).setCellValue(cust.getNetChange());
                }
            }
            mySheet.autoSizeColumn(1);
            mySheet.autoSizeColumn(2);
            mySheet.autoSizeColumn(3);
            mySheet.autoSizeColumn(4);
            mySheet.autoSizeColumn(5);
            mySheet.autoSizeColumn(6);
            mySheet.autoSizeColumn(7);
            mySheet.autoSizeColumn(8);
            mySheet.autoSizeColumn(9);
            mySheet.autoSizeColumn(10);

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ByteArrayOutputStream boas = new ByteArrayOutputStream();
            myWorkBook.write(boas);
            setInputStream(new ByteArrayInputStream(boas.toByteArray()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:4)

您必须创建样式并将此数字格式应用于单元格。 &#34; 0.00; [红色] 0.00&#34; 如果你想要小数位或&#34; 0; [红色] 0&#34; 为整数:

CellStyle style = workbook.createCellStyle();
DataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat("0.00;[Red]0.00"));
cell.setCellStyle(style);