How TP framework implements PHP data export word

Source: Internet
Author: User
Tags phpword
This article is mainly to share with you how the TP framework to achieve PHP data export Word detailed, I hope to help everyone.

1, use composer installation Phpexcel (composer installation method → point me)

Composer require Phpoffice/phpexcel

2. Introduction of related classes

Use Phpword_iofactory;use Phpword;

3, the concrete realization method

Call Plugin vendor (' Phpword '); Vendor (' phpword.iofactory '); $phpWord = new \phpoffice\phpword\phpword ();     Instantiate Phpword class $properties = $phpWord->getdocinfo (); $properties->setcreator (' My name ');    Creator $properties->setcompany (' My factory ');    Company $properties->settitle (' My title ');    Biao$properties->setdescription (' My description ');    Description $properties->setcategory (' My category ');    Category $properties->setlastmodifiedby (' My name ');    Last modified by $properties->setcreated (mktime (0, 0, 0, 3, 12, 2010));    Creation Time $properties->setmodified (mktime (0, 0, 0, 3, 14, 2010));     Modified Time $properties->setsubject (' My subject ');    Theme $properties->setkeywords (' My, key, word '); Keyword $sectionstyle = array (' orientation ' = null,//page orientation default vertical ' marginleft ' = ' Marginr ight ' = ' = ', ' margintop ', ' marginbottom ' and ' (') '; $section = $phpWord->addsection ($sectio    Nstyle); Create a page that has a style

Available Partial style options:
borderBottomColor. The color at the bottom of the border.
Borderbottomsize. The bottom dimension of the border, in twips.
borderLeftColor. The border leaves a color.
Borderleftsize. The left size of the border, in twips.
borderRightColor. The correct color for the border.
Borderrightsize. The correct size of the border (in twips).
borderTopColor. The color at the top of the border.
Bordertopsize. The top dimension of the border (in twips).
Breaktype. The type of section break (Nextpage,nextcolumn,continuous,evenpage,oddpage).
Colsnum. The number of columns.
Colsspace. The spacing between columns.
Footerheight. The spacing at the bottom of the footer.
Gutter. Page gutter spacing.
Headerheight. Spacing to the top of the header.
MarginTop. The page margin in twips.
MarginLeft. The page is blank (in twips).
MarginRight. The correct margin (in twips).
MarginBottom. The bottom margin (in twips) of the margins.
Orientation The page orientation (portrait, which is the default, or Landscape).
Pagesizeh. The height of the page, in twips. Implicitly defined by the Orientation option. Any change is discouraged.

Pagesizew. The width of the page, in twips. Implicitly defined by the Orientation option. Any change is discouraged.

Set Text Style $fontstyle = [    ' bgColor ' + ' red ',     //font background color    ' bold ' = True,     //whether bold    ' size ' = ' 20 ',    ' color ' = ' green ',     //Font color    ' doublestrikethrough ' + true//       double solid line];//set segment style $paragraphstyle = [    ' bidi ' = true,     //left to left]; $text = ' Added text content ';//Add Text $section->addtext ($text, $fontStyle, $ Paragraphstyle);

Available font style options:
AllCaps. All uppercase, right or wrong.
BgColor. Font background color, such as FF0000.
Bold Bold, True or false.
Color Font color, such as FF0000.
DoubleStrikeThrough. Double strikethrough, true or false.
Fgcolor. Font highlighting colors, such as yellow, green, and blue.
See \phpoffice\phpword\style\font::fgcolor_ ... Constants to get more values
Hint Font content type, default, Eastasia or CS.
Italic Italic, True or false.
Name The font name, such as Arial.
Rtl. Right-to-left language, true or false.
Size Font size, such as 20, 22.
SmallCaps. Small capitalization, true or false.
Strikethrough. Delete line, true or false.
Subscript. Subscript, True or false.
Superscript. Superscript, right or wrong.
Underline. Underline, single, dash, dotted, etc.
See \phpoffice\phpword\style\font::underline_ ... Constants to get more values
Lang. Language, such as en-us,fr-be, or if you need to set an object (or an array) of Eastasian or bidirectional languages

View the classes that \phpoffice\phpword\style\language some language codes.

Available paragraph style options:
Alignment Supports all alignment modes since the ECMA-376 Standard 1th Edition, up to ISO/IEC 29,500:2012.
View the details of the \PHPOFFICE\PHPWORD\SIMPLETYPE\JC course.
BasedOn. Parents ' style.
Hanging. How much money to hang.
Indent How much smaller.
Keeplines. Leave all rows on one page, true or FALSE.
KeepNext. Use the next paragraph to preserve the passage, either right or wrong.
Lineheight. The height of the line of text, such as 1.0,1.5, etc.
Next The style of the next paragraph.
PageBreakBefore. Whether to start the paragraph on the next page, True or FALSE.
SpaceBefore. The space before the paragraph.
SpaceAfter. The space behind the paragraph.
Spacing The space between the lines.
Spacinglinerule. The line spacing rule. Automatic, Exact, atLeast
Tabs A set of custom tabs stops.
WidowControl. Allows the first/last line to appear on a separate page, true or FALSE.
Contextualspacing. When the same style is used, the spacing between the upper and lower directions is ignored, true or false.
Bidi Right-to-left paragraph layout, true or false.
Shading. Paragraph shadow.
TextAlignment. Alignment of vertical characters on the line.

View possible values for the \phpoffice\phpword\simpletype\textalignment course.

Add a caption (related styles need to be set separately) $phpWord->addtitlestyle (1, $fontStyle, $paragraphStyle) $section->addtitle (' Added header content ', 1) ;

Add title:

$phpWord->addtitlestyle ($depth, [$fontStyle], [$paragraphStyle]);

$section->addtitle ($text, [$depth]);

$LINKSRC = ' https://www.baidu.com ';     Link address $linkname = ' Baidu search ';     Link name//Add hyperlink (related styles need to be set separately) $section->addlink ($LINKSRC, $linkName, $fontStyle, $paragraphStyle);

To add a hyperlink:

$section->addlink ($LINKSRC, [$linkName], [$fontStyle], [$paragraphStyle]);

//add Footer Method $footer = $section->addfooter (); $footer->addpreservetext ('     Page {page} of {numpages}. ');       Add page numbers or pages to a header or footer $breakcount = 10;       Set number of line breaks $section->addtextbreak ($breakCount, $fontStyle, $paragraphStyle);   Set line break $section->addpagebreak ();    Add a page break $section->addlistitem ("List1", 1, $fontStyle, $paragraphStyle); Create List $section->addlistitem ("List2", 1, $fontStyle, $paragraphStyle); $section->addlistitem ("List3", 1, $  FontStyle, $paragraphStyle); $objWriter = \phpoffice\phpword\iofactory::createwriter ($phpWord, ' Word2007 ');    $objWriter->save (' Aa.docx '); Build Word file 
Related Article

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.