简体   繁体   中英

Apache POI - Excel found unreadable content in xx.xlsx

I'm trying to create an excel file from a List, the excel gets created and a the data is added to it, but, not before I recover the content. When i try to open the excel file, it says, "Excel found unreadable content in xx.xlsx".

Below is my code:

public static String createExcel( List<Localization> locs,
                                      String location,
                                      String revision,
                                      String[] columnTitles ) {

    int columns = columnTitles.length;
    FileOutputStream outputStream;
    File file = null;
    try {
        file = new File( "localization.xlsx" );
        outputStream = new FileOutputStream( file );
        XSSFWorkbook wb = new XSSFWorkbook();
   wb.getPackage().getPackageProperties().setRevisionProperty( revision );

        // create an editable cell style
        CellStyle unlockedCellStyle = wb.createCellStyle();
        unlockedCellStyle.setLocked( false );

        XSSFSheet sheet = wb.createSheet();
        sheet.lockFormatCells( false );
        sheet.lockFormatColumns( false );
        sheet.lockFormatRows( false );
        sheet.lockInsertRows( false );
        // lock the sheet for editing
        sheet.protectSheet( "password" );

        for ( int i = 0; i <= locs.size(); i++ ) {
            Row row = sheet.createRow( i );
            for ( int j = 0; j < columns; j++ ) {
                if ( i == 0 ) {
                    row.createCell( j ).setCellValue( columnTitles[j] );
                }

                else {
                    switch(j){
                        case 0:
                            row.createCell( j ).setCellValue( locs.get( i - 1 ).getId() );
                            break;
                        case 1:
                            row.createCell( j ).setCellValue( locs.get( i - 1 ).getEntityId() );
                            row.getCell( j ).setCellStyle( unlockedCellStyle );
                            break;
                        case 2:
                            row.createCell( j ).setCellValue( locs.get( i - 1 ).getValue() );
                            row.getCell( j ).setCellStyle( unlockedCellStyle );
                            break;
                        case 3:
                            row.createCell( j ).setCellValue( locs.get( i - 1 ).getLanguage().getLanguageCode() );
                            row.getCell( j ).setCellStyle( unlockedCellStyle );
                            break;
                        case 4:
                            row.createCell( j ).setCellValue( locs.get( i - 1 ).getCountry().getCountryCode() );
                            row.getCell( j ).setCellStyle( unlockedCellStyle );
                            break;
                    }
                }
            }
        }
        wb.write( outputStream );
        outputStream.close();
        wb.close();
        // file.delete();
    } catch ( Exception e ) {
        log.error( e.getMessage() );
    }
    if ( file != null )
        return file.getAbsolutePath().toString();
    else
        return null;
}

The Core properties of an Office OpenXML document are not free typeable.

So the Ecma Office Open XML File Formats Standard says for the revision property:

"The revision number. [Example: This value might indicate the number of saves or revisions, provided the application updates it after each revision. end example]"

This means the revision property can only be an integer value since it means how often the document is saved or revised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM