使用 PhpSpreadsheet PHP 设置单元格边框样式

Fox*_*Fox 4 php excel phpexcel

我使用 PhpSpreadsheet 读取或写入 Excel 文件。我想在我的 excel 中添加一个边框样式,所以我使用了以下代码:

<?php
    $fxls ='myfile.xlsx';
    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fxls);
    $xls_data = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
    $sheet = $spreadsheet->getActiveSheet();

    $styleArray = array(
        'borders' => array(
            'outline' => array(
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                'color' => array('argb' => 'FFFF0000'),
            ),
        ),
    );

    $sheet ->getStyle('B2:G8')->applyFromArray($styleArray);

    /* Generate the Excel File */
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="myNEWFile.xlsx"');
    header('Cache-Control: max-age=0');
    header('Cache-Control: max-age=1');
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header ('Cache-Control: cache, must-revalidate');
    header ('Pragma: public');
    $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
    $writer->save('php://output');
    exit;
Run Code Online (Sandbox Code Playgroud)

我没有收到错误,但创建的 excel 文件没有边框。我想念什么!?

tot*_*dli 7

tl;博士

除了样式数组方式之外,您还可以使用方法链接方式:

use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;

$spreadsheet
    ->getActiveSheet()
    ->getStyle('B2')
    ->getBorders()
    ->getOutline()
    ->setBorderStyle(Border::BORDER_THICK)
    ->setColor(new Color('FFFF0000'));
Run Code Online (Sandbox Code Playgroud)

公然抄袭单元格格式官方文档

可用的边框图案

单元格格式文档包含可用模式列表及其在样式数组表单中使用的键。对于方法链接形式,只需get在密钥的大写版本前加上a 。这些方法都可以在->getActiveSheet()->getStyle('B2')->getBorders()示例中使用。

  • 在单个单元格上:
    • 剩下: ->getLeft()
    • 对: ->getRight()
    • 最佳: ->getTop()
    • 底部: ->getBottom()
    • 对角线: ->getDiagonal()
  • 在一个区域:
    • 所有边界: ->getAllBorders()
    • 大纲: ->getOutline()
    • 里面: ->getInside()
    • 垂直的: ->getVertical()
    • 水平的: ->getHorizontal()

可视化的模式(也来自文档):

在此处输入图片说明

可用的边框样式

Border::BORDER_DASHDOT
Border::BORDER_DASHDOTDOT
Border::BORDER_DASHED
Border::BORDER_DOTTED
Border::BORDER_DOUBLE
Border::BORDER_HAIR
Border::BORDER_MEDIUM
Border::BORDER_MEDIUMDASHDOT
Border::BORDER_MEDIUMDASHDOTDOT
Border::BORDER_MEDIUMDASHED
Border::BORDER_NONE
Border::BORDER_SLANTDASHDOT
Border::BORDER_THICK
Border::BORDER_THIN
Run Code Online (Sandbox Code Playgroud)


Alm*_*est 6

$styleArray = array(
    'borders' => array(
        'outline' => array(
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => array('argb' => 'FFFF0000'),
        ),
    ),
);
Run Code Online (Sandbox Code Playgroud)

替换为:

$styleArray = array(
    'borders' => array(
        'outline' => array(
            'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => array('argb' => 'FFFF0000'),
        ),
    ),
);
Run Code Online (Sandbox Code Playgroud)

第 169-203 行。

borderStyle 已在 2017 年 11 月 26 日发布 1.0.0-beta2 后添加。

之前,边框配置还是用 style

  • 对不起。我一定是瞎了。但这是错误的。`style` 用于 PHPExcel。他们在 PhpSpreedsheet 中将其更改为“borderStyle”。https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#styles - OPs代码对我有用。 (2认同)
  • 确实是的,但这是 2017 年 12 月 25 日更新的 1.0.0 版本的配置,这篇文章已于 2017 年 10 月发布,当时 phpspreadsheet 为 1.0.0-beta -&gt; https://packagist.org/packages/ phpoffice/phpspreadsheet#1.0.0-beta,你可以在github上看到配置仍然是“style”而不是“borderStyle” -&gt; https://github.com/PHPOffice/PhpSpreadsheet/blob/1.0.0-beta/src /PhpSpreadsheet/Style.php 第 169-203 行 (2认同)

小智 5

您需要为工作表重新分配值:

$styleArray = array(
    'borders' => array(
        'outline' => array(
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => array('argb' => 'FFFF0000'),
        ),
    ),
);

$sheet = $sheet ->getStyle('B2:G8')->applyFromArray($styleArray);
Run Code Online (Sandbox Code Playgroud)