• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHPWord添加文本到指定位置

php 搞代码 4年前 (2022-01-23) 19次浏览 已收录 0个评论
文章目录[隐藏]

<body>

想导出word文档,将文本添加到文档的指定位置,比如前一段话的后面,而前一段话是循环生成的,该段后面又有循环生成的另一段话,而我想加入的文本不能放在循环里,只能在两次循环输出文本之后添加,这样的话我怎么才能把文本输出到紧接着的第一个循环之后?
例如:

我想将答案放在问题的后面,而问题和选项都是循环生成的,怎么才能实现将其放在“处分”的后面??
求大神赐教

部分代码:

<code>foreach ($ques['answer'] as $key => $an) {            $textrun1 = $section->createTextRun('rStyle');                            if (is_array($an['synopsis'])) {                                $textrun1->addText($this->aZ[$key] . ".", 'rStyle');                                foreach ($an['synopsis'] as $i) {                                    if (isset($i['type']) && $i['type'] == "text") {                                        $textrun1->addText($i['value'], 'rStyle');                                    }                                    if (isset($i['type']) && $i['type'] == "img" && file_exists($img = ROOT_DIR . str_replace(ROOT_URL, '', $i['value']))) {                                        $imageStyle = array(                                            'width' => $i['width'] == 0 ? 350 : $i['width'],                                            'height' => $i['height'] == 0 ? 350 : $i['height']                                            // 'align' => 'center',                                        );                                        $textrun1->addImage($img, $imageStyle);                                    }                                }                                unset($textrun1);                            } else {                                $section->addText(strtoupper($this->aZ[$key]) . "." . $an['synopsis'], 'rStyle');                   <div>)本文来源gaodai.ma#com搞#代!码网_</div><strong>搞代gaodaima码</strong>         }                            if ($an['value']) {                                $answer[] = $this->aZ[$key];                            }                            $section->addTextBreak();                        }                        //判断是否输出答案                        if($withAnswer==1){                            $section->addText("(" . implode($answer, ",") . ")", 'rStyle');                            $section->addTextBreak();                            $answer = array();                        }</code>

回复内容:

<body>

想导出word文档,将文本添加到文档的指定位置,比如前一段话的后面,而前一段话是循环生成的,该段后面又有循环生成的另一段话,而我想加入的文本不能放在循环里,只能在两次循环输出文本之后添加,这样的话我怎么才能把文本输出到紧接着的第一个循环之后?
例如:

我想将答案放在问题的后面,而问题和选项都是循环生成的,怎么才能实现将其放在“处分”的后面??
求大神赐教

部分代码:

<code>foreach ($ques['answer'] as $key => $an) {            $textrun1 = $section->createTextRun('rStyle');                            if (is_array($an['synopsis'])) {                                $textrun1->addText($this->aZ[$key] . ".", 'rStyle');                                foreach ($an['synopsis'] as $i) {                                    if (isset($i['type']) && $i['type'] == "text") {                                        $textrun1->addText($i['value'], 'rStyle');                                    }                                    if (isset($i['type']) && $i['type'] == "img" && file_exists($img = ROOT_DIR . str_replace(ROOT_URL, '', $i['value']))) {                                        $imageStyle = array(                                            'width' => $i['width'] == 0 ? 350 : $i['width'],                                            'height' => $i['height'] == 0 ? 350 : $i['height']                                            // 'align' => 'center',                                        );                                        $textrun1->addImage($img, $imageStyle);                                    }                                }                                unset($textrun1);                            } else {                                $section->addText(strtoupper($this->aZ[$key]) . "." . $an['synopsis'], 'rStyle');                            }                            if ($an['value']) {                                $answer[] = $this->aZ[$key];                            }                            $section->addTextBreak();                        }                        //判断是否输出答案                        if($withAnswer==1){                            $section->addText("(" . implode($answer, ",") . ")", 'rStyle');                            $section->addTextBreak();                            $answer = array();                        }</code>

已解决,采用TextRun,将想要连接在一起的文本用同一个textrun对象表示出来。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHPWord添加文本到指定位置

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址