开发者

How to open an excel file in android

开发者 https://www.devze.com 2023-03-18 00:29 出处:网络
I have downloaded the excel file to sdcard.want开发者_StackOverflow社区 to open the file in my app and process it.

I have downloaded the excel file to sdcard. want开发者_StackOverflow社区 to open the file in my app and process it. Is there any way or any intent to open an excel file in android. I


Use the below mentioned code and try:

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


Use this piece of code which can be used to open arbitrary file (not only Excel).

General idea is to get based on file mime type which Intent can handle it and then start those Intent. For sure it may happen that system doesn't have any intents to handle it or may have several Intents. Anyway here's general direction:

Get mime type for given filename

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 "*/*";
 }

Then get default intent which will handle given mime type

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

And as final step just start Intent returned by getDefaultViewIntent()


Try this:

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


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


Can you elaborate? If you want to read excel file from SD card using File, here is the code

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

精彩评论

暂无评论...
验证码 换一张
取 消