我正在构建一个必须填写 docx 文档的 Web 应用程序。我正在使用 PHPWord 提供的 TemplateProcessor,它采用文档上显示的特定钩子并将其替换为给定的字符串。我遇到的问题是,当字符串中出现“&”时,文档会因缺少“;”而损坏。
我用来清除字符串的函数是
$string = trim(html_entity_decode($string));
$string = strip_tags($string);
$string = trim(preg_replace('/\s+/', ' ', $string));
Run Code Online (Sandbox Code Playgroud)
有谁知道如何转义 ampersant 符号,这样文件就不会被损坏?
我所做的:
如何找到 \n 并插入新行?例如,我在 A1 中有下一个文本“One\nTwo”。我从 A1 读取文本
$ltr='A1';
$vle=$spreadsheet->getActiveSheet()->getCell($ltr);
Run Code Online (Sandbox Code Playgroud)
在此之后,我尝试在 A1 中再次在 A1 中插入文本并将 \n 替换为一个单元格中的新行
$spreadsheet->getActiveSheet()->getCell($ltr)->setValue("${vle}");
$spreadsheet->getActiveSheet()->getStyle($ltr)->getAlignment()->setWrapText(true);
Run Code Online (Sandbox Code Playgroud)
但这对我不起作用。
因此,如果我打开带有文本的模板,则无法替换单元格中的 \n。我能做什么?
更新。我尝试使用 char(10) 并插入公式(例如)="hello"&char(10)&"world"但仅插入="hello"&char(10)
我对 PHPWord 有一个问题,我相信版本是 0.11.0,我使用 PHPWord for CI。
问题是我找不到合适的 php 脚本来制作下划线,这是我得到的最新脚本,
$section->addText(
$text,
[ 'underline' => \PhpOffice\PhpWord\Style\AbstractStyle::UNDERLINE_SINGLE ], // Problem with this line
[ 'align' => \PhpOffice\PhpWord\Style\Alignment::ALIGN_CENTER ] // This one fine
);
Run Code Online (Sandbox Code Playgroud)
但是每当我得到脚本时,它总是显示错误或没有给我想要的东西,(带下划线的文本)。
这是我得到的错误
遇到未捕获的异常
类型:错误
消息:未定义的类常量“UNDERLINE_SINGLE”
文件名:C:\xampp\htdocs\test\application\controllers\Print.php
行号:47
回溯:
文件:C:\xampp\htdocs\test\index.php 行:315 功能:require_once
我感谢任何帮助,提前致谢。
我正在使用这个库.xlsx从我的PHP脚本创建文件。
按照本指南,我尝试将一些数据导出到名为hello.xlsx.
但是,此错误会显示以下消息:
警告:ZipArchive::close():创建临时文件失败:供应商/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer 中的权限被拒绝
致命错误:未捕获的 PhpOffice\PhpSpreadsheet\Writer\Exception:无法关闭 zip 文件 hello.xlsx。在第 399 行的 vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php 中
PhpOffice\PhpSpreadsheet\Writer\Exception: 无法关闭 zip 文件 hello.xlsx。在 /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php 第 399 行
该文件不存在预执行所以我有点得到错误。我总是可以尝试touch hello.xlsx更改我的脚本以读取该文件并附加数据,但实际上,每次运行脚本时我都在尝试编写一个新文件。
这是我的(简化)代码:
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'foo');
$sheet->setCellValue('B1', 'bar');
$sheet->setCellValue('C1', 'foobar');
$sheet->setCellValue('D1', 'trey is the best');
$writer = new Xlsx($spreadsheet);
$writer->save('hello.xlsx');
Run Code Online (Sandbox Code Playgroud)
转到错误中的那些行显示以下代码片段:
// Close file
if ($zip->close() === false) {
throw new WriterException("Could not …Run Code Online (Sandbox Code Playgroud) 我已经phpword在laravel项目中安装了软件包。现在,当我尝试docx使用此代码读取文件时
public function upload(Request $request){
$file = $request->file('file');
$phpWord = \PhpOffice\PhpWord\IOFactory::load($file);
foreach($phpWord->getSections() as $section) {
foreach($section->getElements() as $element) {
if(method_exists($element,'getText')) {
echo $element->getText() . "<br>";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
找不到类“ ZipArchive”
/vendor/phpoffice/common/src/Common/XMLReader.php第54行
在 phpword 中,我发现您曾经能够将样式数组传递给 addHTML 函数,但现在不能了。
如果我的 HTML 没有设置任何内联样式,是否有办法指定 fontStyle 和 paragraphStyle,这样它就不会在 Word 文件中以普通的“Arial 10pt”形式结束?
我通过在调用 addHTML 之前设置默认样式取得了一些成功:
$phpWord->setDefaultFontName('...');
$phpWord->setDefaultFontSize('...');
$phpWord->setDefaultParagraphStyle('...');
Run Code Online (Sandbox Code Playgroud)
但是,这不是完整的字体样式。例如,您将如何更改文本的颜色?
我正在尝试使用 PhpOffice (PhpSpreadsheet) 库将 excel 文件转换为特定的 json 输出。Excel 工作表如下所示:
我希望实现这个 json_encode:
{
"As24 sheet": [
{
"Contract": "656203",
"Vehicle card": "0130",
"Driver card": "",
"Product": "03",
"Fuel type": "DIESEL",
"Volume": "92.00",
"Date": "20210316",
"Hour": "0355"
},
{
"Contract": "656203",
"Vehicle card": "0211",
"Driver card": "",
"Product": "03",
"Fuel type": "DIESEL",
"Volume": "74.30",
"Date": "20210316",
"Hour": "0415"
},
{
"Contract": "656203",
"Vehicle card": "0197",
"Driver card": "",
"Product": "03",
"Fuel type": "DIESEL",
"Volume": "33.70",
"Date": "20210316",
"Hour": "0522"
},
{
"Contract": …Run Code Online (Sandbox Code Playgroud)