3

I have a script that requires an excel spreadsheet to be downloaded from a website and opened- ending in an assertion that a certain value exists or not on the spreadsheet.

Getting the following error:

java.lang.AssertionError: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at Assert.fail(Assert.java:95)

Any ideas?

0

Use file extension to handle WorkSheet Type

String inputFilename = new File(path).getName();

switch (inputFilename.substring(inputFilename.lastIndexOf(".") + 1,
   inputFilename.length())) {
      case "xls":
      return readXLS(path);

      case "xlsx":
      return readXLSX(path);
      default:
      Log.e(TAG, "No XLS file chosen");
      return "Please select valid \"Excel\" File\"";
}

For XLSX file: use XSSFWorkbook & XSSFSheet

XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(path)));

XSSFSheet sheet = workbook.getSheetAt(0);

For XLS file: use HSSFWorkbook & HSSFSheet

HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(path)));

HSSFSheet sheet = workbook.getSheetAt(0);

Refer the link for a more detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.