JwfsStdPopRealPopNewTemplateVo jwfsStdPopRealPopNewTemplateVo = JwfsStdPopRealPopNewTemplateVo.builder().build();
export(
data,
"C:\\Users\\Administrator\\Desktop\\1\\aa\\",
"aa1",
jwfsStdPopRealPopNewTemplateVo
);
public static void export(List<JwfsStdPopRealPopNewTemplateVo> data, String path, String fileName, JwfsStdPopRealPopNewTemplateVo excelData) throws IOException {
log.info("开始导出");
ExcelWriter writer = ExcelUtil.getWriter();
List<Map> fieldAnnotation = getFieldAnnotation(excelData);
for(Map<String,String> map:fieldAnnotation){
for(String key:map.keySet()){
String value = map.get(key);
writer.addHeaderAlias(key,value);
}
}
//对齐方式,水平左对齐,垂直中间对齐
writer.getStyleSet().setAlign(HorizontalAlignment.LEFT, VerticalAlignment.CENTER);
//标题样式
CellStyle headCellStyle = writer.getHeadCellStyle();
//设置背景色
headCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
//必须设置 否则背景色不生效
headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//创建标题字体
Font headFont = writer.createFont();
headFont.setFontName("宋体");
//大小
headFont.setFontHeightInPoints((short) 11);
//加粗
headFont.setBold(true);
headCellStyle.setFont(headFont);
writer.setColumnWidth(-1, 30);
writer.setRowHeight(-1,20);
//保存路径
FileOutputStream output = new FileOutputStream(path+fileName+".xls");
// 一次性写出内容
writer.write(data);
//自适应列宽
int columnCount = writer.getColumnCount();
for (int i = 0; i < columnCount; ++i) {
double width = SheetUtil.getColumnWidth(writer.getSheet(), i, false);
if (width != -1.0D) {
width *= 256.0D;
//此处可以适当调整,调整列空白处宽度
width += 220D;
writer.setColumnWidth(i, Math.toIntExact(Math.round(width / 256D)));
}
}
writer.flush(output,true);
// 关闭writer,释放内存
writer.close();
}
public static List<Map> getFieldAnnotation(Object object) {
Field[] fields = object.getClass().getDeclaredFields();
Map<String, String> resultMap = new HashMap();
List<Map> list=new ArrayList<>();
for (Field field : fields) {
// 是否引用ApiModelProperty注解
boolean bool = field.isAnnotationPresent(ApiModelProperty.class);
if (bool) {
resultMap = new HashMap();
String value = field.getAnnotation(ApiModelProperty.class).value();
resultMap.put(field.getName(), value);
list.add(resultMap);
}
}
return list;
}
hutool的ExcelUtil导出excel
最新推荐文章于 2025-04-12 02:26:50 发布