ENOENT(没有那个文件或目录)Apache Poi Android

问题描述 投票:0回答:0

我尝试使用 Apache Poi lib 访问文件 .xlsx,但出现错误

java.io.FileNotFoundException:assets/test.xlsx:打开失败:ENOENT(没有这样的文件或目录)

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();
        }
    }
}

我也尝试用 AssetManager 做这个但是我得到错误

org.apache.poi.ooxml.POIXMLException:错误:启用“命名空间”功能时不支持“命名空间前缀”功能。

我也尝试用 AssetManager 做这个但是我得到错误

org.apache.poi.ooxml.POIXMLException:错误:启用“命名空间”功能时不支持“命名空间前缀”功能。

添加后

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

结果是

java.io.FileNotFoundException: xlsx.xlsx

build.gradle 是

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'
}
java android apache-poi xlsx
© www.soinside.com 2019 - 2024. All rights reserved.