iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > PHP编程 >PHP生成WORD
  • 262
分享到

PHP生成WORD

html前端css 2023-09-29 11:09:53 262人浏览 薄情痞子
摘要

官方实例 function word(Request $request){ //创建一个文档 $phpWord = new \PhpOffice\PhpWord\PhpWord();

官方实例

function word(Request $request){        //创建一个文档        $phpWord = new \PhpOffice\PhpWord\PhpWord();                // 添加新段落 换页        $section = $phpWord->addSection();        // 将文本元素添加到默认字体样式的段落        $section->addText(            '"Learn from yesterday, live for today, hope for tomorrow. '            . 'The important thing is not to stop questioning." '            . '(Albert Einstein)'        );                // 内联样式        //Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null)        $section->addText(            '"Great achievement is usually born of great sacrifice, '            . 'and is never the result of selfishness." '            . '(Napoleon Hill)',            array('name' => 'Tahoma', 'size' => 10)        );        // 使用命名字体样式自定义字体        $fontStyleName = 'oneUserDefinedStyle';        $phpWord->addFontStyle(            $fontStyleName,            array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)        );        $section->addText(            '"The greatest accomplishment is not in never falling, '            . 'but in rising again after you fall." '            . '(Vince Lombardi)',            $fontStyleName        );        // 定制使用显式创建的字体样式对象        $fontStyle = new \PhpOffice\PhpWord\Style\Font();        $fontStyle->setBold(true);        $fontStyle->setName('Tahoma');        $fontStyle->setSize(13);        $fontStyle->setColor('1B2232');        $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');        $myTextElement->setFontStyle($fontStyle);        // 将文档保存为OOXML文件        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');        $objWriter->save('helloWorld.docx');        // 将文档保存为ODF文件,写字板        //$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');        //$objWriter->save('helloWorld.odt');        // 将文档保存为HTML文件        //$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');        //$objWriter->save('helloWorld.html');}

实例2

function word(Request $request){        //创建一个文档        $phpWord = new \PhpOffice\PhpWord\PhpWord();        //语言        $language = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);        $phpWord->getSettings()->setThemeFontLang($language);        //字体样式        $fontStyleName = 'rStyle';        $phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));        //段落样式        $paragraphStyleName = 'pStyle';        //alignment 对齐方式        $phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100));        $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));        $section = $phpWord->addSection();//        $section = $phpWord->addSection(//            array(//                'marginLeft'   => 200,//                'marginRight'  => 200,//                'marginTop'    => 200,//                'marginBottom' => 200,//                'headerHeight' => 50,//                'footerHeight' => 50,//            )//        );        $section->addTitle('addTitle', 1);        $section->addText('addText addText addText addText');        $section->addTextBreak(2);        $section->addText('字体样式 I am styled by a font style definition.', $fontStyleName);        $section->addText('段落样式 I am styled by a paragraph style definition.', null, $paragraphStyleName);        $section->addText('字体样式 + 段落样式 I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName);        $section->addTextBreak();        // 内联样式        $fontStyle['name'] = 'Times New Roman';        $fontStyle['size'] = 20;        $textrun = $section->addTextRun();        $textrun->addText('I am inline styled ', $fontStyle);        $textrun->addText('with ');        $textrun->addText('color', array('color' => '996699'));        $textrun->addText(', ');        $textrun->addText('bold', array('bold' => true)); //加粗        $textrun->addText(', ');        $textrun->addText('italic', array('italic' => true)); //斜体        $textrun->addText(', ');        $textrun->addText('underline', array('underline' => 'dash')); //下划线        $textrun->addText(', ');        $textrun->addText('strikethrough', array('strikethrough' => true)); //删除线        $textrun->addText(', ');        $textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true));// 双删除线        $textrun->addText(', ');        $textrun->addText('superScript', array('superScript' => true)); //上标        $textrun->addText(', ');        $textrun->addText('subScript', array('subScript' => true)); //下标        $textrun->addText(', ');        $textrun->addText('smallCaps', array('smallCaps' => true)); //小型大写字母        $textrun->addText(', ');        $textrun->addText('allCaps', array('allCaps' => true)); //大写        $textrun->addText(', ');        $textrun->addText('fgColor', array('fgColor' => 'yellow')); //背景色        $textrun->addText(', ');        $textrun->addText('scale', array('scale' => 200)); //缩放        $textrun->addText(', ');        $textrun->addText('spacing', array('spacing' => 120)); //距离        $textrun->addText(', ');        $textrun->addText('kerning', array('kerning' => 10)); //字间距        $textrun->addText('. ');        // 链接        $section->addLink('https://www.baidu.com', '百度');        $section->addTextBreak();        // 图片        $section->addImage('logo.jpg', array('width'=>18, 'height'=>18));        $section->addTextBreak();        $section->addImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png', array('width'=>18, 'height'=>18));        //excel 文件关联        //$textrun->addObject('excel.xls');        // 底部        //$footnote = $section->addFootnote();        //$footnote->addText('底部');        // 底部样式        //$footnoteProperties = new \PhpOffice\PhpWord\ComplexType\FootnoteProperties();        //$footnoteProperties->setNumFmt(\PhpOffice\PhpWord\SimpleType\NumberFormat::DECIMAL_ENCLOSED_CIRCLE);        //$section->setFootnoteProperties($footnoteProperties);        // 换页        $section = $phpWord->addSection();        // 定义样式 tab键距离        $multipleTabsStyleName = 'multipleTab';        $phpWord->addParagraphStyle(            $multipleTabsStyleName,            array(                'tabs' => array(                    new \PhpOffice\PhpWord\Style\Tab('left', 1550),                    new \PhpOffice\PhpWord\Style\Tab('center', 3200),                    new \PhpOffice\PhpWord\Style\Tab('right', 5300),                ),            )        );        $rightTabStyleName = 'rightTab';        $phpWord->addParagraphStyle($rightTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', 9090))));        $leftTabStyleName = 'centerTab';        $phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));        // tab内容        $section->addText("Multiple Tabs:\tOne\tTwo\tThree", null, $multipleTabsStyleName);        $section->addText("Left Aligned\tRight Aligned", null, $rightTabStyleName);        $section->addText("\tCenter Aligned", null, $leftTabStyleName);        // 换页        $section = $phpWord->addSection();        // 表格        $rows = 10;        $cols = 5;        $table = $section->addTable();        for ($r = 1; $r <= $rows; $r++) {            $table->addRow();            for ($c = 1; $c <= $cols; $c++) {                $table->addCell(1750)->addText("Row {$r}, Cell {$c}");            }        }        $section->addTextBreak(1);        // 表格样式        $fancyTableStyleName = 'Fancy Table';        $fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50);        $fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');        $fancyTableCellStyle = array('valign' => 'center'); //垂直居中        $fancyTableCellBtlrStyle = array('valign' => 'center', 'textDirection' => \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR); //textDirection文字方向        $fancyTableFontStyle = array('bold' => true);        $phpWord->addTableStyle($fancyTableStyleName, $fancyTableStyle, $fancyTableFirstRowStyle);        $table = $section->addTable($fancyTableStyleName);        $table->addRow(900);        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 1', $fancyTableFontStyle);        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 2', $fancyTableFontStyle);        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 3', $fancyTableFontStyle);        $table->addCell(2000, $fancyTableCellStyle)->addText('Row 4', $fancyTableFontStyle);        $table->addCell(500, $fancyTableCellBtlrStyle)->addText('Row 5', $fancyTableFontStyle);        for ($i = 1; $i <= 8; $i++) {            $table->addRow();            $table->addCell(2000)->addText("Cell {$i}");            $table->addCell(2000)->addText("Cell {$i}");            $table->addCell(2000)->addText("Cell {$i}");            $table->addCell(2000)->addText("Cell {$i}");            $text = (0 == $i % 2) ? 'X' : '';            $table->addCell(500)->addText($text);        }        // html        $section = $phpWord->addSection();        $html = '

Adding element via HTML

'; $html .= '

Some well-fORMed HTML snippet needs to be used

'; $html .= '

With for example some1 inline formatting1

'; $html .= '
header a header b header c
12
This is bold text6
'; \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false); // 将文档保存为OOXML文件 $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('helloWorld2.docx');

表格

HTML

 

来源地址:https://blog.csdn.net/u012467744/article/details/126273344

--结束END--

本文标题: PHP生成WORD

本文链接: https://www.lsjlt.com/news/421419.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

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

下载Word文档
猜你喜欢
  • PHP生成WORD
    官方实例 function word(Request $request){ //创建一个文档 $phpWord = new \PhpOffice\PhpWord\PhpWord(); ...
    99+
    2023-09-29
    html 前端 css
  • php怎么生成word文档
    可以使用PHP库PHPWord来生成和操作Word文档。以下是一个简单的示例: 首先,你需要安装PHPWord库。可以通过Comp...
    99+
    2024-03-15
    php
  • PHP使用PHPWord生成word文档
    阅读目录 阐述 安装 使用 自动加载 实例化 添加文字内容 链接 图片 页眉 ...
    99+
    2023-09-03
    php 开发语言 前端
  • word如何生成目录
    这篇文章主要介绍“word如何生成目录”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“word如何生成目录”文章能帮助大家解决问题。word生成目录的方法:首先打开你的Word文档,并进入或者输入一些...
    99+
    2023-07-01
  • wpsvba如何批量生成word
    这篇文章主要介绍“wpsvba如何批量生成word”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“wpsvba如何批量生成word”文章能帮助大家解决问题。wpsvba批量生成word的方法:1、将以...
    99+
    2023-07-04
  • Java根据word模板生成word文档并转成PDF文件
    1. 处理word模板 1.1 定义word模版 1.2 定义完我们的模板之后,我们要将文档保存为xml的格式 定义完我们的模板之后,我们要将文档保存为xml的格式 1.3 xml格式化 ...
    99+
    2023-09-09
    java word pdf chart
  • Word中怎么一键生成PPT
    小编今天带大家了解Word中怎么一键生成PPT,文中知识点介绍的非常详细。觉得有帮助的朋友可以跟着小编一起浏览文章的内容,希望能够帮助更多想解决这个问题的朋友找到问题的答案,下面跟着小编一起深入学习“Word中怎么一键生成PPT”的知识吧。...
    99+
    2023-06-04
  • Word怎样自动生成目录
    在Word中,可以通过使用标题样式和快速样式来自动生成目录。以下是一种简单的方法:1. 在Word文档中,将标题样式应用于所需的标题...
    99+
    2023-09-15
    Word
  • mysql 表结构生成word文档
    1、背景 我们在做项目时,表设计文档都是非常重要的,可以让开发人员快速了解表与业务的关系、表之间的关系。 产品在不停迭代的过程中,表的结构也会有相应的变化,我们需要将变化更新的表设计文档中。以前我们是人工方式更新文档,效率是比较慢的,慢...
    99+
    2023-09-13
    数据库 Powered by 金山文档
  • 如何利用node生成word文档
    这篇“如何利用node生成word文档”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“如何利...
    99+
    2024-04-02
  • php生成并下载word文件到本地实现方法详解
    目录安装phpword包准备一个word模板(docx格式)前端调用代码PHP处理代码one more thing安装phpword包 通过composer安装phpword包。因为...
    99+
    2024-04-02
  • java动态生成word的方法是什么
    在Java中,可以使用Apache POI库来动态生成Word文档。Apache POI是一个用于处理Microsoft文档格式的J...
    99+
    2023-09-16
    java word
  • 最简单的java生成word文档方法
    1.首先新建一个word文档,然后设计好格式,比如说我的标题是黑体二号,居中对其,正文是宋体四号,如下 2.另存为xml格式(文件->另存为)的文件,如下图 3.使用txt...
    99+
    2024-04-02
  • PHP合成生成GIF动图
    目录 环境需要: PHP Imagick 示例: php针对项目目录下图片集处理: php处理 base64格式 图片集处理: 说明:主要是针对纯前端生成GIF图片质量问题做的调研;目标:前端把CANVAS处理的每帧图片转成base64...
    99+
    2023-10-06
    php 开发语言
  • Java通过Freemarker模板实现生成Word文件
    目录1.  准备模板2.  代码实现3. PDF文件加水印1.  准备模板 模板 + 数据 = 模型 1、将准备好的Word模板文件另存为.xml文件(...
    99+
    2024-04-02
  • word自动生成目录的步骤是什么
    生成Word文档目录的步骤如下:1. 编写正文内容:在Word文档中编写完整的正文内容,包括标题、子标题和各个章节的内容。2. 设置...
    99+
    2023-09-29
    word
  • Word自动生成目录的方法有哪些
    在Word中,有多种方法可以自动生成目录。以下是一些常见的方法: 使用“自动目录”功能: 在要插入目录的位置,选择“引用”选项...
    99+
    2023-10-21
    Word
  • python实现某考试系统生成word试卷
    本文实例为大家分享了python实现某考试系统生成word试卷的具体代码,供大家参考,具体内容如下 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 准备条件 1.试...
    99+
    2024-04-02
  • C#实现读取txt文件生成Word文档
    目录dll文件安装(3种方法)读取txt生成Word注意事项总结本文将以C#程序代码为例介绍如何来读取txt文件中的内容,生成Word文档。在编辑代码前,可参考如下代码环境进行配置:...
    99+
    2024-04-02
  • PHP生成PDF文件
    吾生也有涯,而知也无涯~欢迎优化补充、指正! 近期有需求是:前端签名后结合平台协议生成签名文件 本例实现方法为:借助canvas生成签名图片,然后贴至协议底部保存为pdf文档 (本例在TP6环境下)借助TCPDF实现  官方提供多个例子点击...
    99+
    2023-09-05
    前端
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作