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

标签 java excel apache-poi

我正在尝试从列表创建 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/

相关文章:

java - 即使从服务器中删除后,数据仍显示在房间数据库中

vba - Excel VBA 类型不匹配 #N/A

Java:根据整数是奇数还是偶数将整数放在双端队列中的第一个或最后一个

java - 使用 secp256r1 曲线 (BouncyCaSTLe) 生成长度为 127 的签名的 ECDSA 签名

vba - Excel VBA-错误处理和字符串搜索

perl - 如何使用 Perl 和 Excel OLE 将工作表复制到新的或现有的工作簿?

java - 将100万条记录写入一个excel文件

java - Apache POI : Issue during setting foreground color in conditionalFormatting with SOLID_FILL pattern

java - Apache POI 是否支持对单词进行两次签名?

java - 多维数组可以在java中如何处理它们中的两种不同类型