【问题标题】:Android can't open .xls fileAndroid 无法打开 .xls 文件
【发布时间】:2017-06-19 22:08:23
【问题描述】:

在我的应用程序中,我有一些数据想要显示到 Excel 工作表中。

几天前我已经设法构建文件 .xls。

我也可以使用此代码通过电子邮件发送它:

 FileOutputStream fos = null;
            try {
                file = new File(getContext().getFilesDir(), (main.getNomeAzienda() + "" + getDate(System.currentTimeMillis(), "dd-MM-yyyy_HH-mm")) + ".xls");
                fos = new FileOutputStream(file);
                workbook.write(fos);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.flush();
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                            Intent i = new Intent(Intent.ACTION_SEND);
                            i.setType("message/rfc822");
                            i.putExtra(Intent.EXTRA_EMAIL, new String[]{FirebaseAuth.getInstance().getCurrentUser().getEmail()});
                            i.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), "com.example.authority.fileprovider", file));
                            i.putExtra(Intent.EXTRA_SUBJECT, "Raccolto globale aSista");
                            i.putExtra(Intent.EXTRA_TEXT, "In allegato il File Excel con i dati filtrati.");
                            try {
                                startActivity(Intent.createChooser(i, "Seleziona il Client di posta che vuoi utilizzare..."));
                            } catch (android.content.ActivityNotFoundException ex) {
                                Snackbar.make(main.getFab(), "Non ci sono client di posta disponibili installati sul dispositivo.", Snackbar.LENGTH_SHORT).show();
                            }

                }
                Snackbar.make(main.getFab(), "Foglio Excel generato.", Snackbar.LENGTH_SHORT).show();

对不起,如果代码的某些部分是意大利语。

不管怎样,现在我想直接在手机上显示excel文件。

我下载了一些应该能够显示 .xls 文件的应用程序。喜欢Excelthis one from googlepolaris office

使用此代码,我调用了要求用户选择其中一个应用程序并打开文件的意图:

   Uri path = Uri.fromFile(finalFile);
                            Intent excelIntent = new Intent(Intent.ACTION_VIEW);
                            excelIntent.setDataAndType(path , "application/vnd.ms-excel");
                            excelIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            try {
                                startActivity(excelIntent);
                            }
                            catch (ActivityNotFoundException e) {
                                Snackbar.make(main.getFab(),"Impossibile aprire il file su questo dispositivo",Snackbar.LENGTH_LONG).show();
                            }

我遇到了一些与文件名相关的问题,其中不应包含一些特殊字符。然后我终于能够开始打开文件了,但现在我遇到了问题。

Excel 告诉我“无法打开文件,发生错误”。

Google 文档相同。

Polaris 不会显示错误,但会显示一个空文件。

【问题讨论】:

  • 尝试在您的ACTION_VIEW Intent 中使用FileProvider.getUriForFile(getActivity(), "com.example.authority.fileprovider", finalFile),并将您当前的setFlags() 替换为excelIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) 作为配置excelIntent 的一部分。
  • 哇,它的工作。谢谢!如果您想将其写为 aswer,我会将其标记为正确的 ;)

标签: android excel


【解决方案1】:

您的文件位于getFilesDir()。这是internal storage 的一部分,第三方应用无法访问您的内部存储部分。

由于您已经设置了FileProvider,请使用FileProvider Uri,并使用Intent.FLAG_GRANT_READ_URI_PERMISSION 授予对其他应用的临时读取权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 2019-06-14
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多