我尝试在android中读取包含80k单元格的xls文件,但是出现以下错误消息:FATAL EXCEPTION:main java.lang.OutOfMemoryError
为了成功读取xls文件,我该怎么做?
提前致谢, Anestis
public class ReadExcelFile
{
ArrayList<String> list = new ArrayList<String>();
public ReadExcelFile()
{
Workbook workbook1 = null;
WorkbookSettings s = null;
InputStream excelContent = null;
try {
excelContent = this.getClass().getResourceAsStream("/com/product/sopho/lexiko.xls");
s = new WorkbookSettings();
s.setUseTemporaryFileDuringWrite(true);
workbook1 = Workbook.getWorkbook(excelContent,s);
Sheet sheet = workbook1.getSheet(0);
ExcelMap(sheet);
ExcelKeyList(sheet);
} catch(Exception e) {
} finally {
if(workbook1 != null) {
workbook1.close();
}
if(excelContent != null) {
try {
excelContent.close();
} catch (IOException ex) {}
}
}
}
private void ExcelKeyList(Sheet sheet)
{
for (int i=0; i<80001; i++)
{
list.add(sheet.getCell(0,i).getContents());
}
}
public ArrayList<String> getExcelKeyList()
{
return list;
}
}
答案 0 :(得分:0)
您的手机/平板电脑容量超过了。只是尝试以块的形式加载单元格,如下所示:
public class ReadExcelFile {
ArrayList<String> list = new ArrayList<String>();
Workbook myWorkBook;
// open but not close, leave it open
public void OpenExcelFile()
{
WorkbookSettings s = null;
InputStream excelContent = null;
try {
excelContent = this.getClass().getResourceAsStream("/com/product/sopho/lexiko.xls");
s = new WorkbookSettings();
s.setUseTemporaryFileDuringWrite(true);
myWorkbook = Workbook.getWorkbook(excelContent,s);
} catch(Exception e) {
}
// we leave it open
}
// close when you are done
public void closeExcelFile() {
if(myWorkBook != null) {
myWorkBook.close();
}
}
// Once opened, get chunks calling ExcelKeyList(0,0,1000) .... etc
private ArrayList<String> ExcelKeyList(int sheet,int startRow, int endRow)
{
Sheet sheet = myWorkBook.getSheet(sheet);
ExcelMap(sheet);
ExcelKeyList(sheet);
list.clear();
for (int i=startRow; i<endRow; i++)
{
list.add(sheet.getCell(0,i).getContents());
}
return list;
}
public ArrayList<String> getExcelKeyList()
{
return list;
}
}
你必须在手机爆炸之前找出推荐的块大小。