根据word模板导出

1.首先导入pom包

<dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.core</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.template</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
            <version>2.0.2</version>
        </dependency>

2.编写控制器Controller方法(附:实体类如下)

@PostMapping("/exportWord")
  public void exportWord(
      HttpServletResponse response, @RequestParam("orderNumber") String orderNumber)
      throws IOException, XDocReportException {
    // 获取Word模板,模板存放路径在项目的resources目录下
    InputStream ins = this.getClass().getResourceAsStream("/template/word.docx");
    // 注册xdocreport实例并加载FreeMarker模板引擎
    IXDocReport report =
        XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
    // 创建xdocreport上下文对象
    IContext context = report.createContext();

    OrderFlowWord orderFlowWord = orderFlowService.exportWord(orderNumber);
    context.put("order", orderFlowWord);

    // 创建字段元数据
    FieldsMetadata fm = report.createFieldsMetadata();

    ServletOutputStream outputStream = response.getOutputStream();

    response.setHeader(
        "Content-Disposition",
        "attachment;filename="
            + URLEncoder.encode(
                "导出文件名称(" + orderFlowWord.getOrderNumber() + ").docx", "UTF-8"));

    response.setCharacterEncoding("UTF-8");
    response.setHeader("content-Type", "application/msword");
    report.process(context, outputStream);
  }

实体类:

        


import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderFlowWord {
    private String orderNumber;
    private String orderSource;
    private String createDate;
    private String orderType;
    private String appealer;
    private String phoneNumber;
    private String appealDetails;
    private String opinion;
    private String instructions;
    private String handleUnit;
    private String agent;
    private String agentTelPhone;
    private String disposeResult;
    private String signer;
    private String signDate;
    private Integer isAnonymity;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值