本文实例为大家分享了android读取assets中Excel的具体代码,供大家参考,具体内容如下
1.在assets下放的excel表格,命名为excel.xls
2.添加读取excel需要的jar包”jxl.jar”,assets是建在main级别目录下,建错地方找不到文件
3.读取excel中的文件
public ArrayList<ExcelBean> getExcelData(String xlsName, int index) {
ArrayList<ExcelBean> list = new ArrayList<>();
//获取文件管理器
AssetManager manager = context.getAssets();
try {
Workbook workbook = Workbook.getWorkbook(manager.open(xlsName));
Sheet sheet = workbook.getSheet(index);
//表格一共有多少行
int sheetRows = sheet.getRows();
//将数据添加到集合中
for (int i = 0; i < sheetRows; i++) {
ExcelBean bean = new ExcelBean();
//获取列的数据
bean.setChinese(sheet.getCell(0, i).getContents());
bean.setEnglish(sheet.getCell(1, i).getContents());
bean.setSpell(sheet.getCell(2, i).getContents());
list.add(bean);
}
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
4.更新UI
public class ExcelDataAsyncTask extends AsyncTask<String, Void, ArrayList<ExcelBean>> {
private Context context;
private int index;
private ExcelData excelData;
public ExcelDataAsyncTask(Context context, int index, ExcelData excelData) {
this.context = context;
this.index = index;
this.excelData = excelData;
}
@Override
protected ArrayList<ExcelBean> doInBackground(String... params) {
return getExcelData(params[0], index);
}
@Override
protected void onPostExecute(ArrayList<ExcelBean> excelBeen) {
super.onPostExecute(excelBeen);
if (excelBeen != null && excelBeen.size() > 0) {
// ExcelAdapter adapter=new ExcelAdapter(excelBeen,context);
// lv.setAdapter(adapter);
excelData.getData(excelBeen);
}
}
/利用接口回调,更新UI
public interface ExcelData {
void getData(ArrayList<ExcelBean> list);
}
Java代码:
new ExcelDataAsyncTask(ExcelSecondActivity.this, mp3Id, new ExcelDataAsyncTask.ExcelData() {
@Override
public void getData(final ArrayList<ExcelBean> list) {
ExcelAdapter adapter = new ExcelAdapter(list, ExcelSecondActivity.this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}).execute("excel.xls");
您可能感兴趣的文章:Android加载Assets目录中Xml布局文件Android复制assets文件到SD卡Android实现复制Assets文件到SD卡详解vue2.0 资源文件assets和static的区别iOS开发之AssetsLibrary框架使用详解Android studio 添加assets文件夹的方法详解Vue-cli中的静态资源管理(src/assets和static/的区别)vue2.0 资源文件assets和static的区别详解Android开发实现读取Assets下文件及文件写入存储卡的方法Android获取其他应用中的assets资源