我正在尝试使用PHPWord创建一个word文档,其中包含从MySQL数据库中提取的动态数据.数据库有MySQL字符集:UTF-8 Unicode(utf8)MySQL连接校对:utf8_unicode_ci表字段也是如此.
数据以HTML格式存储和预览,但是在使用阿拉伯语变量创建文档时,Word中的输出看起来像Ø£ØÙد ÙØ¨Ø§Ø±Ù اÙÙØ±Ù.
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('templates/.../wtvr.docx');
$document->setValue('name', $name);
$document->setValue('overall_percent_100', $overall_percent_100);
$document->save('Individual Report - ' . $name . '.docx');
Run Code Online (Sandbox Code Playgroud)
反正有没有解决这个问题?
我有下面的代码片段
//create a new word document
$word= new PHPWord();
//create potrait orientation
$section=$word->createSection();
$table = $section->addTable();
$word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
//header row
$table->addRow(400, array('bgColor'=>'dbdbdb'));
$table->addCell(2000, array('bgColor'=>'dbdbdb'))->addText('Cell 1','rStyle');
$table->addCell(3500, array('bgColor'=>'dbdbdb'))->addText('Cell 1');
$table->addCell(1500, array('bgColor'=>'dbdbdb'))->addText('Cell 1','rStyle');
$table->addCell(2000, array('bgColor'=>'dbdbdb'))->addText('Cell 1');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($word, 'Word2007');
$objWriter->save('Text.docx');
echo 'Text.docx created successfully';
}
Run Code Online (Sandbox Code Playgroud)
如何将文本格式添加到单元格值中以粗体、斜体、字体大小等,我已尝试如上所示,但它不起作用
我需要并排对齐徽标和文字.但是在生成word文档时,文本总是位于徽标图像下方.我尝试了解决方案:http://phpword.codeplex.com/discussions/232684
但它对我不起作用.这是我试过的(不是上面提到的解决方案)
$section = $PHPWord->createSection();
$image = $section->addImage('_mars.jpg',array ('width'=>100));
// Add table
$table = $section->addTable();
for($r = 1; $r <= 1; $r++) { // Loop through rows
// Add row
$table->addRow();
for($c = 1; $c <= 1; $c++) { // Loop through cells
// Add Cell
//I tried adding image in this line.
$table->addCell(1750)->addText("Row $r,Cell ".
$section->addImage('_mars.jpg',array('width'=>100)));
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
$section->addImage() partCatchable fatal error: Object of class PHPWord_Section_Image could not be converted to string in
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何在表格单元格中添加图像?
我\xc2\xb4m 尝试在我的Word 文档中使用块,但我\xc2\xb4m 遇到一些问题。首先,当我在文档中声明一个块时,如果我不\xc2\xb4t使用函数“cloneBlock”,结果将如下所示:
\n\n${sec}\nexample\n${/sec}\nRun Code Online (Sandbox Code Playgroud)\n\n也许我必须使用该功能才能正确显示。但我的主要问题是“deleteBlock”不起作用。如果我不\xc2\xb4t克隆该块,则生成的docx会损坏。但是,如果我克隆该块,函数“deleteBlock”不会删除该块,并且它会在我的最终 docx 文件中显示该块内的信息。
\n\n这是我的代码:
\n\n//Word\n// Creating the new document...\n$templateProcessor = new \\PhpOffice\\PhpWord\\TemplateProcessor(\'../example.docx\');\n//set value\n//$templateProcessor->setValue(\'title\', \'Example\');\n\n//Triplicate block\n$templateProcessor->cloneBlock(\'firstblock\', 3, true, true);\n$templateProcessor->setValue(\'firstname#1\', \'John\');\n$templateProcessor->setValue(\'lastname#1\', \'Doe\');\n$templateProcessor->setValue(\'firstname#2\', \'John\');\n$templateProcessor->setValue(\'lastname#2\', \'Doe\');\n$templateProcessor->setValue(\'firstname#3\', \'John\');\n$templateProcessor->setValue(\'lastname#3\', \'Doe\');\n\n//Delete Block\n$templateProcessor->cloneBlock(\'sec\', 1, true, true);\n$templateProcessor->deleteBlock(\'sec\');\n$templateProcessor->saveAs(\'example.docx\');\nRun Code Online (Sandbox Code Playgroud)\n\n文档模板:
\n\n${firstblock}\nHello ${firstname} ${lastname}!\n${/firstblock}\n${sec}\nexample\n${/sec}\nRun Code Online (Sandbox Code Playgroud)\n\n更新:\n我没有使用函数“deleteBlock”,而是使用函数“cloneBlock”,如下所示,它删除了该块:
\n\n//Delete Block\n$templateProcessor->cloneBlock(\'sec\', 0, true, true);\nRun Code Online (Sandbox Code Playgroud)\n\n因此,我已写入克隆该块 0 次,因此它消失了\n但我还有另一个问题。我不\xc2\xb4t 知道为什么,但这只是有时有效
\n我想知道是否还有左右对齐同一行的一些文字.例如,简历的公司名称与左侧对齐,日期与右侧对齐,在同一条线上.
我试图用文本运行来做这件事,但似乎没有用.我知道我可以使用,\t\t但左侧文本的长度不同,因此选项卡将非常不一致.
这只是一个简单的例子:
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText("Left Text", array(), array("align" => "left"));
$textrun->addText("Right Text", array(), array("align" => "right"));
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.谢谢.
是否可以使用 phpword 编辑现有的 docx 文件?
我想将页脚文本添加到我现有的 docx 文件中。
有很多例子,但这些例子是从头开始创建 doc 文件而不是编辑文件有人可以链接给我一个例子吗?谢谢你
像这样。
<?php
require_once 'PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add header
$header = $section->createHeader();
$table = $header->addTable();
$table->addRow();
$table->addCell(4500)->addText('This is the header.');
$table->addCell(4500)->addImage('_earth.jpg', array('width'=>50, 'height'=>50, 'align'=>'right'));
// Add footer
$footer = $section->createFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', array('align'=>'center'));
// Write some text
$section->addTextBreak();
$section->addText('Some text...');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('HeaderFooter.docx');
?>
Run Code Online (Sandbox Code Playgroud) 我可以使用 PHPWord 生成下载一个从头开始生成的 Word 文档: 创建 PhpWord 对象:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
Run Code Online (Sandbox Code Playgroud)
添加部分和行(省略)并创建标题:
$datetime = date('Y_M_d_G_i');
$filename = 'receipt_' . $datetime . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
Run Code Online (Sandbox Code Playgroud)
从 phpWord 对象创建一个 writer:
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
Run Code Online (Sandbox Code Playgroud)
写入输出:
$xmlWriter->save("php://output");
Run Code Online (Sandbox Code Playgroud)
我未能做的是从服务器上的模板下载文档: 创建 TemplateProcessor 有效:没有错误并且 PHP 将 $templateProcessor 识别为该类的对象:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor(asset_url() . 'templates/receipt_template.docx');
Run Code Online (Sandbox Code Playgroud)
但我不知道如何写入输出。如果我可以生成 PhpWord 对象,那么我可以使用 IOFactory::createWriter 方法,但 TemplateProcessor 没有返回 PhpWord 对象的方法。
我能得到的最接近的是尝试从 IOFactory::load 创建 $phpWord …
我在Laravel应用程序中使用PHPOffice / PHPWord。它用于生成带有表结果的.docx文档。这对于包含6行的3个表的文档非常有用,但是当有更多行时会生成该文档,但是在打开它时会发生以下错误:
We're sorry, We can't open (documentname) because we found a problem with its contents.
Details: XML parsing error Location: Part:/word/document.xml, Line: 2, Column 14349.
Run Code Online (Sandbox Code Playgroud)
现在,我开始在另一个结果页面上工作,在该页面上我还想生成一个.docx文档。这将包含5个表,但是在3行中,我得到相同的XML解析错误,但是位置不同(位置:Part:/word/document.xml,Line:4,Column:2888)。有人可以向我解释这是我的代码还是phpword / words错误吗?
我已经通过删除所有内容并缓慢添加新行来进行一些故障排除。我发现了错误,但我该如何解决。前两个表生成良好。
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addImage('../public/img/2.jpg', array('width' => 230, 'height' => 65, 'alignment' => 'left'));
$section->addText('Project IDs:' . $parameter);
$header =$section->addHeader();
$header->addText('Results Summary');
$section->addLine(
array(
'width' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
'height' => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
'positioning' => 'absolute',
)
);
$tableName = 'rStyle';
$phpWord->addFontStyle($tableName, array('italic' => true, 'size' => 12));
$thName …Run Code Online (Sandbox Code Playgroud) 我正在尝试将用户详细信息导出到.docx文件。该文件已成功导出,但是正在提供对象。phpword格式不起作用。如何编写代码以正确导出动态值,请有人帮我吗?
在AdminController.php中 -
public function exportUserToDoc(Request $request, $id)
{
$wordTest = new \PhpOffice\PhpWord\PhpWord();
$newSection = $wordTest->addSection();
$desc1 = User::find($id);
$newSection->addText($desc1, array('name' => $desc1->name, 'email' => $desc1->email, 'phone' => $desc1->phone, 'address' => $desc1->address));
$objectWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordTest, 'Word2007');
try{
$objectWriter->save(storage_path('TestWordFile.docx'));
}catch (Exception $e){
}
return response()->download(storage_path('TestWordFile.docx'));
}
Run Code Online (Sandbox Code Playgroud)
下载doc文件后,结果类似于-
{"id":1,"name":"Emdadul Huq Shikder","email":"emdadulshikder@gmail.com","phone":"+8801674338411","address":"59\/6\/1 West Razabazar, Dhaka","status":0,"created_at":"2018-03-13 05:18:32","updated_at":"2018-03-13 05:35:51"}
Run Code Online (Sandbox Code Playgroud)
我想获取格式正确的结果,例如标头“名称”,“电子邮件”等。
我有 phpword 表的问题。

我有下表,我想克隆第一个表行并替换其中的信息。到目前为止,我没有任何进展。我使用getVariables()方法从文档中获取所有变量并循环遍历它们。我已经检查过该值是否是数组,如果是它属于该行。我按照以下方式构建了数据
Collection {#971 ?
#items: array:12 [?
"ticket_id" => array:1 [?
0 => 7
0 => 6
]
"ticket_number" => array:2 [?
0 => "157-12313121321"
1 => null
]
"price_offered_bgn" => array:2 [?
0 => 978.0
1 => 196.0
]
"ticket_is" => array:1 [?
0 => "Requested"
]
"departure_date" => array:2 [?
0 => "2020-10-20 00:00:00"
1 => "2020-01-29 00:00:00"
]
"return_date" => array:2 [?
0 => "2020-10-29 00:00:00"
1 => null
]
"company_address" => …Run Code Online (Sandbox Code Playgroud)