EasyExcel 是对POI的进一步优化 用到复杂逻辑还是得使用poi进行设置
maven引入
// 注意解决包冲突 使用maven-help 插件进行解决
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
<exclusions>
<exclusion>
<artifactId>poi-ooxml-schemas</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
</exclusions>
</dependency>
实体实例
@Data
@ColumnWidth(16)
@HeadFontStyle(bold = false)
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 1)
public class ArmpPPSReconciliaReportContents {
/**
* 商户财务编码
*/
@ColumnWidth(10)
@ExcelProperty(value ="编码")
private String Code;
/**
* 商户财务编码
*/
@ContentStyle(borderLeft = BorderStyle.THIN, borderRight= BorderStyle.THIN, borderTop= BorderStyle.THIN, borderBottom= BorderStyle.THIN)
@ExcelProperty(value ="月")
private String Month;
}
添加复杂样式实例
主要是继承AbstractCellWriteHandler 完成afterCellDispose 方法
这里主要记录数字的科学显示以及添加链接
注意:这里设置会影响easyExcel 注解 不要同时操作一个属性 会有冲突
public class CustomCellWriteHandler extends AbstractCellWriteHandler {
private String sheetName;
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// 设置单元格 格式
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
CellStyle cellStyle = createStyle(workbook);
DataFormat dataFormat = workbook.createDataFormat();
try {
//行数大于0,列数为3
if(cell.getRowIndex() > 0 && cell.getColumnIndex() >=3) {
cellStyle.setDataFormat(dataFormat.getFormat("_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)"));
}
// 设置连接
setCellLink(cellDataList,cell,writeSheetHolder, workbook, cellStyle);
} catch (ParseException e) {
e.printStackTrace();
}
cell.setCellStyle(cellStyle);
}
private void setCellLink(List<? extends CellData> cellDataList, Cell cell, WriteSheetHolder writeSheetHolder, Workbook workbook, CellStyle cellStyle) {
if(cell.getRowIndex() > 0 && cell.getColumnIndex() == 1) {
cellDataList.forEach(c -> {
if(c != null) {
String cellString = c.toString();
if (cellString.matches("^[0-9]{3}$")) {
sheetName = cellString;
}
}
});
}
if(cell.getRowIndex() > 0 && cell.getColumnIndex() == 2) {
cellDataList.forEach(c -> {
if(c != null) {
CreationHelper helper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
Hyperlink hyperlink = helper.createHyperlink(HyperlinkType.DOCUMENT);
hyperlink.setAddress("#"+ sheetName +"!A"+ 1 +"");
cell.setHyperlink(hyperlink);
Font font = workbook.createFont();
font.setUnderline(Font.U_SINGLE);
font.setColor(IndexedColors.BLUE.getIndex());
cellStyle.setFont(font);
}
});
}
}
/**
* 实际中如果直接获取原单元格的样式进行修改, 最后发现是改了整行的样式, 因此这里是新建一个样* 式
*/
private CellStyle createStyle(Workbook workbook) {
CellStyle cellStyle = workbook.createCellStyle();
// 下边框
cellStyle.setBorderBottom(BorderStyle.THIN);
// 左边框
cellStyle.setBorderLeft(BorderStyle.THIN);
// 上边框
cellStyle.setBorderTop(BorderStyle.THIN);
// 右边框
cellStyle.setBorderRight(BorderStyle.THIN);
// 垂直对齐方式
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return cellStyle;
}
}
easyExcel 常用注解
@ExcelProperty
@ColumnWith 列宽
@ContentFontStyle 文本字体样式
@ContentLoopMerge 文本合并
@ContentRowHeight 文本行高度
@ContentStyle 文本样式
@HeadFontStyle 标题字体样式
@HeadRowHeight 标题高度
@HeadStyle 标题样式
@ExcelIgnore 忽略项
@ExcelIgnoreUnannotated 忽略未注解
@ExcelProperty 必要的一个注解,注解中有三个参数value,index分别代表列明,列序号 value和index只能二选一,通常不用设置converter 1.value 通过标题文本对应 2.index 通过文本行号对应 @ExcelProperty(value = "人员姓名",index = 1) private String comparisonName;
@ColumnWith 设置列宽度,只有一个参数value,value的单位是字符长度,最大可以设置255个字符,因为一个excel单元格最大可以写入的字符个数就是255个字符
public class ImeiEncrypt { @ColumnWidth(value = 18) private String imei; }
@ContentFontStyle 用于设置单元格内容字体格式的注解
参数 含义 fontName 字体名称 fontHeightInPoints 字体高度 italic 是否斜体 strikeout 是否设置删除水平线 color 字体颜色 typeOffset 偏移量 underline 下划线 bold 是否加粗 charset 编码格式
@ContentStyle
设置内容格式注解
参数 含义
dataFormat 日期格式
hidden 设置单元格使用此样式隐藏
locked 设置单元格使用此样式锁定
quotePrefix 在单元格前面增加`符号,数字或公式将以字符串形式展示
horizontalAlignment 设置是否水平居中
wrapped 设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
verticalAlignment 设置是否垂直居中
rotation 设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
indent 设置单元格中缩进文本的空格数
borderLeft 设置左边框的样式
borderRight 设置右边框样式
borderTop 设置上边框样式
borderBottom 设置下边框样式
leftBorderColor 设置左边框颜色
rightBorderColor 设置右边框颜色
topBorderColor 设置上边框颜色
bottomBorderColor 设置下边框颜色
fillPatternType 设置填充类型
fillBackgroundColor 设置背景色
fillForegroundColor 设置前景色
shrinkToFit 设置自动单元格自动大小
@HeadFontStyle
用于定制标题字体格式
参数 含义
fontName 设置字体名称
fontHeightInPoints 设置字体高度
italic 设置字体是否斜体
strikeout 是否设置删除线
color 设置字体颜色
typeOffset 设置偏移量
underline 设置下划线
charset 设置字体编码
bold 设置字体是否加粗
@ExcelIgnore 不将该字段转换成Excel