PHPOffice/PHPExcel 设置单元颜色, 行宽, 加粗

场景

. 限制于php版本的问题 选择了这个包
. 使用这个包肯定要有这个问题的

解决

. 颜色
$excel_php = new \PHPExcel();
$obj_color = ($product['diff_count'] > 0) ? \PHPExcel_Style_Color::COLOR_BLUE : \PHPExcel_Style_Color::COLOR_RED;
$excel_php->setActiveSheetIndex(0)->getStyle('H' . $row)->getFont()->getColor()->setARGB($obj_color);
. 行宽
            // 自动调整行宽
            $item_cell = 'A';
            $excel_php->setActiveSheetIndex(0)
                ->getColumnDimension($item_cell)
                ->setAutoSize(true);
. 加粗
    1. 数组的形式
             $item_cell = 'A';
            $excel_php->setActiveSheetIndex(0)->getStyle($item_cell . 1)->applyFromArray(
                [
                    'font' => ['bold' => true],
                    'alignment' => ['horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER]
                ]
            );
    2. 单行的形式
$excel_php->setActiveSheetIndex(0)->getStyle("A2:A19")->getFont()->setBold(true);
. 居中
    1. 全局居中
     $excel_php->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $excel_php->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

你可能感兴趣的:(php)