栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java导出world文档 poi-tl开源框架使用demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java导出world文档 poi-tl开源框架使用demo

一、准备
添加依赖

 
      com.deepoove
      >poi-tl
      1.7.3
 

定义模板
注意:需要把模板放在springboot项目的resources目录下,我这边放在了resources/template下

说明:
{{googslist}}这个是用来渲染动态表格标记点,定义这个后,才能在指定位置渲染
二、后台代码

@Controller
public class Test {

    @GetMapping("downloadWorld")
    public void downloadWorld(HttpServletResponse response,String hiddenDangerId){
        Map data = new HashMap<>();
        data.put("center","这个是简单描述内容");
        //创建表格数据
        List> goodsList = new ArrayList<>();
        Map d1 = new HashMap();
        d1.put("name","手机");
        d1.put("googsNum","10");
        goodsList.add(d1);
        //渲染表格
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        //注意goodslist就是表格上定义的goodslist
        Configure config = Configure.newBuilder().bind("goodslist", policy).build();
        data.put("goodslist",goodsList);
                //本地测试这样写
//        ClassPathResource classPathResource = new ClassPathResource("/template/test.docx");
//        String path = classPathResource.getURL().getPath();
//        XWPFTemplate template = XWPFTemplate.compile(path,config).render(data);
        //渲染数据  
        //这种写法支持打包成jar在linux服务器中使用能解决掉文件获取不到的问题
        XWPFTemplate template = XWPFTemplate.compile(WordUtil.class.getClassLoader().getResourceAsStream("template/test.docx"),config).render(data);
        downloadServlet(template,response);
    }

    private static void downloadServlet(XWPFTemplate template, HttpServletResponse response){
        try{
            response.setHeader("Content-disposition", "attachment;filename=" + new String("model".getBytes("gb2312"), "ISO8859-1") + ".docx");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.setContentType("multipart/form-data;charset=UTF-8");
            ServletOutputStream out = response.getOutputStream();
            template.write(out);
            out.flush();
            template.close();
            out.close();
        }catch (Exception e){
            e.getMessage();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992463.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号