13

I'm working on an Android app in which I have to open & close excel files on button click. These excel files will be readonly. After closing the excel file, it should direct me to the app.

Please suggest me a way to do this.

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56

1 Answers1

24

Android 7.0 Update:

Android 7.0 will throw FileUriExposedException if you try to open your app document with an external app. You need to implement FileProvider refer This Answer.


here is a manual route.

Using JExcelApi in an Android App

How to read excel file using JXL 2.6.12 jar


but here is a little more easier one.

open application

but i guess you have to find out the MIME TYPE.

EDIT

got the mime type as well

Setting mime type for excel document

UPDATE

so something like this might work.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


try {
    startActivity(intent);
} 
catch (ActivityNotFoundException e) {
    Toast.makeText(OpenDoc.this, "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
}
Samuel
  • 9,883
  • 5
  • 45
  • 57
  • @Samuel Not working Android 7.0 please update answer – demo Oct 24 '17 at 18:08
  • @Samuel Android app in which I have to open excel files on button click...same above code is working upto marshmellow 6.0.....But in android 7.1 nougat it's not opening when i click,,,Please help me sir,,,, – demo Oct 25 '17 at 06:49
  • I Was tried this code `Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(path, "application/vnd.ms-excel"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); try { startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(OpenDoc.this, "No Application Available to View Excel", Toast.LENGTH_SHORT).show(); }` – demo Oct 25 '17 at 06:54
  • @Samuel Please try to help sir...i checked `moto E4 Plus` – demo Oct 25 '17 at 07:00
  • @Mariyappan, check the exception it may be an `FileUriExposedException` if so refer the update, else you will have better visibility if you post a question with the exception you are facing. – Samuel Oct 25 '17 at 08:13