Java读取Excel日期格式 Java获取单元格内容(支持日期及小数) getCellValue() 兼容xls2003及xlsx2007 jar包从3.17升级到5.2.2后 必须为枚举常量

   仅xls2003

//获取单元格内容(支持日期及小数)
private String getCellValue(HSSFCell cell) {
    if(cell == null){
        return null;
    }

    String cellValue = "";
    //like12 modified bug,20171124,不能保留小数
    //DecimalFormat df = new DecimalFormat("#");//不保留小数
    DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
    switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            cellValue = cell.getRichStringCellValue().getString().trim();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            // like12 add,20180622,支持日期格式
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                Date d = cell.getDateCellValue();
                DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
                cellValue = df2.format(d);
            }
            // 数字
            else {
                cellValue = df.format(cell.getNumericCellValue()).toString();
            }
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            cellValue = cell.getCellFormula();
            break;
        default:
            cellValue = "";
    }
    return cellValue;
}

兼容xls2003及xlsx2007 

//获取单元格内容(支持日期及小数)(兼容xls2003及xlsx2007)
private String getCellValue(Cell cell) {
    if(cell == null){
        return null;
    }

    String cellValue = "";
    //like12 modified bug,20171124,不能保留小数
    //DecimalFormat df = new DecimalFormat("#");//不保留小数
    DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
    switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            cellValue = cell.getRichStringCellValue().getString().trim();
            break;
        case  Cell.CELL_TYPE_NUMERIC:
            // like12 add,20180622,支持日期格式
            if (DateUtil.isCellDateFormatted(cell)) {
                Date d = cell.getDateCellValue();
                DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
                cellValue = df2.format(d);
            }
            // 数字
            else {
                cellValue = df.format(cell.getNumericCellValue()).toString();
            }
            break;
        case  Cell.CELL_TYPE_BOOLEAN:
            cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
            break;
        case  Cell.CELL_TYPE_FORMULA:
            cellValue = cell.getCellFormula();
            break;
        default:
            cellValue = "";
    }
    return cellValue;
}
//获取文件的后缀名,不能是exe/dll/com/bat
String fileName = file.getOriginalFilename(); // 获取文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();// 获取文件的后缀名,并转化为小写
if(suffixName.equals(".exe")||suffixName.equals(".dll")||suffixName.equals(".com")||suffixName.equals(".bat")) {
    result.put("code", 999);
    result.put("msg", "文件格式错误");
    return result.toJSONString();
}

//兼容xls2003及xlsx2007
Workbook wb = null;
if(".xls".equals(suffixName)){
    wb = new HSSFWorkbook(file.getInputStream());
}else if(".xlsx".equals(suffixName)){
    wb = new XSSFWorkbook(file.getInputStream());
}else{
    result.put("code", 999);
    result.put("msg", "不支持的文件后缀");
    return result.toJSONString();
}

jar包从3.17升级到5.2.2后

解决jar升级到5.2.2后报”枚举 switch case 标签必须为枚举常量的非限定名称“的问题

//like12 modified,20220818,解决jar升级到5.2.2后报”枚举 switch case 标签必须为枚举常量的非限定名称“的问题
//获取单元格内容(支持日期及小数)(兼容xls2003及xlsx2007)
private String getCellValue(Cell cell) {
    if(cell == null){
        return null;
    }

    String cellValue = "";
    //like12 modified bug,20171124,不能保留小数
    //DecimalFormat df = new DecimalFormat("#");//不保留小数
    DecimalFormat df = new DecimalFormat("#.##");//最多保留x位小数
    switch (cell.getCellType()) {
        case STRING://Cell.CELL_TYPE_STRING
            cellValue = cell.getRichStringCellValue().getString().trim();
            break;
        case NUMERIC://CELL_TYPE_NUMERIC
            // like12 add,20180622,支持日期格式
            if (DateUtil.isCellDateFormatted(cell)) {
                Date d = cell.getDateCellValue();
                DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
                cellValue = df2.format(d);
            }
            // 数字
            else {
                cellValue = df.format(cell.getNumericCellValue()).toString();
            }
            break;
        case BOOLEAN://Cell.CELL_TYPE_BOOLEAN
            cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
            break;
        case FORMULA://Cell.CELL_TYPE_FORMULA
            cellValue = cell.getCellFormula();
            break;
        default:
            cellValue = "";
    }
    return cellValue;
}

参考:

POI 操作 Excel 中 HSSFCell.CELL_TYPE_STRING 等无定义解决方法_竹林幽深的博客-CSDN博客_cell_type_stringcell.getCellType() is deprecated 与 HSSFCell.CELL_TYPE_NUMERIC is deprecated的替代方法_神蜗牛的博客-CSDN博客(后面这个有点误差5.2.2版本找不到cell.getCellTypeEnum()而依然是cell.getCellType()) 

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用Java的POI库来读取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中的每一个单元格,检查其类型并格式日期数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值