感觉 sheet.setDefaultColumnStyle 存在 bug

场景是已经有模板,有表头,获取模板后还需要动态增加一些表头,在使用 org.apache.poi 的包导出 excel 的时候,我用了 sheet.setDefaultColumnStyle 来设置动态表头整列的格式为文本,这个也是参考了网上的很多办法,但是一直没生效

java 复制代码
Sheet sheet = workbook.getSheetAt(0);
Row headRow = sheet.getRow(1);

CellStyle headStyle = workbook.createCellStyle();
headStyle.setAlignment(HorizontalAlignment.CENTER);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);

// 以下是网上找到的办法
// CellStyle textCellStyle = workbook.createCellStyle();
// textCellStyle.setDataFormat(STRING_FORMAT);
// 设置整列为文本格式 防止读取异常
// DataFormat textDataFormat = workbook.createDataFormat();
// headStyle.setDataFormat(textDataFormat.getFormat("@"));

int index = 15;
for (DynamicFieldConf conf : configList) {  // DynamicFieldConfResp 是我自己的动态累
    sheet.setColumnWidth(index, 5120);
    sheet.setDefaultColumnStyle(index, textCellStyle);   // 这个可能是工具的bug,没有生效 后面考虑优化
    Cell cell = headRow.createCell(index);
    cell.setCellStyle(headStyle);
    cell.setCellType(CellType.STRING);
    // cell.setCellType(Cell.CELL_TYPE_STRING);
    if (conf .getMust() == 1) {
        cell.setCellValue("*" + conf .getName());
    } else {
        cell.setCellValue(conf .getName());
    }
    index ++;
}

后来我妥协了,我想了另外一个愚蠢的办法,就是事先在模板上将固定表头后面的其他很多列先手动将单元格格式设置成文本,然后再去读取渲染动态表头,但是其实如果直接动态从0到1生成一个 excel 上面的代码还是有效果的,所以我怀疑是因为我读取了现有的 excel 再去动态生成,也不知道是不是

相关推荐
v***91301 天前
Spring boot创建时常用的依赖
java·spring boot·后端
代码or搬砖1 天前
MyBatisPlus讲解(二)
java·mybatis
lcu1111 天前
Java 学习42:抽象
java
Mr.朱鹏1 天前
RocketMQ安装与部署指南
java·数据库·spring·oracle·maven·rocketmq·seata
雨中飘荡的记忆1 天前
Spring表达式详解:SpEL从入门到实战
java·spring
Coder-coco1 天前
个人健康管理|基于springboot+vue+个人健康管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·mysql·论文
5***26221 天前
Spring Boot问题总结
java·spring boot·后端
xkroy1 天前
Spring Boot日志
java·spring boot·后端
n***F8751 天前
【Spring Boot】SpringBoot自动装配-Import
java·spring boot·后端
〝七夜5691 天前
JVM内存结构
java·开发语言·jvm