【问题标题】:Android open XLSX fileAndroid打开XLSX文件
【发布时间】:2019-04-03 17:54:43
【问题描述】:

我正在尝试在我的 Android 应用中打开一个 XLSX 文件。

我知道我必须触发的 Intent 类型是 application/excel,但尽管我已经安装了 Google Sheets,但我的代码显示没有应用程序可以打开我的 excel 文件。

这是我用来触发Intent的代码:

private void openXLS(){
        File xls = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "prova.xlsx");
        Uri path = Uri.fromFile(xls);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/excel");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(context, "No Application available to view XLS", Toast.LENGTH_SHORT).show();
        }
    }

注意: prova.xlsx 存在,我可以到达并打开它。

【问题讨论】:

标签: android excel android-intent


【解决方案1】:

已解决

使用 MIME 类型 application/vnd.ms-excel、*.xls 和 *.xlsx 可以打开文件。

【讨论】:

  • 不只打开 xls 文件
【解决方案2】:
private void openXLS(final String path) {
    File file = new File(path);
    Uri uri ;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
            } else {
                uri = Uri.fromFile(file);
            }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, "application/vnd.ms-excel");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
    }
}

【讨论】:

    猜你喜欢
    • 2017-09-11
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多