Apache POI - Excel在xx.xlsx中找到了不可读的内容

时间:2016-08-26 05:30:05

标签: java excel apache-poi

我试图从List创建一个excel文件,创建excel并将数据添加到它,但是,在我恢复内容之前。当我尝试打开excel文件时,它说," Excel在xx.xlsx"中找到了不可读的内容。

以下是我的代码:

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;
}

1 个答案:

答案 0 :(得分:2)

Office OpenXML文档的Core properties不可自由输入。

所以Ecma Office Open XML File Formats Standard对$("#button1")属性说:

  

&#34;修订号。 [示例:此值可能表示数字   保存或修订,只要应用程序在每个之后更新它   修订。结束例子&#34;

这意味着$("#button2")属性只能是一个整数值,因为它表示文档保存或修改的频率。