Laravel Word document Generation-Phpword

Source: Internet
Author: User
Tags phpword xml parser zip extension composer install zend

In actual project development or learning, you will encounter the need to export data to a Word document. Here is the elegant, popular laravel framework, to simply teach you to achieve.
Phpword, it makes it easy to build Word documents, and you can add tables, catalogs, pictures, hyperlinks, headers, footers, and more in Word.

Installation Phpword Requirements

Mandatory:

PHP 5.3.3+

    • XML Parser Extension
    • Zend \ Escaper Components
    • Zend \ Stdlib Components
    • Zend \ Validator Components

Optional:

-Zip extension
-GD Extension
-XmlWriter Extension
-XSL Extension
-Dompdf Library

Phpword is installed via composer. You only need to add a dependency to Phpword in your package.

{"     require": {     "Phpoffice/phpword": "v0.14". *?? "     }}

Add after Execute

Composer Install

  

Alternatively, you can install it through the command line, such as

Composer require Phpoffice/phpword

Sample code
1 $phpWord=New\phpoffice\phpword\phpword ();2         //Set Default Style3         $phpWord->setdefaultfontname (' imitation ');//Font4         $phpWord->setdefaultfontsize (16);//Font Size5 6 //Add page7         $section=$phpWord-createsection ();8 9         //Add DirectoryTen         $styleTOC= [' tableader ' = + \phpoffice\phpword\style\toc::Tableader_dot]; One         $styleFont= [' spaceafter ' = ', ' name ' = ' Tahoma ', ' size ' = 12]; A         $section->addtoc ($styleFont,$styleTOC); -  -         //Default Style the         $section->addtext (' Hello php! '); -         $section->addtextbreak ();//line Break -  - //The specified style +         $section-AddText ( -' Hello world! ', +             [ A' Name ' = ' Arial ', at' Size ' = 16, -' Bold ' =true, -             ] -         ); -         $section->addtextbreak (5);//Multiple newline characters -  in //Custom style -         $myStyle= ' MyStyle '; to         $phpWord-Addfontstyle ( +             $myStyle, -             [ the' Name ' = ' Verdana ', *' Size ' = 12, $' Color ' = ' 1bff32 ',Panax Notoginseng' Bold ' =true, -' SpaceAfter ' = 20, the             ] +         ); A         $section->addtext (' Hello laravel! ',$myStyle); the         $section->addtext (' Hello vue.js! ',$myStyle); +         $section->addpagebreak ();//page Breaks -  $ //Add a text resource $         $textrun=$section-Createtextrun (); -         $textrun->addtext (' bold ', [' bold ' = =true]); -         $section->addtextbreak ();//line Break the         $textrun->addtext (' tilt ', [' italic ' =true]); -         $section->addtextbreak ();//line BreakWuyi         $textrun->addtext (' Font Color ', [' color ' = ' AACC00 ')]); the  -         //Hypertext Links Wu         $linkStyle= [' Color ' = ' 0000FF ', ' underline ' = ' \phpoffice\phpword\style\font::Underline_single]; -         $phpWord->addlinkstyle (' Mylinkstyle ',$linkStyle); About         $section->addlink (' http://www.baidu.com ', ' Baidu a bit ', ' mylinkstyle '); $         $section->addlink (' http://www.baidu.com ',NULL, ' Mylinkstyle '); -  -         //Add a picture -         $imageStyle= [' width ' = + 480, ' height ' = ' 640 ', ' align ' = ' center ']; A         $section->addimage ('./img/t1.jpg ',$imageStyle); +         $section->addimage ('./img/t2.jpg ',$imageStyle); the  -         //Add title $         $phpWord->addtitlestyle (1, [' bold ' = =true, ' color ' = ' 1bff32 ', ' size ' = ', ' name ' = ' Verdana ']); the         $section->addtitle (' Heading 1 ', 1); the         $section->addtitle (' Heading 2 ', 1); the         $section->addtitle (' Heading 3 ', 1); the  -         //Add a table in         $styleTable= [ the' BorderColor ' = ' 006699 ', the' Bordersize ' = 6, About' Cellmargin ' = 50, the         ]; the         $styleFirstRow= [' bgColor ' = ' 66BBFF '];//First line Style the         $phpWord->addtablestyle (' myTable ',$styleTable,$styleFirstRow); +  -         $table=$section->addtable (' myTable '); the         $table->addrow (400);//Line HeightBayi         $table->addcell (+)->addtext (' School Number ')); the         $table->addcell (->addtext) (' name ')); the         $table->addcell (->addtext) (' Professional '); -         $table->addrow (400);//Line Height -         $table->addcell (+)->addtext (' 2015123 '); the         $table->addcell (+)->addtext (' Xiao Ming ')); the         $table->addcell (->addtext) (' Computer Science and Technology ')); the         $table->addrow (400);//Line Height the         $table->addcell (+)->addtext (' 2016789 '); -         $table->addcell (+)->addtext (' Little fool ')); the         $table->addcell (->addtext) (' Pedagogy technology ')); the  the         //Header and Footer94         $header=$section-CreateHeader (); the         $footer=$section-Createfooter (); the         $header->addpreservetext (' header '); the         $footer->addpreservetext (' footer-page {page}-{numpages}. '));98  About         //The resulting document is Word2007 -         $writer= \phpoffice\phpword\iofactory::createwriter ($phpWord, ' Word2007 ');101         $writer->save ('./word/hello.docx '));102 103         //Save the document as an ODT file ...104         $writer= \phpoffice\phpword\iofactory::createwriter ($phpWord, ' Odtext '); the         $writer->save ('./word/hello.odt '));106 107         //Save the document as an HTML file ...108         $writer= \phpoffice\phpword\iofactory::createwriter ($phpWord, ' HTML ');109         $writer->save ('./word/hello.html ');

Note When Word goes to HTML, the Word document loads the directory that is written relative to the path, and the absolute path is written
will be error, this I did not solve, if there are methods please leave a message

Reference Document Address: http://phpword.readthedocs.io/en/latest/intro.html

As follows:

Laravel Word document Generation-Phpword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.