需求:动态生成的表头 key:value的形式 List<List>这样的形式。
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
数据形式
List<List<String>> dataList=new ArrayList<>();
List<String> data=new ArrayList<>();
data.add("项目编号");data.add(info.getProjectCode());
dataList.add(data);
List<String> data2=new ArrayList<>();
data2.add("项目名称");data2.add(info.getProjectName());
dataList.add(data2);
List<String> data3=new ArrayList<>();
data3.add("方案编号");data3.add(StrUtil.isNotEmpty(info.getSchemeCode())?info.getSchemeCode():"");
dataList.add(data3);
List<String> data4=new ArrayList<>();
data4.add("研究分期");data4.add(StrUtil.isNotEmpty(info.getStudyAging())?info.getStudyAging():"");
dataList.add(data4);
List<String> data5=new ArrayList<>();
data5.add("研究产品");data5.add(StrUtil.isNotEmpty(info.getStudyProduct())?info.getStudyProduct():"");
dataList.add(data5);
List<String> data6=new ArrayList<>();
data6.add("项目经理");data6.add(StrUtil.isNotEmpty(info.getMainPM())?info.getMainPM():"");
dataList.add(data6);
主要代码;
正常导出即可;添加一个样式注册器;
public class DynamicHeadHorizontalCellStyleStrategy extends AbstractVerticalCellStyleStrategy {
@Override
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
// 因为我这里只在第一列添加 样式 所以我判断是否第一列
Integer columnIndex = context.getColumnIndex();
// 获取样式实例
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 设置字体
WriteFont contentWriteFont = new WriteFont();
if(columnIndex==0){
contentWriteFont.setColor(IndexedColors.WHITE1.getIndex());
}
contentWriteFont.setFontHeightInPoints((short) 12);//设置字体大小
contentWriteFont.setFontName("宋体"); //设置字体名字
contentWriteCellStyle.setWriteFont(contentWriteFont);//在样式用应用设置的字体;
//设置样式;
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;
contentWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); //设置左边框;
contentWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;
contentWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;
contentWriteCellStyle.setTopBorderColor((short) 0); ///设置顶边框颜色;
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平居中
contentWriteCellStyle.setWrapped(true); //设置自动换行;
contentWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适
if(columnIndex==0){
contentWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
// contentWriteCellStyle.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex());
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
}
return contentWriteCellStyle;
}
}
导出结果;