数据库超10万数据导出Excel

ps: 首先科普一下基础知识

  Excel 2003及以下的版本。一张表最大支持65536行数据,256列。也就是说excel2003完全不可能满足百万数据导出的需求。

  Excel 2007-2010版本。一张表最大支持1048576行,16384列;

 

 

POI的官网。http://poi.apache.org/spreadsheet/index.html

  官方提到自POI3.8版本开始提供了一种SXSSF的方式,用于超大数据量的操作。于是...

  原文:

  SXSSF is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced

...

 

// 创建一个空的 Excel 表格
        String sheetName = "Sheet1";
        //创建workbook
        XSSFWorkbook hWorkbook = new XSSFWorkbook();
        //新建文件
        FileOutputStream fileOutputStream = null;
        try {
            hWorkbook.createSheet(sheetName);
// 文件路径
            fileOutputStream = new FileOutputStream(addr + makeId + ".xlsx");
            hWorkbook.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 空表的路径
        XSSFWorkbook workbook1 = new XSSFWorkbook(new FileInputStream(new File(addr + makeId + ".xlsx")));
        SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook1, 100);
        Sheet first = sxssfWorkbook.getSheetAt(0);
        //序号
        int num = 1;
        //设置单元格样式居中 这个不知道为什么居中无效
        CellStyle style = sxssfWorkbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        for (int i = 0; i < dataMap.size(); i++) {
            Map<String, Object> resultMap = labelMakeResultMap.get(i);
            Row row = first.createRow(i);
            if (i == 0) {
                row.createCell(0).setCellStyle(style);
                row.createCell(0).setCellValue("序号");
                // 表头
                

            } else {
                row.createCell(0).setCellStyle(style);
                row.createCell(0).setCellValue(num++);
                // 数据
            }
        }
// 这个必须和前面的文件路径一致,否则不好使
        FileOutputStream out = new FileOutputStream(addr+ makeId + ".xlsx");
        sxssfWorkbook.write(out);
        out.close();
        //返回流 前台下载
        return new FileInputStream(addr+ makeId + ".xlsx");

 

添加自定义路径空表,用此方法,必须为空表,如有数据,则报错,目前未找到解决方法。

 

转载自:

https://www.cnblogs.com/zou90512/p/3989450.html

 

后自己修改部分代码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值