我的编程空间,编程开发者的网络收藏夹
学习永远不晚

记一次php 导出word文档

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

记一次php 导出word文档

导出试卷  先看效果

  $paper = Exam::where('id', $data['examid'])->field('paper_id,title')->find();        $bankIds = PaperStorehouse::where('id', $paper['paper_id'])->value('banks');        $field = 'type,rate,name,options,parsing,value,question_id,parsing_id';        $bankInfo = Banks::where('id', 'in', $bankIds)->field($field)->select()            ->map(function ($item) {                if ($item['question_id'] == 0) {                    $item['question_id'] = '';                } else {                    $item['question_id'] = getDomain(File::where('id', $item['question_id'])->value('filepath'));                }                if ($item['parsing_id'] == 0) {                    $item['parsing_id'] = '';                } else {                    $item['parsing_id'] = getDomain(File::where('id', $item['parsing_id'])->value('filepath'));                }                $item['rate'] = taskdegreeName($item['rate']);                $item['typeTxt'] = bankTxt($item['type']);                $options = json_decode($item['options'], true);                $item['Aanswer'] = $options[0]['value'] ?: '';                $item['Banswer'] = $options[1]['value'] ?: '';                $item['Canswer'] = $options[2]['value'] ?: '';                $item['Danswer'] = $options[3]['value'] ?: '';                return $item;            });//        $singles  $multi  $judge  $operate  $filling  $read  $sub        $singles = [];        $multi = [];        $subjectivity = [];        $judge = [];        foreach ($bankInfo as $k => $v) {            if ($v['type'] == 1) {                $singles[] = $v;            } elseif ($v['type'] == 2) {                $multi[] = $v;            } elseif ($v['type'] == 3) {                $judge[] = $v;            } else {                $subjectivity[] = $v;            }        }        $path = public_path();        $tmp = new TemplateProcessor($path . 'word.docx');        $singlesNum = count($singles);        $multiNum = count($multi);        $judgeNum = count($judge);        $subjectivityNum = count($subjectivity);        $tmp->setValue('examTitle', $paper['title']);        $tmp->cloneRow('singles', $singlesNum);        $tmp->cloneRow('multi', $multiNum);        $tmp->cloneRow('judge', $judgeNum);        $tmp->cloneRow('subjectivity', $subjectivityNum);        for ($i = 0; $i < $singlesNum; $i++) {            $tmp->setValue("singles#" . ($i + 1), $singles[$i]['typeTxt']);            $tmp->setValue("singlesName#" . ($i + 1), $singles[$i]['name']);            $tmp->setValue("AAanswer#" . ($i + 1), $singles[$i]['Aanswer']);            $tmp->setValue("ABanswer#" . ($i + 1), $singles[$i]['Banswer']);            $tmp->setValue("ACanswer#" . ($i + 1), $singles[$i]['Canswer']);            $tmp->setValue("ADanswer#" . ($i + 1), $singles[$i]['Danswer']);        }        for ($i = 0; $i < $multiNum; $i++) {            $tmp->setValue("multi#" . ($i + 1), $multi[$i]['typeTxt']);            $tmp->setValue("multiName#" . ($i + 1), $multi[$i]['name']);            $tmp->setValue("MAanswer#" . ($i + 1), $multi[$i]['Aanswer']);            $tmp->setValue("MBanswer#" . ($i + 1), $multi[$i]['Banswer']);            $tmp->setValue("MCanswer#" . ($i + 1), $multi[$i]['Canswer']);            $tmp->setValue("MDanswer#" . ($i + 1), $multi[$i]['Danswer']);        }        for ($i = 0; $i < $judgeNum; $i++) {            $tmp->setValue("judge#" . ($i + 1), $judge[$i]['typeTxt']);            $tmp->setValue("judgeName#" . ($i + 1), $judge[$i]['name']);            $tmp->setValue("Eanswer#" . ($i + 1), $judge[$i]['Aanswer']);            $tmp->setValue("Fanswer#" . ($i + 1), $judge[$i]['Banswer']);        }        for ($i = 0; $i < $subjectivityNum; $i++) {            $tmp->setValue("subjectivity#" . ($i + 1), $subjectivity[$i]['typeTxt']);            $tmp->setValue("subjectivityName#" . ($i + 1), $subjectivity[$i]['name']);        }                $tmp->saveAs($path . 'target.docx');        //浏览器下载        ob_clean();        ob_start();        $fp = fopen($path . 'target.docx', "r");        $file_size = filesize($path . 'target.docx');        Header("Content-type:application/octet-stream");        Header("Accept-Ranges:bytes");        Header("Accept-Length:" . $file_size);        Header("Content-Disposition:attchment; filename=" . '测试文件.docx');        $buffer = 1024;        $file_count = 0;        while (!feof($fp) && $file_count < $file_size) {            $file_con = fread($fp, $buffer);            $file_count += $buffer;            echo $file_con;        }        fclose($fp);        ob_end_flush();

