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

安稳与你 提交于 2019-12-13 01:28:47

问题


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

I have a xml file like below image :

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;
}

来源:https://stackoverflow.com/questions/31891025/how-to-read-data-from-xls-excel-file-in-java-or-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!