50,528
社区成员
发帖
与我相关
我的任务
分享
public void readExcel() {
try {
InputStream is = getResources().getAssets().open("android_read.xls");
Workbook book = Workbook.getWorkbook(is);
int num = book.getNumberOfSheets();
main_tv2.setText("the num of sheets : " + num+ "\n");
// 获得第一个工作表对象
Sheet sheet1 = book.getSheet(0);
int Rows1 = sheet1.getRows(); //行
int Cols1 = sheet1.getColumns(); //列
jsonArray_single = new JSONArray();
JSONObject jsonObject = null;
//excel sheet.getCell(列,行)
for (int i = 0; i < Rows1; i++) {
jsonObject = new JSONObject();
for (int j = 0; j < Cols1; j++) {
// getCell(Col,Row)获得单元格的值
jsonObject.put((i+1)+"行"+(j+1)+"列", sheet1.getCell(j,i).getContents());
}
jsonArray_single.put(jsonObject); //最后拿到的jsonArray
}
System.out.println(jsonArray_single.get(0)); //元素顺序不确定 ??? 3,7,1,6,5,2,4 why?
System.out.println(jsonArray_single.get(1)); //元素顺序不确定 ??? 6,4,3,2,5,1,7 why?
book.close();
} catch (Exception e) {
System.out.println(e);
}
}