PHPExcel特定于样式对象的单元格格式 [英] PHPExcel specific cell formatting from style object

查看:128
本文介绍了PHPExcel特定于样式对象的单元格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我所做的是创建一个像这样的PHPExcel样式对象:

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

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

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

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

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

现在,我正在尝试在单元格中使用此样式对象.我试图像这样使用applyFromArray函数:

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

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

我认为这不是做到这一点的方法.对我来说,这是分配样式的最易读和一致的方法,但是如果更精通PHPExcel的人可以将我引向正确的方法,那我将非常有义务!

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!

P.S.请原谅;这是我的第一篇文章:)

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

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

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

推荐答案

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

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']);

或者:

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

这篇关于PHPExcel特定于样式对象的单元格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