excel读取导入功能及多个sheet页如何读取

controller

前端读取的key值为 @RequestParam("file") 中的参数(本文为file)

  //导入模板
  @ApiOperation("导入模板")
  @PostMapping(value = "/importFile")
  @AnonymousAccess
  public List<Map<String,Object>> importFile(@RequestParam("file") MultipartFile file) throws IOException {
    return fileService.importFile(file);
  }
}

service

public List<Map<String,Object>> importFile(MultipartFile file) throws IOException {


    // 获取文件名
    String fileName = multipartFile.getOriginalFilename();
    // 获取文件后缀
    String prefix = null;
    if (fileName != null) {
        prefix = fileName.substring(fileName.lastIndexOf("."));
    }
    // 生成临时文件
    File file = null;
    if (fileName != null) {
        file = File.createTempFile(fileName, prefix);
    }
    // 将传过来的文件及内容复制到file临时文件中
    if (file != null) {
        multipartFile.transferTo(file);
    }
    // 读取数据字节
    InputStream inputStream = new FileInputStream(file);
    // 读取整个Excel
    XSSFWorkbook sheets = new XSSFWorkbook(inputStream);

    // 获取想要的表单Sheet
    //从0开始,i就写i-1
    sheets.getSheetAt(i-1)
}

封装成额外的方法

    // excel转化流式文件
    public InputStream fileConventIO(MultipartFile multipartFile) throws IOException {
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = null;
        if (fileName != null) {
            prefix = fileName.substring(fileName.lastIndexOf("."));
        }
        // 生成临时文件
        File file = null;
        if (fileName != null) {
            file = File.createTempFile(fileName, prefix);
        }
        // 将传过来的文件及内容复制到file临时文件中
        if (file != null) {
            multipartFile.transferTo(file);
        }
        // 读取数据字节
        InputStream inputStream = new FileInputStream(file);
        return inputStream;
    }

通过行列确定具体值

sheetAt 就是上边的  sheets.getSheetAt(i-1)

sheetAt.getRow(row).getCell(cell)  取到当前页具体某一个数据

  //找第几行第几列的方法
  //sheetAt为第几个sheet页
  //row:行    从0开始
  //cell:列   从0开始   
  public String getSheetValue(XSSFSheet sheetAt, int row, int cell) {
    return getStringValue(sheetAt.getRow(row).getCell(cell));
  }



  //封装值转换成String的方法
  public String getStringValue(XSSFCell getCell) {
    String value = null;
    if (getCell == null || "".equals(getCell.toString())) return value;
    switch (getCell.getCellType()) {
      case STRING: //string类型
        value = getCell.toString();
        break;
      case NUMERIC: //数字类型
        value = NumberToTextConverter.toText(getCell.getNumericCellValue());
        break;
      default:
        break;
    }
    return value;
  }



  //封装值转换成BigDecimal的方法 --- 传具体单元格里的值
  public BigDecimal getBigDecimalValue(XSSFCell getCell) {
    String value;
    if (getCell == null || "".equals(getCell.toString())) {
      value = "0";
    } else {
      value = getStringValue(getCell);
    }
    return new BigDecimal(value);
  }
    
  
  //封装值转换成BigDecimal的方法 --- 传String类型
  public BigDecimal getBigDecimalValue(String str) {
    String value;
    if (str == null || "".equals(str)) {
      value = "0";
    } else {
      value = str;
    }
    return new BigDecimal(value);
  }


  //封装值转换成Integer的方法 --- 传具体单元格里的值
  public Integer getIntegerValue(XSSFCell getCell){
    String value;
    if (getCell == null || "".equals(getCell.toString())) {
      value = "0";
    } else {
      value = getStringValue(getCell);
    }
    return new Integer(value);
  }

  //封装值转换成Integer的方法 --- 传String类型
  public Integer getIntegerValue(String str){
    String value;
    if (str == null || "".equals(str)) {
      value = "0";
    } else {
      value = str;
    }
    return new Integer(value);
  }

循环取行列

sheetAt.getPhysicalNumberOfRows()  一共多少行

XSSFRow row = sheetAt.getRow(i)  当前行

sheetAt.getRow(0).getLastCellNum()  一共多少列(默认第一行列 >= 其他行)

  public String[][] getArrTwo(XSSFSheet sheetAt) {
    String[][] arr;
    //第一行为空时判断为空表
    if (sheetAt.getRow(0) == null) {
      return arr= null;
    }
    //创建二维数组
    arr = new String[sheetAt.getPhysicalNumberOfRows()][sheetAt.getRow(0).getLastCellNum()];
    // 循环获取每一行数据
    for (int i = 0; i < sheetAt.getPhysicalNumberOfRows(); i++) {
      XSSFRow row = sheetAt.getRow(i);
      //循环获取每一列的值
      for (int j = 0; j < row.getLastCellNum(); j++) {
        arr[i][j] = getStringValue(row.getCell(j));
      }
    }
    return arr;
  }

整体

