当前位置:   article > 正文

poi excel 设置单元格富文本 粗体 斜体 无效处理_excel富文本单元格

excel富文本单元格

原因:

poi版本 4.1.2

设置RichTextString 的时候,给 font 设置  setBold(true) ; setItalic(true)  粗体 斜体 ,发现在office有效果,在wps 看不到加粗斜体效果。后来在电脑上wps新建的xlsx和程序生成的xlsx解压,对比sharedString.xml文件发现,电脑的生成的xlsx的加粗斜体是

  1. <b/>
  2. <i/>

而程序生成的加粗斜体是

  1. <b val="true"/>
  2. <i val="true"/>

程序生成的标签里多了属性val,手工删除属性替换,再以xslx表格打开,发现就有效果了

现在知道了原因了,就查看代码处理代码不生成这个属性就好了

解决方法:

方法1、简单粗暴

重写RIchTextString的以下两个方法,这两个方法除了被注释的地方改写,其他地方和父类XSSFRichTextString的一样,使用的时候就使用重写的RichTextString,并且调用append方法设置内容以及对应字体样式

方法2、其实就是 方法1中提到的append设置之后把属性抹去

  1. richTextString.applyFont(index, endIndex, font);
  2. CTRPrElt ctrElt = null;
  3. if(excelStyle.getFont() != null){
  4. if(excelStyle.getFont().isBold()){
  5. List<CTRElt> CTRElt = richTextString.getCTRst().getRList();
  6. if(CTRElt != null && CTRElt.size()>0){
  7. ctrElt = CTRElt.get(CTRElt.size() - 1).getRPr();
  8. if(ctrElt != null){
  9. List<CTBooleanProperty> d = ctrElt.getBList();
  10. if(d != null && d.size() > 0){
  11. d.get(d.size() - 1).unsetVal();
  12. }
  13. }
  14. }
  15. }
  16. if(excelStyle.getFont().isItalic()){
  17. if(ctrElt == null){
  18. List<CTRElt> CTRElt = richTextString.getCTRst().getRList();
  19. if(CTRElt != null && CTRElt.size()>0){
  20. ctrElt = CTRElt.get(CTRElt.size() - 1).getRPr();
  21. }
  22. }
  23. if(ctrElt != null){
  24. List<CTBooleanProperty> d = ctrElt.getIList();
  25. if(d != null && d.size() > 0){
  26. d.get(d.size() - 1).unsetVal();
  27. }
  28. }
  29. }
  30. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/889545
推荐阅读
  

闽ICP备14008679号