安卓无法读取文件

发布于 2024-12-04 06:22:19 字数 1672 浏览 3 评论 0原文

我在我的项目中使用这段代码。我需要阅读我放在原始文件夹中的 .xls。 ReadExcel 测试 = new ReadExcel();

    test.setInputFile(BitmapFactory.decodeStream(getClass().getResourceAsStream(("/SPPDashProject/res/raw/aging_busket_key.xls"))).toString());
    try {
        test.read();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();

我应该给出什么路径

public class ReadExcel {

    private String inputFile;

    public void setInputFile(String inputFile) {
        this.inputFile = inputFile;
    }

    public void read() throws IOException  {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over first 10 column and lines

            for (int j = 0; j < sheet.getColumns(); j++) {
                for (int i = 0; i < sheet.getRows(); i++) {
                    Cell cell = sheet.getCell(j, i);
                    CellType type = cell.getType();
                    if (cell.getType() == CellType.LABEL) {
                        System.out.println("I got a label: "
                                + cell.getContents());
                    }

                    if (cell.getType() == CellType.NUMBER) {
                        System.out.println("I got a number "
                                + cell.getContents());
                    }

                }
            }
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }


}

,因为我的主要读取类采用字符串格式的路径。请建议

I am using this code in my project. I need to read .xls which i have placed in my raw folder.
ReadExcel test = new ReadExcel();

    test.setInputFile(BitmapFactory.decodeStream(getClass().getResourceAsStream(("/SPPDashProject/res/raw/aging_busket_key.xls"))).toString());
    try {
        test.read();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();

}

public class ReadExcel {

    private String inputFile;

    public void setInputFile(String inputFile) {
        this.inputFile = inputFile;
    }

    public void read() throws IOException  {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over first 10 column and lines

            for (int j = 0; j < sheet.getColumns(); j++) {
                for (int i = 0; i < sheet.getRows(); i++) {
                    Cell cell = sheet.getCell(j, i);
                    CellType type = cell.getType();
                    if (cell.getType() == CellType.LABEL) {
                        System.out.println("I got a label: "
                                + cell.getContents());
                    }

                    if (cell.getType() == CellType.NUMBER) {
                        System.out.println("I got a number "
                                + cell.getContents());
                    }

                }
            }
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }


}

what path should i give as my main read class takes path in string format.Plz suggest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-12-11 06:22:19

您可以使用以下代码:

    Uri uri=Uri.parse("android.resource://com.mypackage.myapp" + R.raw.MyXLS);
    String filePath=uri.getPath();

You can use the following code:

    Uri uri=Uri.parse("android.resource://com.mypackage.myapp" + R.raw.MyXLS);
    String filePath=uri.getPath();
━╋う一瞬間旳綻放 2024-12-11 06:22:19

您应该使用以下代码打开 /res/raw
中的文件
getResources().openRawResource(R.raw.yourfilename)
您可以将文件放在资产文件夹中并使用 AssetManager 来访问它。

You should use the following code to open file in the /res/raw
getResources().openRawResource(R.raw.yourfilename)
You can put your file in the assets folder and use AssetManager to acess it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文