3.POI SXSSF导出大量数据实例

来源:互联网 发布:ubuntu镜像下载地址 编辑:程序博客网 时间:2024/05/15 00:00

1.下面的示例编写了一个包含100行窗口的工作表。 当行计数达到101时,将rownum = 0的行刷新到磁盘并从内存中删除,当rownum达到102时,则刷新rownum = 1的行等。

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.util.CellReference;import org.apache.poi.xssf.streaming.SXSSFSheet;import org.apache.poi.xssf.streaming.SXSSFWorkbook;import org.junit.Test;import com.esen.util.StrFunc;/** * 下面的示例编写了一个包含100行窗口的工作表。  * 当行计数达到101时,将rownum = 0的行刷新到磁盘并从内存中删除, * 当rownum达到102时,则刷新rownum = 1的行 * */public class SXSSF {    @Test    public void Test() throws Exception {        long start1 = System.currentTimeMillis();        // keep 100 rows in memory, exceeding rows will be flushed to disk        SXSSFWorkbook wb1 = new SXSSFWorkbook(100);        for (int sheetnum = 0; sheetnum < 20; sheetnum++) {            Sheet sh1 = wb1.createSheet();            for(int rownum = 0; rownum < 50000; rownum++){                Row row1 = sh1.createRow(rownum);                for(int cellnum = 0; cellnum < 10; cellnum++){                    Cell cell1 = row1.createCell(cellnum);                    String address1 = new CellReference(cell1).formatAsString();                    cell1.setCellValue(address1);                }            }        }        FileOutputStream out1 = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\sxssf1.xlsx");        wb1.write(out1);        out1.close();        wb1.dispose();        System.out.println("for循环内定义对象,耗时:" + StrFunc.formatTime(System.currentTimeMillis()-start1));        // for循环内定义对象,耗时:116698毫秒        // for循环内定义对象,耗时:118691毫秒        // for循环内定义对象,耗时:121567毫秒        // for循环内定义对象,耗时:117721毫秒        // for循环内定义对象,耗时:13048毫秒}
  1. 下一个示例关闭自动刷新(windowSize = -1),代码手动控制部分数据如何写入磁盘
    /**     * 关闭自动刷新(windowSize = -1)     * 代码控制将部分满足要求的数据写入磁盘     * @throws Exception      * */    @Test    public void TestPartData () throws Exception {        /**         * turn off auto-flushing and accumulate all rows in memory         * windowSize = -1表示关闭自动刷新         * 所有的行都存放在内存中         * */         SXSSFWorkbook wb = new SXSSFWorkbook(-1); //         Sheet sh = wb.createSheet();        Row row = null;        Cell cell = null;        String address = null;        for(int rownum = 0; rownum < 10000; rownum++){            row = sh.createRow(rownum);            for(int cellnum = 0; cellnum < 10; cellnum++){                cell = row.createCell(cellnum);                address = new CellReference(cell).formatAsString();                cell.setCellValue(address);            }            /**             * manually control how rows are flushed to disk              * 手动控制行如何刷新到磁盘             * */             if(rownum % 100 == 0) {                /**                 * retain 100 last rows and flush all others                 * 保留最后一行并刷新所有其他行                 * */                ((SXSSFSheet)sh).flushRows(100);                 /**                 * ((SXSSFSheet)sh).flushRows() is a shortcut for ((SXSSFSheet)sh).flushRows(0),                 * this method flushes all rows                 * ((SXSSFSheet)sh).flushRows()是((SXSSFSheet)sh).flushRows(0)的快捷方式,                 *  此方法flushRows()刷新所有行                 * */            }        }        FileOutputStream out = new FileOutputStream("/temp/sxssf.xlsx");        wb.write(out);        out.close();        /**         * dispose of temporary files backing this workbook on disk         * 处理在磁盘上备份此工作簿的临时文件         * SXSSF分配临时文件,您必须始终清除显式,通过调用dispose方法          * */         wb.dispose();    }
0 0