大數據量excel分頁

package excel;


//import junit.framework.Assert;
import java.io.FileOutputStream;

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 class SXSSFDemo {
    public static void main(String[] args) throws Throwable {
        
        SXSSFWorkbook wb = new SXSSFWorkbook(100); // 在內存當中保持 100 行 , 超過的數據放到硬盤中
        Sheet sh = wb.createSheet();
        for(int rownum = 0; rownum < 10000; 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);
            }

        }     
        
        FileOutputStream out = new FileOutputStream("/Users/tootwo2/Documents/sxssf.xlsx");
        wb.write(out);
        out.close();

        // dispose of temporary files backing this workbook on disk
        wb.dispose();
    }

}
減少存儲在內存當中的數據,達到一定行數就存儲到硬盤的臨時文件中。避免大數據量導致出現JVM內存溢出問題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章