# 06-EasyPoi
# 1、引入依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
# 2、4.2.0版本Bug
ExportParams参数类的getHeaderHeight()方法取的是titleHeight的值
/** * Excel 导出参数 * 重写 getHeaderHeight 方法 * * @Author qixiaodong * @Date 2020/12/22 9:46 */ @Data public class ExportParams extends ExcelBaseParams { /** * 表格名称 */ private String title; /** * 表格名称 */ private short titleHeight = 10; /** * 第二行名称 */ private String secondTitle; /** * 表格名称 */ private short secondTitleHeight = 8; /** * sheetName */ private String sheetName; /** * 过滤的属性 */ private String[] exclusions; /** * 是否添加需要需要 */ private boolean addIndex; /** * 是否添加需要需要 */ private String indexName = "序号"; /** * 冰冻列 */ private int freezeCol; /** * 表头颜色 & 标题颜色 */ private short color = HSSFColor.HSSFColorPredefined.BLACK.getIndex(); /** * 第二行标题颜色 * 属性说明行的颜色 例如:HSSFColor.SKY_BLUE.index 默认 */ private short headerColor = HSSFColor.HSSFColorPredefined.BLACK.getIndex(); /** * Excel 导出版本 */ private ExcelType type = ExcelType.HSSF; /** * Excel 导出style */ private Class<?> style = ExcelExportStylerDefaultImpl.class; /** * 表头高度 */ private double headerHeight = 9D; /** * 是否创建表头 */ private boolean isCreateHeadRows = true; /** * 是否动态获取数据 */ private boolean isDynamicData = false; /** * 是否追加图形 */ private boolean isAppendGraph = true; /** * 是否固定表头 */ private boolean isFixedTitle = true; /** * 单sheet最大值 * 03版本默认6W行,07默认100W */ private int maxNum = 0; /** * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 * 全局设置,优先使用 */ private short height = 0; /** * 只读 */ private boolean readonly = false; public ExportParams() { } public ExportParams(String title, String sheetName) { this.title = title; this.sheetName = sheetName; } public ExportParams(String title, String sheetName, ExcelType type) { this.title = title; this.sheetName = sheetName; this.type = type; } public ExportParams(String title, String secondTitle, String sheetName) { this.title = title; this.secondTitle = secondTitle; this.sheetName = sheetName; } public short getSecondTitleHeight() { return (short) (secondTitleHeight * 50); } public short getTitleHeight() { return (short) (titleHeight * 50); } public short getHeight() { return height == -1 ? -1 : (short) (height * 50); } //重写 public short getHeaderHeight() { return (short) (headerHeight * 50); } }
默认导出样式实现中,标题和标题字体颜色设置
/**
* 样式的默认实现
* 重写 getTitleStyle、getHeaderStyle 方法中的字体颜色设置
*
* @Author qixiaodong
* @Date 2020/12/22 9:46
*/
public class ExcelExportStylerDefaultImpl extends AbstractExcelExportStyler
implements IExcelExportStyler {
public ExcelExportStylerDefaultImpl(Workbook workbook) {
super.createStyles(workbook);
}
@Override
public CellStyle getTitleStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
//设置颜色为:参数color,这样可以通过ExportParams设置颜色
font.setColor(color);
titleStyle.setFont(font);
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setWrapText(true);
titleStyle.setDataFormat(STRING_FORMAT);
return titleStyle;
}
@Override
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setDataFormat(STRING_FORMAT);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
@Override
public CellStyle getHeaderStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
//设置颜色为:参数color,这样可以通过ExportParams设置颜色
font.setColor(color);
titleStyle.setFont(font);
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setDataFormat(STRING_FORMAT);
return titleStyle;
}
@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setDataFormat(STRING_FORMAT);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
}
# 3、使用
/**
* 导出工具类 ExcelExportUtil
* 导入工具类 ExcelImportUtil
* excel页面工具类 ExcelXorHtmlUtil
* cvs 导出工具类 CsvExportUtil
* cvs 导入工具类 CsvImportUtil
* pdf 导出工具类 PdfExportUtil
* word 导出工具类 WordExportUtil
*/