繁体   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