用POI讀取Excel數(shù)據(jù):(版本號(hào):POI3.7)
1、讀取Excel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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ù)值類(lèi)型進(jìn)行存儲(chǔ),讀取時(shí)POI先判斷是是否是數(shù)值類(lèi)型,再進(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)容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
private String parseExcel(Cell cell) { String result = new String(); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: // 數(shù)字類(lèi)型 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類(lèi)型 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//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è)參考,也希望大家多多支持服務(wù)器之家。