Search code examples
androidandroid-intentxlsxxls

Intent to read both .xls and xlsx files in Android?


My intent picks .xls while

fileintent.setType("application/vnd.ms-excel");

Picks .xlsx while

fileintent.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

How to make both .xls and .xlsx selectable and readable? Appreciate help!


Solution

  • For .xls and .xlsx extensions,

    Mimetypes are clubbed.

    Intent intent;
        
            String[] mimetypes =
                    { "application/vnd.ms-excel", // .xls
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" // .xlsx
                           };
    
            intent = new Intent(Intent.ACTION_GET_CONTENT); // or use ACTION_OPEN_DOCUMENT
            intent.setType("*/*");
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
    
    

    For other extensions:

    Check the following link, find the corresponding mimetype and include it in the above code.

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types