I have downloaded the excel file to sdcard. want 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
Asked
Active
Viewed 2.2k times
5
-
for down voting please mention the reason of down voting. – Jul 08 '11 at 08:34
-
1I'm not downvoting, but you should explain whether you want to launch an application for opening Excel files or whether you want to open the file in your app and process it (I'm guessing the former?) – Tony the Pony Jul 08 '11 at 08:40
-
I want to open the file in my app and process it – Jul 08 '11 at 09:19
-
try this link http://stackoverflow.com/questions/3387478/how-to-read-excel-file-using-jxl-2-6-12-jar – kannappan Jul 08 '11 at 09:08
5 Answers
11
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);

nhahtdh
- 55,989
- 15
- 126
- 162

Ankit Adlakha
- 1,436
- 12
- 15
2
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();
}

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343

Dhina k
- 1,481
- 18
- 24
2
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);
}

kenlukas
- 3,616
- 9
- 25
- 36

chetehouna
- 21
- 2
2
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()

Barmaley
- 16,638
- 18
- 73
- 146
0
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");

nhahtdh
- 55,989
- 15
- 126
- 162

Sandeep Kumar P K
- 7,412
- 6
- 36
- 40