[英]How to use POI library to read large excel file(.xlsx)?
我正在嘗試讀取一個大的 excel 文件。 我查看了 POI 庫,但我無法理解POI 事件庫中的代碼
我只想在更改數據類型后讀取excel文件並將每個元素寫入一個新文件,然后將其保存在數據庫中。
我也試過 sjxlsx.jar。 但是在實現時,我發現 jar 並不包含所有的類方法。
SimpleXLSXWorkbook workbook = new SimpleXLSXWorkbook(new File("C:/test.xlsx"));
HSSFWorkbook hsfWorkbook = new HSSFWorkbook();
org.apache.poi.ss.usermodel.Sheet hsfSheet = hsfWorkbook.createSheet();
Sheet sheetToRead = workbook.getSheet(0, false);
SheetRowReader reader = sheetToRead.newReader();
Cell[] row;
int rowPos = 0;
while ((row = reader.readRow()) != null) {
org.apache.poi.ss.usermodel.Row hfsRow = hsfSheet.createRow(rowPos);
int cellPos = 0;
for (Cell cell : row) {
if(cell != null){
org.apache.poi.ss.usermodel.Cell hfsCell = hfsRow.createCell(cellPos);
hfsCell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
hfsCell.setCellValue(cell.getValue());
}
cellPos++;
}
rowPos++;
}
return hsfSheet;
誰能幫助我如何使用 XSSF 和 SAX(事件 API)來讀取大型 excel 文件?
通過添加 -Xmx500m(例如)解決內存不足錯誤。 JVM 有一個默認的 max memory,實際上是比較低的。
java -Xmx500m com.yourpackage.Application
本示例將其設置為 500 兆。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.