本文实例为大家分享了android读取assets中Excel的具体代码,供大家参考,具体内容如下
1.在assets下放的excel表格,命名为excel.xls
2.添加读取excel需要的jar包”jxl.jar”,assets是建在main级别目录下,建错地方找不到文件
3.读取excel中的文件
/*获取excel表格中的数据不能在主线程中调用 xlsName 为表格的名称 index 表示第几张表格 */ public ArrayListgetExcelData(String xlsName, int index) { ArrayList 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
4.更新UI
public class ExcelDataAsyncTask extends AsyncTask> { 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 doInBackground(String... params) { return getExcelData(params[0], index); } @Override protected void onPostExecute(ArrayList 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(ArrayListlist); }
Java代码:
new ExcelDataAsyncTask(ExcelSecondActivity.this, mp3Id, new ExcelDataAsyncTask.ExcelData() { @Override public void getData(final ArrayListlist) { ExcelAdapter adapter = new ExcelAdapter(list, ExcelSecondActivity.this); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<&#63;> parent, View view, int position, long id) { } }); } }).execute("excel.xls");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。