在android

时间:2016-11-01 18:28:49

标签: java android excel file android-studio

我有一个程序,我想使用jxl库将一些数据发送到Excel工作表。我正在按照教程进行操作,并提供以下代码片段,我认为应该使用Sheet和一些数据创建一个.xls文件。但是,无论我给出什么样的路径,我都找不到文件错误。如果我没有输入路径,则会出现只写系统错误,尽管我在清单中有读/写内部/外部权限。如果有人可以指出错误我会很感激,因为我还是有点新手java / android。

         try {
                String exlFile = "Workbook1.xls";
                WritableWorkbook writableWorkbook = Workbook.createWorkbook(new File("/Users/xxxxxx/Documents/", exlFile));

                WritableSheet writableSheet = writableWorkbook.createSheet("Sheet1test", 0);

                //Create Cells with contents of different data types.
                //Also specify the Cell coordinates in the constructor
                Label label = new Label(0, 0, "Label (String)");
                DateTime date = new DateTime(1, 0, new Date());


                //Add the created Cells to the sheet
                writableSheet.addCell(label);
                writableSheet.addCell(date);


                //Write and close the workbook
                writableWorkbook.write();
                writableWorkbook.close();

            } catch (IOException e) {
                e.printStackTrace();
            } catch (RowsExceededException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            }

错误:

java.io.FileNotFoundException: /Users/xxxxxx/Documents/Workbook1.xls (No such file or directory)

2 个答案:

答案 0 :(得分:0)

java.io.FileNotFoundException: the system cannot find the file specified

例如,如果您将项目结构更改为ProjectRoot \ src \ resources \ word.txt,则可以使用此项:

InputStream = Hangman1.class.getResourceAsStream(“/ resources / word.txt”); BufferedReader reader = new BufferedReader(new InputStreamReader(is));

答案 1 :(得分:0)

更改

(new File("/Users/xxxxxx/Documents/", exlFile));

 (new File(Environment.getExternalStorageDirectory(), exlFile));