java使用 FreeMarker 生成 word文档

本文介绍了如何使用Java和ApachePOI库,结合Freemarker模板,从前端Map数据源生成Word文档的详细步骤,包括数据获取、FTL文件操作、HTML到Word的转换过程以及HTTP响应设置。
摘要由CSDN通过智能技术生成

根据业务需求,整合数据生成一个word文档

流程:

1、对需要填充数据进行获取,value

2、编写一个.ftl文档,样式为word所需样式

3、把.ftl文件与value进行结合,生成一个html文件

4、html文件转换为word文件

5、文件流形式进行返回

开发流程与分区:

1:整合需要填充的数据(返回前端的数据形式),测试可以使用Map格式的数据--方便;

2:编写.ftl文件设计html样式:整体语法与html没有太大区别;文件名称free_wordtest.ftl

3.:生成html文件   String freeWordtest =“free_wordtest.ftl”;

        注意点:

               文件地址:测试放本地(String path = this.getclass.getResource("/").getPath+"freemarker/ test/")

(名称记得替换为业务的名称最好保证唯一): String htmlName =  “测试”+".html";

                1):第一步创建一个Configuration 对象:直接new方法显示废弃,建议使用方式为

Configuration configuration = new  Configuration(Configuration.getVersion());

                2):第二步设置模板文件所在路径  

                       File file = new File( path);

                        configuration.setDirectoryForTemlateLoading(file);

                 3:设置字符集

                        configuration.setDefaultEncoding("utf-8");

                4):加载并创建模板对象

                        Template template = configuration.getTemplate(freeWordtest );

                5):创建写对象

                        String htmlPath =  path +htmlName;

                        Writer out = new FileWriter(new File(htmlPath));

                 6).调用模板导入数据 date 为需要填充的数据

                        template.process(data,out);

                         out.close();

                   7):返回地址  return htmlPath;

4:html生成worde :htmlFile html路径;filePath:生成文件地址

         BuffereReader buffereReader = new BuffereReader(new  FileReader(htmlFile));

        StringBuffer stringBuffer = new StringBuffer();

        Stream<String>  lines = buffereReader.lines();

               lines.forEach(item ->{

                stringBuffer.append(item);

                });

        byte htmlContextBytes[] = stringBuffer.toString.getBytes();

        ByteArrayInputStream bais = new ByteArrayInputStream(htmlContextBytes);

        POIFSPileSystem poifs = new POIFSFileSystem();

        FirectoryEntry directory = poifs.getRoot();

        DocumentEntry  documentEntry = directory.createDocument("WordDocument",bais);

        FileOutputStream ostream  = new  FileOutputStream(filePath);

        poifs.writeFilesstem(ostream);

        bais.close();

        ostream.close();

        //转换为byte

        ByteArrayOutputStream  byteArrayOutputStream = new ByteArrayOutputStream();

        poifs.writeFilesystem(byteArrayOutputStream );

        if(StringUtils.isEmpty(name)){

                name = "名称获取失败";

        }

        String fileName = name +".doc";

        try{

        fileName = URLEncoder.encode(fileName,"UTF-8");

        }catch(Exception e){
        e.printStackTrace();

         }

        byte[]  bytes = byteArrayOutputStream.toByteArray();

        File file = new File(filePath);

        ResponseEntity.ok.header("Content-Disposition","attachment;filename="+fileName).header("filename",fileName).header("Access-Control-Expose-Headers","fileName").contenType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.wordprocessingml.document")).contentType(MediaType.parseMediaType("application"/octet-stream)).body(bytes);

        

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值