记一次php 导出word文档

news2024/11/24 2:09:06

导出试卷  先看效果

  $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" => "<p>11111</p>"
    "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" => "<p>11111</p>"
    "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" => "<p>RETYUIO</p>"
    "options" => "[{"name":"A","value":"WERTYUI","attrid":"","attr":""},{"name":"B","value":"FDGHJKL","attrid":"","attr":""}]"
    "parsing" => "<p>EWRTYUI</p>"
    "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" => "<p>???</p>"
    "options" => "[{"name":"A","value":"1","attrid":"","attr":""},{"name":"B","value":"2","attrid":"","attr":""},{"name":"C","value":"3","attrid":"","attr":""}]"
    "parsing" => "<p>888</p>"
    "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');保存新文件

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/567039.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

北邮22信通:二叉树显示路径的两种方法 递归函数保存现场返回现场的实例

北邮22信通一枚~ 跟随课程进度每周更新数据结构与算法的代码和文章 持续关注作者 解锁更多邮苑信通专属代码~ 获取更多文章 请访问专栏~ 北邮22信通_青山如墨雨如画的博客-CSDN博客 一.讲解 要想实现二叉树的路径显示&#xff0c;我们要按照…

gitbook在centos上安装

1&#xff09;官网下载Node.js的Linux64位的二进制包:Download | Node.js 或者在线下载&#xff1a; wget https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz ​​2)到指定目录​解压 cd /opt/gitbook tar -xJf node-v12.16.1-linux-x64.tar.xz mv node-…

记录--按钮级别权限怎么控制

这里给大家分享我在网上总结出来的一些知识&#xff0c;希望对大家有所帮助 最近的面试中有一个面试官问我按钮级别的权限怎么控制&#xff0c;我说直接v-if啊&#xff0c;他说不够好&#xff0c;我说我们项目中按钮级别的权限控制情况不多&#xff0c;所以v-if就够了&#xff…

wifiWG233移植

驱动的移植 交叉编译WIFI原厂固件驱动 根据网上资料&#xff0c;和官方文档编译成功。 修改Makefile 修改交叉编译环境 执行 make 编译成功&#xff0c;生成88x2bu.ko 加载原厂驱动 Insmod 88x2bu.ko 生成wlan0 Hostapd工具移植生成可连接热点AP 安装libnl库 使用命令tar …

ApiKit 接口调用、自动化测试工具

作为一位后端开发&#xff0c;我们平时经常需要维护API文档、对API接口进行调试、有时候还得Mock数据。Postman虽然作为接口调试工具非常好用&#xff0c;但是对于维护API文档这类工作却不太合适。今天给大家推荐一款功能更强大的工具ApiKit&#xff0c;足以满足我们对API的各种…

如何清除电脑缓存?分享3个有效的方法!

案例&#xff1a;我发现我的电脑运行速度越来越慢&#xff0c;影响使用体验。朋友说可能是我没有及时清理电脑缓存文件所致&#xff0c;那如何清理缓存的文件&#xff1f; 【我想通过清理电脑缓存文件的方法&#xff0c;提高我电脑运行的速度&#xff0c;但是我不知道具体的操…

如何利用Citespace和vosviewer既快又好地写出高质量的论文及快速锁定热点和重点文献进行可视化分析?

基于Citespace和vosviewer文献计量学可视化SCI论文高效写作方法 CiteSpace是什么&#xff1f; 简单来说&#xff0c;它一款通过将国内外文献进行可视化分析来帮助你了解一门学科前世今生的软件。 面对成千上万篇的文献&#xff0c;怎样才能快速锁定自己最感兴趣的主题及科学…

取代 Docker Desktop?Podman Desktop 发布 GA 版本 1.0

Podman&#xff08;POD MANager&#xff09;是一个跨平台的容器管理工具&#xff0c;可用于管理容器、镜像、卷以及以容器组形式存在的 Pod。Podman 可以在 Linux 上直接运行容器&#xff0c;但在像 macOS 和 Windows 这样的平台&#xff0c;是通过虚拟机间接运行容器。 Podma…

「实在RPA·电商数字员工」契合电商数智转型需求

一、为什么说电商数智化转型很重要&#xff1f; 如今&#xff0c;电商发展速度惊人&#xff0c;并且已经取代了实体店购物的时代。在众多新型的消费方式下&#xff0c;各式的电商行业如何运作&#xff0c;并且在短时间内完成各项任务&#xff0c;提升人们的生活质量。人们生活…

django admin后台列表页、修改/详情页图片预览功能

