标签: phpoffice

PHPOffice/PhpSpreadsheet 获取单元格列索引为整数

如何将单元格列索引设为整数?我已经搜索了文档,但没有找到任何东西。

php excel phpoffice phpspreadsheet

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

我们发现“Spreadsheet.xlsx”中的某些内容存在问题。您希望我们尽力恢复吗?

我有以下基本 PHP 项目(只有一个文件加上 Composer 配置):

作曲家.json

{
    "config": {
        "optimize-autoloader": true,
        "platform": {
            "php": "7.4.9"
        }
    },
    "require": {
        "phpoffice/phpspreadsheet": "1.10.0"
    }
}
Run Code Online (Sandbox Code Playgroud)

索引.php

<?php

require_once __DIR__ . '/vendor/autoload.php';
function errorHandler() {
    return true;
}
set_error_handler('errorHandler');

$sheets = array(
    array('index' => 0, 'title' => 'Graph'),
    array('index' => 1, 'title' => 'Data'),
);

$phpSpreadsheetObject = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

foreach ($sheets as $sheet) {
    $name = $sheet['title'];
    if ($sheet['index']) {
        $worksheet[$name] = $phpSpreadsheetObject->createSheet($sheet['index']);
    } else {
        $worksheet[$name] = $phpSpreadsheetObject->getActiveSheet();
    }
    $phpSpreadsheetObject->setActiveSheetIndex($sheet['index']);
    $worksheet[$name]->setTitle($sheet['title']);
} …
Run Code Online (Sandbox Code Playgroud)

php excel phpoffice phpoffice-phpspreadsheet

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

PhpSpreadsheet 正在损坏文件

我正在使用 PhpSpreadsheet 修改现有文件并将其发送到浏览器,但每次下载文件 excel 时都会出现以下错误:

我们发现 filename.xlsx 中的某些内容存在问题。您希望我们尽可能多地尝试恢复吗?如果您信任此工作簿的来源,请单击是。

我已将所有内容剥离为以下代码。我打开的模板文件是一个全新的 excel 文件,没有对其进行任何编辑(以避免模板中已经存在错误的可能性)。我可以从驱动器打开这个文件,没有任何问题。

$spreadsheet = IOFactory::load(storage_path() ."\Template - English.xlsx");

// Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="filename.xlsx"');
header('Cache-Control: max-age=0');

// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y …
Run Code Online (Sandbox Code Playgroud)

excel phpexcel phpoffice phpspreadsheet

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

PhpSpreadsheet:权限| ZipArchive :: close():无法创建临时文件

我想提供一个Excel文件供PhpSpreadsheet下载

这是我的代码:

    require 'vendor/autoload.php';

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'Hello World !');

    $writer = new Xlsx($spreadsheet);
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="hello_world.xlsx"');
    $writer->save("php://output");
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

    PHP Warning:  ZipArchive::close(): Failure to create temporary file: No such file or directory in /Users/sg/GitWorkingCopies/xxx1/xxx2/library/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php on line 374
    PHP Fatal error:  Uncaught exception 'PhpOffice\PhpSpreadsheet\Writer\Exception' with message 'Could not close zip file php://output.' in /Users/sg/GitWorkingCopies/xxx1/xxx2/library/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php:375
Run Code Online (Sandbox Code Playgroud)

PHPSpreadsheet的dokumentation说:

\ PhpOffice \ PhpSpreadsheet \ Writer \ Xlsx在写入php:// output时使用临时存储。默认情况下,临时文件存储在脚本的工作目录中。如果没有访问权限,它将退回到操作系统的临时文件位置。 …

php phpoffice phpspreadsheet

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

无法正确设置setPrintArea

我正在使用 phpspreadsheet v1.3.1。

我正在构建一个 Excel 文件(下载),一切正常。唯一的问题是定义 setPrintArea...它总是将标准 PrintArea 返回到 excel 中。基本上,我构建了 Excel 行并在定义 printArea 之后...我之前也尝试过这样做,但没有成功。

$spreadsheet = IOFactory::load("template.xlsx");
$worksheet = $spreadsheet->getActiveSheet();

foreach ($rows AS $k => $v ) {
    $worksheet->getCell('F' . ($k + 3))->setValue($v);
    $worksheet->getCell('G' . ($k + 3))->setValue($v);
}

$worksheet->getPageSetup()->setPrintArea('A1:AG58');

...

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output'); //-- force download version
Run Code Online (Sandbox Code Playgroud)

该区域有 58 行,但我无法将区域设置为“AG”单元格...基本上,我想在一页上打印所有 excel,而不是打印到 8 行。

谢谢 !

php phpoffice phpspreadsheet

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

XML解析错误;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)

php xml laravel phpword phpoffice

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

PhpWord TemplateProcessor 克隆表格行并在其中插入信息

我有 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)

invoice laravel phpword phpoffice

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

无法从 url phpoffice/spreadsheet 加载文件

试图从 url 加载 .xlsx 文件,但它给出了类似的错误,

Fatal error: Uncaught InvalidArgumentException: File 
"http://localhost/test/csvfile/samplesms.xlsx" does not exist. in
D:\wamp\www\test\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Shared\File.php on line 137
Run Code Online (Sandbox Code Playgroud)

加载文件如下,

 $filename = "http://localhost/test/csvfile/samplesms.xlsx";
 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
 $reader->setReadDataOnly(TRUE);
 $spreadsheet = $reader->load($filename);
Run Code Online (Sandbox Code Playgroud)

文件已在给定位置。

phpoffice

2
推荐指数
2
解决办法
2454
查看次数

在phpWord模板文档中插入表格

我有里面template.docx有标记的模板${table}。我需要使用phpWord创建表,并在我的插入template.docx,而不是${table}标记内。这是我的代码示例

//Create simple table
$document_with_table = new PhpWord();
$section = $document_with_table->addSection();
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
    $table->addRow();
    for ($c = 1; $c <= 5; $c++) {
        $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
    }
}

//Open template with ${table}
$template_document = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');
// some code to replace ${table} with table from $document_with_table
// ???


//save template with table
$template_document->saveAs('template_with_table.docx');
Run Code Online (Sandbox Code Playgroud)

首先,我$document_with_table使用新的 PhpWord 实例在单独的变量中创建表。接下来我加载我的template.docxin$template_document …

php phpword phpoffice

2
推荐指数
2
解决办法
5867
查看次数

如何使用phpspreadsheet保护单个单元格

我想保护特定的单元格内容不被修改。当我试图保护整张纸时,没问题。

$sheet->getProtection()->setSheet(true)->setDeleteRows(true);
Run Code Online (Sandbox Code Playgroud)

但是,无法为单个单元格设置保护。我尝试了以下代码。

1

$sheet->protectCellsByColumnAndRow(0, 1, 100, 100, 'asdf');
Run Code Online (Sandbox Code Playgroud)

2

$sheet->protectCells('A1','password',false);
Run Code Online (Sandbox Code Playgroud)

提前致谢。

phpoffice phpspreadsheet

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