樂小伍 2023-07-26 15:34 采纳率: 0%
浏览 46

Java导出Word文档报错

Java导出Word文档报错,Null template engine.Set template engine with IXDocReport#set TemolateEngine

借鉴网址:https://blog.csdn.net/qq_43842093/article/details/124935696

主要问题 是找不到能够正确设置Freemarker模板引擎的方法

package com.tzsj.test;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import fr.opensagres.xdocreport.core.XDocReportException;
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 io.renren.entity.Goods;
 
@Controller
@RequestMapping("/word")
public class WordTest {
    @Test
    public void test() throws IOException, XDocReportException {
        generateWord();
    }
    
    public void generateWord() throws IOException, XDocReportException {
        //获取Word模板,模板存放路径在项目的resources目录下
        InputStream ins = this.getClass().getResourceAsStream("/模板.docx");
        //注册xdocreport实例并加载FreeMarker模板引擎
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,     
                               TemplateEngineKind.Freemarker);
        //创建xdocreport上下文对象
       // 这行报错,1111111111111111111111111111111111111
        IContext context = report.createContext();
        
        //创建要替换的文本变量
        context.put("city", "北京市");
        context.put("startDate", "2020-09-17");
        context.put("endDate", "2020-10-16");
        context.put("totCnt", 3638763);
        context.put("totAmt", "6521");
        context.put("onCnt", 2874036);
        context.put("onAmt", "4768");
        context.put("offCnt", 764727);
        context.put("offAmt", "1753");
        context.put("typeCnt", 36);
        
        List<Goods> goodsList = new ArrayList<Goods>();
        Goods goods1 = new Goods();
        goods1.setNum(1);
        goods1.setType("臭美毁肤");
        goods1.setSv(675512);
        goods1.setSa("589");
        Goods goods2 = new Goods();
        goods2.setNum(2);
        goods2.setType("女装");
        goods2.setSv(602145);
        goods2.setSa("651");
        Goods goods3 = new Goods();
        goods3.setNum(3);
        goods3.setType("手机");
        goods3.setSv(587737);
        goods3.setSa("866");
        Goods goods4 = new Goods();
        goods4.setNum(4);
        goods4.setType("家具家装");
        goods4.setSv(551193);
        goods4.setSa("783");
        Goods goods5 = new Goods();
        goods5.setNum(5);
        goods5.setType("食物饮品");
        goods5.setSv(528604);
        goods5.setSa("405");
        goodsList.add(goods1);
        goodsList.add(goods2);
        goodsList.add(goods3);
        goodsList.add(goods4);
        goodsList.add(goods5);
        context.put("goods", goodsList);
        
        //创建字段元数据
        FieldsMetadata fm = report.createFieldsMetadata();
        //Word模板中的表格数据对应的集合类型
        fm.load("goods", Goods.class, true);
       
        //输出到本地目录
        FileOutputStream out = new FileOutputStream(new File("D://商品销售报表.docx"));
        report.process(context, out);
    }
 
}


  • 写回答

4条回答 默认 最新

  • IT小辉同学 Java领域优质创作者 2023-07-26 15:58
    关注

    在Java中导出Word文档时,出现"Null template engine. Set template engine with IXDocReport#setTemplateEngine"的错误提示,是因为在使用XDocReport库时,需要设置模板引擎。

    以下是一个基本的Java导出Word文档的示例:

    import fr.opensagres.xdocreport.core.document.DocumentKind;
    import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
    import fr.opensagres.xdocreport.template.TemplateEngineKind;
    import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
    
    public class WordExporter {
    
        public static void main(String[] args) {
            try {
                // 加载Word模板
                XDocReportRegistry registry = XDocReportRegistry.getRegistry();
                InputStream in = new FileInputStream("path/to/your/template.docx");
                IXDocReport report = registry.loadReport(in, TemplateEngineKind.Freemarker);
    
                // 设置模板引擎
                ITemplateEngine templateEngine = report.getTemplateEngine();
                FieldsMetadata metadata = new FieldsMetadata();
                metadata.addFieldAsTextStyling("propertyName", SyntaxKind.Html);
                templateEngine.setFieldsMetadata(metadata);
    
                // 填充数据
                IContext context = report.createContext();
                context.put("propertyName", "propertyValue"); // 设置模板中的参数
                report.process(context, out);
    
                // 将文档输出到文件
                OutputStream out = new FileOutputStream("path/to/your/output.docx");
                report.process(context, out);
    
                System.out.println("Word文档生成成功!");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("生成Word文档出错!");
            }
        }
    }
    

    在这个示例中,你需要将"path/to/your/template.docx"替换为你实际的Word模板文件路径,"path/to/your/output.docx"替换为你要输出的Word文档路径。另外,根据你的模板中所使用的模板引擎,也需要相应地设置TemplateEngineKind变量。

    如果你仍然遇到"Null template engine. Set template engine with IXDocReport#setTemplateEngine"错误,请确保在使用IXDocReport对象之前,通过调用IXDocReport#setTemplateEngine方法设置了正确的模板引擎。

    希望这能帮助你解决问题!如有其他疑问,请随时提问。非常乐意无偿提供帮助,一起学习

    评论

报告相同问题?

问题事件

  • 修改了问题 7月27日
  • 创建了问题 7月26日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line