Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Exception(org.apache.poi.openxml4j.exception - no content type [M1.13]) when reading xlsx file using Apache POI?
    primarykey
    data
    text
    <p>i'm using Apache POI(XSSF API) for reading xlsx file.when i tried to read file.i got the following error:</p> <pre><code>org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] </code></pre> <p>Code:</p> <pre><code>public class ReadXLSX { private String filepath; private XSSFWorkbook workbook; private static Logger logger=null; private InputStream resourceAsStream; public ReadXLSX(String FilePath) { logger=LoggerFactory.getLogger("ReadXLSX"); this.filepath=FilePath; resourceAsStream = ClassLoader.getSystemResourceAsStream(filepath); } public ReadXLSX(InputStream fileStream) { logger=LoggerFactory.getLogger("ReadXLSX"); this.resourceAsStream=fileStream; } private void loadFile() throws FileNotFoundException, NullObjectFoundException { if(resourceAsStream==null) throw new FileNotFoundException("Unable to locate give file.."); else { try { workbook = new XSSFWorkbook(resourceAsStream); } catch(IOException ex) { } } }// end loadxlsFile public String[] getSheetsName() { int totalsheet=0;int i=0; String[] sheetName=null; try { loadFile(); totalsheet=workbook.getNumberOfSheets(); sheetName=new String[totalsheet]; while(i&lt;totalsheet) { sheetName[i]=workbook.getSheetName(i); i++; } } catch (FileNotFoundException ex) { logger.error(ex); } catch (NullObjectFoundException ex) { logger.error(ex); } return sheetName; } public int[] getSheetsIndex() { int totalsheet=0;int i=0; int[] sheetIndex=null; String[] sheetname=getSheetsName(); try { loadFile(); totalsheet=workbook.getNumberOfSheets(); sheetIndex=new int[totalsheet]; while(i&lt;totalsheet) { sheetIndex[i]=workbook.getSheetIndex(sheetname[i]); i++; } } catch (FileNotFoundException ex) { logger.error(ex); } catch (NullObjectFoundException ex) { logger.error(ex); } return sheetIndex; } private boolean validateIndex(int index) { if(index &lt; getSheetsIndex().length &amp;&amp; index &gt;=0) return true; else return false; } public int getNumberOfSheet() { int totalsheet=0; try { loadFile(); totalsheet=workbook.getNumberOfSheets(); } catch (FileNotFoundException ex) { logger.error(ex.getMessage()); } catch (NullObjectFoundException ex) { logger.error(ex.getMessage()); } return totalsheet; } public int getNumberOfColumns(int SheetIndex) { int NO_OF_Column=0;XSSFCell cell = null; XSSFSheet sheet=null; try { loadFile(); //load give Excel if(validateIndex(SheetIndex)) { sheet = workbook.getSheetAt(SheetIndex); Iterator rowIter = sheet.rowIterator(); XSSFRow firstRow = (XSSFRow) rowIter.next(); Iterator cellIter = firstRow.cellIterator(); while(cellIter.hasNext()) { cell = (XSSFCell) cellIter.next(); NO_OF_Column++; } } else throw new InvalidSheetIndexException("Invalid sheet index."); } catch (Exception ex) { logger.error(ex.getMessage()); } return NO_OF_Column; } public int getNumberOfRows(int SheetIndex) { int NO_OF_ROW=0; XSSFSheet sheet=null; try { loadFile(); //load give Excel if(validateIndex(SheetIndex)) { sheet = workbook.getSheetAt(SheetIndex); NO_OF_ROW = sheet.getLastRowNum(); } else throw new InvalidSheetIndexException("Invalid sheet index."); } catch (Exception ex) { logger.error(ex);} return NO_OF_ROW; } public String[] getSheetHeader(int SheetIndex) { int noOfColumns = 0;XSSFCell cell = null; int i =0; String columns[] = null; XSSFSheet sheet=null; try { loadFile(); //load give Excel if(validateIndex(SheetIndex)) { sheet = workbook.getSheetAt(SheetIndex); noOfColumns = getNumberOfColumns(SheetIndex); columns = new String[noOfColumns]; Iterator rowIter = sheet.rowIterator(); XSSFRow Row = (XSSFRow) rowIter.next(); Iterator cellIter = Row.cellIterator(); while(cellIter.hasNext()) { cell = (XSSFCell) cellIter.next(); columns[i] = cell.getStringCellValue(); i++; } } else throw new InvalidSheetIndexException("Invalid sheet index."); } catch (Exception ex) { logger.error(ex);} return columns; }//end of method public String[][] getSheetData(int SheetIndex) { int noOfColumns = 0;XSSFRow row = null; XSSFCell cell = null; int i=0;int noOfRows=0; int j=0; String[][] data=null; XSSFSheet sheet=null; try { loadFile(); //load give Excel if(validateIndex(SheetIndex)) { sheet = workbook.getSheetAt(SheetIndex); noOfColumns = getNumberOfColumns(SheetIndex); noOfRows =getNumberOfRows(SheetIndex)+1; data = new String[noOfRows][noOfColumns]; Iterator rowIter = sheet.rowIterator(); while(rowIter.hasNext()) { row = (XSSFRow) rowIter.next(); Iterator cellIter = row.cellIterator(); j=0; while(cellIter.hasNext()) { cell = (XSSFCell) cellIter.next(); if(cell.getCellType() == cell.CELL_TYPE_STRING) { data[i][j] = cell.getStringCellValue(); } else if(cell.getCellType() == cell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { String formatCellValue = new DataFormatter().formatCellValue(cell); data[i][j] =formatCellValue; } else { data[i][j] = Double.toString(cell.getNumericCellValue()); } } else if(cell.getCellType() == cell.CELL_TYPE_BOOLEAN) { data[i][j] = Boolean.toString(cell.getBooleanCellValue()); } else if(cell.getCellType() == cell.CELL_TYPE_FORMULA) { data[i][j] = cell.getCellFormula().toString(); } j++; } i++; } // outer while } else throw new InvalidSheetIndexException("Invalid sheet index."); } catch (Exception ex) { logger.error(ex);} return data; } public String[][] getSheetData(int SheetIndex,int noOfRows) { int noOfColumns = 0; XSSFRow row = null; XSSFCell cell = null; int i=0; int j=0; String[][] data=null; XSSFSheet sheet=null; try { loadFile(); //load give Excel if(validateIndex(SheetIndex)) { sheet = workbook.getSheetAt(SheetIndex); noOfColumns = getNumberOfColumns(SheetIndex); data = new String[noOfRows][noOfColumns]; Iterator rowIter = sheet.rowIterator(); while(i&lt;noOfRows) { row = (XSSFRow) rowIter.next(); Iterator cellIter = row.cellIterator(); j=0; while(cellIter.hasNext()) { cell = (XSSFCell) cellIter.next(); if(cell.getCellType() == cell.CELL_TYPE_STRING) { data[i][j] = cell.getStringCellValue(); } else if(cell.getCellType() == cell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { String formatCellValue = new DataFormatter().formatCellValue(cell); data[i][j] =formatCellValue; } else { data[i][j] = Double.toString(cell.getNumericCellValue()); } } j++; } i++; } // outer while }else throw new InvalidSheetIndexException("Invalid sheet index."); } catch (Exception ex) { logger.error(ex); } return data; } </code></pre> <p>please help me to sort out this problem.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload