xlsx 导入 layui导入文件

html:

<div>
<button type="button" class="layui-btn" id="uploadExcel">
<i class="layui-icon">&#xe67c;</i>导入</button>
</div>

js:

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

        //执行实例
        var uploadInst = upload.render({
            elem: '#uploadExcel' //绑定元素
            ,url: '/evaluation/import' //上传接口
            ,accept:'file'
            ,field:"multipartFile"
            ,acceptMime:'file/xlsx,file/xls,file/xlsm,file/xltx'
            ,done: function(res){
                if (res.code==200){
                    alert(res.msg)
                    layer.msg(res.msg)
                    setTimeout("alert(msg)",3000)
                    location.href='/evaluation/page';//返回列表页
                }else{
                    layer.msg(res.msg);
                }
            }
        });
    });
</script>

java:

 //导入
    @RequestMapping("/evaluation/import")
    @ResponseBody
    public ResponseObject doImport(@RequestParam(value = "multipartFile", required = false) MultipartFile multipartFile, HttpServletResponse response) throws IOException {
        int code=200;
        msg="保存成功!";
        String fileName = multipartFile.getOriginalFilename();
        String xlsx= fileName.split("\\.")[1];

        //xls返回的是HSSFWorkbook , 而xlsx返回的是XSSFWorkbook.
        if("xlsx".equals(xlsx)){
            XSSFWorkbook  workbook = new XSSFWorkbook(multipartFile.getInputStream());
            XSSFSheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1;i<=lastRowNum;i++){
                try {
                    XSSFRow row = sheet.getRow(i);
                    SystemEvaluation bean=new SystemEvaluation();
                    bean.setId(UUID.randomUUID().toString());
                    bean.setEvaluation_mcss(row.getCell(0).toString());
                    bean.setEvaluation_tzqd(row.getCell(1).toString());
                    bean.setEvaluation_sjsywz(row.getCell(2).toString());
                    bean.setEvaluation_sjwzzzl(row.getCell(3).toString());
                    bean.setEvaluation_jckze(row.getCell(4).toString());
                    bean.setEvaluation_jkzzl(row.getCell(5).toString());
                    bean.setEvaluation_slbi(row.getCell(6).toString());
                    bean.setEvaluation_dnzjrs(row.getCell(7).toString());
                    bean.setEvaluation_rcgxd(row.getCell(8).toString());
                    bean.setEvaluation_cyjjd(row.getCell(9).toString());
                    bean.setEvaluation_sqtzbl(row.getCell(10).toString());

                    int s=  service.insert(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    msg="导入失败,检查导入格式!";
                    code=500;
                }
            }
        }else if("xls".equals(xlsx)){
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(multipartFile.getInputStream());
            HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
            int lastNum = sheetAt.getLastRowNum();
            for (int i = 1;i<=lastNum;i++) {
                try {
                    HSSFRow row = sheetAt.getRow(i);
                    SystemEvaluation bean = new SystemEvaluation();
                    bean.setId(UUID.randomUUID().toString());
                    bean.setEvaluation_mcss(row.getCell(0).toString());
                    bean.setEvaluation_tzqd(row.getCell(1).toString());
                    bean.setEvaluation_sjsywz(row.getCell(2).toString());
                    bean.setEvaluation_sjwzzzl(row.getCell(3).toString());
                    bean.setEvaluation_jckze(row.getCell(4).toString());
                    bean.setEvaluation_jkzzl(row.getCell(5).toString());
                    bean.setEvaluation_slbi(row.getCell(6).toString());
                    bean.setEvaluation_dnzjrs(row.getCell(7).toString());
                    bean.setEvaluation_rcgxd(row.getCell(8).toString());
                    bean.setEvaluation_cyjjd(row.getCell(9).toString());
                    bean.setEvaluation_sqtzbl(row.getCell(10).toString());

                    int s = service.insert(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    msg = "导入失败,检查导入格式!!";
                    code = 500;
                }
            }
        }else {
            msg = "导入失败,检查导入格式!!";
            code = 500;
        }
        return new ResponseObject(code,msg);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值