詳解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í)候,如下圖情況:

可能會使用自定義的數(shù)據(jù)格式來在單元格展示數(shù)據(jù)。如下圖,部分自定義數(shù)據(jù)格式示例:


設(shè)置單元格格式源碼分析:

下面開始源碼部分

BuiltinFormats.getBuiltinFormat(format)執(zhí)行如下圖:

返回-1時(shí)會進(jìn)行自定義數(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ù)為綠色,負(fù)數(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ù)為綠色,負(fù)數(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();
}示例運(yùn)行結(jié)果:

參考鏈接:
java poi導(dǎo)入純數(shù)字等格式問題及解決
Java 中使用POI設(shè)置EXCEL單元格格式為文本、小數(shù)、百分比、貨幣、日期、科學(xué)計(jì)數(shù)法和中文大寫、單元格邊框等
到此這篇關(guān)于Java POI excel設(shè)置單元格格式,自定義設(shè)置的文章就介紹到這了,更多相關(guān)Java POI excel單元格格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用Lambda表達(dá)式查找list集合中是否包含某值問題
Java使用Lambda表達(dá)式查找list集合中是否包含某值的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
如何保證RabbitMQ全鏈路數(shù)據(jù)100%不丟失問題
這篇文章主要介紹了如何保證RabbitMQ全鏈路數(shù)據(jù)100%不丟失問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Spring?Boot整合阿里開源中間件Canal實(shí)現(xiàn)數(shù)據(jù)增量同步
這篇文章主要為大家介紹了Spring?Boot整合阿里開源中間件Canal實(shí)現(xiàn)數(shù)據(jù)增量同步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
在SpringBoot3中spring.factories配置不起作用的原因和解決方法
本文給大家介紹了在SpringBoot3中spring.factories配置的自動裝配不生效的原因和解決方法,文中通過代碼和圖文給出了詳細(xì)的解決方法,具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02
一篇文章帶你搞懂Java線程池實(shí)現(xiàn)原理
線程池?zé)o論是工作還是面試都是必備的技能,但是很多人對于線程池的實(shí)現(xiàn)原理卻一知半解,并不了解線程池內(nèi)部的工作原理,今天就帶大家一塊剖析線程池底層實(shí)現(xiàn)原理2022-11-11
MyBatis查詢數(shù)據(jù),賦值給List集合時(shí),數(shù)據(jù)缺少的問題及解決
這篇文章主要介紹了MyBatis查詢數(shù)據(jù),賦值給List集合時(shí),數(shù)據(jù)缺少的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

