大量数据的excel导出

对于大型excel的创建且不会内存溢出的,就只有SXSSFWorkbook了。它的原理很简单,用硬盘空间换内存(就像hash map用空间换时间一样)。

private void writeToAlarmExcel(List<Map<String, Object>> list,String alarmTime, String subCenterName,HttpServletResponse response) throws Exception {
        String excelName = subCenterName+"电气火灾预警统计表";
        response.addHeader("Content-Disposition",
                "attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xlsx"); // 设置文件的名称
        response.setContentType("application/msexcel;charset=gb2312");// 设置下载类型
        OutputStream out = response.getOutputStream();
        try {
            // 声明一个工作簿
            XSSFWorkbook wb = new XSSFWorkbook();
            SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(wb, 100);//内存中保留 1000 条数据,以免内存溢出,其余写入 硬盘
            // 创建HSSFCellStyle单元格样式
            CellStyle cellStyle = wb.createCellStyle();
            // 设置水平对齐的样式为居中对齐;
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            // 生成一个表格
            Sheet sheet = wb.createSheet("sheet1");
            // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
            Row row0 = sheet.createRow((int) 0);
            // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
            Cell cell = row0.createCell(0);
            // 设置单元格内容
            String newTime="";
            if (StringUtils.isNotBlank(alarmTime)) {
                if (alarmTime.trim().length() == 4) {// 2018
                    newTime = alarmTime+"年";
                } else if (alarmTime.trim().length() == 7) {// 2018-06
                    String[] aStrings = alarmTime.split("-");
                    newTime = Integer.parseInt(aStrings[0])+"年"+Integer.parseInt(aStrings[1])+"月";
                } else if (alarmTime.trim().length() == 10) {// 2018-07-02
                    String[] aStrings = alarmTime.split("-");
                    newTime = Integer.parseInt(aStrings[0])+"年"+Integer.parseInt(aStrings[1])+"月"+Integer.parseInt(aStrings[2])+"日";
                }
            }else {
                newTime = DateUtils.DateToString(new Date(), "yyyy年MM月");
            }
            //设置字体样式
            Font beforefont = wb.createFont();
            beforefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗提显示
            beforefont.setFontHeightInPoints((short)18);
            cellStyle.setFont(beforefont);
            cell.setCellValue(excelName);
            // 将样式应用于单元格
            cell.setCellStyle(cellStyle);
            // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
            Cell cell2 = row0.createCell(8);
            // 创建HSSFCellStyle单元格样式
            CellStyle cellStyle3 = sxssfWorkbook.createCellStyle();
            // 设置字体样式
            Font afterfont = sxssfWorkbook.createFont();
            afterfont.setFontHeightInPoints((short) 10);
            cellStyle3.setFont(afterfont);
            cell2.setCellValue("(统计时间:" + newTime + ")");
            cell2.setCellStyle(cellStyle3);
            Row row1 = sheet.createRow((int) 1);
            //设置字体样式
            CellStyle cellStyle2 = wb.createCellStyle();
            Font font2 = wb.createFont();
            font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
            font2.setFontHeightInPoints((short)11);
            cellStyle2.setFont(font2);
            row1.createCell(0).setCellValue("序号");
            row1.getCell(0).setCellStyle(cellStyle2);
            row1.createCell(1).setCellValue("单位");
            row1.getCell(1).setCellStyle(cellStyle2);
            row1.createCell(2).setCellValue("预警设施");
            row1.getCell(2).setCellStyle(cellStyle2);
            row1.createCell(3).setCellValue("预警地址");
            row1.getCell(3).setCellStyle(cellStyle2);
            row1.createCell(4).setCellValue("预警次数");
            row1.getCell(4).setCellStyle(cellStyle2);
            row1.createCell(5).setCellValue("首次预警时间");
            row1.getCell(5).setCellStyle(cellStyle2);
            row1.createCell(6).setCellValue("最后一次预警时间");
            row1.getCell(6).setCellStyle(cellStyle2);
            row1.createCell(7).setCellValue("预警状态");
            row1.getCell(7).setCellStyle(cellStyle2);
            int rowNum = 2;
            int num = 1;
            for (Map<String, Object> b : list) {
                int cellNo = 0;
                row1 = sheet.createRow(rowNum);
                row1.createCell(cellNo++).setCellValue(num++);
                row1.createCell(cellNo++).setCellValue(b.get("unitName") == null ? "" : b.get("unitName").toString());
                row1.createCell(cellNo++)
                        .setCellValue(b.get("deviceName") == null ? "" : b.get("deviceName").toString());
                row1.createCell(cellNo++)
                        .setCellValue(b.get("deviceAddress") == null ? "" : b.get("deviceAddress").toString());
                row1.createCell(cellNo++).setCellValue(b.get("warnNum") == null ? "" : b.get("warnNum").toString());
                row1.createCell(cellNo++)
                        .setCellValue(b.get("firstWarnTime") == null ? "" : b.get("firstWarnTime").toString());
                row1.createCell(cellNo++)
                        .setCellValue(b.get("lastWarnTime") == null ? "" : b.get("lastWarnTime").toString());
                row1.createCell(cellNo++)
                        .setCellValue(b.get("operationState") == null ? "" : b.get("operationState").toString());
                rowNum++;
            }
            wb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("写入失败!");
        }
    }
View Code
原文地址:https://www.cnblogs.com/skj0330insn/p/10147571.html