每当我调用getExcelData()
方法时,我都会java.lang.OutOfMemoryError: Java heap space error
。如你所见,我的代码中也没有任何循环。但仍面临问题。
ExcelLibrary exlb= new ExcelLibrary();
customerId+=exlb.getExcelData("ListCustomer", 1, 0);
而且,上课
public class ExcelLibrary {
String filepath;
public ExcelLibrary()
{
filepath="C:\\Users\\Use Me\\Desktop\\Test Data.xlsx";
}
public Sheet getSheet(String sheetName) throws InvalidFormatException, IOException
{
FileInputStream fis= new FileInputStream(filepath);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet(sheetName);
return sh;
}
public String getExcelData(String sheetName, int rowNo,int colNo) throws InvalidFormatException, IOException
{
String value="";
Sheet sh=getSheet(sheetName);
Row row=sh.getRow(rowNo);
Cell cell=row.getCell(colNo,Row.CREATE_NULL_AS_BLANK);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" + cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
value = "STRING value=" + cell.getStringCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
value = "FORMULA value=" + cell.getCellFormula();
break;
case Cell.CELL_TYPE_BLANK:
Reporter.log("No values Exist for the cell",true);
value="";
break;
case Cell.CELL_TYPE_ERROR:
Reporter.log("Cell Value Format Error",true);
value="error";
break;
default:
break;
}
Reporter.log("CELL col=" + cell.getColumnIndex() + " VALUE=" + value, true);
return value;
}
答案 0 :(得分:0)
如果您使用的是32位架构,则可以将堆增加到4GB。 64位架构可以更高。如果选择的值太高,JVM将无法启动并发出问题警告。
无论如何,要使用例如为堆保留的6 GB启动程序,请使用以下-X
选项:
java -Xmx6g myprogram