【问题标题】:How to open an excel file in android如何在android中打开一个excel文件
【发布时间】:2011-10-01 02:18:52
【问题描述】:

我已将 excel 文件下载到 sdcard。想在我的应用程序中打开文件并处理它。 有什么方法或意图在 android.xml 中打开一个 excel 文件。我

【问题讨论】:

  • 投反对票请注明投反对票的原因。
  • 我没有反对,但您应该说明您是要启动应用程序来打开 Excel 文件,还是要在应用程序中打开文件并处理它(我猜是前者?)
  • 我想在我的应用中打开文件并处理它

标签: android excel


【解决方案1】:
Uri path = Uri.fromFile(file);
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) {
    Toast.makeText(EmptyBlindDocumentShow.this,"No Application available to viewExcel", Toast.LENGTH_SHORT).show();
}

【讨论】:

    【解决方案2】:

    试试这个:

    public void ouvrir(View view) {
        String csvFile = "myData.xls";
    
        File yourDir = new File(Environment.getExternalStorageDirectory()+ "/CHETEHOUNA/" + csvFile);
    
        String davUrl = "ms-excel:ofv|u|" + yourDir.toString();
    
        Uri uri =  Uri.parse(davUrl);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
    

    【讨论】:

      【解决方案3】:

      你能详细说明一下吗? 如果你想使用 File 从 SD 卡中读取 excel 文件,这里是代码

      File root = Environment.getExternalStorageDirectory();
      File excelFile = new File(root, "filename.xlsx");
      

      【讨论】:

        【解决方案4】:

        使用下面提到的代码并尝试:

        File file = new File(Environment.getExternalStorageDirectory()+ "/filepath/" + filename);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),"application/vnd.ms-excel");
        startActivity(intent);
        

        【讨论】:

          【解决方案5】:

          使用这段代码可以打开任意文件(不仅仅是Excel)。

          一般的想法是基于Intent可以处理的文件mime类型,然后启动那些Intent。当然,系统可能没有任何意图来处理它,或者可能有多个意图。无论如何,这是大方向:

          获取给定文件名的 mime 类型

          public String getMimeType(String filename)
          {
              String extension = FileUtils.getExtension(filename);
              // Let's check the official map first. Webkit has a nice extension-to-MIME map.
              // Be sure to remove the first character from the extension, which is the "." character.
              if (extension.length() > 0)
              {
                  String webkitMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1));
                  if (webkitMimeType != null)
                     return webkitMimeType;
              }
            return "*/*";
           }
          

          然后获取将处理给定 mime 类型的默认意图

          public Intent getDefaultViewIntent(Uri uri)
          {
              PackageManager pm = this.getPackageManager();
              Intent intent = new Intent(Intent.ACTION_VIEW);
              // Let's probe the intent exactly in the same way as the VIEW action
              String name=(new File(uri.getPath())).getName();
              intent.setDataAndType(uri, this.getMimeType(name));
              final List<ResolveInfo> lri = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
              if(lri.size() > 0)
                  return intent;
              return null;
          }
          

          最后一步是从getDefaultViewIntent()返回的Intent开始

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-01-30
            • 1970-01-01
            • 1970-01-01
            • 2013-06-29
            • 1970-01-01
            • 2011-03-15
            • 1970-01-01
            相关资源
            最近更新 更多