:::

8-6-2 將值套用到樣板檔

您沒有觀看影片的權限

您沒有觀看影片的權限

請先登入,登入後,確認您的權限後,即可觀看影片。

  1. 完整用法可參考:https://phpword.readthedocs.io/en/latest/templates-processing.html
  2. 套用樣板架構:
    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');
    

     

  3. 若標籤值有HTML語法,需用 strip_tags() 去除
  4. 若有換行,可用此方式處理:
    $值 = str_replace("\n", "</w:t><w:br/><w:t>", 值);

     

  5. 若有連結,連結中的 & 需用 &amp; 取代
  6. 建立 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&amp;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 \


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1788%26tbsn%3D48

書籍目錄

展開 | 闔起

線上使用者

232人線上 (85人在瀏覽線上書籍)

會員: 0

訪客: 232

更多…