最近项目需要导出Excel, 而且Excel的表头不固定, 还涉及表头合并和样式调整, 以及某些列需要千位分隔符和百分比, 在Github上找到了阿里开源的Easy-Excel
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beta5</version>
</dependency>
定义需要导出的类
@ExcelProperty注解式用来标记字段在Excel中的表头, value值支持多级表头, 用一级表头一致框架自动会对表头进行合并, index是用来标记字段在Excel中的顺序(不是Excel中的位置), 因为项目实际需求中有几个字段是可选导出的, 所以index没有设置成连续的; 其次, 导出的字段中总量要求有千位分隔符, 两个满足率字段要求有百分号, 因此需要单独设置单元格样式
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class BasePurchaseExecutionResponse extends BaseRowModel {
/**
* 序号
*/
@ExcelProperty(value = {"", "", "序号"}, index = 0)
private String num;
/**
* 供应商类型
*/
@ExcelProperty(value = {"", "", "供应商类型"}, index = 1)
private String supplierType;
/**
* 品牌
*/
@ExcelProperty(value = {"", "", "品牌"}, index = 2)
private String brandNameListString;
/**
* 年份
*/
@ExcelProperty(value = {"", "", "年份"}, index = 3)
private String productYear;
/**
* 产品季节
*/
@ExcelProperty(value = {"", "", "产品季节"}, index = 4)
private String productSeason;
/**
* 总量
*/
@ExcelProperty(value = {"", "", "总量"}, index = 9)
private int totalShipment;
/**
* 计划交期满足率
*/
@ExcelProperty(value = {"", "", "计划交期满足率"}, index = 10)
private String planDeliverRate;
/**
* 确认交期满足率
*/
@ExcelProperty(value = {"", "", "确认交期满足率"}, index = 11)
private String confirmDeliverRate;
}
写一个样式类实现Easy-Excel提供的WriteHandler接口
/**
* @author Eric on 2019/4/5.
* @version 1.0
*/
public class StyleExcelHandler implements WriteHandler {
@Override
public void sheet(int i, Sheet sheet) {
}
@Override
public void row(int i, Row row) {
}
@Override
public void cell(int i, Cell cell) {
// 从第二行开始设置格式,第一行是表头
Workbook workbook = cell.getSheet().getWorkbook();
CellStyle cellStyle = createStyle(workbook);
if (cell.getRowIndex() > 2) {
if (i == 5) {
DataFormat dataFormat = workbook.createDataFormat();
// 设置千位分隔符
cellStyle.setDataFormat(dataFormat.getFormat("#,##0"));
}
if (i == 7 || i == 6) {
String stringCellValue = cell.getStringCellValue();
cell.setCellValue(new BigDecimal(stringCellValue.replaceAll("%", "")).divide(new BigDecimal(100), 8, BigDecimal.ROUND_HALF_UP).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
// 设置百分比
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
}
if (i == 0 || i == 3) {
cell.setCellValue(Long.parseLong(cell.getStringCellValue()));
}
}
cell.getRow().getCell(i).setCellStyle(cellStyle);
}
/**
* 实际中如果直接获取原单元格的样式进行修改, 最后发现是改了整行的样式, 因此这里是新建一个样* 式
*/
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.setAlignment(HorizontalAlignment.CENTER);
// 垂直对齐方式
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return cellStyle;
}
}
写一个测试类
@Test
public void test3() throws IOException {
StyleExcelHandler handler = new StyleExcelHandler();
OutputStream outputStream = new FileOutputStream("D://2007.xlsx");
// 这里要把上面创建的样式类通过构造函数传入
ExcelWriter writer = new ExcelWriter(null, outputStream, ExcelTypeEnum.XLSX, true, handler);
Sheet sheet1 = new Sheet(1, 1, BasePurchaseExecutionResponse.class, "含供应商和地区", null);
sheet1.setAutoWidth(true);
writer.write(createResponseList(), sheet1);
writer.finish();
outputStream.close();
}
/**
* 创建数据集合
*
* @return
*/
private List<? extends BaseRowModel> createResponseList() {
List<BasePurchaseExecutionResponse> responses = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
BasePurchaseExecutionResponse response = new BasePurchaseExecutionResponse().setTotalShipment(i * 1000000)
.setConfirmDeliverRate(i + "%").setAddiOtherNum(i * 100000)
.setNum(String.valueOf(i)).setProductSeason("冬").setProductYear("19")
.setSupplierType("本厂").setBrandNameListString("耐特")
.setPlanDeliverRate(i * 2 + "%");
responses.add(response);
}
return responses;
}
不足之处
- 样式的设置可维护性太差, 通过在样式类中硬编码各个列号对应的样式确实不好, 问题点在于, 单元格样式的创建需要workbook对象实例才可以, 框架本身并没有提供获取workbook的方法, 因此, 如果想要在其他地方设置样式的话, 可以通过反射的方式来获取workbook对象