Apache POI – Excel在xx.xlsx中发现不可读的内容

我试图从一个列表创build一个Excel文件,Excel创build和数据添加到它,但不是在我恢复的内容之前。 当我尝试打开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; } 

Office OpenXML文档的核心属性不是可自由input的。

因此,Ecma Office Open XML文件格式标准针对revision属性说:

“修订版本号[例如:这个值可能表示保存或修订的数量,只要应用程序在每次修改后更新它。

这意味着revision属性只能是一个整数值,因为这意味着文档保存或修改的频率。