public function createWord()
{
$phpWord = new PhpWord();
$cmScale = 567;
$imageScale = 30;
$pageH= 21 * $cmScale;
$pageW = 29.7 * $cmScale;
$section = $phpWord->addSection(
['pageSizeW' =>$pageW,
'pageSizeH'=>$pageH,
'orientation'=>'landscape',
'marginTop'=>0.3*$cmScale,
'marginLeft'=>0.3*$cmScale,
'marginRight'=>0.3*$cmScale,
'marginBottom'=>0.3*$cmScale,
]
);
$pageH-=0.6*$cmScale;
$pageW-=0.6*$cmScale;
$table = $section->addTable();
//添加行
$table->addRow($pageH/2-100);
//添加行
$cell = $table->addCell($pageW/4);
$cell->addText('你好啊,111');
$cell = $table->addCell($pageW/4);
$cell->addText('乌拉拉222');
$cell = $table->addCell($pageW/4);
$cell->addText('你好啊333,');
$cell = $table->addCell($pageW/4);
$cell->addText('乌拉拉4444');
//例:
$table->addRow($pageH/2-100);
$cell = $table->addCell($pageW/4,array( 'bgColor'=>'66BBFF'));
$cell->addTextBreak(1);
$textrun1 = $cell->addTextRun(array( 'alignment' =>'center'));
$textrun1->addText('六年级二班兑换积分');
$cell->addTextBreak(2);
$textrun2 = $cell->addTextRun(array( 'alignment' =>'center'));
$textrun2->addText('内容');
$textrun3 = $cell->addTextRun(array( 'alignment' =>'center'));
$textrun3->addText('描述');
$cell->addTextBreak(3);
$imageStyle = array('width'=>3.4*$imageScale, 'height'=>3.4*$imageScale, 'align'=>'center');
$cell->addImage('路径.png', $imageStyle);
$textrun4 = $cell->addTextRun(array( 'alignment' =>'center'));
$textrun4->addText('xxxx');
$phpWord->save('文件名.doc','Word2007',true);
}
html 生成word下载
#html生成word文档并下载
/*
* $arr: $content
* string $fileName
* */
public function downloadWord($content, $fileName='new_file.doc'){
if(empty($content)){
return;
}
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$fileName");
/*$html = '<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">';*/
$html = '
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>';
$html .= '<head><meta charset="UTF-8" /></head>';
echo $html . '<body>'.$content .'</body></html>';
}