PHPExcel设置单元格的边框颜色

时间:2015-07-03 10:03:22

标签: phpexcel

如何设置单元格的边框颜色? 我使用了这段代码,但它没有用:

$objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getAllBorders()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN)
                ->getColor()
                ->setRGB('DDDDDD');
  

我知道applyFromArray()工作正常。但我不想使用它。

4 个答案:

答案 0 :(得分:24)

有点晚了,但是为了记录,我发现这种语法工作得很好;

{{1}}

我还在这里找到了完整的样式和属性列表:http://www.bainweb.com/2012/01/phpexcel-style-reference-complete-list.html

答案 1 :(得分:3)

可以设置没有applyFromArray()的边框。

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getBottom()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                -> getBorders()
                ->getLeft()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getRight()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getBottom()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);


边框样式列表是

BORDER_NONE - 'none'
BORDER_DASHDOT - 'dashDot'
BORDER_DASHDOTDOT - 'dashDotDot'
BORDER_DASHED - 'dashed'
BORDER_DOTTED - 'dotted'
BORDER_DOUBLE - 'double'
BORDER_HAIR - 'hair'
BORDER_MEDIUM - 'medium'
BORDER_MEDIUMDASHDOT - 'mediumDashDot'
BORDER_MEDIUMDASHDOTDOT - 'mediumDashDotDot'
BORDER_MEDIUMDASHED - 'mediumDashed'
BORDER_SLANTDASHDOT - 'slantDashDot'
BORDER_THICK - 'thick'
BORDER_THIN - 'thin'

更多PHPExcel

答案 2 :(得分:3)

整个工作簿(所有工作表)的默认样式可以设置为:

$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getTop()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getBottom()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getLeft()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getRight()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

您还可以设置为使用applyFromArray()

$styleArray = array(
      'borders' => array(
          'allborders' => array(
              'style' => PHPExcel_Style_Border::BORDER_THIN
          )
      )
  );
$objPHPExcel->getDefaultStyle()->applyFromArray($styleArray);

答案 3 :(得分:0)

网站关闭,因此这里是PHPExcel样式的完整列表:

/ * PHPExcel对象* /

/* Get the default Style object */
(PHPExcel_Style) $style = ((PHPExcel) $excel)->getDefaultStyle()

/ * PHPExcel_Cell对象* /

/* Get the Style object for a Cell */
(PHPExcel_Style) $style = ((PHPExcel) $excel)->getStyle('A1')

/ *样式* /

/* Apply new Style from array */
$style->applyFromArray(
    array(
        'alignment' => array(
            'horizontal' =>
                PHPExcel_Style_Alignment::HORIZONTAL_GENERAL           = 'general'
                PHPExcel_Style_Alignment::HORIZONTAL_LEFT              = 'left'
                PHPExcel_Style_Alignment::HORIZONTAL_RIGHT             = 'right'
                PHPExcel_Style_Alignment::HORIZONTAL_CENTER            = 'center'
                PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous'
                PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY           = 'justify'
            'vertical' =>
                PHPExcel_Style_Alignment::VERTICAL_BOTTOM  = 'bottom'
                PHPExcel_Style_Alignment::VERTICAL_TOP     = 'top'
                PHPExcel_Style_Alignment::VERTICAL_CENTER  = 'center'
                PHPExcel_Style_Alignment::VERTICAL_JUSTIFY = 'justify'
            'rotation' => (int)
            'wrap' => (boolean)
            'shrinkToFit' => (boolean)
            'indent' => (int)
        )
        'borders' => array(
            'allborders' => array(
                'style' => 
                    PHPExcel_Style_Border::BORDER_NONE               = 'none';
                    PHPExcel_Style_Border::BORDER_DASHDOT            = 'dashDot';
                    PHPExcel_Style_Border::BORDER_DASHDOTDOT         = 'dashDotDot';
                    PHPExcel_Style_Border::BORDER_DASHED             = 'dashed';
                    PHPExcel_Style_Border::BORDER_DOTTED             = 'dotted';
                    PHPExcel_Style_Border::BORDER_DOUBLE             = 'double';
                    PHPExcel_Style_Border::BORDER_HAIR               = 'hair';
                    PHPExcel_Style_Border::BORDER_MEDIUM             = 'medium';
                    PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT      = 'mediumDashDot';
                    PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT   = 'mediumDashDotDot';
                    PHPExcel_Style_Border::BORDER_MEDIUMDASHED       = 'mediumDashed';
                    PHPExcel_Style_Border::BORDER_SLANTDASHDOT       = 'slantDashDot';
                    PHPExcel_Style_Border::BORDER_THICK              = 'thick';
                    PHPExcel_Style_Border::BORDER_THIN               = 'thin';
                'color' => array(
                    'rgb' =>
                        PHPExcel_Style_Color::COLOR_BLACK            = 'FF000000';
                        PHPExcel_Style_Color::COLOR_WHITE            = 'FFFFFFFF';
                        PHPExcel_Style_Color::COLOR_RED              = 'FFFF0000';
                        PHPExcel_Style_Color::COLOR_DARKRED          = 'FF800000';
                        PHPExcel_Style_Color::COLOR_BLUE             = 'FF0000FF';
                        PHPExcel_Style_Color::COLOR_DARKBLUE         = 'FF000080';
                        PHPExcel_Style_Color::COLOR_GREEN            = 'FF00FF00';
                        PHPExcel_Style_Color::COLOR_DARKGREEN        = 'FF008000';
                        PHPExcel_Style_Color::COLOR_YELLOW           = 'FFFFFF00';
                        PHPExcel_Style_Color::COLOR_DARKYELLOW       = 'FF808000';
                )

            )
            'left' => // See 'allborders'
            'top' => // See 'allborders'
            'right' => // See 'allborders'
            'bottom' => // See 'allborders'
            'diagonal' =>
            'diagonaldirection' => 
        )
        'fill' => array(
            'type' =>
                PHPExcel_Style_Fill::FILL_NONE                         = 'none';
                PHPExcel_Style_Fill::FILL_SOLID                        = 'solid';
                PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR              = 'linear';
                PHPExcel_Style_Fill::FILL_GRADIENT_PATH                = 'path';
                PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN             = 'darkDown';
                PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY             = 'darkGray';
                PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID             = 'darkGrid';
                PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL       = 'darkHorizontal';
                PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS          = 'darkTrellis';
                PHPExcel_Style_Fill::FILL_PATTERN_DARKUP               = 'darkUp';
                PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL         = 'darkVertical';
                PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625             = 'gray0625';
                PHPExcel_Style_Fill::FILL_PATTERN_GRAY125              = 'gray125';
                PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN            = 'lightDown';
                PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY            = 'lightGray';
                PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID            = 'lightGrid';
                PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL      = 'lightHorizontal';
                PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS         = 'lightTrellis';
                PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP              = 'lightUp';
                PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL        = 'lightVertical';
                PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY           = 'mediumGray';
            'rotation' => (double)
            'startcolor' => // See 'borders' => 'allborders' => 'color'
            'endcolor' => // See 'borders' => 'allborders' => 'color'
            'color' => // See 'borders' => 'allborders' => 'color'
        ) 
        'font' => array(
            'name' =>
                'Arial'
                'Calibri'
                // etc.
            'bold' => (boolean)
            'italic' => (boolean)
            'superScript' => (boolean)
            'subScript' => (boolean)
            'underline' => (boolean)
            'strike' => (boolean)
            'size' => (float)
            'color' => // See 'borders' => 'allborders' => 'color'
        )
        'numberformat' =>
        'protection' =>
    )
)