最近項目需要導出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對象
導出的Excel