文章目录
1、设置单元格格式:来源_formats
更多数据类型从formats里面发现
java
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", "@"};
java
/**
* 设置单元格数据类型
*/
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 数据格式不能完全支持的时候,如下图情况:
可能会使用自定义的数据格式来在单元格展示数据。如下图,部分自定义数据格式示例:
设置单元格格式源码分析:
下面开始源码部分
BuiltinFormats.getBuiltinFormat(format)
执行如下图:
返回-1时会进行自定义数据格式设置:
stylesSource.putNumberFormat(format)
执行如下图:
2.2、自定义单元格格式,代码示例
更多自定义数据格式在代码示例。
java
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();
}
示例运行结果:
参考链接:
Java 中使用POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写、单元格边框等_Julian.zhou的博客-CSDN博客