POI對(duì)Excel自定義日期格式的讀取(實(shí)例代碼)
用POI讀取Excel數(shù)據(jù):(版本號(hào):POI3.7)
1、讀取Excel
private List<String[]> rosolveFile(InputStream is, String suffix,
int startRow) throws IOException, FileNotFoundException {
Workbook xssfWorkbook = null;
if ("xls".equals(suffix)) {
xssfWorkbook = new HSSFWorkbook(is);
} else if ("xlsx".equals(suffix)) {
xssfWorkbook = new XSSFWorkbook(is);
}
Sheet xssfSheet = xssfWorkbook.getSheetAt(0);
if (xssfSheet == null) {
return null;
}
ArrayList<String[]> list = new ArrayList<String[]>();
int lastRowNum = xssfSheet.getLastRowNum();
for (int rowNum = startRow; rowNum <= lastRowNum; rowNum++) {
if (xssfSheet.getRow(rowNum) != null) {
Row xssfRow = xssfSheet.getRow(rowNum);
short firstCellNum = xssfRow.getFirstCellNum();
short lastCellNum = xssfRow.getLastCellNum();
if (firstCellNum != lastCellNum) {
String[] values = new String[lastCellNum];
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell xssfCell = xssfRow.getCell(cellNum);
if (xssfCell == null) {
values[cellNum] = "";
} else {
values[cellNum] = parseExcel(xssfCell);
}
}
list.add(values);
}
}
}
return list;
}
2、Excel數(shù)據(jù)處理:
Excel存儲(chǔ)日期、時(shí)間均以數(shù)值類型進(jìn)行存儲(chǔ),讀取時(shí)POI先判斷是是否是數(shù)值類型,再進(jìn)行判斷轉(zhuǎn)化
1、數(shù)值格式(CELL_TYPE_NUMERIC):
1.純數(shù)值格式:getNumericCellValue() 直接獲取數(shù)據(jù)
2.日期格式:處理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式
1).判斷是否是日期格式:HSSFDateUtil.isCellDateFormatted(cell)
2).判斷是日期或者時(shí)間
cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")
OR: cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")
3.自定義日期格式:處理yyyy年m月d日,h時(shí)mm分,yyyy年m月等含文字的日期格式
判斷cell.getCellStyle().getDataFormat()值,解析數(shù)值格式
yyyy年m月d日----->31
m月d日---->58
h時(shí)mm分--->32
2、字符格式(CELL_TYPE_STRING):直接獲取內(nèi)容
private String parseExcel(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:// 數(shù)字類型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 處理日期格式、時(shí)間格式
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58) {
// 處理自定義日期格式:m月d日(通過(guò)判斷單元格的格式id解決,id的值是58)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil
.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
CellStyle style = cell.getCellStyle();
DecimalFormat format = new DecimalFormat();
String temp = style.getDataFormatString();
// 單元格設(shè)置成常規(guī)
if (temp.equals("General")) {
format.applyPattern("#");
}
result = format.format(value);
}
break;
case HSSFCell.CELL_TYPE_STRING:// String類型
result = cell.getRichStringCellValue().toString();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result;
}
*萬(wàn)能處理方案:
所有日期格式都可以通過(guò)getDataFormat()值來(lái)判斷
yyyy-MM-dd----- 14
yyyy年m月d日--- 31
yyyy年m月------- 57
m月d日 ---------- 58
HH:mm----------- 20
h時(shí)mm分 ------- 32
//1、判斷是否是數(shù)值格式
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
short format = cell.getCellStyle().getDataFormat();
SimpleDateFormat sdf = null;
if(format == 14 || format == 31 || format == 57 || format == 58){
//日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}else if (format == 20 || format == 32) {
//時(shí)間
sdf = new SimpleDateFormat("HH:mm");
}
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
}
以上這篇POI對(duì)Excel自定義日期格式的讀取(實(shí)例代碼)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)word文檔批量轉(zhuǎn)成自定義格式的excel文檔的思路及實(shí)例代碼
- php 自定義函數(shù)實(shí)現(xiàn)將數(shù)據(jù) 以excel 表格形式導(dǎo)出示例
- Python實(shí)現(xiàn)自定義順序、排列寫入數(shù)據(jù)到Excel的方法
- asp.net DataTable導(dǎo)出Excel自定義列名的方法
- C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel
- C#自定義導(dǎo)出數(shù)據(jù)到Excel的類實(shí)例
- Excel自定義關(guān)閉按鈕實(shí)現(xiàn)代碼
相關(guān)文章
myatisplus的saveOrUpdate的提交總是update問題
這篇文章主要介紹了myatisplus的saveOrUpdate的提交總是update問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Springboot項(xiàng)目實(shí)現(xiàn)將類從@ComponentScan中排除
這篇文章主要介紹了Springboot項(xiàng)目實(shí)現(xiàn)將類從@ComponentScan中排除,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
eclipse報(bào)錯(cuò) eclipse啟動(dòng)報(bào)錯(cuò)解決方法
本文將介紹eclipse啟動(dòng)報(bào)錯(cuò)解決方法,需要了解的朋友可以參考下2012-11-11
探究MyBatis插件原理以及自定義插件實(shí)現(xiàn)
這篇文章主要介紹了探究MyBatis插件原理以及自定義插件實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
詳解SpringBoot 解決攔截器注入Service為空問題
這篇文章主要介紹了詳解SpringBoot 解決攔截器注入Service為空問題的解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
詳解多云架構(gòu)下的JAVA微服務(wù)技術(shù)解析
本文介紹了基于開源自建和適配云廠商開發(fā)框架兩種構(gòu)建多云架構(gòu)的思路,以及這些思路的優(yōu)缺點(diǎn)2021-05-05

