大数据量(例如几十万或者几百万的量)怎么导入到excel中

现有的方案是:

 1. 数据量达到N行(例如:100行的时候)就往磁盘中写数据(就是将结果先写到磁盘的导入文件中,最后直接导出)

     思考:前端直接导出这么大的文件,能导出吗?

实现方案:POI 解决写入excel内存溢出

POI写入excel表

下面是结构图

大数据量(例如几十万或者几百万的量)怎么导入到excel中

大批量数据读取写入的问题 

在项目中遇到二十万行数据要写入到excel中时会内存溢出,一般方法是调大tomcat的内存,但是调到2048M还是会内存溢出报错

poi官网给了一种大批量数据写入的方法

使用SXXFWorkbook 类进行大批量写入操作解决了这个问题
import junit.framework.Assert;
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.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 
    public static void main(String[] args) throws Throwable {
        SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
        Sheet sh = wb.createSheet();
        for(int rownum = 0; rownum < 1000; rownum++){
            Row row = sh.createRow(rownum);
            for(int cellnum = 0; cellnum < 10; cellnum++){
                Cell cell = row.createCell(cellnum);
                String address = new CellReference(cell).formatAsString();
                cell.setCellValue(address);
            }
 
        }
 
        // Rows with rownum < 900 are flushed and not accessible
        for(int rownum = 0; rownum < 900; rownum++){
          Assert.assertNull(sh.getRow(rownum));
        }
 
        // ther last 100 rows are still in memory
        for(int rownum = 900; rownum < 1000; rownum++){
            Assert.assertNotNull(sh.getRow(rownum));
        }
        
        FileOutputStream out = new FileOutputStream("/temp/sxssf.xlsx");
        wb.write(out);
        out.close();
 
        // dispose of temporary files backing this workbook on disk
        wb.dispose();
    }

通过设置SXXFWorkbook的构造参数,可以设置每次在内存中保持的行数,当达到这个值的时候,那么会把这些数据flush到磁盘上,这样就不会出现内存不够的情况

参考地址: http://poi.apache.org/spreadsheet/how-to.html#sxssf

转载于:https://blog.csdn.net/daiyutage/article/details/53010491

类似的方案参考:https://blog.csdn.net/weixin_39468277/article/details/79863065