ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
>首先 安装 phpword: composer require phpoffice/phpword ` ~~~ /** * @author: Jackhhy * @date: 2019/12/28 0028 * @name: export * @describe:内容文本导出word下载和保存到本地 */ public function export(){ $str='需要导出到word的字符串'; $phpWord=new PhpWord(); //实例化 //调整页面样式 $sectionStyle = array('orientation' => null, 'marginLeft' => 400, 'marginRight' => 400, 'marginTop' => 400, 'marginBottom' => 400); $section = $phpWord->addSection($sectionStyle); //添加页眉 $header=$section->addHeader(); $k=$header->addTextRun(); //页眉添加一张图片 $k->addImage(ROOT_PATH.'public'.DS_PROS."jrkadmin/admin/images/a.jpg",array( 'width' => '100%', 'height' => 60, 'marginTop' => -1, 'marginLeft' => 1, 'wrappingStyle' => 'behind', )); //添加页脚 $footer = $section->addFooter(); $f=$footer->addTextRun(); $f->addImage(ROOT_PATH.'public'.DS_PROS."jrkadmin/admin/images/a.jpg",array( 'width' => 580, 'height' => 140, 'marginTop' => -1, 'marginLeft' => 1, 'wrappingStyle' => 'behind', )); $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.',array('align'=>'center')); $section->addText( '标题', array('name' => '黑体', 'size' => 15), array('align'=>'center') ); //添加换行符 $section->addTextBreak(2); //添加文本 $section->addText( $str, array('name' => 'Arial', 'size' => 13), array('lineHeight'=>1.5,'indent'=>1) ); /*直接下载文档*/ //$tk=parent::makeToken(); $name=time().".docx"; //文件名称 $phpWord->save($name,"Word2007",true); /*保存文档到本地*/ /* $objwrite =IOFactory::createWriter($phpWord); $t = date("Ymd", time()); $pk = md5($t);// //保存的路径和中文名称适应 $dir = iconv("UTF-8", "GBK", ROOT_PATH.'public'.DS_PROS.'uploads'.DS."save/".$t); if (!file_exists($dir)) { @mkdir($dir, 0777, true); } $pa = "save/".$t."/".$pk.".docx"; $objwrite->save(ROOT_PATH.'public'.DS_PROS.'uploads'.DS_PROS.$pa); $this->success("", '', 'http://www.scirobot.com.cn/public/uploads/'.$pa);*/ exit(); } ~~~ `