基于easyexcel的导出

后端代码

    @RequestMapping(value = "/exporExcel", method = RequestMethod.GET)
    public void exporExcel(HttpServletResponse response) {
        ExcelWriter writer = null;
            OutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
            //添加响应头信息
            response.setHeader("Content-disposition", "attachment; filename=" + "catagory.xls");
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma", "No-cache");//设置头
            response.setHeader("Cache-Control", "no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头

            //实例化 ExcelWriter
            writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLS, true);

            //实例化表单
            Sheet sheet = new Sheet(1, 0, Catagory.class);
            sheet.setSheetName("目录");

            //获取数据
            List<Catagory> catagoryList = new ArrayList<>();
            for (int i = 0 ; i <  10; i++){
                Catagory catagory = new Catagory();
                catagory.setId(1);
                catagory.setName("11");
                catagoryList.add(catagory);
            }


            //输出
            writer.write(catagoryList, sheet);
            writer.finish();
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//        return "index";
    }

前段: --不能用ajax请求
atest(row){
location.href=“http://localhost:8080/terminal/exporExcel”;
},

发布了33 篇原创文章 · 获赞 0 · 访问量 223

猜你喜欢

转载自blog.csdn.net/lemonmr/article/details/103594900