easypoi使用list导出word修改字体大小以及字体样式

文章介绍了如何在SpringBoot项目中使用Easypoi库来处理Word文档,特别是关注于表格内容的读取和字体设置。在依赖管理部分,列出了Easypoi的相关组件及其版本要求。在代码示例中,展示了如何遍历并修改Word表格中的字体样式,确保内容按模板设置的字体显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先导包:
 

  <!-- easypoi -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.2.0</version>
        </dependency>
        <!-- base必须在4.40以上不然,列表合成word时,表格会错乱 -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.4.0</version>
        </dependency>
        <!-- Word 需要使用 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.4</version>
            <optional>true</optional>
        </dependency>

代码:
 

                List<XWPFTableCell> tableCells = table.getRow(rowIndex).getTableCells();
                for(XWPFTableCell xwpfTableCell:tableCells){
                    //获取一格里的内容
                    List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
                    for(XWPFParagraph xwpfParagraph:paragraphs){
                        List<XWPFRun> run1=xwpfParagraph.getRuns();
                        for(XWPFRun r:run1){
                            //当只是读取文件时,以下方法就可以直接设置字体跟大小,但是注意大小只能设置整数
                            //r.setFontFamily("黑体");//字体
                            //r.setFontSize(20);//字体大小 只能传整数

                            //此方法时获取底层CTR对象,利用CTR对象来设置字体大小,可以设置为带小数的大小
                            // CTRPr ctrPr = r.getCTR().addNewRPr();
                            //ctrPr.addNewSzCs().setVal(new BigInteger(String.valueOf(30)));//字体效果大小
                            //ctrPr.addNewSz().setVal(new BigInteger(String.valueOf(30)));//正常字体大小,传入的值是磅数x2


//                            此方法就是设置字体效果为模板设置的默认效果,因为集合合成word时,如果不进行设置可能就会不管你模板设置的字体,输出Times New Roman字体
                            //获取字体样式对象
                            CTFonts tmpFonts = r.getCTR().getRPr().getRFonts();
                            CTFonts tmpFonts0 = r.getCTR().getRPr().getRFonts();
                            //获取字体样式名称
                            String fontFamily0 = r.getFontFamily();

                            tmpFonts.setHint(tmpFonts0.getHint());//设置字体样式的提示信息
                            tmpFonts.setAscii(fontFamily0);//设置西方字符
                            tmpFonts.setEastAsia(fontFamily0);//设置东亚字符
                            tmpFonts.setHAnsi(fontFamily0);//设置汉字字符
                            tmpFonts.setCs(fontFamily0);//设置复合字体样式
                            tmpFonts.unsetAsciiTheme();//清除字体样式
                            tmpFonts.unsetEastAsiaTheme();//清除字体样式
                            tmpFonts.unsetHAnsiTheme();//清除字体样式
                            
                        }

                    }
                }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值