POI操作excel日期格式数据

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

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

例:

switch (cell.getCellType()) {

 case HSSFCell.CELL_TYPE_NUMERIC: // 数字类型
   if (HSSFDateUtil.isCellDateFormatted(cell)) { // 处理日期格式、时间格式
     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();
     // 单元格设置成常规
     if (temp.equals( "General" )) {
       format.applyPattern( "#" );
     }
     result = format.format(value);
   }
   break ;

*通用处理方案:
所有日期格式都可以通过getDataFormat()值来判断

yyyy-MM-dd----- 14

yyyy年m月d日— 31

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

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

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

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

例:
String cellValue = “”;

    switch (cell.getCellType()) {
    case XSSFCell.CELL_TYPE_STRING:// 字符串类型
        cellValue = cell.getStringCellValue();
        if (cellValue.trim().equals("") || cellValue.trim().length() <= 0)
            cellValue = "";
        break;
    case XSSFCell.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");
            double value = cell.getNumericCellValue();
            Date date = org.apache.poi.ss.usermodel.DateUtil
                    .getJavaDate(value);
            cellValue = sdf.format(date);
        } else {
            // 返回数值类型的值
            Long longVal = Math.round(cell.getNumericCellValue());
            Double doubleVal = cell.getNumericCellValue();
            if (Double.parseDouble(longVal + ".0") == doubleVal) { // 判断是否含有小数位.0
                cellValue = longVal.toString();
            } else {
                cellValue = doubleVal.toString();
            }
        }
        break;

————————————————

原文链接:https://blog.csdn.net/weixin_38084097/article/details/77242508

你可以使用JavaPOI库来读取Excel日期格式数据。具体步骤如下: 1. 使用POI库中的Workbook类打开Excel文件。 2. 确定要读取的工作表。 3. 遍历工作表的每一行,读取日期格式的单元格。 4. 使用Java中的日期格式化函数将日期数据转换为标准日期格式。 以下是示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ReadExcelDate { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("your_file.xls")); //Create Workbook instance holding reference to .xlsx file Workbook workbook = WorkbookFactory.create(file); //Get first/desired sheet from the workbook Sheet sheet = workbook.getSheetAt(0); //Iterate through each rows one by one for (Row row : sheet) { //Iterate through each cell one by one for (Cell cell : row) { //Check the cell type and format accordingly switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.print(cell.getDateCellValue() + "\t"); } else { System.out.print(cell.getNumericCellValue() + "\t"); } break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t"); break; } } System.out.println(""); } file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 该代码将遍历Excel中的每一个单元格,检查其类型并格式日期数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值