2015-08-28 132 views
0

我不知道为什么,但我有一个非常困难的时间让PHPWord正常工作。通过错误工作,我相信我的下面的代码正在运行,但我找不到任何输出。所以要么1)它没有输出任何东西或2)我找不到保存的文件(尽管我相信我已经查看了每个目录)。有没有人有任何想法?谢谢PHPWord保存目录

require_once '/home/public_html/biz/PHPWord-master/src/PhpWord/Autoloader.php'; 
    \PhpOffice\PhpWord\Autoloader::register(); 
    require_once '/home/public_html/biz/PHPWord-master/src/PhpWord/PhpWord.php'; 
    $PHPWord = new \PhpOffice\PhpWord\PhpWord(); 
    $document = $PHPWord->loadTemplate('/home/public_html/biz/WordTemp.docx'); 

    $document->setValue('HERE', 'test'); 

    // Saving the document as OOXML file... 
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007'); 
    $objWriter->save('helloWorld.docx'); 

回答

0

如果我正确地得到它,你正在尝试处理一个模板。 也许我错了,但你只能通过TemplateProcessor来做到这一点。

我使用类似这样的东西。我希望它有帮助。

<?php 
function processTemplate($templateFile,$outputFile,$data){ 
    //PHPWORD INIT 
     require_once __DIR__ . '/../src/PhpWord/Autoloader.php';//Your actual path here obviously, here it is set for the sample dir 

     \PhpOffice\PhpWord\Autoloader::register(); 

     // Template processor instance creation 
     $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templateFile); 
    //CORE 
     foreach($data as $key => $value){ 
      if(is_array($value)) { 
       $rowNum = sizeof($value); 
       $actRow = 0; 
       $templateProcessor->cloneRow($key, $rowNum); 
       foreach($value as $row) { 
        $actRow++; 
        foreach($row as $col => $cellValue){ 
         $templateProcessor->setValue($col."#".$actRow, $cellValue); 
        } 
       } 
      }else{ 
       $templateProcessor->setValue($key, $value); 
      } 
     } 

    //SAVING FILE 
     $templateProcessor->saveAs($outputFile); 
    return true; 
} 
//example 
$data = array(

     "setval1" => "Orange",//for setvalue 

     "setval2" => "Apple", //for setvalue 

     "clonerow1" => array(

      array("clonerow1" => "First entry","animal" => "Elephant","owner" => "Jack"), 

      array("clonerow1" => "Second entry","animal" => "Cat", "owner" => "Alice") 

     ) //for clonerow 

    ); 
?> 
+0

更改为模板处理器确实可以正常工作!谢谢你的帮助。 – Buster