您现在的位置是:主页 > news > 平面设计素材网站知乎/东莞seo优化公司

平面设计素材网站知乎/东莞seo优化公司

admin2025/6/28 23:48:45news

简介平面设计素材网站知乎,东莞seo优化公司,企业文化墙内容设计,山东青岛网站建设公司排名本文简单介绍如何在ThinkPHP3.2中使用PHPWord生成报表页面的word文档并下载,报表带文字和百度Echarts的图表图片。 有关在ThinkPHP3.2中使用PHPWord,可以参考“thinkphp3.2集成phpword,生成word文档并下载”这篇文章。而使用模板创建word文档&#xff0c…

平面设计素材网站知乎,东莞seo优化公司,企业文化墙内容设计,山东青岛网站建设公司排名本文简单介绍如何在ThinkPHP3.2中使用PHPWord生成报表页面的word文档并下载,报表带文字和百度Echarts的图表图片。 有关在ThinkPHP3.2中使用PHPWord,可以参考“thinkphp3.2集成phpword,生成word文档并下载”这篇文章。而使用模板创建word文档&#xff0c…

   本文简单介绍如何在ThinkPHP3.2中使用PHPWord生成报表页面的word文档并下载,报表带文字和百度Echarts的图表图片。

    有关在ThinkPHP3.2中使用PHPWord,可以参考“thinkphp3.2集成phpword,生成word文档并下载”这篇文章。而使用模板创建word文档,步骤如下:

    一、新建word模板

    模板的格式如下图,在模板中需要替换的内容,使用${替换内容}代替内容。本文示例模板名称为“template.docx”。

    1-201104100G9115.png

    二、装载word模板    

    可以初始化“PhpOffice\PhpWord\TemplateProcessor”类或使用“PhpOffice\PhpWord\PhpWord”的loadTemplate方法,装载word模板。代码如下:

$document = new TemplateProcessor('wordfile/template.docx');
//载入模板文件
// $document = $PHPWord->loadTemplate('wordfile/temp.doc');

    三、设置模板字符串参数值

    通过模板类的setValue方法,设置模板的字符串参数值,代码如下:

$document->setValue('reportTitle', '温度监测综合日报');
$document->setValue('projectName', 'XX监测项目');
$document->setValue('addr', '陕西西安');
$document->setValue('startDate', '2020-01-01');
$document->setValue('endDate', '2020-10-01');

    四、设置模板图片参数值

    本文图片使用的是Echarts图表的getDataURL方法返回的图片内容,并通过post方法传递到服务端。如何将dataurl内容转换为图片,参考“PHP将canvas的dataurl转成图片image”。将图片保存到本地磁盘后,使用模板类的setImageValue方法,设置模板的图片参数,方法调用及参数构造如下代码(参数设置完成后,可以使用unlink删除临时图片):

// 图片处理
$img = I('post.temp');
$imgdata = substr($img, strpos($img, ",") + 1);
$decodedData = base64_decode($imgdata);
$filename = "wordfile/temp.png";
$pngfiles[] = $filename;// 转存到硬盘
file_put_contents($filename, $decodedData);// 设置图片参数
$picParam = array("path" => $filename, "width" => 400, "height" => 200);
$document->setImageValue('temp', $picParam);//删除csv临时文件
unlink($filename);

    五、生成word文档

    通过以上步骤,设置完参数后,调用模板文档类的saveAs方法,生成word文件。代码如下:

$path = 'wordfile/report.docx';
$document->saveAs($path);

    六、效果图

    1-20110410291S19.png    

    注意:

        word模板和导出需使用docx,用doc会报错:Could not close zip file    

    完整代码如下:   

function createDoc()
{// word使用docx,用doc会报错:Could not close zip file$document = new TemplateProcessor('wordfile/template.docx');//载入模板文件// $document = $PHPWord->loadTemplate('wordfile/temp.doc');$document->setValue('reportTitle', '温度监测综合日报');$document->setValue('projectName', 'XX监测项目');$document->setValue('addr', '陕西西安');$document->setValue('startDate', '2020-01-01');$document->setValue('endDate', '2020-10-01');// 图片处理$img = I('post.temp');$imgdata = substr($img, strpos($img, ",") + 1);$decodedData = base64_decode($imgdata);$filename = "wordfile/temp.png";$pngfiles[] = $filename;// 转存到硬盘file_put_contents($filename, $decodedData);// 设置图片参数$picParam = array("path" => $filename, "width" => 400, "height" => 200);$document->setImageValue('temp', $picParam);//删除csv临时文件unlink($filename);$path = 'wordfile/report.docx';$document->saveAs($path);die();
}