如何在android中打开一个excel文件
我已经下载了Excel文件到SD卡。 想要在我的应用程序中打开文件并处理它。 有任何方式或任何意图在Android中打开Excel文件。 一世
使用下面提到的代码,并尝试:
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);
使用这段代码可以用来打开任意文件(不仅仅是Excel)。
一般的想法是获取基于文件MIMEtypes的Intent
可以处理它,然后启动这些Intent
。 可以肯定的是,系统可能没有任何意图来处理它,或者可能有几个意图。 无论如何,这里的总体方向是:
获取给定文件名的MIMEtypes
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 "*/*"; }
然后得到默认的意图,这将处理给定的MIMEtypes
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
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(); }
或更多的参考请点击这里http://androiddhina.blogspot.in/2015/07/how-to-view-excel-in-android-application.html
不知道它是否在Android上工作(这可能是你需要的矫枉过正),但我想我会列出Apache POI – 用于处理Microsoft Office文档(包括Excel)的Java API。
你能详细点吗? 如果你想使用File从SD卡读取excel文件,这里是代码
File root = Environment.getExternalStorageDirectory(); File excelFile = new File(root, "filename.xlsx");