1.制作模板doc

java在线编辑excel java在线编辑模板生成docx_xml

 

 

 

2保存为xml

 

java在线编辑excel java在线编辑模板生成docx_ci_02

 

 

 3用文本编辑工具打开xml,我这里用的notepad++,使用在线格式化工具格式化xml

https://c.runoob.com/front-end/710/(在线格式化xml)

格式化如图:把被分开的标签调整一下

java在线编辑excel java在线编辑模板生成docx_java在线编辑excel_03

 

 

 

java在线编辑excel java在线编辑模板生成docx_xml_04

 

 

 表格的velocity循环标签插入和结束:

java在线编辑excel java在线编辑模板生成docx_xml_05

 

java在线编辑excel java在线编辑模板生成docx_xml_06

 

 

 

 

 

 

 

 

方法代码(我这里是返回了文件流,拿到文件流可以处理成自己想要的文件):

public static ByteArrayInputStream createDoc(VelocityContext vc, String templateurl, String templateName) throws Exception {

        ByteArrayInputStream in = null;
        try {
            Properties ps = new Properties();
            ps.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, templateurl);// 这是模板所在路径
            VelocityEngine ve = new VelocityEngine();
            ve.init(ps);
            ve.init();
            Template template = ve.getTemplate(templateName, "utf-8");

            StringWriter swriter = new StringWriter();
            template.merge(vc, swriter);
            swriter.flush();
            swriter.close();
             in = new ByteArrayInputStream(swriter.toString().getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return in;
    }

测试代码:

public static void main(String[] args) {
        //总的数据map
         VelocityContext allMap = new VelocityContext();
         
        //标题 单个字段替换值
         allMap.put("date", "20220917");
         
        // 文件table数据汇总
         List<ReportEntity> tableList = new ArrayList<ReportEntity>();
        ReportEntity reportEntity = new ReportEntity();
        reportEntity.setDjcs("1");
        reportEntity.setDjsc(0);
        reportEntity.setTz("tz");
        reportEntity.setYxl(20);
        tableList.add(reportEntity);
        //table第二条
        ReportEntity reportEntity2 = new ReportEntity();
        reportEntity2.setDjcs("1");
        reportEntity2.setDjsc(0);
        reportEntity2.setTz("tz");
        reportEntity2.setYxl(20);
        tableList.add(reportEntity2);
        
        allMap.put("table", reportEntity2);
        
        //调用方法
        try {
            //我这是拿到了数据流/根据流直接输出或者转为文件保存都行
            InputStream fis = VelocityUtil.createDoc(allMap, "E:\\", "test.xml");
            //根据流处理输出还是生成文件
        } catch (Exception e) {
            e.printStackTrace();
        }

 

附 文件流操作:

   直接响应前端的部分代码:

byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            String fileName = URLEncoder.encode("test.doc", CharsetUtil.UTF_8);//文件名带中文的话防止乱码
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.setContentType("application/octet-stream;charset=UTF-8");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();