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 = 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.
An exception occurred in the Excel operation in POI. org. Apache. Poi. openxml4j. Exceptions. invalidformatexception: Package shoshould contain a C.