0

i have some problem with my android app; i want that users could upload an excel file on Firebase and populate a RecyclerView with data on this file. I don't have problem about populate the RecyclerView, i know how to do it, i have problem on read the file. I uploaded it on Firebase with this:

StorageReference stRef = storageRef.child("clienti.xlsx");
UploadTask task = stRef.putFile(file);
task.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Toast.makeText(getActivity(), "Upload error: "+exception.getMessage() , Toast.LENGTH_SHORT).show();
                }
            }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    downloadFile("clienti");

                }

It all works until now, i also download the file from firebase to a specific folder but now i don't know how to "read" it and take data from it; i have tried this, but it said to me always that the file doesn't exists.

String[][] arrays = read( Environment.getExternalStorageDirectory() + localFile.getPath());

            if(arrays == null){strHyouji="no such file";}else{

                for (String[] array : arrays) {
                    for (String v : array) {
                        strHyouji = strHyouji + v + ",";
                    }
                    strHyouji = strHyouji + "\n";
                    aList.add(strHyouji);
                }
            }


            Toast.makeText(getActivity(), strHyouji, Toast.LENGTH_SHORT).show();

Here the read() method:

Workbook workbook = null;
    try {
        WorkbookSettings ws = new WorkbookSettings();
        ws.setGCDisabled(true);



        workbook = Workbook.getWorkbook(new File(dbStr), ws);
        Sheet sheet = workbook.getSheet(0);

        int rowCount = sheet.getRows();
        String[][] result = new String[rowCount][];
        for (int i = 0; i < rowCount; i++) {
            Cell[] row = sheet.getRow(i);

            result[i] = new String[row.length];
            for (int j = 0; j < row.length; j++) {
                result[i][j] = row[j].getContents();
            }
        }
        return result;


    } catch (BiffException e) {
        strHyouji=strHyouji+ e.toString();

    } catch (IOException e) {
        strHyouji=strHyouji+ e.toString();
    } catch (Exception e) {
        strHyouji=strHyouji+ e.toString();
    } finally {
        if (workbook != null) {
            workbook.close();
        }
    }

    return null;
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
pazz98
  • 73
  • 1
  • 9

1 Answers1

0

With Apache POI, you can read the excel. Here ColumnHeaderString is the actual column header of the excel sheet and datafromcell is the value under the header converted to a String.

Reading the file

File file = new File( path ) // String file path
FileInputStream fileInputStream = new FileInputStream( file )
XSSFWorkbook workbook = new XSSFWorkbook( fileInputStream )

Using the workbook

    XSSFSheet sheet = workbook.getSheetAt( 0 );
    private DataFormatter excelDataFormatter = new DataFormatter()

    Iterator<Row> rowIterator = sheet.rowIterator();
    Row header = null;
    if( rowIterator.hasNext() )
    {
        header = rowIterator.next();
    }
    while( rowIterator.hasNext() )
    {
        Iterator<Cell> cellIterator = row.cellIterator();
        while( cellIterator.hasNext() )
        {
            Cell cell = cellIterator.next();
            int cellIndex = cell.getColumnIndex();
            String colHead = header.getCell( cellIndex ).toString();

            if( ColumnHeaderString.equals( colHead ) )  // Match the desired column header
            {
                String datafromcell = excelDataFormatter.formatCellValue( cell ).strip();
            }
        }
    }
Klaus
  • 1,641
  • 1
  • 10
  • 22
  • I have tried to use POI but i can't, do you have any example of how to add this library to my project? I mean an example of gradle – pazz98 Apr 18 '20 at 13:49
  • May be this link will help you https://stackoverflow.com/questions/39626522/unable-to-use-the-apache-poi-library-after-adding-gradle-dependency/43425074 and use the version 3.12.. i.e. `org.apache.poi:poi:3.12` – Klaus Apr 18 '20 at 13:55
  • This is due to dependency injection. Please read this https://stackoverflow.com/questions/49676155/what-does-program-type-already-present-mean and exclude the conflicting dependency. Also make sure you have only one poi dependency in your gradle file. – Klaus Apr 18 '20 at 14:23
  • ok, i have added the 3.17 version that is newest, but it doesn't recognize XSSFWorkbook and XSSFSheet – pazz98 Apr 18 '20 at 14:33
  • Please use 3.12 version, newer versions have a conflict. I experienced this working on a project and let me know if it works – Klaus Apr 18 '20 at 14:40
  • Also with 3.12 version i coudn't find XSSFWorkbook, i have found XSSFSheet like only sheet but in your code, what is workbook? an XSSFWorkbook? I have found a Workbook from poi but i can't initialize it – pazz98 Apr 18 '20 at 14:44
  • Maybe i have found the way using HSSFWorkbook instead of XSSFWorkbook... – pazz98 Apr 18 '20 at 14:56
  • Then may be the location you are searching for does not have the xlsx file. Please post your resource location String to see if it has errors – Klaus Apr 18 '20 at 14:57
  • it doesn't work, the HSSFWorkbook is too old, the error said to me that i have to use XSSFWorkbook, but it can't import it... – pazz98 Apr 18 '20 at 15:09
  • It isn't problem about location, android studio give me an error of importing the XSSFWorkbook, like i haven't added properly the library like this: error: cannot find symbol class XSSFWorkbook – pazz98 Apr 18 '20 at 15:10
  • yes, then that has to do something with the dependency, how ever, adding the above dependency in gradle with version 3.12 and doing a `./gradlew clean` should fix. – Klaus Apr 18 '20 at 15:15