我在 MainActivity.java 中有以下代码行:
Workbook myWorkBook = WorkbookFactory.create(new File("C://sampleWB.xlsx"));
抛出以下错误:java.io.FileNotFoundException
我还尝试在 Assets 文件夹(app > src > main > assets)中添加excel文件,并尝试了以下代码,但抛出了相同的异常。
Workbook myWorkBook = WorkbookFactory.create(new File("sampleWB.xlsx"));
我在 AndroidManifest.xml 中添加了以下内容,但也没有解决任何问题:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
如果有任何帮助,这是我的 build.gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.test.testProj"
minSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation "org.apache.poi:poi:3.17"
implementation "org.apache.poi:poi-ooxml:3.17"
implementation 'com.fasterxml:aalto-xml:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
最佳答案
您似乎正在使用“C://sampleWB.xlsx”
作为文件名。如 this answer 中所述:
和 /
不是文件名的有效字符。
要访问 assets
文件夹中的文件,请参阅 this answer :
Place your text file in the /assets directory under the Android project and use AssetManager class as follows to access it.
AssetManager am = context.getAssets();
InputStream is = am.open("default_book.txt");
Or you can also put the file in the /res/raw directory, from where the file can be accessed by an id as follows
InputStream is =
context.getResources().openRawResource(R.raw.default_book);
关于java - 尝试在 Android 中打开 Excel 文件时不断收到 FileNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54354157/