PHPExcel更改柱形图的条形颜色

时间:2016-05-04 16:11:07

标签: php phpexcel

我正在使用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$1', NULL, 1)

            );                

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

            $dataSeriesValues = array(
                new PHPExcel_Chart_DataSeriesValues('Number', $sheetTitle . '!$B$1:$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')));
    }

1 个答案:

答案 0 :(得分:2)

我认为这是一个hacky解决方案,但在那个时刻我找不到允许我改变颜色的任何公共方法。我通过改变变量&#39; accent1&#39;的值来解决它。位于PhpExcel / Writer / Excel2007文件夹中的主题文件的第122行。请注意,我使用Excel2007进行编写。

我发布这个以防任何人发现它有用。