easyexcel实现代码生成xlsx文件并保存到云端

发布时间:2024-09-07 14:01

文章目录

  • easyexcel实现代码生成xlsx文件并保存到云端
    • 保存到云端
    • 保存在自己的服务器
    • 响应给浏览器

easyexcel实现代码生成xlsx文件并保存到云端

保存到云端

	@IgnoreAuth
    @RequestMapping(\"/test3\")
    @ResponseBody
    public Map<String,Object> test3() throws IOException, InterruptedException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
        // 设置SHEET
        Sheet sheet = new Sheet(1, 0);
        sheet.setSheetName(\"sheet1\");
        // 设置标题
        Table table = new Table(1);
        List<List<String>> titles = new ArrayList<>();
        titles.add(Arrays.asList(\"用户ID\"));
        titles.add(Arrays.asList(\"名称\"));
        titles.add(Arrays.asList(\"年龄\"));
        titles.add(Arrays.asList(\"生日\"));
        table.setHead(titles);

        // 查询数据导出即可 比如说一次性总共查询出100条数据
        List<List<String>> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            userList.add(Arrays.asList(\"ID_\" + i, \"小明\" + i, String.valueOf(i), new Date().toString()));
        }
        writer.write0(userList, sheet, table);
        writer.finish();
		
        String url = OSSFactory.build().upload(new ByteArrayInputStream(out.toByteArray()),\"orderData/5.xlsx\");

        return toResponsOK(url);
    }

保存在自己的服务器

	@IgnoreAuth
    @RequestMapping(\"/test3\")
    @ResponseBody
    public Map<String,Object> test3() throws IOException, InterruptedException {
        //OutputStream out = new FileOutputStream(\"E:\\\\user.xlsx\");
        OutputStream out = new FileOutputStream(request.getServletContext().getRealPath(\"/\")+\"user.xlsx\");
      
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
        // 设置SHEET
        Sheet sheet = new Sheet(1, 0);
        sheet.setSheetName(\"sheet1\");
        // 设置标题
        Table table = new Table(1);
        List<List<String>> titles = new ArrayList<>();
        titles.add(Arrays.asList(\"用户ID\"));
        titles.add(Arrays.asList(\"名称\"));
        titles.add(Arrays.asList(\"年龄\"));
        titles.add(Arrays.asList(\"生日\"));
        table.setHead(titles);

        // 查询数据导出即可 比如说一次性总共查询出100条数据
        List<List<String>> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            userList.add(Arrays.asList(\"ID_\" + i, \"小明\" + i, String.valueOf(i), new Date().toString()));
        }
        writer.write0(userList, sheet, table);
        writer.finish();

        return toResponsOK(\"OK\");
    }

响应给浏览器

	@IgnoreAuth
    @RequestMapping(\"/test3\")
    @ResponseBody
    public Map<String,Object> test3(HttpServletResponse response) throws IOException, InterruptedException {
        ExcelWriter writer = new ExcelWriter(response.getOutputStream(), ExcelTypeEnum.XLSX);
        // 设置SHEET
        Sheet sheet = new Sheet(1, 0);
        sheet.setSheetName(\"sheet1\");
        // 设置标题
        Table table = new Table(1);
        List<List<String>> titles = new ArrayList<>();
        titles.add(Arrays.asList(\"用户ID\"));
        titles.add(Arrays.asList(\"名称\"));
        titles.add(Arrays.asList(\"年龄\"));
        titles.add(Arrays.asList(\"生日\"));
        table.setHead(titles);

        // 查询数据导出即可 比如说一次性总共查询出100条数据
        List<List<String>> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            userList.add(Arrays.asList(\"ID_\" + i, \"小明\" + i, String.valueOf(i), new Date().toString()));
        }
        writer.write0(userList, sheet, table);
        writer.finish();

        return toResponsOK(\"OK\");
    }

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号