首页 > 其他分享 >Xdocreport 生成docx文件 (三)

Xdocreport 生成docx文件 (三)

时间:2022-10-19 11:27:13浏览次数:90  
标签:docx name Xdocreport list 生成 className new public String

相关文章:

 

基于 Xdocreport 生成docx文件 (二)  对文件中添加一个列表

添加一个表格,并表头信息,对于下面添加域信息,怎么添加见(一)

 

 代码:

 1 public static void main(String[] args) throws Exception {
 2         list();
 3     }
 4     //实现类
 5     public static void list() throws Exception {
 6         //1.通过freemarker模板引擎加载文档,并缓存到registry中
 7         InputStream in = new FileInputStream(BASE_PATH + "list.docx");
 8         IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker);
 9         //2.设置填充字段、填充类以及是否为list。
10         FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
11         IContext context = report.createContext();
12         context.put("name", "踢足球");
13         context.put("sex", "男");
14         /**********新增img************/
15         //添加图片
16         IImageProvider img = new FileImageProvider(
17                 new File("/Users/tongzuqi/Pictures/timg (10).jpeg"),
18                 true);
19         //设置图片大小
20         img.setSize(70f, 50f);
21         context.put("img", img);
22         //特殊字符
23         fieldsMetadata.addFieldAsImage("img");
24         /**********新增img************/
25         /**********表格************/
26         List<User> list = Arrays.asList(
27                 new User("小明","5","5班"),
28                 new User("小红","3","3班"),
29                 new User("小白","2","2班")
30         );
31         context.put("list", list);
32         fieldsMetadata.load("list",User.class,true);
33         /**********表格************/
34         OutputStream out = new FileOutputStream( new File(BASE_PATH + "list-out.docx"));
35         report.setFieldsMetadata(fieldsMetadata);
36         report.process(context, out);
37     }
public class User {
    private String name;
    private String age;
    private String className;

    public User(String name, String age, String className) {
        this.name = name;
        this.age = age;
        this.className = className;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}

 

生成后的文件结果为:

 

标签:docx,name,Xdocreport,list,生成,className,new,public,String
From: https://www.cnblogs.com/mytzq/p/16805546.html

相关文章