当前位置:   article > 正文

Java POI excel设置单元格格式,自定义设置_poi向excel中写入数据设置格式

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", "@"};
  • 1
    /**
     * 设置单元格数据类型
     */
    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();
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

注意可能会出现不兼容的问题,但是不影响导出
QQ截图20230728152405.png

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

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

formats数据格式不能完全支持的时候,如下图情况:
image.png
可能会使用自定义的数据格式来在单元格展示数据。如下图,部分自定义数据格式示例:
image.png
image.png
设置单元格格式源码分析:
image.png
下面开始源码部分
image.png
BuiltinFormats.getBuiltinFormat(format)执行如下图:
image.png
返回-1时会进行自定义数据格式设置:
image.png
stylesSource.putNumberFormat(format)执行如下图:
image.png

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

示例运行结果:
image.png

参考链接:

Java 中使用POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写、单元格边框等_Julian.zhou的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/906621
推荐阅读
相关标签
  

闽ICP备14008679号