您好,欢迎来到筏尚旅游网。
搜索
您的当前位置:首页Java POI excel设置单元格格式,自定义设置

Java POI excel设置单元格格式,自定义设置

来源:筏尚旅游网

1、设置单元格格式:来源_formats

更多数据类型从formats里面发现

private static final String[] _formats = new String[]{"General", "0", "0.00", "#,##0", "#,##0.00", "\"$\"#,##0_);(\"$\"#,##0)", "\"$\"#,##0_);[Red](\"$\"#,##0)", "\"$\"#,##0.00_);(\"$\"#,##0.00)", "\"$\"#,##0.00_);[Red](\"$\"#,##0.00)", "0%", "0.00%", "0.00E+00", "# ?/?", "# ??/??", "m/d/yy", "d-mmm-yy", "d-mmm", "mmm-yy", "h:mm AM/PM", "h:mm:ss AM/PM", "h:mm", "h:mm:ss", "m/d/yy h:mm", "reserved-0x17", "reserved-0x18", "reserved-0x19", "reserved-0x1A", "reserved-0x1B", "reserved-0x1C", "reserved-0x1D", "reserved-0x1E", "reserved-0x1F", "reserved-0x20", "reserved-0x21", "reserved-0x22", "reserved-0x23", "reserved-0x24", "#,##0_);(#,##0)", "#,##0_);[Red](#,##0)", "#,##0.00_);(#,##0.00)", "#,##0.00_);[Red](#,##0.00)", "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)", "_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)", "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)", "_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* \"-\"??_);_(@_)", "mm:ss", "[h]:mm:ss", "mm:ss.0", "##0.0E+0", "@"};
    /**
     * 设置单元格数据类型
     */
    public static void setExcelCellDataFormat() throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("单元格数据类型");

        XSSFRow row1 = sheet.createRow(0);
        // 单元格样式
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        // 单元格数据格式
        XSSFDataFormat cellDataFormat = workbook.createDataFormat();
        // 来源:org.apache.poi.ss.usermodel.BuiltinFormats
        // private static final String[] _formats = new String[]{"General", "0", "0.00", "#,##0", "#,##0.00", "\"$\"#,##0_);(\"$\"#,##0)", "\"$\"#,##0_);[Red](\"$\"#,##0)", "\"$\"#,##0.00_);(\"$\"#,##0.00)", "\"$\"#,##0.00_);[Red](\"$\"#,##0.00)", "0%", "0.00%", "0.00E+00", "# ?/?", "# ??/??", "m/d/yy", "d-mmm-yy", "d-mmm", "mmm-yy", "h:mm AM/PM", "h:mm:ss AM/PM", "h:mm", "h:mm:ss", "m/d/yy h:mm", "reserved-0x17", "reserved-0x18", "reserved-0x19", "reserved-0x1A", "reserved-0x1B", "reserved-0x1C", "reserved-0x1D", "reserved-0x1E", "reserved-0x1F", "reserved-0x20", "reserved-0x21", "reserved-0x22", "reserved-0x23", "reserved-0x24", "#,##0_);(#,##0)", "#,##0_);[Red](#,##0)", "#,##0.00_);(#,##0.00)", "#,##0.00_);[Red](#,##0.00)", "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)", "_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)", "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)", "_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* \"-\"??_);_(@_)", "mm:ss", "[h]:mm:ss", "mm:ss.0", "##0.0E+0", "@"};

        // 单元格百分比:从_formats里面选择一个包含的单元格数据类型
        cellStyle.setDataFormat(cellDataFormat.getFormat("0.00%"));
        XSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyle);
        // 需要注意一下,poi已经不建议使用setCellType设置数据类型了,可以直接通过重载的setCellValue判断当前单元格是什么数据类型
        // cell1.setCellType(CellType.BOOLEAN);
        cell1.setCellValue(0.06);

        FileOutputStream outputStream = new FileOutputStream("D:\\temp\\Excel单元格数据类型.xlsx");
        workbook.write(outputStream);
        outputStream.flush();

        workbook.close();
        outputStream.close();
    }

注意可能会出现不兼容的问题,但是不影响导出

2、设置单元格格式:自定义格式

2.1、自定义格式分析&源码分析

formats数据格式不能完全支持的时候,如下图情况:

2.2、自定义单元格格式,代码示例

更多自定义数据格式在代码示例。

public static void setExcelCellDataFormat2() throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("单元格数据类型");

    XSSFRow row1 = sheet.createRow(0);
    // 单元格样式
    XSSFCellStyle cellStyle = workbook.createCellStyle();
    // 单元格数据格式
    XSSFDataFormat cellDataFormat = workbook.createDataFormat();
    // 人民币货币格式
    cellStyle.setDataFormat(cellDataFormat.getFormat("¥#,##0.00"));
    XSSFCell cell1 = row1.createCell(0);
    cell1.setCellStyle(cellStyle);
    cell1.setCellValue(0.06);
    sheet.setColumnWidth(0, 256 * 14 + 184);

    // 美元货币格式
    XSSFCellStyle cellStyle2 = workbook.createCellStyle();
    cellStyle2.setDataFormat(cellDataFormat.getFormat("$#,##0.00"));
    XSSFCell cell2 = row1.createCell(1);
    cell2.setCellStyle(cellStyle2);
    cell2.setCellValue(0.06);
    sheet.setColumnWidth(1, 256 * 14 + 184);

    // 添加文字描述的数据格式
    XSSFCellStyle cellStyle3 = workbook.createCellStyle();
    cellStyle3.setDataFormat(cellDataFormat.getFormat("占比#,##0.00%"));
    XSSFCell cell3 = row1.createCell(2);
    cell3.setCellStyle(cellStyle3);
    cell3.setCellValue(0.06);
    sheet.setColumnWidth(2, 256 * 14 + 184);


    // 带颜色的数据格式化,正数为绿色,负数为红色
    XSSFCellStyle cellStyle4 = workbook.createCellStyle();
    cellStyle4.setDataFormat(cellDataFormat.getFormat("提升[绿色]#,##0.00%;[红色]下降#,##0.00%"));
    XSSFCell cell4 = row1.createCell(3);
    cell4.setCellStyle(cellStyle4);
    cell4.setCellValue(-0.06);
    sheet.setColumnWidth(3, 256 * 14 + 184);

    // 带颜色的数据格式化,正数为绿色,负数为红色
    XSSFCellStyle cellStyle5 = workbook.createCellStyle();
    cellStyle5.setDataFormat(cellDataFormat.getFormat("提升[绿色]#,##0.00%;[红色]下降#,##0.00%"));
    XSSFCell cell5 = row1.createCell(4);
    cell5.setCellStyle(cellStyle5);
    cell5.setCellValue(0.06);
    sheet.setColumnWidth(4, 256 * 14 + 184);


    FileOutputStream outputStream = new FileOutputStream("D:\\temp\\Excel单元格数据类型2.xlsx");
    workbook.write(outputStream);
    outputStream.flush();

    workbook.close();
    outputStream.close();
}

示例运行结果:

参考链接:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务