Easyexcel导出并下载excel到本地

后端: 

    public AjaxResult easyExcelAction(Tj0staff tj0staff)
    {
        //创建文件
        FileOutputStream fileOut;

        String reFileName = "static/img";
        if (!new File(Global.getProfile() + "/" + reFileName).exists()) {
            new File(Global.getProfile() +  "/" + reFileName).mkdirs();
        }
        reFileName = reFileName+"/tj.xlsx";

        StringBuilder fileName = new StringBuilder();
        fileName.append(Global.getProfile() +  "/" + reFileName);
        try {
            //String dateStr = tools.getStringTodayNoKong();
            System.out.println("准备插入的文件名是:" + fileName);
            //需要先成一个后缀是。xlsx的文件,格式不对,不能正常打开
            File fileFill07 = new File(fileName.toString());
            if(!fileFill07.exists()){
                fileFill07.createNewFile();
            }

            String LargFilePath = "large";
            String large = LargFilePath+"/fill_v02.xlsx";
            StringBuilder fileNameLarg = new StringBuilder();
            fileNameLarg.append(Global.getProfile() +  "/" + large);
            System.out.println("模板路径:"+fileNameLarg.toString());
            File template07 = new File(fileNameLarg.toString());

            ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();


            List tj0staffs = tj0staffService.selectTjAllList(tj0staff);
            System.out.println("待导出的数据" + tj0staffs);
            excelWriter.fill(tj0staffs, writeSheet);
            excelWriter.finish();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出出错:{}", e.getMessage());
            return error("导出出错!");

        }

        return success(Constants.RESOURCE_PREFIX+"/"+reFileName);

    }

难点是读取和保存路径,在打包后不能正常,不要放到项目中。

 前端

    function easyExcel() {
        var formatter = $("#formId").serialize();
        console.log(formatter)
        if ($("#startTime").val() == "") {
            $.modal.alertError("请选择开始时间");
            return false;
        }if ($("#endTime").val() == "") {
            $.modal.alertError("请选择结束时间");
            return false;
        }
            $.operate.post(ctx + "tj/0staff/easyExcel", formatter, function (data) {
                if (data.code == 0) {
                    window.open(ctx + 'common/download/resource?resource=' + data.msg);
                } else {
                    $.modal.alertError(data.msg);
                }
            }, 'json');
            /*$.ajax({
                type: "post",
                dataType: "json",
                contentType: "application/json",
                url: ctx + "tj/0staff/easyExcel/",
                data: formatter,
                success: function (data) {
                    if (data.code == 0) {
                        window.location.href = ctx + 'common/download/resource?resource=' + data.msg;
                    } else {
                        $.modal.alertError(data.msg);
                    }
                }
            });*/

        /*  var aa = httpPost("http://127.0.0.1/tj/0staff/easyExcel", formatter);

          alert("------执行结果--------"+aa);*/


    }

调用了系统中的组件,而不是用ajax,实现了传递对象参数。

你可能感兴趣的:(文件导入导出)