Home > Backend Development > PHP Tutorial > Detailed explanation of how TP framework implements php data export to word

Detailed explanation of how TP framework implements php data export to word

小云云
Release: 2023-03-22 18:28:02
Original
6251 people have browsed it

This article mainly shares with you the detailed explanation of how the TP framework implements PHP data export to word, hoping to help everyone.

1. Use composer to install phpExcel (composer installation method → ​​click me)

composer require phpoffice/phpexcel

2. Introduction of related classes

use PHPWord_IOFactory;
use PHPWord;
Copy after login
3. Specific implementation methods
//调用插件
vendor('PHPWord');
vendor('PHPWord.IOFactory');
$phpWord  =  new \PhpOffice\PhpWord\PhpWord();      //实例化phpWord类
$properties = $phpWord->getDocInfo();
$properties->setCreator('My name');     //创建者
$properties->setCompany('My factory');    //公司
$properties->setTitle('My title');    //biao
$properties->setDescription('My description');    //描述
$properties->setCategory('My category');    //分类
$properties->setLastModifiedBy('My name');    //最后修改者
$properties->setCreated( mktime(0, 0, 0, 3, 12, 2010) );    //创建时间
$properties->setModified( mktime(0, 0, 0, 3, 14, 2010) );    //修改时间
$properties->setSubject('My subject');     //主题
$properties->setKeywords('my, key, word');    //关键字
$sectionStyle = array(
        'orientation' => null,    //页面方向默认竖向
        'marginLeft' => 900, 
        'marginRight' => 900,
        'marginTop' => 900,
        'marginBottom' => 900);
$section = $phpWord->addSection($sectionStyle);    //创建一个有样式的页面
Copy after login

Available section style options:
borderBottomColor. The color of the border bottom.
borderBottomSize. Border bottom size in twips.
borderLeftColor. The border leaves color.
borderLeftSize. Border left size in twips.
borderRightColor. The border is the correct color.
borderRightSize. The correct size of the border in twips.
borderTopColor. The color of the top of the border.
borderTopSize. Border top size in twips.
breakType. Section break type (nextPage, nextColumn, continuous, evenPage, oddPage).
colsNum. 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 title.
marginTop. Page margins in twips.
marginLeft. Page blank (measured in twips).
marginRight. Correct page margins in twips.
marginBottom. Bottom margin in twips.
orientation. Page orientation (portrait, which is the default, or landscape).
pageSizeH. Page height in twips. Implicitly defined by the orientation option. No change is encouraged.

pageSizeW. Page width in twips. Implicitly defined by the orientation option. No change is encouraged.

//设置文本样式
$fontStyle = [
    'bgColor' => 'red',     //字体背景色
    'bold' => true,     //是否加粗
    'size' => '20',
    'color' => 'green',     //字体颜色
    'doubleStrikethrough' => true       //双实线
];
//设置段样式
$paragraphStyle = [
    'bidi' => true,     //从左到左
];
$text = '所添加的文本内容';
//添加文本
$section->addText($text, $fontStyle, $paragraphStyle);
Copy after login

Available font style options:
allCaps. All caps, true or false.
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 highlight colors like yellow, green, blue.
See \PhpOffice\PhpWord\Style\Font::FGCOLOR_... constants for more values ​​
hint. Font content type, default, eastAsia or cs.
italic. Italics, true or false.
name. Font name, such as Arial.
rtl. Right-to-left language, true or false.
size. Font size, e.g. 20, 22.
smallCaps. Small caps, true or false.
strikethrough. Strikethrough, true or false.
subScript. Subscript, true or false.
superScript. Superscript, true or false.
underline. Underline, single, dash, dot, etc.
See \PhpOffice\PhpWord\Style\Font::UNDERLINE_... constants for more values ​​
lang. Language, such as en-US, fr-BE and other language codes or if you need to set the object (or array) of eastAsian or bidirectional language

View \PhpOffice\PhpWord\Style\Language classes for some language codes .

Available paragraph style options:
alignment. Supports all alignment modes since ECMA-376 standard version 1, up to ISO/IEC 29500:2012.
View the details of the \PhpOffice\PhpWord\SimpleType\Jc course.
basedOn. Parental style.
hanging. How much is the price?
indent. How much to shrink.
keepLines. Keep all rows on one page, true or false.
keepNext. Keep the paragraph with the next paragraph, true or false.
lineHeight. The height of the text line, such as 1.0, 1.5, etc.
next. Next style.
pageBreakBefore. Start paragraph on next page, true or false.
spaceBefore. Space before paragraph.
spaceAfter. Space after paragraph.
spacing. The space between the lines.
spacingLineRule. Line spacing rules. Automatically, exactly, atLeast
tabs. A set of custom tabs discontinued.
widowControl. Allow first/last row to be displayed on separate pages, true or false.
contextualSpacing. Ignore top and bottom spacing when using the same style, true or false.
bidi. Right-to-left paragraph layout, true or false.
shading. Paragraph shading.
textAlignment. Vertical character alignment on the line.

View the possible values ​​of \PhpOffice\PhpWord\SimpleType\TextAlignment class.

//添加标题(相关样式需要单独设置)
$phpWord->addTitleStyle(1, $fontStyle, $paragraphStyle);
$section->addTitle('所添加的标题内容', 1);
Copy after login

Add title:

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

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

$linkSrc = 'https://www.baidu.com';     //链接地址
$linkName = '百度搜索';     //链接名称
//添加超链接(相关样式需要单独设置)
$section->addLink($linkSrc, $linkName, $fontStyle, $paragraphStyle);
Copy after login

Add hyperlink:

$section->addLink ($linkSrc, [$linkName], [$fontStyle], [$paragraphStyle]);

//添加页脚方法
$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.');     //向页眉或页脚添加页码或页数
$breakCount = 10;       //设置换行数
$section->addTextBreak($breakCount, $fontStyle, $paragraphStyle);       //设置换行
$section->addPageBreak();   //添加换页符
$section->addListItem("list1", 1, $fontStyle,  $paragraphStyle);    //创建列表
$section->addListItem("list2", 1, $fontStyle,  $paragraphStyle);
$section->addListItem("list3", 1, $fontStyle,  $paragraphStyle);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');  
$objWriter->save('AA.docx');    //生成word文件
Copy after login

Related recommendations:

Quickly master the principles of exporting Word documents from PHP_ PHP tutorial

php export word form

Sample code for php to export word format data

The above is the detailed content of Detailed explanation of how TP framework implements php data export to word. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template