pom文件依赖:poi处理excel文件上传下载

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>

页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" th:src="@{/static/jquery-1.9.1.min.js}"></script>
    <script type="text/javascript" th:src="@{/static/jquery.ocupload-1.1.2.js}"></script>

    <script type="javascript">
        $(function () {
            $("#upload_confirm").upload({
                action: '/upload',
                name: 'file'
            });
        })

    </script>
</head>
<body>
   <form th:action="@{/upload}" enctype="multipart/form-data" th:method="post">
       上传文件:<input type="file" name = "file">
       <input type="submit" value="提交">
   </form>
</body>
</html>

  后台接收

@RequestMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws Exception{
        if(file == null){
            System.out.println("上传文件为空");
        }
        System.out.println("上传文件:00");
        updataExamineService.upload(file);
        return "成功";
    }

  service层

@Override
    public int upload(MultipartFile file) throws Exception{
        //判断excel文件是03版本还是07版本
        String extention = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        final File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
        // MultipartFile to File
        file.transferTo(excelFile);
        //这个是07版本
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(excelFile));
        //这个是03版本
        //HSSFWorkbook  workbook2 = new HSSFWorkbook();
        //获取文件中的表格
        Sheet sheet = workbook.getSheetAt(0);
        for(Row row : sheet){
            if(row.getRowNum() == 0){
                continue;//过滤标题行
            }
            String name = row.getCell(0).getStringCellValue();
            String code = row.getCell(1).getStringCellValue();
            Double rId = row.getCell(4).getNumericCellValue();
            Integer rId2 = rId.intValue();
            System.out.println("营销员姓名:"+name+" ,获取的code:"+code+",获取的销服人员Id:"+rId2);
 
        }

        return 0;
    }

  

posted on 2019-08-20 15:50  lazyli  阅读(2957)  评论(0编辑  收藏  举报