JavaPOI在解决导出大数据量的处理方案(导出数据在一百万行左右)
2018-03-30 13:54
429 查看
Excel2003版最大行数是65536行。Excel2007开始的版本最大行数是1048576行。Excel2003的最大列数是256列,2007以上版本是16384列。 poi导出excel,不使用模板的http://happyqing.iteye.com/blog/2075985 xls格式导出使用HSSFWorkbook,(这个暂时没有好办法) xlsx格式导出以前使用XSSFWorkbook,可以使用新的SXSSFWorkbook(poi3.8+) Workbook wb = new SXSSFWorkbook(1000); //大于1000行时会把之前的行写入硬盘 Row,Cell还跟之前的一样 官方样例http://poi.apache.org/spreadsheet/how-to.html#sxssfJava代码
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();
<
cd95
li style="font-size:1em;margin:0px 0px 0px 38px;padding:0px 0px 0px 10px;border-left:1px solid rgb(209,215,220);background-color:rgb(250,250,250);line-height:18px;"> 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();
}
值得注意的是SXSSFWorkbook只能写不能读。但是往往我们需要向一个Excel模版里导出数据,这样才更好提前定义里面的格式和vba代码。这里就需要使用SXSSFWorkbook的另外一个构造函数:SXSSFWorkbook(XSSFWorkbook workbook)
Construct a workbook from a template. 通过XSSFWorkbook来读取模版,然后用SXSSFWorkbook来设置样式和写数据,详细使用就参考API吧。 http://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html
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();
<
cd95
li style="font-size:1em;margin:0px 0px 0px 38px;padding:0px 0px 0px 10px;border-left:1px solid rgb(209,215,220);background-color:rgb(250,250,250);line-height:18px;"> 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();
}
值得注意的是SXSSFWorkbook只能写不能读。但是往往我们需要向一个Excel模版里导出数据,这样才更好提前定义里面的格式和vba代码。这里就需要使用SXSSFWorkbook的另外一个构造函数:SXSSFWorkbook(XSSFWorkbook workbook)
Construct a workbook from a template. 通过XSSFWorkbook来读取模版,然后用SXSSFWorkbook来设置样式和写数据,详细使用就参考API吧。 http://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFWorkbook.html
相关文章推荐
- 【前端知识点】前后端关于post请求中,对body的不同数据格式的解决处理方案实现
- 解决大批量数据导出Excel产生内存溢出的方案
- 解决大批量数据导出Excel产生内存溢出的方案
- 解决大批量数据导出Excel产生内存溢出的方案
- sql导出excel数据量过大的处理(需解决)
- sql导出excel数据量过大的处理(需解决)
- 解决大批量数据导出Excel产生内存溢出的方案
- JAVA解决大批量导出数据到excel产生内存溢出的方案
- 解决大批量数据导出Excel产生内存溢出的方案
- MYSQL在导出数据过程中行数受限问题解决方案
- 解决大批量数据导出Excel产生内存溢出的方案
- 解决大批量数据导出Excel产生内存溢出的方案
- poi分页导出,只能解决十几万数据的导出,内存问题没解决,有方案的可以留言
- POI : 解决大批量数据导出Excel产生内存溢出的方案
- 解决大批量数据导出Excel产生内存溢出的方案
- oracle 重建索引以及导出所有的索引脚本(可以解决还原数据库文件时先还原数据,在重新用脚本创建索引)
- 用sql2005导出数据到access或excel时,出现" 找不到sourceconnectionoledb"的错误 (已解决)
- PHPExcel导出大量数据超时及内存错误解决方法
- 高并发热点/单点数据_性能问题解决方案
- SQL大量数据查询的优化 及 非用like不可时的处理方案