2016-05-04 93 views
2

我正在使用PHPExcel庫,以便對4列柱狀圖進行總結。我設法做到了這一點,但現在我想改變列的顏色,我沒有找到任何方法來做到這一點。任何幫助將非常感激。PHPExcel更改柱狀圖的顏色

這是我建立我的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'))); 
    } 
+0

@ Fred -ii,@GrumpyCrouton,對不起,我不是問如何改變單元格的顏色,我看到了,我想改變圖表中條形的顏色。 – lfal

回答

2

我認爲這是一個哈克解決方案,但我並沒有在那一刻任何公開的發現方法,允許我改變顏色。我通過在位於PhpExcel/Writer/Excel2007文件夾中的主題文件的第122行中更改變量'accent1'的值來解決此問題。請注意,我正在使用Excel2007進行編寫。

我發佈這個萬一任何人認爲它有用。

+0

你有這個解決方案嗎?發佈請 –

+0

@Marcelo費利佩聖地牙哥迪尼茲,我更新了答案,以便更清楚。我希望它對你有用 – lfal

+0

如果我明白了,你已經在文件中更改了,是嗎? –