Я использую Intent OnActivityResult, чтобы получить путь к файлу
Код: Выделить всё
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();
}
Код: Выделить всё
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();
}
}
и это путь, хранящийся в excelFilePath
Код: Выделить всё
/storage/emulated/0/Download/202407.xlsx
Подробнее здесь: https://stackoverflow.com/questions/788 ... opcpackage
Мобильная версия