In the code below I'm trying to read a xls file and do whatever is necessary classes through the JXL api, but when converting to Workbook.getWorkbook (dbInputStream) exception occurs
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;
}
}
Any idea how to fix this problem?
Thanks!
Edit
Below is the exception
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)
If I put the above code in a java project works normally!