POI XSSFSheet shiftRows bug问题解决
问题
业务中需要往给定格式的excel中写入数据。
使用shiftRows函数往excel中插入新行时,xls文件没问题,xlsx文件问题多多
- 执行如下代码,xls格式插入了3行,xlsx格式却只插入了2行
- xlsx执行shiftRows操作之后,合并单元格丢失
- 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

以上就是POI XSSFSheet shiftRows bug问题解决的详细内容,更多关于POI XSSFSheet shiftRows bug的资料请关注脚本之家其它相关文章!
相关文章
SpringBoot2.7 WebSecurityConfigurerAdapter类过期配置
这篇文章主要为大家介绍了SpringBoot2.7中WebSecurityConfigurerAdapter类过期应该如何配置,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-06-06
springboot项目中controller层与前端的参数传递方式
这篇文章主要介绍了springboot项目中controller层与前端的参数传递方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-10-10
Java调用ChatGPT API并实现流式接收方式(Server-Sent Events,SSE)
文章介绍如何在Java中通过OkHttp和SSE技术实现流式获取ChatGPT响应,解决传统HTTP阻塞问题,提升用户体验,需配置stream参数,利用SseEmitter封装后端推送,前端使用EventSourcePolyfill插件处理Token,同时注意资源管理和避免换行符干扰2025-08-08


最新评论