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.

Marek
  • 3,935
  • 10
  • 46
  • 70

2 Answers2

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;
}
dev2d
  • 4,245
  • 3
  • 31
  • 54
  • 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. – Marek Jun 03 '13 at 05:08
  • 1
    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. – dev2d Jun 03 '13 at 05:09
  • 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? – Marek Jun 03 '13 at 05:16
  • what is the error? also where exactly the file is and where is your source code file, are they in the same folder/package? – dev2d Jun 03 '13 at 05:22
  • where exactly the file is and where is your source code file, are they in the same folder/package? – dev2d Jun 03 '13 at 05:27
  • in the folder PACKAGE_NAME I have all the folders like src,res and I have also folder B, inside which I have file A. Source code is in src.com.(....) – Marek Jun 03 '13 at 05:29
  • here is nice solution for the problem with readind file http://stackoverflow.com/questions/16890883/how-to-get-reference-to-a-file-inside-a-package – Marek Jun 06 '13 at 00:15
  • From android point of view, how to pass value to inputFile?? My xlsx file is located in assets/excels. – Pranav Mahajan Jan 24 '15 at 13:10
  • plz help me and other users : http://stackoverflow.com/questions/31891025/how-to-read-data-from-xls-excel-file-in-java-or-android – S.M_Emamian Aug 08 '15 at 08:22
  • Why can't I find this `Workbook.getWorkbook();` –  Aug 16 '16 at 07:54
  • You should be able to find it, check if you have properly added jxl jar file to classpath check API docs here : http://jexcelapi.sourceforge.net/resources/javadocs/current/docs/ – dev2d Aug 16 '16 at 17:24
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();
    }
}
  • Hi! Please, specify the library (name and version) you are using ( I suppose iText) and add some description to the code – Daniele Licitra Dec 14 '20 at 11:01
  • 1
    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; – Abdullah Maqbool Dec 14 '20 at 11:39