I am trying to read an excel file in order to fill my SQLite database with it’s data, but when I try to open the file using **OPCPackage **or FileInputStream it won’t open even the file is exist
I’m using Intent OnActivityResult to get the file path
if(resultCode == Activity.RESULT_OK){
if(resultData.getData().getPath().substring(resultData.getData().getPath().length() - 5).equals(".xlsx")) {
Uri uri = resultData.getData();
String [] pathsections = resultData.getData().getPath().split(":");
excelFilePath = Environment.getExternalStorageDirectory().getPath() + "/" + pathsections[pathsections.length-1];
}else
Toast.makeText(MainActivity.this, getString(R.string.errorProperDB), Toast.LENGTH_SHORT).show();
}
and here is my method to read file
private void readExcelData(int firstRow, int firstCol, String tableName, String year, String month) {
File file = new File(excelFilePath);
if (file.exists()) {
// Open existing workbook
try {
OPCPackage pkg = OPCPackage.open(file, PackageAccess.READ_WRITE);
//InputStream inputStream = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(pkg );
XSSFSheet sheet = workbook.getSheetAt(0);
Toast.makeText(MainActivity.this, "File Found and Read", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "File Not Read", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "File Not Found. ", Toast.LENGTH_SHORT).show();
}
}
when I call the method it shows File Not Read
, my poi apachi version is 5.3.0 and minSdk is 28
and this is the path hold by excelFilePath
/storage/emulated/0/Download/202407.xlsx
I hope you can help with it and thanks.