在下面的代码中,我试图读取 xls 文件并通过 JXL api 执行任何必要的类,但是在转换为 Workbook.getWorkbook (dbInputStream) 时发生异常
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import android.content.Context;
public class ReadExcel {
public static List<ChaveEloCoordenada> read(Context context) {
List<ChaveEloCoordenada> list = new ArrayList<ChaveEloCoordenada>();
try {
InputStream dbInputStream = context.getAssets().open("file.xls", Context.MODE_WORLD_READABLE);
int cols = 9;
Cell[] row;
Cell cell;
Workbook w;
ChaveEloCoordenada chave = null;
w = Workbook.getWorkbook(dbInputStream);//error here
Sheet sheet = w.getSheet(0);
for (int r = 1; r < sheet.getRows(); r++) {
chave = new ChaveEloCoordenada();
row = sheet.getRow(r);
if (row != null) {
for (int c = 0; c < cols; c++) {
cell = sheet.getCell(c, r);
if (cell != null) {
if (c == 0) {
chave.setBarramento(cell.getContents());
} else if (c == 1) {
chave.setCoordX(cell.getContents());
} else if (c == 2) {
chave.setCoordY(cell.getContents());
} else if (c == 3) {
chave.setPlaca(cell.getContents());
} else if (c == 4) {
chave.setTipo(cell.getContents());
} else if (c == 5) {
chave.setSe(cell.getContents());
} else if (c == 6) {
chave.setAlim(cell.getContents());
} else if (c == 7) {
chave.setElo(cell.getContents());
} else if (c == 8) {
chave.setTipoElo(cell.getContents());
}
}
}
list.add(chave);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
return list;
}
}
知道如何解决这个问题吗?
谢谢!
编辑
下面是例外
java.io.IOException
at android.content.res.AssetManager.readAsset(Native Method)
at android.content.res.AssetManager.access$700(AssetManager.java:36)
at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
at jxl.read.biff.File.<init>(File.java:91)
at jxl.Workbook.getWorkbook(Workbook.java:268)
at jxl.Workbook.getWorkbook(Workbook.java:253)
如果我把上面的代码放在一个java项目中就可以正常工作了!