如何部分填充背景线phpword
How to part fill background line phpword
目前我可以为我想要的每个事件设置背景填充颜色。
如何达到图中所示的整条线
Question How can I make it go to the day number like in this image
代码
$results = $this->get_events_for_export();
foreach ($results as $result) {
//$date = strtotime($result['event_date']);
$date = strtotime($result['event_date']);
$month_group = date('M', $date);
$year_group = date('Y', $date);
if (!isset($month_isset[$year_group][$month_group]))
{
$month_isset[$year_group][$month_group] = [];
} else {
$month_group = '';
}
if (!empty($result['bg_color'])) {
$font = array('fgColor' => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW);
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), $font, $event_lists);
} else {
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $event_lists);
}
}
完整代码
public function export() {
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$leftTabStyleName = 'centerTab';
$phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));
// New portrait section
$section = $phpWord->addSection();
// Add listitem elements
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(false);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(16);
$section->addText("\tClub Program " . date('Y') .' / ' . date('Y', strtotime('+1 year')), $fontStyle, $leftTabStyleName);
$section->addTextBreak();
$event_lists = 'event_lists';
$phpWord->addParagraphStyle(
$event_lists,
array(
'tabs' => array(
new \PhpOffice\PhpWord\Style\Tab('left', 1000),
new \PhpOffice\PhpWord\Style\Tab('center', 1000),
new \PhpOffice\PhpWord\Style\Tab('right', 1000),
)
)
);
$results = $this->get_events_for_export();
foreach ($results as $result) {
//$date = strtotime($result['event_date']);
$date = strtotime($result['event_date']);
$month_group = date('M', $date);
$year_group = date('Y', $date);
if (!isset($month_isset[$year_group][$month_group]))
{
$month_isset[$year_group][$month_group] = [];
} else {
$month_group = '';
}
if (!empty($result['bg_color'])) {
$font = array('fgColor' => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW);
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), $font, $event_lists);
} else {
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $event_lists);
}
}
$filename = 'club_program-' . time() . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save("php://output");
exit();
}
在addText()
之前添加addTextRun()
方法:
$textrun = $section->addTextRun();
if (!empty($result['bg_color'])) {
$font = array('fgColor' => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW);
$textrun->addText($month_group ."\t");
$textrun->addText(date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), $font, $event_lists);
} else {
$textrun->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $event_lists);
}
目前我可以为我想要的每个事件设置背景填充颜色。
如何达到图中所示的整条线
Question How can I make it go to the day number like in this image
代码
$results = $this->get_events_for_export();
foreach ($results as $result) {
//$date = strtotime($result['event_date']);
$date = strtotime($result['event_date']);
$month_group = date('M', $date);
$year_group = date('Y', $date);
if (!isset($month_isset[$year_group][$month_group]))
{
$month_isset[$year_group][$month_group] = [];
} else {
$month_group = '';
}
if (!empty($result['bg_color'])) {
$font = array('fgColor' => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW);
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), $font, $event_lists);
} else {
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $event_lists);
}
}
完整代码
public function export() {
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$leftTabStyleName = 'centerTab';
$phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));
// New portrait section
$section = $phpWord->addSection();
// Add listitem elements
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(false);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(16);
$section->addText("\tClub Program " . date('Y') .' / ' . date('Y', strtotime('+1 year')), $fontStyle, $leftTabStyleName);
$section->addTextBreak();
$event_lists = 'event_lists';
$phpWord->addParagraphStyle(
$event_lists,
array(
'tabs' => array(
new \PhpOffice\PhpWord\Style\Tab('left', 1000),
new \PhpOffice\PhpWord\Style\Tab('center', 1000),
new \PhpOffice\PhpWord\Style\Tab('right', 1000),
)
)
);
$results = $this->get_events_for_export();
foreach ($results as $result) {
//$date = strtotime($result['event_date']);
$date = strtotime($result['event_date']);
$month_group = date('M', $date);
$year_group = date('Y', $date);
if (!isset($month_isset[$year_group][$month_group]))
{
$month_isset[$year_group][$month_group] = [];
} else {
$month_group = '';
}
if (!empty($result['bg_color'])) {
$font = array('fgColor' => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW);
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), $font, $event_lists);
} else {
$section->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $event_lists);
}
}
$filename = 'club_program-' . time() . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save("php://output");
exit();
}
在addText()
之前添加addTextRun()
方法:
$textrun = $section->addTextRun();
if (!empty($result['bg_color'])) {
$font = array('fgColor' => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW);
$textrun->addText($month_group ."\t");
$textrun->addText(date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), $font, $event_lists);
} else {
$textrun->addText($month_group ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $event_lists);
}