Ran*_*nga 33 java jdbc apache-poi
这是一种内联w/将大型ResultSet写入文件但有问题的文件是Excel文件.
我正在使用Apache POI库编写一个Excel文件,其中包含从ResultSet对象检索到的大型数据集.数据范围从几千条记录到大约一百万条; 不确定这如何转换为Excel格式的文件系统字节.
以下是我编写的测试代码,用于检查编写如此大的结果集所花费的时间以及CPU和内存的性能影响.
protected void writeResultsetToExcelFile(ResultSet rs, int numSheets, String fileNameAndPath) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileNameAndPath));
int numColumns = rs.getMetaData().getColumnCount();
Workbook wb = ExcelFileUtil.createExcelWorkBook(true, numSheets);
Row heading = wb.getSheetAt(0).createRow(1);
ResultSetMetaData rsmd = rs.getMetaData();
for(int x = 0; x < numColumns; x++) {
Cell cell = heading.createCell(x+1);
cell.setCellValue(rsmd.getColumnLabel(x+1));
}
int rowNumber = 2;
int sheetNumber = 0;
while(rs.next()) {
if(rowNumber == 65001) {
log("Sheet " + sheetNumber + "written; moving onto to sheet " + (sheetNumber + 1));
sheetNumber++;
rowNumber = 2;
}
Row row = wb.getSheetAt(sheetNumber).createRow(rowNumber);
for(int y = 0; y < numColumns; y++) {
row.createCell(y+1).setCellValue(rs.getString(y+1));
wb.write(bos);
}
rowNumber++;
}
//wb.write(bos);
bos.close();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没多少运气.创建的文件似乎快速增长(每秒约70Mb).所以我在大约10分钟后停止了执行(当文件达到7Gb时杀死了JVM)并试图在Excel 2007中打开文件.当我打开它时,文件大小变为8k(!)并且只有标题和第一个行已创建.不知道我在这里缺少什么.
有任何想法吗?
Jua*_*jas 47
使用SXSSF poi 3.8
package example;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SXSSFexample {
public static void main(String[] args) throws Throwable {
FileInputStream inputStream = new FileInputStream("mytemplate.xlsx");
XSSFWorkbook wb_template = new XSSFWorkbook(inputStream);
inputStream.close();
SXSSFWorkbook wb = new SXSSFWorkbook(wb_template);
wb.setCompressTempFiles(true);
SXSSFSheet sh = (SXSSFSheet) wb.getSheetAt(0);
sh.setRandomAccessWindowSize(100);// keep 100 rows in memory, exceeding rows will be flushed to disk
for(int rownum = 4; rownum < 100000; 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("tempsxssf.xlsx");
wb.write(out);
out.close();
}
}
Run Code Online (Sandbox Code Playgroud)
这个需要:
哦.我认为你正在编写工作簿944,000次.你的wb.write(bos)调用是在内部循环中.我不确定这是否与Workbook类的语义完全一致?从我在该类的Javadocs中可以看出,该方法将整个工作簿写出到指定的输出流.随着事情的发展,它会写出你目前为每一行添加的每一行.
这就解释了为什么你也看到了一行.要写入文件的第一个工作簿(有一行)是显示的 - 然后是7GB的垃圾.
| 归档时间: |
|
| 查看次数: |
107392 次 |
| 最近记录: |