8-6-2
將值套用到樣板檔
您沒有觀看影片的權限
請先登入,登入後,確認您的權限後,即可觀看影片。
- 完整用法可參考:https://phpword.readthedocs.io/en/latest/templates-processing.html
- 套用樣板架構:
use \PhpOffice\PhpWord\TemplateProcessor;
require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/autoload.php';
$templateProcessor = new TemplateProcessor("樣板檔.docx");
$templateProcessor->setValue('標籤', $標籤值);
//$templateProcessor->saveAs('檔名');
header('Content-Type: application/vnd.ms-word');
header("Content-Disposition: attachment;filename=檔名.docx");
header('Cache-Control: max-age=0');
$templateProcessor->saveAs('php://output');
- 若標籤值有HTML語法,需用
strip_tags()
去除
- 若有換行,可用此方式處理:
$值 = str_replace("\n", "</w:t><w:br/><w:t>", 值);
- 若有連結,連結中的
&
需用 &
取代
- 建立
word.php
<?php
use Xmf\Request;
use XoopsModules\Tad_signup\Tad_signup_actions;
use \PhpOffice\PhpWord\TemplateProcessor;
/*-----------引入檔案區--------------*/
require_once __DIR__ . '/header.php';
require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/autoload.php';
if (!$_SESSION['can_add']) {
redirect_header($_SERVER['PHP_SELF'], 3, "您沒有權限使用此功能");
}
$id = Request::getInt('id');
$action = Tad_signup_actions::get($id);
$templateProcessor = new TemplateProcessor("signup.docx");
$templateProcessor->setValue('title', $action['title']);
$templateProcessor->setValue('detail', strip_tags($action['detail']));
$templateProcessor->setValue('action_date', $action['action_date']);
$templateProcessor->setValue('end_date', $action['end_date']);
$templateProcessor->setValue('number', $action['number']);
$templateProcessor->setValue('candidate', $action['candidate']);
$templateProcessor->setValue('signup', count($action['signup']));
$templateProcessor->setValue('url', XOOPS_URL . "/modules/tad_signup/index.php?op=tad_signup_data_create&action_id={$action['id']}");
// $templateProcessor->saveAs("{$action['title']}報名名單.docx");
header('Content-Type: application/vnd.ms-word');
header("Content-Disposition: attachment;filename={$action['title']}報名名單.docx");
header('Cache-Control: max-age=0');
$templateProcessor->saveAs('php://output');
link to https://github.com/tadlearn/tad_signup/commit/d3730f8440aa83e6adf280ddec385da29c5f87b0 \