我的$bankInfo变量具体是这样的

array:13 [  0 => array:13 [    "type" => 1    "rate" => "低"    "name" => "

11111

" "options" => "[{"name":"A","value":"111","attrid":"","attr":""},{"name":"B","value":"111","attrid":"","attr":""},{"name":"C","value":"111","attrid":"","attr":""},{"name":"D","value":"111","attrid":"","attr":""}]" "parsing" => "" "value" => 6 "question_id" => "" "parsing_id" => "" "typeTxt" => "单选题" "Aanswer" => "111" "Banswer" => "111" "Canswer" => "111" "Danswer" => "111" ] 1 => array:13 [ "type" => 1 "rate" => "低" "name" => "

11111

" "options" => "[{"name":"A","value":"111","attrid":"","attr":""},{"name":"B","value":"111","attrid":"","attr":""},{"name":"C","value":"111","attrid":"","attr":""},{"name":"D","value":"111","attrid":"","attr":""}]" "parsing" => "" "value" => 6 "question_id" => "" "parsing_id" => "" "typeTxt" => "单选题" "Aanswer" => "111" "Banswer" => "111" "Canswer" => "111" "Danswer" => "111" ] 2 => array:13 [ "type" => 1 "rate" => "低" "name" => "

RETYUIO

" "options" => "[{"name":"A","value":"WERTYUI","attrid":"","attr":""},{"name":"B","value":"FDGHJKL","attrid":"","attr":""}]" "parsing" => "

EWRTYUI

" "value" => 10 "question_id" => "http://127.0.0.1:99" "parsing_id" => "" "typeTxt" => "单选题" "Aanswer" => "WERTYUI" "Banswer" => "FDGHJKL" "Canswer" => "" "Danswer" => "" ] 3 => array:13 [ "type" => 2 "rate" => "低" "name" => "

???

" "options" => "[{"name":"A","value":"1","attrid":"","attr":""},{"name":"B","value":"2","attrid":"","attr":""},{"name":"C","value":"3","attrid":"","attr":""}]" "parsing" => "

888

" "value" => 11 "question_id" => "" "parsing_id" => "" "typeTxt" => "多选题" "Aanswer" => "1" "Banswer" => "2" "Canswer" => "3" "Danswer" => "" ] 4 => array:13 [ "type" => 1 "rate" => "中" "name" => "可燃气体( )。" "options" => "[{"name":"A","value":" 5-15%","attrid":"","attr":""},{"name":"B","value":" 6-16%","attrid":"","attr":""},{"name":"C","value":" 5-16%","attrid":"","attr":""},{"name":"D","value":" 4-16%","attrid":"","attr":""}]" "parsing" => "这是单选题解析" "value" => 2 "question_id" => "" "parsing_id" => "" "typeTxt" => "单选题" "Aanswer" => " 5-15%" "Banswer" => " 6-16%" "Canswer" => " 5-16%" "Danswer" => " 4-16%" ] 5 => array:13 [ "type" => 1 "rate" => "中" "name" => "可燃爆炸极限二( )。" "options" => "[{"name":"A","value":" 5-15%","attrid":"","attr":""},{"name":"B","value":" 6-16%","attrid":"","attr":""},{"name":"C","value":" 5-16%","attrid":"","attr":""},{"name":"D","value":" 4-16%","attrid":"","attr":""}]" "parsing" => "这是单选题解析" "value" => 2 "question_id" => "" "parsing_id" => "" "typeTxt" => "单选题" "Aanswer" => " 5-15%" "Banswer" => " 6-16%" "Canswer" => " 5-16%" "Danswer" => " 4-16%" ] 6 => array:13 [ "type" => 2 "rate" => "低" "name" => "低压配电室着火的风险点源识别有哪些一?" "options" => "[{"name":"A","value":"明火造成人员烧伤11","attrid":"","attr":""},{"name":"B","value":"变压器自身易爆炸","attrid":"","attr":""},{"name":"C","value":"明火可能造成环境污染","attrid":"","attr":""},{"name":"D","value":" 以上均不对","attrid":"","attr":""}]" "parsing" => "这是多选题解析" "value" => 2 "question_id" => "" "parsing_id" => "" "typeTxt" => "多选题" "Aanswer" => "明火造成人员烧伤11" "Banswer" => "变压器自身易爆炸" "Canswer" => "明火可能造成环境污染" "Danswer" => " 以上均不对" ] 7 => array:13 [ "type" => 2 "rate" => "低" "name" => "测试2低压配电室着火的风险点源识别有哪些二?" "options" => "[{"name":"A","value":"测试2明火造成人员烧伤","attrid":"","attr":""},{"name":"B","value":"测试2变压器自身易爆炸","attrid":"","attr":""},{"name":"C","value":"测试2明火可能造成环境污染","attrid":"","attr":""},{"name":"D","value":"测试2以上均不对","attrid":"","attr":""}]" "parsing" => "这是多选题解析" "value" => 1 "question_id" => "" "parsing_id" => "" "typeTxt" => "多选题" "Aanswer" => "测试2明火造成人员烧伤" "Banswer" => "测试2变压器自身易爆炸" "Canswer" => "测试2明火可能造成环境污染" "Danswer" => "测试2以上均不对" ] 8 => array:13 [ "type" => 3 "rate" => "高" "name" => "测试可然气体泄漏不会造成人员窒息和环境污染一。" "options" => "[{"name":"A","value":" 错误","attrid":"","attr":""},{"name":"B","value":" 正确","attrid":"","attr":""}]" "parsing" => "这是判断题解析" "value" => 3 "question_id" => "" "parsing_id" => "" "typeTxt" => "判断题" "Aanswer" => " 错误" "Banswer" => " 正确" "Canswer" => "" "Danswer" => "" ] 9 => array:13 [ "type" => 3 "rate" => "中" "name" => "测试判断题二。" "options" => "[{"name":"A","value":" 错误","attrid":"","attr":""},{"name":"B","value":" 正确","attrid":"","attr":""}]" "parsing" => "这是判断题解析" "value" => 1 "question_id" => "" "parsing_id" => "" "typeTxt" => "判断题" "Aanswer" => " 错误" "Banswer" => " 正确" "Canswer" => "" "Danswer" => "" ] 10 => array:13 [ "type" => 5 "rate" => "中" "name" => "测试这是填空题一()" "options" => "" "parsing" => "填空题解析(若无解析本行可不填)" "value" => 3 "question_id" => "" "parsing_id" => "" "typeTxt" => "填空题" "Aanswer" => "" "Banswer" => "" "Canswer" => "" "Danswer" => "" ] 11 => array:13 [ "type" => 4 "rate" => "高" "name" => "测试如何保持身体健康一? " "options" => "" "parsing" => "无 (若无解析本行可不填)" "value" => 3 "question_id" => "" "parsing_id" => "" "typeTxt" => "简答题" "Aanswer" => "" "Banswer" => "" "Canswer" => "" "Danswer" => "" ] 12 => array:13 [ "type" => 6 "rate" => "中" "name" => "测试这是阅读理解题目一 " "options" => "" "parsing" => "无 (若无解析本行可不填)" "value" => 1 "question_id" => "" "parsing_id" => "" "typeTxt" => "阅读理解题" "Aanswer" => "" "Banswer" => "" "Canswer" => "" "Danswer" => "" ]]

核心代码:

$tmp = new TemplateProcessor($path . 'word.docx');  打开一个模板

模板是这样的:

$tmp->setValue('examTitle', $paper['title']);  php程序会识别模板里的examTitle  模板中的${examTitle} 相当于占位符   当程序识别到的时候   会对其进行替换(小坑  模板其实是表格形式   不过我这里对border设置的颜色和背景色一致)$tmp->cloneRow('singles', $singlesNum);  克隆行数  (若不在表格中  则会报错)
for ($i = 0; $i < $subjectivityNum; $i++) {    $tmp->setValue("subjectivity#" . ($i + 1), $subjectivity[$i]['typeTxt']);    $tmp->setValue("subjectivityName#" . ($i + 1), $subjectivity[$i]['name']);}

想循环变量赋值该怎么办呢   只需要将占位符标记后面加上#即可

$tmp->saveAs($path . 'target.docx');保存新文件

 

来源地址:https://blog.csdn.net/qq_58778333/article/details/130861360

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

记一次php 导出word文档

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

PHP导出Word文档原理是什么

本篇文章给大家分享的是有关PHP导出Word文档原理是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。PHP语言可以被我们用来实现很多功能。比如在网站建设中,我们经常会用到网
2023-06-17

java 导出word,java根据提供word模板导出word文档

本文主要讲解,利用poi-tl在word中动态生成表格行,进行文字、图片填充。一共提供了两种方式,1.基于本地文件 2.基于网络文件 本文讲解思路,1.先看示例,2. 示例对应的代码展示 3. 基本概念讲解(api自行查阅文档)。 这
2023-08-17

Java实现导出Word文档的示例代码

poi-tl是一个基于ApachePOI的Word模板引擎,也是一个免费开源的Java类库,你可以非常方便的加入到你的项目中。本文就利用它实现导出Word文档功能,需要的可以参考一下
2023-02-09

Java SpringBoot集成文件之如何使用POI导出Word文档

这篇文章主要介绍了Java SpringBoot集成文件之如何使用POI导出Word文档,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
2022-11-13

Java项目中的word文档如何利用Freemarker进行导出

本篇文章给大家分享的是有关Java项目中的word文档如何利用Freemarker进行导出,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。实现步骤1.用Microsoft Off
2023-05-31

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录