Execl解析

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>upload模块快速使用</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>

<button type="button" class="layui-btn" id="test3">
    <i class="layui-icon"></i>上传文件
</button>

<script src="/layui/layui.js"></script>
<script>
    layui.use('upload', function(){
        var upload = layui.upload;

        //指定允许上传的文件类型
        upload.render({
            elem: '#test3',
            type:'post',
            url: '/execla', //改成您自己的上传接口
            accept: 'file', //普通文件
            done: function(res){
               alert("上传成功");
               alert(res);
            }
        });


    });
</script>
</body>
</html>


        /**
         *解析execl 并上传至数据库
         * @param file
         * @return
         * @throws IOException
         */
        @ResponseBody
        @PostMapping("/execla")
        public AjaxMsg JieXiExecl(@RequestParam("file") MultipartFile file) throws IOException {
            AjaxMsg am = new AjaxMsg();
            // 获取文件名
            String fileName = file.getOriginalFilename();
            if (StringUtils.isEmpty(fileName)){
                am.setString("文件不能为空");
                return am;
            }
            // 获取文件后缀
            String prefix=fileName.substring(fileName.lastIndexOf("."));
            if (!prefix.toLowerCase().contains("xls") && !prefix.toLowerCase().contains("xlsx") ){
                am.setString("文件格式异常,请上传Excel文件格式");
                return am;
            }
            // 防止生成的临时文件重复-建议使用UUID
            final File excelFile = File.createTempFile(System.currentTimeMillis()+"", prefix);
            file.transferTo(excelFile);

            //由于2003和2007的版本所使用的接口不一样,所以这里统一用Workbook做兼容  xls xlsx
            boolean isExcel2003 = prefix.toLowerCase().endsWith("xls")?true:false;
            Workbook workbook = null;
            if(isExcel2003){
                workbook = new HSSFWorkbook(new FileInputStream(excelFile));
            }else{
                workbook = new XSSFWorkbook(new FileInputStream(excelFile));
            }

            Sheet sheet = workbook.getSheetAt(0);//获取到excel的第一个sheet页

            //这里重1开始,跳过了标题,直接从第二行开始解析
            for(int i=4; i<sheet.getLastRowNum()+1; i++) {
                Row row = sheet.getRow(i);//第几行
                row.getCell(0);//第几列 0表示第一列

                System.out.println("row.getCell(0);========" + row.getCell(0) +
                        "row.getCell(1)===" + row.getCell(1) + "" +
                        "row.getCell(2)===" + row.getCell(2));//1 2 3 列

                User  u = new User();
                u.setUgonghao(Integer.valueOf(row.getCell(0).toString()));
                u.setUname(row.getCell(1).toString());

                String cell = row.getCell(2).toString();//第三列  所属的部门
                Bumen b = bumenDao.Bnaemiscunzai(cell);
                if(b != null){
                    u.setBid(b.getBid());
                }else{
                    Bumen bb = new Bumen();
                    bb.setBname(cell);
                    int bid = bumenDao.save(bb);
                    u.setBid(bid);
                }
                userDao.save(u);
            }

            am.setCode(1);
            return am;
        }


        <!--poi Execl 依赖-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值