带注解
public class EmergencyAreaMsgExport {
@ColumnWidth(10)
@ExcelProperty(value = "序号",index = 0)
private Integer no;
}
打印方法:通过泛型list获取类属性方法返回值
public static <E> Table getPdf(List<E> list)throws Exception{
Class<?> className = list.get(0).getClass();
int t1 = className.getDeclaredFields().length;
PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Table table = new Table(t1).setWidth(UnitValue.createPercentValue(100));
Field[] fields = className.getDeclaredFields();
for (Field field : fields) {
ExcelProperty a = field.getAnnotation(ExcelProperty.class);
String[] a1 = a.value();
for (String s : a1) {
table.addCell(new Cell().add(new Paragraph(s)).setFont(sysFont)
.setBackgroundColor(new DeviceRgb(221, 234, 238)));
}
}
for (E obj : list ){
for (Field field : fields) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), className);
Method getMethod = pd.getReadMethod();
Object o = getMethod.invoke(obj);
log.info("属性get值:"+o);
table.addCell(new Cell().add(new Paragraph(o + "")).setFont(sysFont).setMaxWidth(50));
}
}
return table;
}