POI对Excel自定义日期格式的读取

时间:2021-07-03 19:34:27

用POI读取Excel数据:(版本号:POI3.7)

1、读取Excel

  1. private List<String[]> rosolveFile(InputStream is, String suffix,
  2. int startRow) throws IOException, FileNotFoundException {
  3. Workbook xssfWorkbook = null;
  4. if ("xls".equals(suffix)) {
  5. xssfWorkbook = new HSSFWorkbook(is);
  6. } else if ("xlsx".equals(suffix)) {
  7. xssfWorkbook = new XSSFWorkbook(is);
  8. }
  9. Sheet xssfSheet = xssfWorkbook.getSheetAt(0);
  10. if (xssfSheet == null) {
  11. return null;
  12. }
  13. ArrayList<String[]> list = new ArrayList<String[]>();
  14. int lastRowNum = xssfSheet.getLastRowNum();
  15. for (int rowNum = startRow; rowNum <= lastRowNum; rowNum++) {
  16. if (xssfSheet.getRow(rowNum) != null) {
  17. Row xssfRow = xssfSheet.getRow(rowNum);
  18. short firstCellNum = xssfRow.getFirstCellNum();
  19. short lastCellNum = xssfRow.getLastCellNum();
  20. if (firstCellNum != lastCellNum) {
  21. String[] values = new String[lastCellNum];
  22. for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
  23. Cell xssfCell = xssfRow.getCell(cellNum);
  24. if (xssfCell == null) {
  25. values[cellNum] = "";
  26. } else {
  27. values[cellNum] = parseExcel(xssfCell);
  28. }
  29. }
  30. list.add(values);
  31. }
  32. }
  33. }
  34. return list;
  35. }

2、Excel数据处理:

Excel存储日期、时间均以数值类型进行存储,读取时POI先判断是是否是数值类型,再进行判断转化

1、数值格式(CELL_TYPE_NUMERIC):

1.纯数值格式:getNumericCellValue() 直接获取数据

2.日期格式处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含文字的日期格式

1).判断是否是日期格式:HSSFDateUtil.isCellDateFormatted(cell)

2).判断是日期或者时间

cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")

OR: cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd")

3.自定义日期格式处理yyyy年m月d日,h时mm分,yyyy年m月等含文字的日期格式

判断cell.getCellStyle().getDataFormat()值,解析数值格式

yyyy年m月d日----->31

m月d日---->58

h时mm分--->32

2、字符格式(CELL_TYPE_STRING):直接获取内容

  1. private String parseExcel(Cell cell) {
  2. String result = new String();
  3. switch (cell.getCellType()) {
  4. case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
  5. if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
  6. SimpleDateFormat sdf = null;
  7. if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
  8. .getBuiltinFormat("h:mm")) {
  9. sdf = new SimpleDateFormat("HH:mm");
  10. } else {// 日期
  11. sdf = new SimpleDateFormat("yyyy-MM-dd");
  12. }
  13. Date date = cell.getDateCellValue();
  14. result = sdf.format(date);
  15. } else if (cell.getCellStyle().getDataFormat() == 58) {
  16. // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
  17. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  18. double value = cell.getNumericCellValue();
  19. Date date = org.apache.poi.ss.usermodel.DateUtil
  20. .getJavaDate(value);
  21. result = sdf.format(date);
  22. } else {
  23. double value = cell.getNumericCellValue();
  24. CellStyle style = cell.getCellStyle();
  25. DecimalFormat format = new DecimalFormat();
  26. String temp = style.getDataFormatString();
  27. // 单元格设置成常规
  28. if (temp.equals("General")) {
  29. format.applyPattern("#");
  30. }
  31. result = format.format(value);
  32. }
  33. break;
  34. case HSSFCell.CELL_TYPE_STRING:// String类型
  35. result = cell.getRichStringCellValue().toString();
  36. break;
  37. case HSSFCell.CELL_TYPE_BLANK:
  38. result = "";
  39. default:
  40. result = "";
  41. break;
  42. }
  43. return result;
  44. }

*万能处理方案

所有日期格式都可以通过getDataFormat()值来判断

yyyy-MM-dd----- 14

yyyy年m月d日--- 31

yyyy年m月------- 57

m月d日  ---------- 58

HH:mm----------- 20

h时mm分  ------- 32

  1. //1、判断是否是数值格式
  2. if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
  3. short format = cell.getCellStyle().getDataFormat();
  4. SimpleDateFormat sdf = null;
  5. if(format == 14 || format == 31 || format == 57 || format == 58){
  6. //日期
  7. sdf = new SimpleDateFormat("yyyy-MM-dd");
  8. }else if (format == 20 || format == 32) {
  9. //时间
  10. sdf = new SimpleDateFormat("HH:mm");
  11. }
  12. double value = cell.getNumericCellValue();
  13. Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
  14. result = sdf.format(date);
  15. }