我正在尝试从列表创建 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;
}
最佳答案
Core properties Office OpenXML 文档的内容不能自由键入。
因此 Ecma Office Open XML 文件格式标准对于 revision
属性的规定:
"The revision number. [Example: This value might indicate the number of saves or revisions, provided the application updates it after each revision. end example]"
这意味着 revision
属性只能是整数值,因为它表示文档保存或修改的频率。
关于java - Apache POI - Excel 在 xx.xlsx 中发现不可读的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39158862/