Laravel Excel图表

问题描述 投票:2回答:1

我正在尝试使用Laravel-excel包创建一个图表,但是当创建和导出文件时没有图表。我正在使用laravel 5.1。 excel.php文件的属性'includeCharts'=> true

    public function getChart(){
      $excel=  Excel::create('Users by Country-BetonLove',   function($excel)  {
       $excel->setTitle('Users by Country');
       $excel->setCompany('BetonLove');
       $excel->sheet('First sheet', function($sheet) {
       $sheet->fromArray(
        array(
            array('',   2010,   2011,   2012),
            array('Q1',   12,   15,     21),
            array('Q2',   56,   73,     86),
            array('Q3',   52,   61,     69),
            array('Q4',   30,   32,     0),
        )
      );
      $dataSeriesLabels = array(
        new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),   //  2010
        new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),   //  2011
        new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),   //  2012
      );
      $xAxisTickValues = array(
        new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4),  //  Q1 to Q4
      );

      $dataSeriesValues = array(
        new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4),
        new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
        new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),
      );

      $series = new PHPExcel_Chart_DataSeries(
        PHPExcel_Chart_DataSeries::TYPE_BARCHART,       // plotType
        PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,  // plotGrouping
        range(0, count($dataSeriesValues)-1),           // plotOrder
        $dataSeriesLabels,                              // plotLabel
        $xAxisTickValues,                               // plotCategory
        $dataSeriesValues                               // plotValues
      );
      $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);

      $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('Test Bar Chart');
      $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');
      //    Create the chart
      $chart = new PHPExcel_Chart(
        'chart1',       // name
        $title,         // title
        $legend,        // legend
        $plotArea,      // plotArea
        true,           // plotVisibleOnly
        0,              // displayBlanksAs
        NULL,           // xAxisLabel
        $yAxisLabel     // yAxisLabel
      );

      //    Set the position where the chart should appear in the worksheet
      $chart->setTopLeftPosition('A7');
      $chart->setBottomRightPosition('H20');

      //    Add the chart to the worksheet
      $sheet->addChart($chart);
    });
  });
  $excel->export();
}
laravel-5 laravel-excel
1个回答
0
投票

'.xlsx'支持Excel图表; $ excel-> export()默认导出为'.xls'

所以你应该使用$excel->export('xlsx');

© www.soinside.com 2019 - 2024. All rights reserved.