代码
依赖
<dependencies>
<!-- easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.3.0</version>
</dependency>
<!-- pdf -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
</dependencies>
自定义注解
用于定义需要到处的实体类的属性信息
package com.zdd.export;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author zdd
*/
@Target({
ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExportFiled {
String name() default "";
String type() default "string";
int height() default 200;
int width() default 500;
String format() default "yyy-MM-dd HH:mm:ss";
String replace() default "";
}
属性类型定义类
package com.zdd.export;
/**
* @description:
* @author: zdd
* @time: 2021/5/10 11:26
*/
public class FiledTypeConstant {
public final static String STRING = "string";
public final static String IMAGE = "image";
public final static String DATE = "date";
public final static String LIST = "list";
public final static String IMAGE_LIST = "imageList";
public final static String OBJECT = "object";
public final static String MAP = "map";
}
测试实体类
/**
* @description:
* @author: zdd
* @time: 2021/5/10 10:17
*/
@Data
public class Model {
@ExportFiled
private String name;
@ExportFiled(replace = "1_男,0_女")
private Integer gender;
@ExportFiled
private Integer age;
@ExportFiled(type = "image", width = 100, height = 50)
private String imagePath;
@ExportFiled(type = "date", format = "yyy-MM-dd HH:mm:ss")
private LocalDateTime date;
@ExportFiled(type = "list")
private List<Item> list;
@ExportFiled(type = "map")
private Map<String, Object> imageTest;
}
/**
* @description:
* @author: zdd
* @time: 2021/5/20 11:08
*/
@Data
public class ImageTest {
@ExportFiled
private String name;
@ExportFiled(type = "image")
private String mediaPath;
}
/**
* @description:
* @author: zdd
* @time: 2021/5/18 13:50
*/
@Data
public class Item {
@ExportFiled
private String name;
@ExportFiled
private Integer age;
@ExportFiled(type = "date", format = "yyy-MM-dd HH:mm:ss")
private LocalDateTime date;
}
导出工具类
package com.zdd.export.util;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.zdd.export.ExportFiled;
import com.zdd.export.FiledTypeConstant;
import com.zdd.export.Item;
import com.zdd.export.Model;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java