- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个包含 1,300,000 条记录的数组。每条记录本身就是一个数组。我读取了数组的每条记录,并将该记录的每个存储桶插入到 Excel 工作表的一行的单元格中,最后,我将该 Excel 工作表写入 Excel 文件。写入 100k 条记录后,它变得越来越慢,然后在最后中断。我使用 POI apache 来完成它,这是我的代码,我不确定是什么原因导致写入过程变慢了那么多。有什么提示吗?
try {
//save to excel file
FileOutputStream out = new FileOutputStream(new File(path));
XSSFWorkbook resultWorkBook = new XSSFWorkbook();
XSSFSheet sheet = resultWorkBook.createSheet("Comparison_result");
int sizeOfOriginalTermMain = 0;
int sizeOfOriginalTermMatch = 0;
//blue cell style
CellStyle blueStyle = resultWorkBook.createCellStyle();
XSSFFont cellFont = resultWorkBook.createFont();
cellFont.setColor(IndexedColors.BLUE.getIndex());
blueStyle.setFont(cellFont);
//yellow bg cell style
CellStyle GreenStyle = resultWorkBook.createCellStyle();
GreenStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());
//create heading
Row heading = sheet.createRow(0);
heading.createCell(0).setCellValue("Main List ID");
heading.createCell(1).setCellValue("Match number > 0");
heading.createCell(2).setCellValue("Found Match ID");
heading.createCell(3).setCellValue("Source list: 2");
heading.createCell(4).setCellValue("Matched Trems");
for(int i=0; i<5;i++) {
CellStyle styleRowHeading = resultWorkBook.createCellStyle();
XSSFFont font = resultWorkBook.createFont();
font.setBold(true);
font.setFontName(XSSFFont.DEFAULT_FONT_NAME);
font.setFontHeightInPoints((short)11);
styleRowHeading.setFont(font);
heading.getCell(i).setCellStyle(styleRowHeading);
}
ArrayList<Object> currentList = new ArrayList<Object>();
RecordId mainRecordId = new RecordId();
String mainRecordIdValue = "";
LinkedHashSet<String> commonStrings = new LinkedHashSet<String>();
int numberOfMatch=0;
RecordId matchRecordId = new RecordId();
String matchRecordIdValue = "";
int size = processResult.size();
int matchRecordIdListNumber = 0;
String concatenatedMatchTerms = "";
ArrayList<String> OrininalTemrsInMainList = new ArrayList<String>();
ArrayList<String> OrininalTemrsInMatchList = new ArrayList<String>();
//adding value to each row of the excel sheet
int q= 0;
for (int i = 0; i < size; i++) {
currentList = processResult.get(i);
Row row = sheet.createRow(i+1);
//object ppmsID column
Cell mainIdCell = row.createCell(0);
mainRecordId = (RecordId)(currentList.get(0));
mainRecordIdValue = mainRecordId.getIdValue();
mainIdCell.setCellValue(mainRecordIdValue);
mainIdCell.setCellStyle(blueStyle);
//productDB column
Cell matchNumberCell = row.createCell(1);
commonStrings = (LinkedHashSet<String>)(currentList.get(2));
numberOfMatch = commonStrings.size();
matchNumberCell.setCellValue(Integer.toString(numberOfMatch));
//match record Id
Cell matchIdCell = row.createCell(2);
matchRecordId = (RecordId)(currentList.get(1));
matchRecordIdValue = matchRecordId.getIdValue();
matchRecordIdListNumber = matchRecordId.getListNumber();
matchIdCell.setCellValue(matchRecordIdValue);
Cell sourceListNumber = row.createCell(3);
sourceListNumber.setCellValue(Integer.toString(matchRecordIdListNumber));
//terms of match
Cell matchTerms = row.createCell(4);
concatenatedMatchTerms = getConcatenatedStringFromList(commonStrings);
matchTerms.setCellValue(concatenatedMatchTerms);
OrininalTemrsInMainList = (ArrayList<String>) currentList.get(3);
sizeOfOriginalTermMain = OrininalTemrsInMainList.size();
OrininalTemrsInMatchList = (ArrayList<String>) currentList.get(4);
sizeOfOriginalTermMatch = OrininalTemrsInMatchList.size();
for (int k = 0; k<sizeOfOriginalTermMain;k++) {
Cell newCell = row.createCell(5+k);
newCell.setCellValue(OrininalTemrsInMainList.get(k));
newCell.setCellStyle(blueStyle);
}
Cell emptyCell = row.createCell(5+sizeOfOriginalTermMain);
emptyCell.setCellValue("emptyCell");
emptyCell.setCellStyle(GreenStyle);
for (int n = 0; n<OrininalTemrsInMatchList.size();n++) {
Cell newCell = row.createCell(5+sizeOfOriginalTermMain+1+n);
newCell.setCellValue(OrininalTemrsInMatchList.get(n));
}
}
resultWorkBook.write(out);
out.close();
resultWorkBook.close();
}catch(Exception e) {
System.out.println(e.getMessage());
}
最佳答案
不要使用 XSSF
创建包含这么多单元格的电子表格。XSSF
依赖于消耗大量内存的对象。
改为使用 SXSSF
那是一个 Streaming Usermodel API。
SXSSF (package: org.apache.poi.xssf.streaming) is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document. Older rows that are no longer in the window become inaccessible, as they are written to the disk.
将使用 XSSF
的代码更新为使用 SXSSF
是小菜一碟。
两件重要的事情:
窗口大小(内存中可访问的行数):使用默认值或在合适的情况下显式配置
You can specify the window size at workbook construction time via new SXSSFWorkbook(int windowSize) or you can set it per-sheet via SXSSFSheet#setRandomAccessWindowSize(int windowSize)
When a new row is created via createRow() and the total number of unflushed records would exceed the specified window size, then the row with the lowest index value is flushed and cannot be accessed via getRow() anymore.
The default window size is 100 and defined by SXSSFWorkbook.DEFAULT_WINDOW_SIZE.
清理要求
SXSSF allocates temporary files that you must always clean up explicitly, by calling the dispose method.
它应该被调用:
SXSSFWorkbook.dispose();
所以你应该这样写:
SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
// write rows ...
...
// dispose of temporary files backing this workbook on disk
wb.dispose();
关于 SXSSF
限制:
Due to the streaming nature of the implementation, there are the following limitations when compared to XSSF:
Only a limited number of rows are accessible at a point in time.
Sheet.clone() is not supported.
Formula evaluation is not supported
关于您损坏的文件:
According to official SXSSF
limitations ,如果您不依赖公式计算,则损坏的 excel 文件的原因可能与 SXSSF
模型无关。
在尝试任何操作之前,您可以更新到最新的稳定 POI 版本。
然后,很难给出具体的指示,但作为一般建议,隔离事物以尝试理解到底发生了什么。
您可以从减少生成的行数并仅处理一些特定的列开始,看看是否能解决问题。
如果不行,你也可以使用默认样式进行测试。
关于java - 将许多记录写入 excel 文件会变得非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104804/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!