Search code examples
javaandroidapache-poixlsx

ENOENT (No such file or directory) Apache Poi Android


I try access the file .xlsx with Apache Poi lib, but I get an error

java.io.FileNotFoundException: assets/test.xlsx: open failed: ENOENT (No such file or directory)

package com.example.apachepoi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

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;

import java.io.FileInputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.textView);

        readingFile(textView);
    }
    public void readingFile(TextView textView) {
        try {
            FileInputStream fileInputStream = new FileInputStream("assets/test.xlsx");
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            Row row = sheet.getRow(0);
            Cell cell = row.getCell(0);
            String cellValue = cell.getStringCellValue();
            textView.setText(cellValue);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

also i try do this with AssetManager but then i get error of

org.apache.poi.ooxml.POIXMLException: error: The 'namespace-prefix' feature is not supported while the 'namespaces' feature is enabled.

after adding

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false);

result was

java.io.FileNotFoundException: xlsx.xlsx

build.gradle is

android { 
...
    compileSdk 33    
        defaultConfig {
            ...
            minSdk 26
            targetSdk 33
            ...
        }
}
dependencies {

    implementation 'org.apache.poi:poi:4.1.2'
    implementation 'org.apache.poi:poi-ooxml:4.1.2'
}

Solution

  • I found the answer. I just needed change version library to 5.2.3 in the dependencies.

    dependencies {
    
        implementation 'org.apache.poi:poi:5.2.3'
        implementation 'org.apache.poi:poi-ooxml:5.2.3'
    }