19

I have searched stackoverflow but I didn't find a clear answer. How can I read data from particular rows and columns of XLS file to my Android application? How can I read XLS file? I don't want to convert it to CSV because I get errors when I try to convert them.

Maybe I could use this http://www.andykhan.com/jexcelapi/tutorial.html#reading but I even don't know how could I import it into my project. Please help.

0

2 Answers 2

17

Hi you just need to include an external jxl jar and you can go through the same tutorial which will help you understand the process of reading excel files.. for your referance i am pasting some ref. code which reads very first sheet of excel and creates a resultset.

    public List<String> read(String key) 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;
}
Sign up to request clarification or add additional context in comments.

11 Comments

The problem is that I have already imported that file, but I get an error that class source is not found...I don't understand why the question was down voted.
can you share the exact error which you are getting? also just one suggestion, whenever you raise a question, mention your problem very precisely(with errors and/or code if you think it is required). as all new questions gets reviewed by other friends on Stackoverflow, if someone finds your question improper/incomplete they will vote down. but let us not worry about that and try getting a solution to your problem because thats more important.
I just copied it into the LIBS folder and it is working (previously I have imported it and it was not working). I need to open file A that is in folder B (B is in the package main folder). This is not working: workBook = Workbook.getWorkbook(new File("/B/A.xls")); Any idea?
what is the error? also where exactly the file is and where is your source code file, are they in the same folder/package?
where exactly the file is and where is your source code file, are they in the same folder/package?
|
1
private void printlnToUser(Cell str) {
    final Cell string = str;
    if (output.length() > 8000) {
        CharSequence fullOutput = output.getText();
        fullOutput = fullOutput.subSequence(5000, fullOutput.length());
        output.setText(fullOutput);
    }
    output.append(string + "\n");
}

public void ReadXLSX(File path) {
    try {
        FileInputStream fis = new FileInputStream(path);
        XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator<Row> rowIterator = mySheet.iterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        break;
                    default:
                }
                printlnToUser(cell);
            }
        }
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2 Comments

Hi! Please, specify the library (name and version) you are using ( I suppose iText) and add some description to the code
Library I'm using is apache poi "aa-poi-3.10-min-0.1.5.jar" and "aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5.jar" and imported library are import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.