簡體   English   中英

在創建表格並將其插入MS Word文檔/模板PHP時遇到問題

[英]Encountering problems creating and inserting a table into an MS word doc/template PHP

我試圖在MS Word文檔中創建新表並將其插入,但是遇到了麻煩,我們將不勝感激。 我遇到以下錯誤:

'無法查找`InsertTable':未知名稱。

因此很明顯,我訪問該函數的語法不正確,但是我很難找到可以簡潔地告訴我如何執行此操作的資源。

代碼示例:

//1. Instanciate Word 
        $word1 = new COM("word.application") or die("Unable to instantiate Word");
        echo "Loaded Word, version {$word1->Version}\n";
//2. specify the MS Word template document (with Bookmarks inside) 
        $template_file = "C:\PHP/TEST.docx";
//3. open the template document 
        $word1->Documents->Open($template_file);

// get the bookmark and create a new MS Word Range (to enable text substitution)        
    $bookmarkname2 = "TABLE_Budget";

        if ($word1->ActiveDocument->Bookmarks->Exists($bookmarkname2)) {
            //then create a Table and perform the substitution of bookmark with table 
            $objBookmark1 = $word1->ActiveDocument->Bookmarks($bookmarkname2);
            $table1 = $word1->ActiveDocument->Tables->Add($word1->Selection->Range, 1, 2); //creates table with 2 columns
            //now substitute the bookmark with actual value 
            $objBookmark1->InsertTable = $table1;
        } else {

            echo 'Problem found on ' . $bookmarkname2 . 'insert.';
        }

您沒有提及正在使用的Word PHP庫,因此無法給出具體示例,但是InsertTable很可能是函數而不是參數

你會用它像

$objBookmark1->InsertTable($tableData);

如果您使用的是第三方庫(例如PHPDocX),則可以在其網站上找到文檔( http://www.phpdocx.com/documentation/api-documentation

如果您正在使用COM對象,則可以嘗試搜索MSDN( http://msdn.microsoft.com/zh-cn/library/office/bb726434 ( v=office.12 ) .aspx ),因為該對象將是記錄您正在使用的Word版本

(盡管如果您直接使用COM,則可以嘗試執行以下操作:

//add a table at the bookmark position
$table1 = $word1->ActiveDocument->Tables->Add($objBookmark1->Range, 1, 2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM