标签: phpoffice

使用 PHPWord 填写 docx 文档时转义“&”

我正在构建一个必须填写 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 符号,这样文件就不会被损坏?

regex string escaping phpword phpoffice

1
推荐指数
1
解决办法
3394
查看次数

PhpExcel单元格中的新行

我所做的:

  1. 从db查询数据
  2. 分解数据并插入\n
  3. 准备数组
  4. 打开模板
  5. 将数组插入到模板中
  6. 尝试用新行替换 \n

如何找到 \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)

php phpexcel phpoffice

1
推荐指数
1
解决办法
9560
查看次数

PHPWord 0.11.0 字体样式下划线

我对 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

我感谢任何帮助,提前致谢。

php codeigniter phpword codeigniter-3 phpoffice

1
推荐指数
1
解决办法
2763
查看次数

PHPOffice写新文件致命错误

我正在使用这个.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)

php phpoffice phpspreadsheet

1
推荐指数
1
解决办法
3759
查看次数

在Laravel phpword中找不到类“ ZipArchive”

我已经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行

zip 已经安装在我的PHP中。 在此处输入图片说明

php laravel phpword phpoffice

0
推荐指数
1
解决办法
990
查看次数

如何在 PHPWord 中向传入的 HTML 添加样式

在 phpword 中,我发现您曾经能够将样式数组传递给 addHTML 函数,但现在不能了。

如果我的 HTML 没有设置任何内联样式,是否有办法指定 fontStyle 和 paragraphStyle,这样它就不会在 Word 文件中以普通的“Arial 10pt”形式结束?

我通过在调用 addHTML 之前设置默认样式取得了一些成功:

$phpWord->setDefaultFontName('...');
$phpWord->setDefaultFontSize('...');
$phpWord->setDefaultParagraphStyle('...');
Run Code Online (Sandbox Code Playgroud)

但是,这不是完整的字体样式。例如,您将如何更改文本的颜色?

phpword phpoffice

0
推荐指数
1
解决办法
6588
查看次数

PhpOffice 将 excel 转换为 json

我正在尝试使用 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)

php phpexcel phpoffice

0
推荐指数
1
解决办法
2744
查看次数