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.

  • 相关阅读:
    HDU 4782 Beautiful Soup (模拟+注意细节)
    Linux 简单socket实现UDP通信
    Linux 简单socket实现TCP通信
    HDU 1698 Just a Hook(线段树区间覆盖)
    HDU 1271 整数对(思路题)
    HDU 2222 Keywords Search (AC自动机模板题)
    Windows平台使用Gitblit搭建Git服务器图文教程
    Git克隆
    移动端布局,div按比例布局,宽度为百分比,高度和宽度一样,即让div为正方形
    calc()问题
  • 原文地址:https://www.cnblogs.com/shootercheng/p/12772358.html
Copyright © 2011-2022 走看看