zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    消息服务实时消费设备状态变化和数据
    消息服务实时消费设备状态变化和数据
    连载33:软件体系设计新方向:数学抽象、设计模式、系统架构与方案设计(简化版)(袁晓河著)...
    Java利用Zxing生成二维码
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/shootercheng/p/12772358.html
Copyright © 2011-2022 走看看