Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function export(Request $request)
- {
- //nombre del archivo
- $name = date("Y-m-d H-i-s");
- //de alguna manera obetenes los datos, supongamos tenemos una Collection de items como la siguiente
- $datosDelGrafico = [
- {
- "nombre": "Reloj",
- "total": 50
- },
- {
- "nombre": "Cartera",
- "total": 25
- },
- {
- "nombre": "Bolse",
- "total": 75
- },
- ]
- $excel = Excel::create($name, function($excel) use (datosDelGrafico) {
- $excel->sheet('Worksheet', function($sheet) use (datosDelGrafico) {
- $datos = array(
- array("Producto", "Cantidad")
- );
- foreach ($datosDelGrafico as $producto){
- $data= array($producto->nombre, $producto->total);
- array_push($datos, $data);
- }
- //agregamos los datos al excel
- $sheet->fromArray($datos, null, 'A1', false, false);
- //seleccionamos el campo que tiene la leyenda, "Cantidad" en la celda B1
- $dataSeriesLabels = array(
- new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),
- );
- //seleccionamos las categorias en este caso reloj, cartera, etc que que para este ejemplo se encuentran en A2 hasta A4
- $xAxisTickValues = array(
- new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$4', NULL, 10),
- );
- //seleccionamos los valores totales que para este ejemplo se encuentran en B2 hasta B4
- $dataSeriesValues = array(
- new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$4', NULL, 10),
- );
- //todo lo que sigue crea el chart en base a lo que seleccionamos anteriormente
- $layout = new PHPExcel_Chart_Layout();
- $layout->setShowVal(TRUE);
- $ChartGroupType = NULL;
- $yAxis = NULL;
- $chartType = PHPExcel_Chart_DataSeries::TYPE_BARCHART;
- $layout->setShowPercent(TRUE);
- $ChartGroupType = PHPExcel_Chart_DataSeries::GROUPING_STANDARD;
- $yAxis = new PHPExcel_Chart_Axis();
- $yAxis->setAxisOptionsProperties(
- PHPExcel_Chart_Axis::AXIS_LABELS_NEXT_TO,
- null,
- null,
- PHPExcel_Properties::ORIENTATION_REVERSED
- );
- $series = new PHPExcel_Chart_DataSeries(
- $chartType, // plotType
- $ChartGroupType, // tipo de agrupamiento
- range(0, count($dataSeriesValues)-1), // plotOrder
- $dataSeriesLabels, // plotLabel
- $xAxisTickValues, // plotCategory
- $dataSeriesValues // plotValues
- );
- $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_VERTICAL);
- $plotArea = new PHPExcel_Chart_PlotArea($layout, array($series));
- $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
- $title = new PHPExcel_Chart_Title("titulo del chart");
- $chart = new PHPExcel_Chart(
- "nombredechart", // name
- $title, // title
- $legend, // legend
- $plotArea, // plotArea
- true, // plotVisibleOnly
- 0, // displayBlanksAs
- null, // xAxisLabel
- null, // yAxisLabel
- NULL, // xAxis
- $yAxis // yAxis
- );
- //seteamos en que posicion queremos que se vea el chart
- $chart->setTopLeftPosition('D1');
- $chart->setBottomRightPosition('J10');ç
- //lo agregamos al excel.
- $sheet->addChart($chart);
- });
- });
- //le podemos setear alguna info
- $excel->setTitle('Un titulo');
- $excel->setCreator('tu nombre');
- $excel->setCompany('tu empresa');
- $excel->setDescription('Datos exportados de seccion Cultura');
- //a partir de aca hacemos lo que nos parezca, por ejemplo lo guardamos
- $excel->store('xlsx', storage_path('app/public/excel'), true);
- //o iniciamos la descarga
- $excel->download('xlsx');
- }
Add Comment
Please, Sign In to add comment