An exception occurred in POI Excel. org. apache. poi. openxml4j. exceptions. invalidformatexception: package shoshould contain a c, excelcontain
The following exception occurs in POI Excel operations: org. apache. poi. openxml4j. exceptions. invalidformatexception: package shoshould contain a content type part code: public boolean parseToExcel (String oldFileName, String newFileName ){
Map <Object, Object> map = new HashMap <Object, Object> ();
Map. put (0, "FYMC ");
Map. put (1, "GXFYS ");
Map. put (2, "LS_YSJGYS ");
Map. put (4, "LS_ZDHJAJS ");
Map. put (5, "LS_CGRKAJS ");
Map. put (6, "ls_rksbjs ");
Map. put (7, "LS_XSAJS ");
Map. put (8, "LS_YJAJS ");
Map. put (9, "LS_FYS ");
Map. put (10, "SS_XSAJS ");
Map. put (11, "SS_YJAJS ");
Map. put (12, "SS_FYS ");
List <Map <String, String> parseList = getDwgroupdtbgParse ();
// Read the template path
File filePath = new File (oldFileName );
Try {
// Read the Excel template
Wrokbook dd = WorkbookFactory. create (new FileInputStream (filePath); // This sentence throws an exception
// Workbook wb = createworkbook (new FileInputStream (filePath ));
// Read all sheet contents in the template
Sheet sheet = wb. getSheetAt (0 );
For (int I = EXCEL_START_ROW; I <EXCEL_ROW_NUMS; I ++ ){
// Row row = sheet. getRow (I );
Row row = sheet. createRow (I );
For (int j = 0; j <EXCEL_CELL_NUMS; j ++ ){
Cell cell = row. createCell (j + 1 );
If (j! = NO_CREATE_CELL ){
Cell. setCellValue (String. valueOf (parseList. get (i-EXCEL_START_ROW). get (map. get (Object) j ))));
}
}
}
// Modify the template content and export the new template
FileOutputStream out = new FileOutputStream (newFileName );
Wb. write (out );
Out. close ();
Return true;
} Catch (InvalidFormatException e ){
E. printStackTrace ();
Return false;
} Catch (FileNotFoundException e ){
E. printStackTrace ();
Return false;
} Catch (IOException e ){
E. printStackTrace ();
Return false;
}
}
Solution: The workbook object is not obtained from the WorkbookFactory class. Write a method to obtain the Workbook object as follows:
/**
* The static method solves the problem of creating a Workbook.
* @ Param Indium
* @ Return
* @ Throws IOException
* @ Throws InvalidFormatException
*/
Public static Workbook createworkbook (InputStream indium) throws IOException, InvalidFormatException {
If (! Indium. markSupported ()){
Indium = new PushbackInputStream (indium, 8 );
}
If (POIFSFileSystem. hasPOIFSHeader (indium )){
Return new HSSFWorkbook (indium );
}
If (POIXMLDocument. hasOOXMLHeader (indium )){
Return new XSSFWorkbook (OPCPackage. open (indium ));
}
Throw new IllegalArgumentException ("your excel version cannot be resolved by poi currently ");
}
This is all the jar packages used for POI Excel operations.
Orgapachepoiopenxml4jexceptionsInvalidFormatException
I also encountered the same problem.
If there is no problem when operating the xls file, this error will be reported when operating the xlsx file.
My problem has been solved.
Cause: the parsing method of xls is different from that of xlsx, resulting in different classes.
Xls returns HSSFWorkbook, and xlsx returns XSSFWorkbook.
I tried one of the following methods:
HSSFWorkbook;
Try {
Book = new HSSFWorkbook (in );
} Catch (Exception ex ){
Book = new XSSFWorkbook (in );
}
The results are not satisfactory, and the error is inexplicable (I didn't understand it anyway ).
Enter the topic: the following method is positive,
1. Create a new method and return the interfaces implemented by HSSFWorkbook and XSSFWorkbook.
Public static Workbook create (InputStream indium) throws IOException, InvalidFormatException {
If (! Indium. markSupported ()){
Indium = new PushbackInputStream (indium, 8 );
}
If (POIFSFileSystem. hasPOIFSHeader (indium )){
Return new HSSFWorkbook (indium );
}
If (POIXMLDocument. hasOOXMLHeader (indium )){
Return new XSSFWorkbook (OPCPackage. open (indium ));
}
Throw new IllegalArgumentException ("your excel version cannot be resolved by poi currently ");
}
2. Call the static method to construct the new Workbook object.
Workbook book = XlsImpUtil. create (in );
3. Remember, if the workbook uses an interface, Sheet must also use an interface instead of an implementation class.
Sheet sheet = book. getSheetAt (0 );
The answer is complete. It's not easy for me to give me a score.
POI excel (2007) throw an exception.
Download the latest poi
Www.apache.org/..26.zip
Add package
Poi-3.8 \ All jar packages
Poi-3.8 \ ooxml-lib: Three jar packages