function word(Request $request){
//创建一个文档
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/* Note: 添加到文档中的任何元素都必须在Section中 */
// 添加新段落 换页
$section = $phpWord->addSection();
// 将文本元素添加到默认字体样式的段落
$section->addText(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
);
/*
* Note: 三种自定义样式方式
* - 1. 内联;
* - 2. 使用命名字体样式(将隐式创建新的字体样式对象);
* - 3. 使用显式创建的字体样式对象.
*/
// 内联样式
//Text addText(string $text, mixed $fStyle = null, mixed $pStyle = null)
$section->addText(
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)',
array('name' => 'Tahoma', 'size' => 10)
);
// 使用命名字体样式自定义字体
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
$fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)',
$fontStyleName
);
// 定制使用显式创建的字体样式对象
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$fontStyle->setColor('1B2232');
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);
// 将文档保存为OOXML文件
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
// 将文档保存为ODF文件,写字板
//$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
//$objWriter->save('helloWorld.odt');
// 将文档保存为HTML文件
//$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
//$objWriter->save('helloWorld.html');
}
function word(Request $request){
//创建一个文档
$phpWord = new \PhpOffice\PhpWord\PhpWord();
//语言
$language = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::ZH_CN);
$phpWord->getSettings()->setThemeFontLang($language);
//字体样式
$fontStyleName = 'rStyle';
$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
//段落样式
$paragraphStyleName = 'pStyle';
//alignment 对齐方式
$phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100));
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
$section = $phpWord->addSection();
// $section = $phpWord->addSection(
// array(
// 'marginLeft' => 200,
// 'marginRight' => 200,
// 'marginTop' => 200,
// 'marginBottom' => 200,
// 'headerHeight' => 50,
// 'footerHeight' => 50,
// )
// );
$section->addTitle('addTitle', 1);
$section->addText('addText addText addText addText');
$section->addTextBreak(2);
$section->addText('字体样式 I am styled by a font style definition.', $fontStyleName);
$section->addText('段落样式 I am styled by a paragraph style definition.', null, $paragraphStyleName);
$section->addText('字体样式 + 段落样式 I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName);
$section->addTextBreak();
// 内联样式
$fontStyle['name'] = 'Times New Roman';
$fontStyle['size'] = 20;
$textrun = $section->addTextRun();
$textrun->addText('I am inline styled ', $fontStyle);
$textrun->addText('with ');
$textrun->addText('color', array('color' => '996699'));
$textrun->addText(', ');
$textrun->addText('bold', array('bold' => true)); //加粗
$textrun->addText(', ');
$textrun->addText('italic', array('italic' => true)); //斜体
$textrun->addText(', ');
$textrun->addText('underline', array('underline' => 'dash')); //下划线
$textrun->addText(', ');
$textrun->addText('strikethrough', array('strikethrough' => true)); //删除线
$textrun->addText(', ');
$textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true));// 双删除线
$textrun->addText(', ');
$textrun->addText('superScript', array('superScript' => true)); //上标
$textrun->addText(', ');
$textrun->addText('subScript', array('subScript' => true)); //下标
$textrun->addText(', ');
$textrun->addText('smallCaps', array('smallCaps' => true)); //小型大写字母
$textrun->addText(', ');
$textrun->addText('allCaps', array('allCaps' => true)); //大写
$textrun->addText(', ');
$textrun->addText('fgColor', array('fgColor' => 'yellow')); //背景色
$textrun->addText(', ');
$textrun->addText('scale', array('scale' => 200)); //缩放
$textrun->addText(', ');
$textrun->addText('spacing', array('spacing' => 120)); //距离
$textrun->addText(', ');
$textrun->addText('kerning', array('kerning' => 10)); //字间距
$textrun->addText('. ');
// 链接
$section->addLink('https://www.baidu.com', '百度');
$section->addTextBreak();
// 图片
$section->addImage('logo.jpg', array('width'=>18, 'height'=>18));
$section->addTextBreak();
$section->addImage('https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png', array('width'=>18, 'height'=>18));
//excel 文件关联
//$textrun->addObject('excel.xls');
// 底部
//$footnote = $section->addFootnote();
//$footnote->addText('底部');
// 底部样式
//$footnoteProperties = new \PhpOffice\PhpWord\ComplexType\FootnoteProperties();
//$footnoteProperties->setNumFmt(\PhpOffice\PhpWord\SimpleType\NumberFormat::DECIMAL_ENCLOSED_CIRCLE);
//$section->setFootnoteProperties($footnoteProperties);
// 换页
$section = $phpWord->addSection();
// 定义样式 tab键距离
$multipleTabsStyleName = 'multipleTab';
$phpWord->addParagraphStyle(
$multipleTabsStyleName,
array(
'tabs' => array(
new \PhpOffice\PhpWord\Style\Tab('left', 1550),
new \PhpOffice\PhpWord\Style\Tab('center', 3200),
new \PhpOffice\PhpWord\Style\Tab('right', 5300),
),
)
);
$rightTabStyleName = 'rightTab';
$phpWord->addParagraphStyle($rightTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', 9090))));
$leftTabStyleName = 'centerTab';
$phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));
// tab内容
$section->addText("Multiple Tabs:\tOne\tTwo\tThree", null, $multipleTabsStyleName);
$section->addText("Left Aligned\tRight Aligned", null, $rightTabStyleName);
$section->addText("\tCenter Aligned", null, $leftTabStyleName);
// 换页
$section = $phpWord->addSection();
// 表格
$rows = 10;
$cols = 5;
$table = $section->addTable();
for ($r = 1; $r <= $rows; $r++) {
$table->addRow();
for ($c = 1; $c <= $cols; $c++) {
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
}
}
$section->addTextBreak(1);
// 表格样式
$fancyTableStyleName = 'Fancy Table';
$fancyTableStyle = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50);
$fancyTableFirstRowStyle = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');
$fancyTableCellStyle = array('valign' => 'center'); //垂直居中
$fancyTableCellBtlrStyle = array('valign' => 'center', 'textDirection' => \PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR); //textDirection文字方向
$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);
}
// html
$section = $phpWord->addSection();
$html = 'Adding element via HTML
';
$html .= 'Some well-formed HTML snippet needs to be used
';
$html .= 'With for example some1 inline formatting1
';
$html .= '
header a
header b
header c
1 2
This is bold text 6
';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// 将文档保存为OOXML文件
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld2.docx');
表格
HTML