Java 导出Excel xlsx、xls, CSV文件

通用导出功能:

  1.支持Excel xlsx、xls

  2.支持CSV文件导出

  3.数据库查询分页导出、内存导出

       4.支持大批量数据导出

使用步骤如下

导入jar

<dependency>
    <groupId>com.github.catdou</groupId>
    <artifactId>common-export</artifactId>
    <version>1.3</version>
</dependency>

导出方式一:List 数据导出

1.创建每一列对应的po字段映射关系

public ExportParam buildUserExportParam() {
        Map<String, String> fieldColumnMap = new HashMap<>();
        fieldColumnMap.put("A", "userName");
        fieldColumnMap.put("C", "seq");
        fieldColumnMap.put("B", "passWord");
        // build setter method
        List<Method> getterMethod = ExportCommon.buildParamGetter(User.class, fieldColumnMap);
        return new ExportParam()
                .setHeader("username,password,seq")
                .setGetterMethod(getterMethod);
    }

2.导出数据到文件

csv 文件

public void testExportCsvPath() {
        String exportDir = "file" + File.separator + UUID.randomUUID().toString();
        File dirFile = new File(exportDir);
        dirFile.mkdirs();
        String filePath = exportDir + File.separator + "test.csv";
        ExportParam exportParam = buildUserExportParam();
        CsvExport csvExport = new CsvExport(filePath, exportParam);
        List<User> userList = createDataList(100);
        csvExport.exportList(userList);

    }

excel 文件

public void testManySheet() {
        String exportDir = "file" + File.separator + UUID.randomUUID().toString();
        File dirFile = new File(exportDir);
        dirFile.mkdirs();
        String filePath = exportDir + File.separator + "test-many.xlsx";
        ExportParam exportParam1 = buildUserExportParam();
        ExportParam exportParam2 = buildUserExportParam();
        Map<Integer, ExportParam> exportParamMap = new HashMap<>(16);
        exportParamMap.put(0, exportParam1);
        exportParamMap.put(1, exportParam2);
        ExcelMultiSheetExport excelMultiSheetExport = new ExcelMultiSheetExport(filePath, null,
                false, exportParamMap);
        List<User> userList = createDataList(Constants.EXCEL_MAX_ROW_XLSX * 3);
        excelMultiSheetExport.exportListByParamIndex(userList, 0);
        excelMultiSheetExport.exportListByParamIndex(userList, 1, true);
    }

导出方式二:数据获取方法导出

数据量比较大的情况,这时候需要分页查询导出,需要设置查询方法,符合条件数据的总条数

public void testExcel2007() {
        ExportParam exportParam = buildUserExportParam();
        String exportDir = "file" + File.separator + UUID.randomUUID().toString();
        File dirFile = new File(exportDir);
        dirFile.mkdirs();
        String filePath = exportDir + File.separator + "test.xlsx";
        List<User> userList = createDataList(Constants.EXCEL_MAX_ROW_XLSX * 2);
        BaseExport baseExport = new ExcelExport(filePath, null, false, exportParam);
        baseExport.exportList(userList);
    }


    public void testManySheet() {
        String exportDir = "file" + File.separator + UUID.randomUUID().toString();
        File dirFile = new File(exportDir);
        dirFile.mkdirs();
        String filePath = exportDir + File.separator + "test-many.xlsx";
        ExportParam exportParam1 = buildUserExportParam();
        ExportParam exportParam2 = buildUserExportParam();
        Map<Integer, ExportParam> exportParamMap = new HashMap<>(16);
        exportParamMap.put(0, exportParam1);
        exportParamMap.put(1, exportParam2);
        ExcelMultiSheetExport excelMultiSheetExport = new ExcelMultiSheetExport(filePath, null,
                false, exportParamMap);
        List<User> userList = createDataList(Constants.EXCEL_MAX_ROW_XLSX * 3);
        excelMultiSheetExport.exportListByParamIndex(userList, 0);
        excelMultiSheetExport.exportListByParamIndex(userList, 1, true);
    }

项目地址

https://github.com/CatDou/common-export

如果大家有好的想法,fork代码到你的仓库,然后pull request.

posted @ 2020-04-25 12:20  shootercheng  阅读(1129)  评论(0编辑  收藏  举报