导入EXCEL处理 日期格式

  1. List item
    //获取单元格各类型值,返回字符串类型
    public static String getCellValueByCell(Cell cell) {
    //判断是否为null或空串
    if (cell==null || cell.toString().trim().equals("")) {
    return “”;
    }
    String cellValue = “”;
    int cellType=cell.getCellType();
    switch (cellType) {
    case Cell.CELL_TYPE_NUMERIC: // 数字
    short format = cell.getCellStyle().getDataFormat();
    if (DateUtil.isCellDateFormatted(cell)) {
    SimpleDateFormat sdf = null;
    //System.out.println(“cell.getCellStyle().getDataFormat()=”+cell.getCellStyle().getDataFormat());
    if (format == 20 || format == 32) {
    sdf = new SimpleDateFormat(“HH:mm”);
    } else if (format == 14 || format == 31 || format == 57 || format == 58) {
    // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是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 {// 日期
    sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
    }
    try {
    cellValue = sdf.format(cell.getDateCellValue());// 日期
    } catch (Exception e) {
    try {
    throw new Exception(“exception on get date data !”.concat(e.toString()));
    } catch (Exception e1) {
    e1.printStackTrace();
    }
    }finally{
    sdf = null;
    }
    } else {
    BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
    cellValue = bd.toPlainString();// 数值 这种用BigDecimal包装再获取plainString,可以防止获取到科学计数值
    }
    break;
    case Cell.CELL_TYPE_STRING: // 字符串
    cellValue = cell.getStringCellValue();
    break;
    case Cell.CELL_TYPE_BOOLEAN: // Boolean
    cellValue = cell.getBooleanCellValue()+"";;
    break;
    case Cell.CELL_TYPE_FORMULA: // 公式
    cellValue = cell.getCellFormula();
    break;
    case Cell.CELL_TYPE_BLANK: // 空值
    cellValue = “”;
    break;
    case Cell.CELL_TYPE_ERROR: // 故障
    cellValue = “ERROR VALUE”;
    break;
    default:
    cellValue = “UNKNOW VALUE”;
    break;
    }
    return cellValue;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java导入Excel文件时,日期格式可能会出现问题。为了正确地读取和处理Excel中的日期,需要使用Java中的SimpleDateFormat类来解析日期字符串。 下面是一个示例代码片段,演示如何导入Excel日期格式: ``` import java.io.File; import java.io.FileInputStream; import java.text.SimpleDateFormat; import java.util.Date; 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.xssf.usermodel.XSSFWorkbook; public class ExcelImporter { public static void main(String[] args) { try { File file = new File("data.xlsx"); FileInputStream fis = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(fis); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); int sheetIndex = 0; int rowIndex = 0; int cellIndex = 0; Row row = workbook.getSheetAt(sheetIndex).getRow(rowIndex); Cell cell = row.getCell(cellIndex); if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); String dateString = dateFormat.format(date); System.out.println("Date: " + dateString); } workbook.close(); fis.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们首先创建一个SimpleDateFormat对象,用于指定日期格式。然后,我们使用Apache POI库打开Excel文件,并获取工作簿、工作表、行和单元格对象。接下来,我们检查单元格是否包含日期值,并使用getDateCellValue()方法获取日期值。最后,我们将日期格式化为指定的日期格式,并将其打印到控制台上。 请注意,上面的代码仅处理单个单元格中的日期值。如果要处理整个工作表中的日期值,需要编写循环来遍历所有行和单元格。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值