• 导入


    https://blog.csdn.net/qq_32063079/article/details/85341586

    获取逻辑行
    Workbook wb = null;
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(r);
    判断隐藏行和隐藏列
    row.getZeroHeight() //判断隐藏行
    sheet.isColumnHidden(j) //判断隐藏列
    遍历单元格
    //遍历行
    for (int r = 0; r <= sheet.getPhysicalNumberOfRows(); r++) {
    Row row = sheet.getRow(r);
    if(row.getZeroHeight()){
    System.out.println("第"+(r + 1)+"行隐藏行");
    continue;
    }
    //遍历列
    for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
    if (sheet.isColumnHidden(j)){
    System.out.println("第" + j + "列隐藏列");
    continue;
    }
    //获取单元格的值
    String value = checkValue(r,j,row);
    }
    }
    获取单元格值-数据处理util
    public static String checkValue(int r, int j, Row row) {
    Cell cell = row.getCell(j);
    String value = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    if(cell == null){
    cell = row.createCell(0);
    cell.setCellValue("");
    }
    try {
    switch (cell.getCellType()){
    case Cell.CELL_TYPE_STRING:
    value = cell.getStringCellValue().trim();
    break;
    case Cell.CELL_TYPE_BOOLEAN:
    value = String.valueOf(cell.getBooleanCellValue());
    break;
    case Cell.CELL_TYPE_FORMULA:
    //处理数字和公式
    value = String.valueOf(Double.valueOf(cell.getNumericCellValue()));
    value = value.replaceAll("0+?$", "");
    value = value.replaceAll("[.]$", "");
    break;
    case Cell.CELL_TYPE_NUMERIC:
    //处理日期格式的Excel数据
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
    Date date = cell.getDateCellValue();
    value = sdf.format(date);
    } else {
    //处理科学计数法
    double temp = cell.getNumericCellValue();
    DecimalFormat df = new DecimalFormat("0");
    value = df.format(temp);
    }
    break;
    default:
    return "";
    }
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException("导入失败!第"+(i + 1)+"行第"+(j + 1)+"列,格式有误 ");
    }
    return value;
    }
    拓展:

    获取单元格格式和对应的值获取
    单元格类型 数值类型和java类型 取值(Cell.getCellType())
    文本 1 / CELL_TYPE_STRING cell.getStringCellValue()
    公式 2 / CELL_TYPE_FORMULA cell.getNumericCellValue())
    数值 0 / CELL_TYPE_NUMERIC cell.getNumericCellValue()
    布尔 4 / CELL_TYPE_BOOLEAN cell.getBooleanCellValue()
    日期 0 / CELL_TYPE_NUMERIC cell.getDateCellValue()
    异常 5 / CELL_TYPE_ERROR cell.getErrorCellValue()

    原文:https://blog.csdn.net/qq_32063079/article/details/85341586

  • 相关阅读:
    Linux权限及归属管理
    Linux账户管理
    随笔记录 磁盘坏道故障 2019.8.7
    随笔记录 MBR扇区故障系统备份与还原 2019.8.7
    随笔记录 grub引导故障修复 2019.8.7
    随笔记录 综合训练 2019.8.5
    随笔记录 磁盘配额2019.8.2
    随笔记录 2019.7.31
    随笔记录 2019.7.31
    随笔记录 linux命令 2019.7.29
  • 原文地址:https://www.cnblogs.com/sanhao/p/11262397.html
Copyright © 2020-2023  润新知