How to read data from XLS (Excel) file in java or

2019-08-11 02:20发布

I'm using JExcelApi v2.6.12 jar file. http://www.andykhan.com/jexcelapi/download.html .

I have a xml file like below image :

enter image description here

now,I would like to get all row from this file:

I'm following this answer How to read data from XLS (Excel) file [Java, Android]

but it doesn't works and my result always returns "Data not found..!"

my codes:

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条回答
登录 后发表回答