PHPWord模板循环查询结果不填充值

时间:2014-01-12 05:06:23

标签: php mysql phpword

我收到此错误:

Fatal error: Uncaught exception 'Exception' with message 'Could not close zip file.' in /home/dyken/public_html/PHPWord/Template.php:109 Stack trace: #0 /home/dyken/public_html/lettersWord.php(40): PHPWord_Template->save('MemberLetters.d...') #1 {main} thrown in /home/dyken/public_html/PHPWord/Template.php on line 109

我有这段代码:

$sql = "SELECT distinct p.person_id, fname_mi, lname, company_name,
addr1, addr2, city, st, zip, greeting, email, fullName
FROM person p
INNER JOIN person_category c
on c.person_id = p.person_id
WHERE category_id = 1
order by lname, fname_mi, company_name";

$result = mysqli_query($dlink,$sql);

require_once 'PHPWord.php';

while ($row = mysqli_fetch_array($result)) {

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('letters/testLetter.docx');

$document->setValue('Value1', $row[1]);
$document->setValue('Value2', $row[2]);
$document->setValue('Value3', $row[3]);
$document->setValue('Value4', $row[4]);
$document->setValue('Value5', $row[5]);
$document->setValue('Value6', $row[6]);
$document->setValue('Value7', $row[7]);
$document->setValue('Value8', $row[8]);
$document->setValue('Value9', $row[9]);
$document->setValue('Value10', $row[10]);
$document->setValue('Value11', $row[11]);
$file = 'MemberLetters.docx' . $row[0];
$document->save($file);

if(!$file) {
    // File doesn't exist, output error
    die('file not found');
}
else {
     header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    header("Content-Transfer-Encoding: binary");

    readfile($file);
 }

 unlink($file);

 exit;
  }

testLetter.docx包含以下内容:

${Value1} ${Value2}
${Value3}
${Value4}
${Value5}
${Value6}, ${Value7}  ${Value8}

我无法让查询填充字母中的值。

查询会在网站的其他地方生成结果,所以我知道这有效。

$dlink是我的$host, $user, $password$dbname连接

我在字母文件夹中获取文档,但没有我期望的那么多,它们都是testLetter.docx的{​​{1}}的精确副本,而不是我查询中的数据。< / p>

我尝试了不同的代码变体,但我永远无法获得任何查询结果来代替${Value1}。有人可以帮我这个吗?我是PHPWord的新手。

1 个答案:

答案 0 :(得分:0)

我也有一段时间与此斗争,似乎也是同一类问题。显然,word会生成不同类型的xml,具体取决于您创建文档的方式,例如,如果您在编写$ {Value1}时出错并返回更正它,则生成的xml与从正确编写的xml不同一开始。

在互联网上搜索我发现https://phpword.codeplex.com/workitem/49中的这段代码对我有用,也许可以为你工作。它将取代setValue方法的工作方式。

您还可以在http://phpword.codeplex.com/discussions/268012

找到有关我所谈论内容的更多信息