php doc 入门,phpWord入门使用

一、引入声明PHPWord

$phpWord = new \PhpOffice\PhpWord\PhpWord(); // New Word document

$section = $phpWord->addSection(); // New portrait section

$section->addTextBreak(); // 换行

$section->addPageBreak(); // 下一页

二、引入页眉和页尾

// Add header for all other pages

$header = $section->addHeader();

$header->addText('Subsequent pages in Section 1 will Have this!');

$header->addImage('resources/_mars.jpg', array('width' => 80, 'height' => 80));

// Add footer

$footer = $section->addFooter();

$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER)); // {}中内容为变量可自定义

$footer->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');

三、添加简单文字

$section->addText('Hello World!'); //第一行

$section->addText('Hello World!'); //第二行

$textrun = $section->addTextRun();

$textrun->addText('Hello '); //第一行

$textrun->addText('World!'); //第一行

//添加样式

$section->addText('Hello World!', $h2); //h2为已定义样,下文有介绍样式使用,$h2 可多处使用

$section->addText('bold', array('bold' => true)); // 样式仅限本字段使用

//在用一行中使用不同样式

$textrun = $section->addTextRun();

$textrun->addText('Hello ', $h2); //第一行

$textrun->addText('World!', array('bold' => true)); //第一行

四、添加文本框

// In section

$textbox = $section->addTextBox(

array(

'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,

'width' => 400,

'height' => 150,

'borderSize' => 1,

'borderColor' => '#FF0000',

)

);

$textbox->addText('Text box content in section.');

$textbox->addText('Another line.');

五、添加table

// 基础表格 一行两列,建议总宽度为8920,电脑不同可适当修改

$table = $section->addTable();

$table->addRow(900); //行,行高度度900

$table->addCell(2000)->addText('Row 1'); // 列 宽度2000

$table->addCell(2000)->addText('Row 2'); // 列 宽度2000

// 有样式的表格

$fancyTableStyleName = 'Fancy Table'; // 定义表格名称,不会显示在页面

$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER);

$fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');

$fancyTableCellStyle = array('valign' => 'center');

$fancyTableCellBtlrStyle = array('valign' => 'center', 'textDirection' => \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR);

$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);

}

// 表格中,一列显示多行文字

方法1:

$table->addRow();

$cell = $table->addCell(4460); // 列宽4460

$textrun = $cell->addTextRun();

$textrun->addText('Hello', $h4);

$textrun->addText('World!');

方法2:

$table->addRow(900);

$table->addCell(980)->addText('Hello
World'); //两行,word中换行符

// 以上都为为一行一列

// 表格中列合并居中

$cellColSpan = array('gridSpan' => 2, 'valign' => 'center'); //合并两列 gridSpan 合并列数

$cellHCentered = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER ); //TOP CENTER RIGHT ...

$table->addRow(); //居中样式

$cell1 = $table->addCell(7680, $cellColSpan); // 合并

$textrun1 = $cell1->addTextRun($cellHCentered)->addText('Hello World');

// 行合并 图片占三行 共两列

$row = $table->addRow();

$row->addCell(5000)->addText('SERVICE TYPE: DNA CONSTRUCT');

$row->addCell(null, ['vMerge' => 'restart'])->addImage('images/coa_top_img.png');

$row = $table->addRow();

$row->addCell(5000)->addText('Certificate of Analysis');

$row->addCell(null, ['vMerge' => 'continue']);

$row = $table->addRow();

$row->addCell(5000)->addText('Project ID: ');

$row->addCell(null, ['vMerge' => 'continue']);

六、定义样式

$textrun->addText('color', array('color' => '996699')); //直接调用

$h4 = array('size' => 10, 'bold' => true);

$section->addText('Hello World!', $h4); //调用

$fontStyleName = 'rStyle';

$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));

$section->addText('I am styled by a font style definition.', $fontStyleName); //调用

5,704 total views,  22 views today

Revisions

There are no revisions for this post.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值