如何在android中打开excel文件

如何在android中打开excel文件,android,excel,Android,Excel,我已将excel文件下载到SD卡。要在我的应用程序中打开文件并进行处理。 是否有任何方法或意图在android中打开excel文件。我你能详细说明一下吗? 如果您想使用文件从SD卡读取excel文件,下面是代码 File root = Environment.getExternalStorageDirectory(); File excelFile = new File(root, "filename.xlsx"); 使用这段代码可以打开任意文件(不仅仅是Excel) 一般的想法是基于Inte

我已将excel文件下载到SD卡。要在我的应用程序中打开文件并进行处理。 是否有任何方法或意图在android中打开excel文件。我

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

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

使用这段代码可以打开任意文件(不仅仅是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(Uri) { PackageManager pm=this.getPackageManager(); 意向意向=新意向(意向.行动\视图); //让我们以与查看操作完全相同的方式来探究意图 字符串名称=(新文件(uri.getPath()).getName(); setDataAndType(uri,this.getMimeType(名称)); 最终列表lri=pm.querytentActivities(intent,PackageManager.MATCH_DEFAULT_仅限); 如果(lri.size()>0) 返回意图; 返回null; }
最后一步,只需启动由
getDefaultViewIntent()

返回的
Intent
,使用下面提到的代码并尝试:

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);
试试这个:

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);
}

对于否决投票,请说明否决投票的原因。我不是否决投票,但您应该解释是否要启动用于打开Excel文件的应用程序,或者是否要在应用程序中打开文件并对其进行处理(我猜是前者?)
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);
}