詳解Java?POI?excel自定義設(shè)置單元格格式
1、設(shè)置單元格格式:來源_formats
更多數(shù)據(jù)類型從formats里面發(fā)現(xiàn)
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", "@"};
注意可能會出現(xiàn)不兼容的問題,但是不影響導(dǎo)出
2、設(shè)置單元格格式:自定義格式
2.1、自定義格式分析&源碼分析
在formats數(shù)據(jù)格式不能完全支持的時候,如下圖情況:
可能會使用自定義的數(shù)據(jù)格式來在單元格展示數(shù)據(jù)。如下圖,部分自定義數(shù)據(jù)格式示例:
設(shè)置單元格格式源碼分析:
下面開始源碼部分
BuiltinFormats.getBuiltinFormat(format)
執(zhí)行如下圖:
返回-1時會進行自定義數(shù)據(jù)格式設(shè)置:
stylesSource.putNumberFormat(format)
執(zhí)行如下圖:
2.2、自定義單元格格式,代碼示例
更多自定義數(shù)據(jù)格式在代碼示例。
public static void setExcelCellDataFormat2() throws IOException { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("單元格數(shù)據(jù)類型"); XSSFRow row1 = sheet.createRow(0); // 單元格樣式 XSSFCellStyle cellStyle = workbook.createCellStyle(); // 單元格數(shù)據(jù)格式 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); // 添加文字描述的數(shù)據(jù)格式 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); // 帶顏色的數(shù)據(jù)格式化,正數(shù)為綠色,負數(shù)為紅色 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); // 帶顏色的數(shù)據(jù)格式化,正數(shù)為綠色,負數(shù)為紅色 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單元格數(shù)據(jù)類型2.xlsx"); workbook.write(outputStream); outputStream.flush(); workbook.close(); outputStream.close(); }
示例運行結(jié)果:
參考鏈接:
java poi導(dǎo)入純數(shù)字等格式問題及解決
Java 中使用POI設(shè)置EXCEL單元格格式為文本、小數(shù)、百分比、貨幣、日期、科學(xué)計數(shù)法和中文大寫、單元格邊框等
到此這篇關(guān)于Java POI excel設(shè)置單元格格式,自定義設(shè)置的文章就介紹到這了,更多相關(guān)Java POI excel單元格格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring與Struts整合之讓Spring管理控制器操作示例
這篇文章主要介紹了Spring與Struts整合之讓Spring管理控制器操作,結(jié)合實例形式詳細分析了Spring管理控制器相關(guān)配置、接口實現(xiàn)與使用技巧,需要的朋友可以參考下2020-01-01

Java web基礎(chǔ)學(xué)習(xí)之開發(fā)環(huán)境篇(詳解)

Java中String字符串轉(zhuǎn)具體對象的幾種常用方式

es(elasticsearch)整合SpringCloud(SpringBoot)搭建教程詳解