easypoi导出xlsx类型到excel设置

easypoi导出xlsx类型到excel,本质上是底层使用的Workbook对象不一样,xlsx使用XSSFWorkbook对象,所以在构建Workbook对象的时候,需要注意使用XSSFWorkbook。
那么Workbook是如何构造出来的呢?翻看easypoi源码,如下:

public static Workbook exportBigExcel(ExportParams entity, List excelParams, IExcelExportServer server, Object queryParams) {
    ExcelBatchExportService batchServer = new ExcelBatchExportService();
    batchServer.init(entity, excelParams);
    return batchServer.exportBigExcel(server, queryParams);
}

public static Workbook exportExcel(ExportParams entity, Class pojoClass, Collection dataSet) {
    Workbook workbook = getWorkbook(entity.getType(), dataSet.size());
    (new ExcelExportService()).createSheet(workbook, entity, pojoClass, dataSet);
    return workbook;
}

private static Workbook getWorkbook(ExcelType type, int size) {
    if (ExcelType.HSSF.equals(type)) {
        return new HSSFWorkbook();
    } else {
        return (Workbook)(size < USE_SXSSF_LIMIT ? new XSSFWorkbook() : new SXSSFWorkbook());
    }
}

public static Workbook exportExcel(ExportParams entity, List entityList, Collection dataSet) {
    Workbook workbook = getWorkbook(entity.getType(), dataSet.size());
    (new ExcelExportService()).createSheetForMap(workbook, entity, entityList, dataSet);
    return workbook;
}

public static Workbook exportExcel(List> list, ExcelType type) {
    Workbook workbook = getWorkbook(type, 0);
    Iterator var3 = list.iterator();

    while(var3.hasNext()) {
        Map map = (Map)var3.next();
        ExcelExportService service = new ExcelExportService();
        service.createSheet(workbook, (ExportParams)map.get("title"), (Class)map.get("entity"), (Collection)map.get("data"));
    }

    return workbook;
}

这样看来,Workbook有很多种创建方式,其中ExcelType最为关键,ExcelType这个参数决定创建什么类型的Workbook,仔细看代码,ExcelType是ExportParams的变量,ExportParams提供了如下这些构造函数

public ExportParams(String title, String sheetName) {
    this.color = HSSFColorPredefined.WHITE.getIndex();
    this.headerColor = HSSFColorPredefined.SKY_BLUE.getIndex();
    this.type = ExcelType.HSSF;
    this.style = ExcelExportStylerDefaultImpl.class;
    this.headerHeight = 9.0D;
    this.isCreateHeadRows = true;
    this.isDynamicData = false;
    this.isAppendGraph = true;
    this.isFixedTitle = true;
    this.maxNum = 0;
    this.height = 0;
    this.title = title;
    this.sheetName = sheetName;
}

public ExportParams(String title, String sheetName, ExcelType type) {
    this.color = HSSFColorPredefined.WHITE.getIndex();
    this.headerColor = HSSFColorPredefined.SKY_BLUE.getIndex();
    this.type = ExcelType.HSSF;
    this.style = ExcelExportStylerDefaultImpl.class;
    this.headerHeight = 9.0D;
    this.isCreateHeadRows = true;
    this.isDynamicData = false;
    this.isAppendGraph = true;
    this.isFixedTitle = true;
    this.maxNum = 0;
    this.height = 0;
    this.title = title;
    this.sheetName = sheetName;
    this.type = type;
}

public ExportParams(String title, String secondTitle, String sheetName) {
    this.color = HSSFColorPredefined.WHITE.getIndex();
    this.headerColor = HSSFColorPredefined.SKY_BLUE.getIndex();
    this.type = ExcelType.HSSF;
    this.style = ExcelExportStylerDefaultImpl.class;
    this.headerHeight = 9.0D;
    this.isCreateHeadRows = true;
    this.isDynamicData = false;
    this.isAppendGraph = true;
    this.isFixedTitle = true;
    this.maxNum = 0;
    this.height = 0;
    this.title = title;
    this.secondTitle = secondTitle;
    this.sheetName = sheetName;
}

可见ExcelType默认设置成了ExcelType.HSSF,而xlsx的取值是ExcelType.XSSF,所以设置ExcelType为XSSF就可以实现xlsx文件的导出

你可能感兴趣的:(easypoi,xlsx,导出)