一、PHPword生成word文件原理

2003以上版本(不含2003)的word是通过VML(矢量可标记语言,详细查看微软的开发文档)来生成word文件,同理,word也可以另存为包含VML的xml文件。

phpword会先把要输出信息 还有样式保存为xml文件,这个xml文件就是根据VML生成的,

不同的信息会保存在不同的xml文件中,

 $this->parts = array(
            'ContentTypes'   => '[Content_Types].xml',
            'Rels'           => '_rels/.rels',
            'DocPropsApp'    => 'docProps/app.xml',
            'DocPropsCore'   => 'docProps/core.xml',
            'DocPropsCustom' => 'docProps/custom.xml',
            'RelsDocument'   => 'word/_rels/document.xml.rels',
            'Document'       => 'word/document.xml',  //保存要写入到word的信息
            'Styles'         => 'word/styles.xml',    //保存全局的样式
            'Numbering'      => 'word/numbering.xml',
            'Settings'       => 'word/settings.xml',
            'WebSettings'    => 'word/webSettings.xml',
            'FontTable'      => 'word/fontTable.xml',
            'Theme'          => 'word/theme/theme1.xml',
            'RelsPart'       => '',
            'Header'         => '',
            'Footer'         => '',
            'Footnotes'      => '',
            'Endnotes'       => '',
            'Chart'          => '',

然后用php自带的zipArchive 类把以上的xml写到word文件中

二、VML格式示例

<w:body><w:p><w:pPr/><w:r><w:pict><v:shape type="#_x0000_t0202" style="width:89.285714285714px; height:617.85714285714px; margin-left:0px; margin-top:0px; position:relative; mso-position-horizontal:left; mso-position-vertical:center; mso-position-horizontal-relative:page; mso-position-vertical-relative:page; layout-flow:vertical; mso-layout-flow-alt:bottom-to-top;strokecolor:red;" ><w10:wrap type="inline" anchorx="page" anchory="page"/><v:stroke/><v:textbox><w:txbxContent><w:p><w:r><w:rPr/><w:t xml:space="preserve">学校</w:t></w:r><w:r><w:rPr><w:u w:val="single"/></w:rPr><w:t xml:space="preserve">      </w:t></w:r><w:r><w:rPr/><w:t xml:space="preserve">He is a boy</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></w:r></w:p><w:sectPr><w:footerReference w:type="default" r:id="rId7"/><w:pgSz w:orient="portrait" w:w="11870" w:h="16787"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:num="1" w:space="720"/></w:sectPr></w:body>

二、PHPWord使用手册

https://phpword.readthedocs.org/en/latest/

三、解决word中文本和图片无法居中居中对齐

默认word是按照基线对齐的,如

如果想要让他按照居中对齐,如

就需要修改保存样式的xml文件styles.xml,在其写入

<w:docDefaults><w:pPrDefault><w:pPr><w:textAlignment w:val="center" /></w:pPr></w:pPrDefault></w:docDefaults>

参数详解参考:https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.textalignment%28v=office.14%29.aspx

四、解决文本框中的文字无法改变文字方向问题

四、PHPWord源码下载

https://github.com/PHPOffice/PHPWord

http://www.cnblogs.com/forlina/archive/2011/06/09/2076559.html

https://msdn.microsoft.com/en-us/library/hh846327%28v=vs.85%29.aspx

一、PHPword生成word文件原理phpword会先把要输出的文本或图片 还有样式、字体等信息保存为xml文件,不同的信息会保存在不同的xml文件中, $this->parts = array( 'ContentTypes' => '[Content_Types].xml', 'Rels' => '_rels/
最近一个项目开发要用到PHP技术导出Word文档,比较了几种方案,首先是使用Microsoft Office自带的ActiveX/COM组件,比如Word.Application,这种方式的优点是格式兼容度高,可以生成纯doc的Word2003格式文档,缺点一是比较占资源(调用会启动一个WINWORD.EXE进程),不适合Web多用户访问使用;二是PHP这种Web开发技术大多数是跑在Linux服务器上,当然也就无法使用Windows下的技术了,平台可移植和兼容性不好。 第二种生成Word的方案是生成Word兼容的网页格式,然后以Word方式打开,这种方案总体上感觉怪怪的,毕竟文件格式是HTML
一. 解析步骤二. OPCPackage当使用POI事件模式解析Excel XLSX文档时: POI根据xlsx文档的路径path获取到文件File - file 使用java.util.zip.ZipFile打开file文件 - zip 从zip中获取到[Content_Types].xml 解析[Content_Types].xml,记录解析出Excel各个xml名称:ArrayList
  因为最近项目需要一个生成Word文档的功能,开始折腾起来,找到PhoWord这个类库,PhpWord是一个可以用PHP生成word的类库,用composer安装就可以用了。   搞了半天,终于生成Word文档,现在来记录一下一些用法供大家参考。 PhpWord文档   想实现更多功能同学的可以仔细参考文档   https://phpword.readthedocs.io/en/latest...
This section summarizes the changes in Word 2010. Custom XML markup from XML-based file formats Word 2010 no longer reads the custom XML
To generate a Microsoft Word document using PHP, you can use the PHPWord library, which provides a simple and easy-to-use API for creating and manipulating Word documents in PHP. Here's an example of how to create a basic Word document using PHPWord: 1. First, download and install the PHPWord library by running the following command in your terminal: composer require phpoffice/phpword 2. Next, create a new PHP script and include the PHPWord autoload file at the top of your script: require_once 'vendor/autoload.php'; 3. Create a new instance of the `PhpOffice\PhpWord\PhpWord` class: $phpWord = new \PhpOffice\PhpWord\PhpWord(); 4. Add a new section to the document: $section = $phpWord->addSection(); 5. Add some text to the section: $section->addText('Hello, world!'); 6. Finally, save the document to a file: $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $writer->save('hello_world.docx'); This will create a new Word document named `hello_world.docx` in the current directory, containing the text "Hello, world!" in the first section. You can customize the document further by adding more sections, paragraphs, tables, images, and other elements using the PHPWord API. For more information and examples, see the PHPWord documentation.