An exception occurred in the Excel operation in POI. org. Apache. Poi. openxml4j. Exceptions. invalidformatexception: Package shoshould contain a C.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.