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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM