JAVA-DOC文件导出

创建word模板:
1.在对应的字段上用ctrl + F9
再邮件编辑域:
在这里插入图片描述

在这里插入图片描述

maven配置

<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.2</version>
</dependency>

代码示例:

package com.hdlh.calculatenet.util;

import fr.opensagres.xdocreport.core.document.SyntaxKind;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import fr.opensagres.xdocreport.template.formatter.NullImageBehaviour;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.ServletOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @author zwj
 * @date 2023年11月07日
 * @description TODO
 */
public class DocUtil {

    public static void  main(String[] args) {
        InputStream inputStream = null;

        try {
            FileOutputStream outputStream =  new FileOutputStream("C:\\Users\\86180\\Desktop\\fabu\\导出工单报告-结果.docx");
            //读取取resource目录下的模板 

//             inputStream  =  WordUtil.class.getClassLoader().getResourceAsStream("summary.docx");
            inputStream = new FileInputStream("C:\\Users\\86180\\Desktop\\fabu\\导出工单报告-模板-开发.docx");
            IXDocReport report = XDocReportRegistry
                    .getRegistry()
                    .loadReport(inputStream, TemplateEngineKind.Freemarker);
            // 设置特殊字段
            FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsTextStyling("content", SyntaxKind.Html);
            metadata.addFieldAsImage("avatar", "user.avatar", NullImageBehaviour.RemoveImageTemplate);
            report.setFieldsMetadata(metadata);
            // 创建内容-text为模版中对应都变量名称
//            String content = "&#x3c;p&#x3e;我在这里放了一段富文本&#x3c;/p&#x3e;" +
//                    "&#x3c;p&#x3e;我准备测试富文本的处理&#x3c;/p&#x3e;";
//            content = HtmlUtils.htmlUnescape(content);
            IContext context = report.createContext();
            context.put("title", "网络数据共享审批表");
            context.put("systemName", "供应链管理系统");
            context.put("proposer", "申请人");
            context.put("proposerDeptName", "申请人部门");
            context.put("manufacturer", "华为科技有限公司");
            context.put("manufacturerCurator", "华为余明");
            context.put("sharedOutPoint", "共享输出点信息");
            context.put("description", "共享数据约束信息");
            context.put("opinions1", "同意");
            context.put("sign1", "张三");
            context.put("time1", "2023-11-06 10:23");

            context.put("opinions2", "同意");
            context.put("sign2", "李思");
            context.put("time2", "2023-11-06 10:46");

            context.put("opinions3", "同意");
            context.put("sign3", "王武");
            context.put("time3", "2023-11-06 11:23");

            context.put("opinions4", "同意");
            context.put("sign4", "赵柳");
            context.put("time4", "2023-11-06 12:23");

//            context.put("content", content);

            // 生成文件
            report.process(context, outputStream);
//            response.setCharacterEncoding("utf-8");
//            response.setContentType("application/msword");
//            String fileName = "warning_task.docx";
//            response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8")))) ;
            report.process(context, outputStream);
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

另外有循环列表的设计:

在这里插入图片描述
左边编辑域插入三个参数即可:
@before-row[#list data as data]
${data.name}
@after-row[/#list]

后台java设置数据:
– opotions:是列表数据
context.put(“data”, opotions);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

supermiketho

多谢您的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值