php 来自样式对象的 PHPExcel 特定单元格格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12918586/
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-25 04:29:01  来源:igfitidea点击:

PHPExcel specific cell formatting from style object

phpphpexcel

提问by aaron-bond

I'm using PHPExcel in a project and need to style the cells of the excel sheets.

我在一个项目中使用 PHPExcel,需要设置 Excel 工作表的单元格的样式。

What I've done is create a PHPExcel style object like this:

我所做的是创建一个 PHPExcel 样式对象,如下所示:

$style['red_text'] = new PHPExcel_Style();

I then use the set functions on this style to fill up the object like this:

然后我使用此样式的 set 函数来填充对象,如下所示:

$style['red_text']->getFont()
        ->applyFromArray(
                array('name'=>'Arial')
         )

Now I am trying to use this style object in a cell. I tried to use the applyFromArray function like this:

现在我试图在一个单元格中使用这个样式对象。我尝试像这样使用 applyFromArray 函数:

$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($x, $y)->applyFromArray( $style['red_text'] );

$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($x, $y)->applyFromArray( $style['red_text'] );

This isn't the way to do it I don't think. To me, this is the most readable and consistent way of assigning the styles but if someone who is more fluent with PHPExcel could direct me towards the proper method I'd be much obliged!

这不是我认为的方法。对我来说,这是分配样式的最易读和一致的方式,但如果更熟悉 PHPExcel 的人可以指导我采用正确的方法,我将非常感激!

P.S. Excuse the formatting; this is my first post :)

PS请原谅格式;这是我的第一篇文章 :)

EDIT: just found the error in this: "Invalid style array passed"Does this mean I'm creating the style object wrongly?

编辑:刚刚发现错误:这"Invalid style array passed"是否意味着我错误地创建了样式对象?

回答by Mark Baker

Applying from array is literally applying from an array, not from a style object

从数组应用实际上是从数组应用,而不是从样式对象应用

$style['red_text'] = array(
    'font' => array(
        'name' => 'Arial',
        'color' => array(
            'rgb' => 'FF0000'
        )
    ),
);
$objPHPExcel->getActiveSheet()
    ->getStyleByColumnAndRow($x, $y)
    ->applyFromArray($style['red_text']);

or alternatively:

或者:

$style['red_text'] = array(
    'name' => 'Arial',
    'color' => array(
        'rgb' => 'FF0000'
    )
);
$objPHPExcel->getActiveSheet()
    ->getStyleByColumnAndRow($x, $y)
    ->getFont()
    ->applyFromArray($style['red_text']);

回答by syn

You could also do this:

你也可以这样做:

$objPHPExcel->getActiveSheet()
    ->getStyle('A1:B30')
    ->getFont()
    ->applyFromArray(
        array(
            'name' => 'Arial',
            'color' => array(
                'rgb' => 'FF0000'
            )
        )
    );