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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46959282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:16:24  来源:igfitidea点击:

Styling cell borders with PhpSpreadsheet PHP

phpexcelphpexcel

提问by dardar.moh

I use PhpSpreadsheet to read o write in Excel files. I want to add to my excel a border style so I used this code:

我使用 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;

I get no error but the excel file is created without border. What I miss !??

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

回答by Almagest

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

Replace by :

替换为:

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

Seein line 169-203.

第 169-203 行。

borderStylehas been add after the 1.0.0-beta2 release in 2017-11-26.

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

Before, borders configuration was still with style

之前,边框配置还是用 style

回答by Javlonbek Sharipov

You need reassign value to sheet:

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

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

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