poi大数据内存导出Excel表格

1.controller层代码

long t1=System.currentTimeMillis();
        List<Map<String, Object>> list = registrationInformationService.getListMapGSXXInfoExport(param, model);
        long t2=System.currentTimeMillis();
        System.out.println("查询数据用时:"+(t2-t1)+"ms");
        String[] sortKey=new String[]{"字段A","字段B"};
        ExportXlsxUtil exportXlsxUtil =new ExportXlsxUtil();
        exportXlsxUtil.setFilename("某某表.xlsx");
        exportXlsxUtil.write(absolutePath+"/某某表..xlsx",list,3, sortKey);
        long t3=System.currentTimeMillis();
        System.out.println("数据行:"+list.size()+"数据列"+list.get(0).size());
        System.out.println("写入execl用时:"+(t3-t2)+"ms");
        System.out.println("总用时:"+t3+"ms");
        System.out.println("success");

2.ExportXlsxUtil代码

package com.wpkj.common.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;

public class ExportXlsxUtil {
    

    private String filename;

    public String getFilename() {
        return filename;
    }
    
    public void setFilename(String filename) {
        this.filename = filename;
    }
    
    /**
     *借助临时存储空间生成海量excel数据
     */
    public void write(String filePath, List<Map<String, Object>> list,Integer startRow,String[] sortKey) throws Exception {
        HttpServletResponse  resp= ServletActionContext.getResponse();
        List<List<String>> data=sortListMap(list,sortKey);
        XSSFWorkbook wb1=new XSSFWorkbook(new FileInputStream(new File(filePath)));
        SXSSFWorkbook wb = new SXSSFWorkbook(wb1,-1);
        Sheet sh = wb.getSheetAt(0);
        for(int rownum = startRow; rownum < data.size(); rownum++){
            Row row = sh.createRow(rownum);
            for(int cellnum = 0; cellnum < data.get(rownum).size(); cellnum++){
                Cell cell = row.createCell(cellnum);
                cell.setCellValue(data.get(rownum).get(cellnum));
            }
            if(rownum % 100 == 0) {
                ((SXSSFSheet)sh).flushRows(100);
            }
        }

//        FileOutputStream out = new FileOutputStream(filePath);
//        wb.write(out);
//        out.close();
//        wb.dispose();
        exportEx(wb, resp);
    }
    

    /**
     * ListMap根据
     * @param data
     */
    private List<List<String>> sortListMap(List<Map<String, Object>> datas,String[] sortKey){
        List<List<String>> _datas=new ArrayList<List<String>>();
        List<String> temp=null;
        for (int i = 0; i < datas.size(); i++) {
            temp=new ArrayList<String>();
            if (datas.get(i).getClass() == HashMap.class) {
                Map<String, Object> map=(Map<String, Object>) datas.get(i);
                for (String sort : sortKey) {
                    if (map.get(sort)!=null && !"".equals(map.get(sort).toString())) {
                        temp.add(map.get(sort).toString());
                    }else{
                        temp.add("");
                    }
                }
                _datas.add(temp);
            }
        }
        return _datas;
    }
    
    /**
     * 导出到excel
     * @param args
     * @throws UnsupportedEncodingException 
     */
    private void exportEx(SXSSFWorkbook wb,HttpServletResponse  resp) throws UnsupportedEncodingException{
        resp.setHeader("content-disposition", "attachment;filename="+new String(filename.getBytes("utf-8"), "iso-8859-1"));
        resp.setContentType("application/msexcel;charset=UTF-8");
        BufferedOutputStream write;
        
        try {
            ServletOutputStream outputStream = resp.getOutputStream();
            write = new BufferedOutputStream(resp.getOutputStream());
            wb.write(write);
            write.flush();
            write.close();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

}

这样就可以大数据导出来。不会引起jvm内存溢出的情况。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 11,275评论 0 4
  • 公元:2019年11月28日19时42分农历:二零一九年 十一月 初三日 戌时干支:己亥乙亥己巳甲戌当月节气:立冬...
    石放阅读 11,803评论 0 2
  • 今天上午陪老妈看病,下午健身房跑步,晚上想想今天还没有断舍离,马上做,衣架和旁边的的布衣架,一看乱乱,又想想自己是...
    影子3623253阅读 7,956评论 3 8