public List<Map<String,Object>> importBJFL(MultipartFile file) throws IOException {
     // 获取文件名
    String fileName = multipartFile.getOriginalFilename();
    // 获取文件后缀
    String prefix = null;
    if (fileName != null) {
        prefix = fileName.substring(fileName.lastIndexOf("."));
    }
    // 生成临时文件
    File file = null;
    if (fileName != null) {
        file = File.createTempFile(fileName, prefix);
    }
    // 将传过来的文件及内容复制到file临时文件中
    if (file != null) {
        multipartFile.transferTo(file);
    }
    // 读取数据字节
    InputStream inputStream = new FileInputStream(file);
    // 读取整个Excel
    XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
    // 创建返回对象
    List<Map<String,Object>> list = new ArrayList()<>;

    //从excel每个sheet页中获取数据
    for (int i = 0; i <= 12; i++) {
      //工程概况及编制说明
      if (i == 0) list.add(get101(sheets.getSheetAt(i)));
      //工程项目标价分离汇总表
      if (i == 1) list.add(get113(sheets.getSheetAt(i)));
      //指标表
      if (i == 2) list.add(get114(sheets.getSheetAt(i)));
    }
    return list;
  }

  /**
    * 101
    */
  public Map<String,Object> get101(XSSFSheet sheetAt) {
    Map<String,Object> map = new HashMap<>();
    //项目名称
    map.put("101",getSheetValue(sheetAt, 0, 1));
    //人员编制
    map.put("101",getSheetValue(sheetAt, 19, 2));
    return map;
  }

  /**
   * 102
   */
  public Map<String,Object> get102(XSSFSheet sheetAt) {
     Map<String,Object> map = new HashMap<>();
    //循环每一行
    for (int i = 2; i < sheetAt.getPhysicalNumberOfRows(); i++) {
      XSSFRow row = sheetAt.getRow(i);
      //合计及序号
      map.put("102",getStringValue(row.getCell(0)));
      //成本科目
      map.put("102",getStringValue(row.getCell(1)));
    }
    return map;
  }

  /**
   * 103
   */
  public Map<String,Object> get103(XSSFSheet sheetAt) {
    String[][] arr;
    //第一行为空时判断为空表
    if (sheetAt.getRow(0) == null) {
      return arr = null;
    }
    //创建二维数组
    arr = new String[sheetAt.getPhysicalNumberOfRows()][sheetAt.getRow(0).getLastCellNum()];
    // 循环获取每一行数据
    for (int i = 0; i < sheetAt.getPhysicalNumberOfRows(); i++) {
      XSSFRow row = sheetAt.getRow(i);
      //循环获取每一列的值
      for (int j = 0; j < row.getLastCellNum(); j++) {
        arr[i][j] = getStringValue(row.getCell(j));
      }
    }
    return map.put("103",arr);
  }



  /**
   * 指定单元格查询的方法
   *
   * @param sheetAt sheet页名称
   * @param row     行
   * @param cell    列
   * @return
   */
  public String getSheetValue(XSSFSheet sheetAt, int row, int cell) {
    return getStringValue(sheetAt.getRow(row).getCell(cell));
  }

  //封装值转换成BigDecimal的方法
  public BigDecimal getBigDecimalValue(XSSFCell getCell) {
    String value;
    if (getCell == null || "".equals(getCell.toString())) {
      value = "0";
    } else {
      value = getStringValue(getCell);
    }
    return new BigDecimal(value);
  }


  //封装值转换成BigDecimal的方法
  public BigDecimal getBigDecimalValue(String str) {
    String value;
    if (str == null || "".equals(str)) {
      value = "0";
    } else {
      value = str;
    }
    return new BigDecimal(value);
  }


  //封装值转换成Integer的方法
  public Integer getIntegerValue(XSSFCell getCell){
    String value;
    if (getCell == null || "".equals(getCell.toString())) {
      value = "0";
    } else {
      value = getStringValue(getCell);
    }
    return new Integer(value);
  }


  //封装值转换成Integer的方法
  public Integer getIntegerValue(String str){
    String value;
    if (str == null || "".equals(str)) {
      value = "0";
    } else {
      value = str;
    }
    return new Integer(value);
  }


  //封装值转换成String的方法
  public String getStringValue(XSSFCell getCell) {
    String value = null;
    if (getCell == null || "".equals(getCell.toString())) return value;
    switch (getCell.getCellType()) {
      case STRING:
        value = getCell.toString();
        break;
      case NUMERIC:
        value = NumberToTextConverter.toText(getCell.getNumericCellValue());
        break;
//      case FORMULA:
//        value = String.valueOf(getCell.getNumericCellValue());
//        break;
      default:
        break;
    }
    return value;
  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在EasyExcel读取多个sheet,你可以使用以下步骤: 1. 导入EasyExcel库: ```java import com.alibaba.excel.EasyExcel; ``` 2. 创建一个类来存储从Excel文件中读取的数据: ```java public class SheetData { // 声明你需要存储的数据字段 // ... } ``` 3. 创建一个实现`AnalysisEventListener`接口的类来处理Excel中的数据: ```java public class SheetDataListener extends AnalysisEventListener<SheetData> { private List<SheetData> dataList = new ArrayList<>(); @Override public void invoke(SheetData data, AnalysisContext context) { dataList.add(data); // 将每行数据添加到列表中 } @Override public void doAfterAllAnalysed(AnalysisContext context) { // 数据解析完成后的操作,可以在这里进行后续处理 } public List<SheetData> getDataList() { return dataList; } } ``` 4. 使用EasyExcel读取Excel文件的多个sheet: ```java String fileName = "path/to/your/excel/file.xlsx"; SheetDataListener listener = new SheetDataListener(); EasyExcel.read(fileName, SheetData.class, listener).sheet().doRead(); List<SheetData> dataList = listener.getDataList(); // 处理读取的数据 // ... ``` 在上面的代码中,你需要将`path/to/your/excel/file.xlsx`替换为你实际的Excel文件路径。`SheetData`类应该根据你的实际需求进行定义,并且包含需要读取的字段。 通过这种方式,你可以使用EasyExcel库轻松地读取Excel文件中的多个sheet。希望对你有所帮助!如果有任何问题,请随时向我提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值