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

标签 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 连接

我正在 letters 文件夹中获取文档,但没有我预期的那么多,它们都是 testLetter.docx 的精确副本,带有 ${Value1},等等,而不是来 self 的查询的数据。

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

最佳答案

我也纠结了一段时间,好像是同一种问题。显然 word 会根据您创建文档的方式生成不同类型的 xml,例如,如果您在编写 ${Value1} 时出错并返回更正它,则生成的 xml 与您正确编写的 xml 不同开始。

在网上搜索我在 https://phpword.codeplex.com/workitem/49 中找到了这段代码这对我有用,也许对你也有用。它将取代 setValue 方法的工作方式。

您还可以在 http://phpword.codeplex.com/discussions/268012 找到更多关于我所说内容的信息。

关于PHPWord 模板循环遍历查询结果而不填充值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21071514/

相关文章:

mysql - Docker Alpine 图像 : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")

没有 Composer 的PHPWord安装

javascript - 获取 php 错误、警告、通知并使用 javascript 提醒他们

php - 在多个页面之间循环(刷新)?

PHP MySQL 更新集

php将日期转换为两位数年份

MySQL自定义函数将罗马数字转换为阿拉伯数字

mysql - WHERE 有两个表和具体表名

PhpWord 不会替换文本

PHPWord 项目符号列表颜色更改