1、准备一个excel文件,本文解析对象是一个经纬度数据,文件是 : res>raw>locations.xls ,如图。
2、解析excel 核心代码:
<pre name="code" class="java"> public static List<LocationPoint> parseExcel(InputStream in) {
List<LocationPoint> list = new ArrayList<LocationPoint>();
Workbook workbook = null;
try {
try {
workbook = Workbook.getWorkbook(in);
} catch (Exception e) {
e.printStackTrace();
}
Sheet sheet = workbook.getSheet(0);
//得到行数
//int columnCount = sheet.getColumns();
//得到列数
int rowCount = sheet.getRows();
for (int j = 1; j < rowCount; j++) {
// getCell(列,行);
int id = (int) ((NumberCell) sheet.getCell(0, j)).getValue();
String date = sheet.getCell(1, j).getContents();
double lon = ((NumberCell) sheet.getCell(2, j)).getValue();
double lat = ((NumberCell) sheet.getCell(3, j)).getValue();
LocationPoint point = new LocationPoint();
point.setLongitude(lon);
point.setLatitude(lat);
point.setId(id);
point.setLocateTime(date);
list.add(point);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
workbook.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
就本demo,解析的时候注意是从第一行开始解析的(第0行是名称),所以for循环的j初始为1。还要在finally关闭相关的资源。
3、运行效果,如图。
Demo 主要是 BaseAdapter + ListView 。总共有100条数据。