phpword使用

1 篇文章 0 订阅
1 篇文章 0 订阅

效果

模板

在这里插入图片描述

输出模板

代码

下载

composer require phpoffice/phpword

代码案例

include ROOT . "../vendor/autoload.php";

$file = ROOT . 'word/demo.docx';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($file); //打开模板
$templateProcessor->cloneBlock('block', 2, true, true);
$var = [
    'name#1' => '小明',
    'name#2' => '小红'
];
$templateProcessor->setValues($var);
$save_flie = ROOT . 'word/word.docx';
$templateProcessor->saveAs($save_flie);
$open = fopen($save_flie, "rb");
ob_clean();
echo fread($open, filesize($save_flie));   //读取文件内容并直接输出到浏览器
fclose($open);
unlink($save_flie);
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: application/doc");
header("Content-Disposition: attachment; filename=测试.docx");

使用出错解决方式

  • 使用docx后缀的word文档,用doc可能导致出错
  • 改后缀doc成docx也会报错,需要用wps,office或其他软件转成word
  • 链接: 官方文档.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值