目录 一、admin后台列表页的图片预览功能 二、admin后台修改/详情页图片预览功能 1&#xff0c;添加html前端代码 2、在admin.py文件中添加以下代码&#xff1a; 1.列表页图片问题&#xff1a;在admin列表页中&#xff0c;直接在list_display中填写图片字段时在列表页展示的…

YOLOv5从训练到移植

一、图像采集和标注 图像采集 覆盖所有的数据目标&#xff0c;不同场景&#xff08;视角、光照、可能的干扰&#xff09;、距离、运动、背景等&#xff0c;用深度和广度摄像头都行。 若兼顾效率和准确率&#xff0c;可以用迁移学习思路训练&#xff0c;则不同场景下采集的图…

系统方面对文件的打开,读写,关闭

系统方面对文件的操作 1. 系统方面打开文件的函数2. 系统方面对文件的写入3. 系统方面对文件的读取4. 关闭文件close 1. 系统方面打开文件的函数 open函数得到一个指定文件的文件描述符&#xff0c;如果出现错误则返回-1。open函数需要传入一个文件路径和操作模式&#xff0c;…

高德地图AMap.MouseTool插件多次测距不能清除bug

AMap.MouseTool插件是一个很有用的插件&#xff0c;可以在地图上画折线测量距离&#xff0c;也可以在地图上画区域测量面积&#xff0c;这些在客户的一些高级需求里经常出现&#xff0c;最近使用出现了bug&#xff0c;此bug在官网的示例里也能重现 官网demo上重现步骤如下图&a…

剑指 Offer 58 - I. 翻转单词顺序

剑指 Offer 58 - I. 翻转单词顺序 题目&#xff1a; 输入一个英文句子&#xff0c;翻转句子中单词的顺序&#xff0c;但单词内字符的顺序不变。为简单起见&#xff0c;标点符号和普通字母一样处理。例如输入字符串"I am a student. “&#xff0c;则输出"student. a …

Spring Tool Suite(STS)初始化配置记录

目录 1.前言 2.STS安装 3.STS配置 3.1.SpringToolSuite4.ini 3.2、配置maven 3.3.配置jdk 3.4.全局编码设置 3.5.字体配置 3.6.设置自动提示 4. Spring插件 4.1.MyBatipse--mybatis插件 4.2.Spark Builder Generator 4.3.Properties Editor 4.4.Checkstyle 4.5.…

【MySQL】数据库的基本操作

&#x1f3e0; 大家好&#xff0c;我是 兔7 &#xff0c;一位努力学习C的博主~&#x1f4ac; &#x1f351; 如果文章知识点有错误的地方&#xff0c;请指正&#xff01;和大家一起学习&#xff0c;一起进步&#x1f440; &#x1f680; 如有不懂&#xff0c;可以随时向我提问&…

Smoothieware_best-for-pnp 工程文件编译选项含义整理

文章目录 Smoothieware_best-for-pnp 工程文件编译选项含义整理概述arm-none-eabi-gcc 的编译选项含义整理 - S(汇编)文件arm-none-eabi-gcc 的编译选项含义整理 - C文件arm-none-eabi-gcc 的编译选项含义整理 - CPP文件库的打包arm-none-eabi-gcc 的编译选项含义整理 - C文件 …

Python入门教程+项目实战-12.3节-使用字典进行格式化

目录 12.3.1 字符串的格式化 12.3.2 使用字典进行格式化 12.3.3 格式化操作方法的优缺点 12.3.4 知识要点 12.3.5 系统学习python 12.3.1 字符串的格式化 在9.4节介绍了字符串的格式化&#xff0c;我们先来回顾下字符串格式化的定义&#xff0c;以及主要的格式化方法&…

万字长文详述ClickHouse在京喜达实时数据的探索与实践 | 京东云技术团队

1 前言 京喜达技术部在社区团购场景下采用JDQFlinkElasticsearch架构来打造实时数据报表。随着业务的发展 Elasticsearch开始暴露出一些弊端&#xff0c;不适合大批量的数据查询&#xff0c;高频次深度分页导出导致ES宕机、不能精确去重统计&#xff0c;多个字段聚合计算时性能…

从零开始的python教程:全面又好用的学习资料

Hi&#xff0c;大家好&#xff0c;我是蛋糕 最近因为接连带过一些训练营和成长营&#xff0c;也是可以与各位小伙伴进行更多的讨论&#xff0c;发现各位小伙伴最近也是迫切的想要学习一些新的技能&#xff0c;其中呼声最高的可能就是Python了&#xff0c;当然理由也是很多啦&a…