如何读取在java中的大.xls文件?

10读取java中的xls文件。
但是当我要读取大于20 MB的大.xls文件时,它会给我错误。
以下我的代码运行正常的小.xls文件,但给大的.xls文件的Java堆错误。 Java代码 –

public static void main(String[] args) throws IOException { ArrayList<ArrayList<String>> Temp = new ArrayList<ArrayList<String>>(); ArrayList<String> Temp1 = new ArrayList<String>(); int row = 0; String fname = "D:/Vijay/xls/vijay/bookTest.xls"; try { InputStream fis = new FileInputStream(fname); HSSFWorkbook workbook = new HSSFWorkbook(fis); HSSFSheet sheet = workbook.getSheetAt(0); FormulaEvaluator formulaEval = workbook.getCreationHelper().createFormulaEvaluator(); int rowEnd = sheet.getLastRowNum(); int rowStart = sheet.getFirstRowNum(); for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) { Row r = sheet.getRow(rowNum); int lastColumn = r.getLastCellNum(); int cols = 0; Temp1 = new ArrayList<String>(); for (int cn = 0; cn < lastColumn; cn++) { String cellvalue = ""; Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL); if (c == null) { cellvalue = ""; } else { if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_STRING) { cellvalue = r.getCell(cn).getStringCellValue(); } else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(r.getCell(cn))) { DateFormat formatter = new SimpleDateFormat( "E MMM dd HH:mm:ss Z yyyy"); Date date = (Date) formatter.parse(r .getCell(cn).getDateCellValue() .toString()); Calendar cal = Calendar.getInstance(); cal.setTime(date); cellvalue = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR); } else { r.getCell(cn).setCellType( r.getCell(cn).CELL_TYPE_STRING); cellvalue = "" + r.getCell(cn).getStringCellValue(); } } else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { cellvalue = "" + r.getCell(cn).getBooleanCellValue(); } else if (r.getCell(cn).getCellType() == HSSFCell.CELL_TYPE_FORMULA) { cellvalue = "" + formulaEval.evaluate(r.getCell(cn)) .formatAsString(); } } Temp1.add(cols, cellvalue); cols++; } if (Temp1.size() > 0) { Temp.add(row, Temp1); row++; } } for (ArrayList al : Temp) { System.out.println("Contents of temp " + al); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } 

错误-

 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.LinkedHashMap.createEntry(Unknown Source) at java.util.LinkedHashMap.addEntry(Unknown Source) at java.util.HashMap.put(Unknown Source) at sun.util.resources.OpenListResourceBundle.loadLookup(Unknown Source) at sun.util.resources.OpenListResourceBundle.loadLookupTablesIfNecessary(Unknown Source) at sun.util.resources.OpenListResourceBundle.handleGetObject(Unknown Source) at sun.util.resources.TimeZoneNamesBundle.handleGetObject(Unknown Source) at java.util.ResourceBundle.getObject(Unknown Source) at java.util.ResourceBundle.getObject(Unknown Source) at java.util.ResourceBundle.getStringArray(Unknown Source) at sun.util.TimeZoneNameUtility.retrieveDisplayNames(Unknown Source) at sun.util.TimeZoneNameUtility.retrieveDisplayNames(Unknown Source) at java.util.TimeZone.getDisplayNames(Unknown Source) at java.util.TimeZone.getDisplayName(Unknown Source) at java.util.Date.toString(Unknown Source) at com.test.arrayList.ValidateXls.main(ValidateXls.java:69) 

请帮我解决这个问题,或者请build议我另一种方式来读取java中的.xls文件。
提前致谢。

我认为你需要尝试的第一件事是增加java默认堆空间。 例如:-Xms256m -Xmx512m -XX:PermSize = 64M -XX:MaxPermSize = 1000M

还需要根据poi文档将此文件的加载更改为这样(WorkbookFactory.create(new File(“MyExcel.xls”)))

http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream

文件与InputStreams

打开工作簿时,可以使用.xls HSSFWorkbook或.xlsx XSSFWorkbook,可以从File或InputStream中加载工作簿。 使用File对象可以降低内存消耗,而InputStream需要更多的内存,因为它必须缓冲整个文件。

如果使用WorkbookFactory,则使用其中一个或另一个非常简单:

//使用文件工作簿wb = WorkbookFactory.create(new File(“MyExcel.xls”));

//使用InputStream,需要更多内存Workbook wb = WorkbookFactory.create(new FileInputStream(“MyExcel.xlsx”));

如果你仍然面临同样的例外尝试使用

XSSF和SAX(事件API)

http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api