Java excel解析

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

关于file
https://blog.csdn.net/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.csdn.net/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);


  }

你可能感兴趣的:(Java)