Guest User

Untitled

a guest
Jul 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. public function export(Request $request)
  2. {
  3. //nombre del archivo
  4. $name = date("Y-m-d H-i-s");
  5.  
  6. //de alguna manera obetenes los datos, supongamos tenemos una Collection de items como la siguiente
  7. $datosDelGrafico = [
  8. {
  9. "nombre": "Reloj",
  10. "total": 50
  11. },
  12. {
  13. "nombre": "Cartera",
  14. "total": 25
  15. },
  16. {
  17. "nombre": "Bolse",
  18. "total": 75
  19. },
  20. ]
  21.  
  22. $excel = Excel::create($name, function($excel) use (datosDelGrafico) {
  23.  
  24. $excel->sheet('Worksheet', function($sheet) use (datosDelGrafico) {
  25.  
  26. $datos = array(
  27. array("Producto", "Cantidad")
  28. );
  29.  
  30. foreach ($datosDelGrafico as $producto){
  31. $data= array($producto->nombre, $producto->total);
  32. array_push($datos, $data);
  33. }
  34.  
  35. //agregamos los datos al excel
  36. $sheet->fromArray($datos, null, 'A1', false, false);
  37.  
  38.  
  39. //seleccionamos el campo que tiene la leyenda, "Cantidad" en la celda B1
  40. $dataSeriesLabels = array(
  41. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),
  42. );
  43.  
  44. //seleccionamos las categorias en este caso reloj, cartera, etc que que para este ejemplo se encuentran en A2 hasta A4
  45. $xAxisTickValues = array(
  46. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$4', NULL, 10),
  47. );
  48.  
  49. //seleccionamos los valores totales que para este ejemplo se encuentran en B2 hasta B4
  50. $dataSeriesValues = array(
  51. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$4', NULL, 10),
  52. );
  53.  
  54.  
  55. //todo lo que sigue crea el chart en base a lo que seleccionamos anteriormente
  56. $layout = new PHPExcel_Chart_Layout();
  57. $layout->setShowVal(TRUE);
  58.  
  59. $ChartGroupType = NULL;
  60. $yAxis = NULL;
  61.  
  62. $chartType = PHPExcel_Chart_DataSeries::TYPE_BARCHART;
  63. $layout->setShowPercent(TRUE);
  64.  
  65. $ChartGroupType = PHPExcel_Chart_DataSeries::GROUPING_STANDARD;
  66.  
  67. $yAxis = new PHPExcel_Chart_Axis();
  68. $yAxis->setAxisOptionsProperties(
  69. PHPExcel_Chart_Axis::AXIS_LABELS_NEXT_TO,
  70. null,
  71. null,
  72. PHPExcel_Properties::ORIENTATION_REVERSED
  73. );
  74.  
  75. $series = new PHPExcel_Chart_DataSeries(
  76. $chartType, // plotType
  77. $ChartGroupType, // tipo de agrupamiento
  78. range(0, count($dataSeriesValues)-1), // plotOrder
  79. $dataSeriesLabels, // plotLabel
  80. $xAxisTickValues, // plotCategory
  81. $dataSeriesValues // plotValues
  82. );
  83.  
  84. $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_VERTICAL);
  85.  
  86. $plotArea = new PHPExcel_Chart_PlotArea($layout, array($series));
  87. $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
  88. $title = new PHPExcel_Chart_Title("titulo del chart");
  89.  
  90. $chart = new PHPExcel_Chart(
  91. "nombredechart", // name
  92. $title, // title
  93. $legend, // legend
  94. $plotArea, // plotArea
  95. true, // plotVisibleOnly
  96. 0, // displayBlanksAs
  97. null, // xAxisLabel
  98. null, // yAxisLabel
  99. NULL, // xAxis
  100. $yAxis // yAxis
  101. );
  102.  
  103. //seteamos en que posicion queremos que se vea el chart
  104. $chart->setTopLeftPosition('D1');
  105. $chart->setBottomRightPosition('J10');ç
  106.  
  107. //lo agregamos al excel.
  108. $sheet->addChart($chart);
  109. });
  110. });
  111.  
  112. //le podemos setear alguna info
  113. $excel->setTitle('Un titulo');
  114. $excel->setCreator('tu nombre');
  115. $excel->setCompany('tu empresa');
  116. $excel->setDescription('Datos exportados de seccion Cultura');
  117.  
  118.  
  119. //a partir de aca hacemos lo que nos parezca, por ejemplo lo guardamos
  120. $excel->store('xlsx', storage_path('app/public/excel'), true);
  121.  
  122. //o iniciamos la descarga
  123. $excel->download('xlsx');
  124. }
Add Comment
Please, Sign In to add comment