PHPExcel更改柱形图的条形颜色

PHPExcel change bar color of column chart

我正在使用 PHPExcel 库来制作包含 4 个柱形图的摘要 excel。我设法做到了,但现在我想更改列的颜色,但我没有找到任何方法来做到这一点。任何帮助将不胜感激。

这就是我构建 excel 文件的方式

private function createReport($result = null, $pdf = false) {

    if ($result != null) {

        $nameFile = "List";

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator('App')->setTitle($nameFile)->setSubject("S")
            ->setCategory("Test data");

        $objWorksheet = $objPHPExcel->getActiveSheet();
        $charstSheet = $objPHPExcel->createSheet();
        $charstSheet->setTitle("Summary");

        $columnArea = "A";
        $columnCount = "B";

        $indexSheet = 0;
        foreach ($result as $result_value_index => $result_value) {

            if ($indexSheet > 0) {
                $sheet = $objPHPExcel->createSheet($indexSheet);
                $dataSheetTitle = 'Worksheet' . $result_value_index;
                $sheet->setTitle($dataSheetTitle);
            }

            $objPHPExcel->setActiveSheetIndex($indexSheet);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $objWorksheet->setSheetState(PHPExcel_Worksheet::SHEETSTATE_HIDDEN);

            $row = 0;
            $currentArea = null;

            for ($j = 0; $j < count($result[$result_value_index]); $j++) {

                $currentArea = $result[$result_value_index][$j];
                $row = $j + 1;

                $objWorksheet->setCellValue($columnArea . $row, $currentArea['Area_name']);
                $objWorksheet->setCellValue($columnCount . $row, $currentArea['ToDo_count']);
            }

            $sheetTitle = $objWorksheet->getTitle();
            $dataSeriesLabels = array(
                new PHPExcel_Chart_DataSeriesValues('String', $sheetTitle . '!$A', NULL, 1)

            );                

            $xAxisTickValues = array(
                new PHPExcel_Chart_DataSeriesValues('String', $sheetTitle . '!$A:$A$' . $row, NULL, $j),    //    Q1 to Q4
            );

            $dataSeriesValues = array(
                new PHPExcel_Chart_DataSeriesValues('Number', $sheetTitle . '!$B:$B$' . $row, NULL, $j),
            );

            //  Build the dataseries
            $series = new PHPExcel_Chart_DataSeries(
                PHPExcel_Chart_DataSeries::TYPE_BARCHART,        // plotType
                PHPExcel_Chart_DataSeries::GROUPING_STANDARD,    // plotGrouping
                range(0, count($dataSeriesValues) - 1),            // plotOrder
                $dataSeriesLabels,                                // plotLabel
                $xAxisTickValues,                                // plotCategory
                $dataSeriesValues                                // plotValues
            );

                       $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);

                           $plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series));

            $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);

            $title = new PHPExcel_Chart_Title();
            if ($currentArea != null) {
               $calculationEngine = PHPExcel_Calculation::getInstance($objPHPExcel);
                $average = round($calculationEngine->calculateFormula('=AVERAGE(B1:B' . $row . ")"));
                $title = new PHPExcel_Chart_Title($currentArea['ParentAreaName'] . " - Promedio ≈ " . $average .  " reservas");
            }

            $yAxisLabel = new PHPExcel_Chart_Title('Reservas');


            //  Create the chart
            $chart = new PHPExcel_Chart(
                'chart1',        // name
                $title,            // title
                NULL,        // legend
                $plotArea,        // plotArea
                true,            // plotVisibleOnly
                0,                // displayBlanksAs
                NULL,            // xAxisLabel
                $yAxisLabel        // yAxisLabel
            );

             // Set the position where the chart should appear in the worksheet

            $chart->setTopLeftPosition('A' . ($indexSheet * 20 + 1));
            $chart->setBottomRightPosition('N' . ($indexSheet * 20 + 20));

            //  Add the chart to the worksheet
            $charstSheet->addChart($chart);

            $indexSheet++;
        }

        $objPHPExcel->setActiveSheetIndexByName("Resumen");


        if (!$pdf){
            $this->export_excel($objPHPExcel, $nameFile);
        } else {
            $this->export_pdf($objPHPExcel, $nameFile);
        }
    }
}

这就是我导出到 excel

的方式
public function export_excel($objPHPExcel,$nameFile){

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');

        $objWriter->setIncludeCharts(TRUE);
        $date = new DateTime();
        $nameFile = $nameFile.'_'.$date->getTimestamp().'.xlsx';
        $objWriter->save('outputfiles/'. $nameFile);
        $url = Router::url('/outputfiles/', true). $nameFile;
        $this->set(array('url' =>$url,'_serialize' => array('url')));
    }

我认为这是一个 hacky 解决方案,但在那一刻我没有找到任何允许我更改颜色的 public 方法。我通过更改位于 PhpExcel/Writer/Excel2007 文件夹中的主题文件第 122 行中变量 'accent1' 的值来解决它。注意我是用Excel2007写的

我post这个以防有人觉得它有用。