如何读取一个巨大的Excel文件的前n行

所以我试图编写一个程序,扫描一个Excel文件的行中的特定模式。 也就是说,对于N后跟任何字母,然后是S或T(每个字母占据一个单元格)。

问题是,我使用的excel文件非常庞大,大约有3000行和近1000列。 我试图仅在前60行search此模式,以减lessJava堆空间。 我怎样才能适合我的algorithm来做到这一点? 我仍然在摆脱内存exception。

我的代码如下:

import java.awt.List; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelReader { public int Reader(File file) throws IOException, EncryptedDocumentException, InvalidFormatException { FileInputStream fis = new FileInputStream(file); String filepath = file.getPath(); Workbook wb = WorkbookFactory.create(new File(filepath)); XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0); XSSFRow row; XSSFCell cell; ArrayList<Integer> list = new ArrayList<Integer>(); int rows; int cols = 0; int temp = 0; rows = sheet.getPhysicalNumberOfRows(); for (int i = 0; i < 10 || i < 60; i++) { row = sheet.getRow(i); if (row != null) { temp = sheet.getRow(i).getPhysicalNumberOfCells(); if (temp > cols) cols = temp; } } for (int r = 0; r <= 60; r++) { row = sheet.getRow(r); if (row != null) { for (int c = 0; c <= cols; c++) { int numblanks = 0; cell = row.getCell((short) c); if (cell != null) { //System.out.print(cell + "\t\t"); } else { //System.out.print("\t\t"); } if (cell != null && cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { if ("N".equals(cell.getStringCellValue())) { for (int k = c; k <= cols; k++) { if ("-".equals(row.getCell(k).getStringCellValue())) { numblanks++; continue; } if ("S".equals(row.getCell(c + 2 + numblanks).getStringCellValue()) || "T".equals(row.getCell(c + 2 + numblanks).getStringCellValue())) { list.add((int) sheet.getRow(1).getCell(c).getNumericCellValue()); break; } } } } } System.out.println(); } } System.out.println(); System.out.println("Rows: " + rows); System.out.println("Columns: " + cols); System.out.println(list); return temp; } }