在下面的代码中,我试图通过JXL api读取xls文件并执行任何必要的类,但转换为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项目中正常工作!
答案 0 :(得分:2)
感谢所有试图帮助我的人!
问题是我试图读取的文件超出了android支持的大小。 excel文件有9.4 MB,似乎android文件在assets文件夹中只读取了1MB多一点。
所以解决方案我发现可能不是最好的,手动将数据从xls文件复制到txt文件,用gzip压缩这个文件,然后读取压缩文件然后读取txt文件,从而保存所有信息我数据库中需要(超过50,000条记录)! 请遵循以下代码:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
scriptChaveEloCoordenada(db);
}
public void scriptChaveEloCoordenada(SQLiteDatabase db) {
List<ChaveEloCoordenada> list = ReadExcel.read(context);
for (ChaveEloCoordenada chave : list) {
db.execSQL(insertChaveEloCoordenadas(chave.getBarramento(), chave.getCoordX(),chave.getCoordY(),chave.getPlaca(),
chave.getTipo(),chave.getSe(),chave.getAlim(),chave.getElo(),chave.getTipoElo()));
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import business.coordinate;
import android.content.Context;
public class ReadExcel {
public static List<ChaveEloCoordenada> read(Context context) {
List<ChaveEloCoordenada> list = new ArrayList<ChaveEloCoordenada>();
ChaveEloCoordenada chave = null;
String line;
int i = 0;
String split[] = null;
try {
InputStream is = context.getAssets().open("file.zip");
InputStream gzipStream = new GZIPInputStream(is);
Reader decoder = new InputStreamReader(gzipStream, "UTF-8");
BufferedReader buffered = new BufferedReader(decoder);
while((line = buffered.readLine()) != null) {
i++;
chave = new ChaveEloCoordenada();
split = line.split("\t");
chave.setBarramento(split[0]);
chave.setCoordX(split[1]);
chave.setCoordY(split[2]);
chave.setPlaca(split[3]);
chave.setTipo(split[4]);
chave.setSe(split[5]);
chave.setAlim(split[6]);
if (split.length > 7 ) {
chave.setElo(split[7]);
chave.setTipoElo(split[8]);
}
list.add(chave);
}
buffered.close();
System.out.println("TOTAL = " + i);
}
catch (final IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return list;
}
return list;
}
}
答案 1 :(得分:0)
我不认为它是因为尺寸,行
w = Workbook.getWorkbook(dbInputStream);
仅适用于File对象,而不适用于您要使用的InputStream对象