如何从java或android中的XLS(Excel)文件中读取数据

时间:2015-08-08 08:02:07

标签: java android excel xls

我正在使用JExcelApi v2.6.12 jar文件。 http://www.andykhan.com/jexcelapi/download.html

我有一个xml文件,如下图所示:

enter image description here

现在,我想从这个文件中获取所有行:

我关注此答案How to read data from XLS (Excel) file [Java, Android]

但它不起作用,我的结果总是返回"Data not found..!"

我的代码:

private void test(){
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/SmsBaz";
    File sdIconStorageDir = new File(iconsStoragePath);

        String filePath = sdIconStorageDir.toString() +"/m.xls";

    try {
        List<String> resultSet =  read("id",filePath);
        Log.e("size","=>"+resultSet.get(0));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public List<String> read(String key,String inputFile) throws IOException {
    List<String> resultSet = new ArrayList<String>();

    File inputWorkbook = new File(inputFile);
    if(inputWorkbook.exists()){
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over column and lines
            for (int j = 0; j < sheet.getRows(); j++) {
                Cell cell = sheet.getCell(0, j);
                if(cell.getContents().equalsIgnoreCase(key)){
                    for (int i = 0; i < sheet.getColumns(); i++) {
                        Cell cel = sheet.getCell(i, j);
                        resultSet.add(cel.getContents());
                    }
               }
                continue;
            }
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else
    {
        resultSet.add("File not found..!");
    }
    if(resultSet.size()==0){
        resultSet.add("Data not found..!");
    }
    return resultSet;
}

0 个答案:

没有答案