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日(通過判斷單元格的格式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; }
*萬能處理方案:
所有日期格式都可以通過getDataFormat()值來判斷
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)文章
使用HandlerMethodArgumentResolver用于統(tǒng)一獲取當(dāng)前登錄用戶
這篇文章主要介紹了使用HandlerMethodArgumentResolver用于統(tǒng)一獲取當(dāng)前登錄用戶實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12詳解java接口(interface)在不同JDK版本中的變化
這篇文章主要介紹了詳解java接口(interface)在不同JDK版本中的變化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02springboot后端配置多個(gè)數(shù)據(jù)源、Mysql數(shù)據(jù)庫的便捷方法
實(shí)現(xiàn)springboot 后端配置多個(gè)數(shù)據(jù)源、Mysql數(shù)據(jù)庫,只需要新建 Mapper、實(shí)體類 相應(yīng)的文件夾,將不同數(shù)據(jù)源的文件保存到對(duì)應(yīng)的文件夾下,添加綁定數(shù)據(jù)庫配置Config,就可以輕松完成2021-08-08spring*.xml配置文件明文加密的實(shí)現(xiàn)
這篇文章主要介紹了spring*.xml配置文件明文加密的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java API學(xué)習(xí)教程之正則表達(dá)式詳解
正則表達(dá)式的強(qiáng)大眾所周知,它令程序員的頭痛程度也數(shù)一數(shù)二的。下面這篇文章主要給大家介紹了關(guān)于Java API學(xué)習(xí)教程之正則表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07SpringBoot內(nèi)置tomcat參數(shù)調(diào)優(yōu)的實(shí)現(xiàn)
springboot內(nèi)置了tomcat, 并給我們?cè)O(shè)置了默認(rèn)參數(shù), 我們?cè)趺礃有薷膕pringboot內(nèi)置的tomcat參數(shù),本文就詳細(xì)的來介紹一下,感興趣的可以了解一下2023-09-09