美文网首页
POI XSSFSheet shiftRows bug

POI XSSFSheet shiftRows bug

作者: 南岩飞雪 | 来源:发表于2020-11-07 15:35 被阅读0次

    问题

    业务中需要往给定格式的excel中写入数据。
    使用shiftRows函数往excel中插入新行时,xls文件没问题,xlsx文件问题多多

      1. 执行如下代码,xls格式插入了3行,xlsx格式却只插入了2行
      1. xlsx执行shiftRows操作之后,合并单元格丢失
      1. xlsx执行shiftRows操作之后,用excel打开提示格式有问题,用wps打开正常
            InputStream inp = new FileInputStream("/Users/shao/Downloads/模板.xlsx");
            Workbook templateWorkbook = WorkbookFactory.create(inp);
            Sheet sheet = templateWorkBook.getSheetAt(0);
            sheet.shiftRows(5, sheet.getLastRowNum(), 1);
            sheet.shiftRows(5, sheet.getLastRowNum(), 1);
            sheet.shiftRows(5, sheet.getLastRowNum(), 1);
            OutputStream outputStream = new FileOutputStream("/Users/shao/Downloads/模板输出.xlsx");
            templateWorkbook.write(outputStream);
    

    解决

    poi 4.1.1版本修复了该bug,升级到最新的4.1.2,问题解决

                <dependency>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                    <version>4.1.2</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                    <version>4.1.2</version>
                </dependency>
    

    原因

    poi bug

    参考

    https://stackoverflow.com/questions/55980407/apache-poi-shiftrows-corrupts-file-and-deletes-content

    http://poi.apache.org/changes.html

    image.png

    相关文章

      网友评论

          本文标题:POI XSSFSheet shiftRows bug

          本文链接:https://www.haomeiwen.com/subject/orktbktx.html