Java excel解析

这篇博客探讨了在Java中处理Excel文件的上传与解析,包括Excel 98-03和07以后版本的兼容性问题,使用XSSFWorkbook和HSSFWork进行读取,以及File与MultipartFile之间的转化。还提到了数值类型数据格式的处理问题,并提供了相关代码和测试案例链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

excel的上传与解析
https://app.yinxiang.com/fx/1b863972-9a9d-477e-82bf-fe960053068e

关于file
https://blog.youkuaiyun.com/sdut406/article/details/85647982

过程中会遇到的问题:

  1. 关于Excel 98-03版本 与 07以后版本的兼容性问题
    XSSFWorkbook 可读07以后版本Excel HSSFWork 读03之前版本 Workbook 对文件类型进行了判断,达到兼容,不会引起以下包报错。
    Package should contain a content type part [M1.13]]

  2. File 与MultipartFile转化问题
    https://www.liangzl.com/get-article-detail-30596.html
    M 转 F

File file = new File(path); 

FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);  

F 转 M

File file = new File("src/test/resources/input.txt");

FileInputStream input = new FileInputStream(file);

MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
  1. 取数值类型数据格式问题
    https://blog.youkuaiyun.com/ghsau/article/details/10163043

代码见下

public void importGirdInfo(MultipartFile file) {

    //检测是否为空
    if (file == null || file.getSize() == 0) {
      System.out.println("请重新选择导入文件");
    }
    //检测格式
    String fn = file.getOriginalFilename();
    if (!(fn.endsWith("xls") || fn.endsWith("xlsx"))) {
      System.out.println("文件类型错误,请重新选择导入文件");
    }

    InputStream input = null;
    StringBuffer failedRows = new StringBuffer();
    try {
      input = file.getInputStream();

      Workbook workbook = null;
      try {
        workbook = WorkbookFactory.create(input);
      } catch (InvalidFormatException e) {
        e.printStackTrace();
      }
      Sheet sheet = workbook.getSheetAt(0);
      //XSSFWorkbook 可读07版本Excel   HSSFWork 读03版本  Workbook 对文件类型进行了判断
      //XSSFWorkbook wb = new XSSFWorkbook(input);
      //XSSFSheet sheet = wb.getSheetAt(0);
      //分析数据

      System.out.println(sheet.getLastRowNum());
      System.out.println(sheet.getFirstRowNum());
      for (int r = 0; r <= sheet.getLastRowNum(); r++) {

        Row row = sheet.getRow(r);

        if (row == null || row.getCell(0) == null) {
          continue;
        }
        try {
          //DistrictDTO dto = new DistrictDTO();
          String gridCode = "";
          long gridCode1 = 0;
          if (row.getCell(0).getCellType() == Cell.CELL_TYPE_NUMERIC) {
            gridCode1 = Math.round(row.getCell(0).getNumericCellValue());
          } else if (row.getCell(0).getCellType() == Cell.CELL_TYPE_STRING) {
            gridCode = row.getCell(0).getStringCellValue();
          }
          System.out.println("打印");
          System.out.println(gridCode);
          System.out.println(gridCode1);
          //分析数据的操作
          //...

        } catch (Exception e) {

        }
      }

    } catch (IOException e) {
      System.out.println("解析异常,请重新导入");
    } finally {
      if (input != null) {
        try {
          input.close();
        } catch (Exception e) {
          System.out.println("关闭错误!");
        }
      }
    }
  }

测试类

 @Test
  public void test3() throws Exception {

    File file = new File("/Users/peach/Desktop/text.xls");

    FileInputStream input = new FileInputStream(file);

    MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
      IOUtils
        .toByteArray(input));

    excelGridInfo.importGirdInfo(multipartFile);


  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值