troubleshooting Question

Could you point how to make an Excel sheet to be correctly generated by using Laravel?

Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil asked on
JavaScriptPHPLaravel
17 Comments1 Solution93 ViewsLast Modified:
Hi Experts

Could you point how to make an Excel sheet to be correctly generated by using Laravel?

HTML code:

<div class="btn-group" role="group">
<button type="button" class="btn btn-primary" onclick="hotsite.relatorio.exportar();">
	<i class="fa fa-file-excel-o" aria-hidden="true"></i> Exportar Excel
  </button>
</div>


JS code:
Relatorio.prototype.exportar = function () {
  mesID = $("#filtroMesID").val();
  anoID = $("#filtroAnoID").val();
  trimestreID = $("#filtroTrimestreID").val();
  distribuidorID = $("#filtroDistribuidorID").val();
  
  console.log(mesID);
  console.log(anoID); 
  console.log(trimestreID);
  console.log(distribuidorID);
  
  var valores = {
    mesID: mesID,
    anoID: anoID,
    trimestreID: trimestreID,
    distribuidorID: distribuidorID
  };

  $.ajax({
    url: "/admin/relatorios/analiticoParticipante/exportar",
    method: "GET",
    data: valores,
    error: function (data) {
      $("#dlgAvisoTexto").html(data.retorno);
      hotsite.openModal("dlgAviso");
    },
    success: function (data) {
      $("#filtros").html(data);
      $(".selecttwo").select2();
    }
  });
};



Controller code pointed by the route above:
use Excel;

...
public function exportarAnaliticoParticipante(Request $request){
      $dados = [];
      $item = array();
      
      
      $mes = $request->input('mesID');
      $ano = $request->input('anoID');
      $trimestreID = $request->input('trimestreID');
      $distribuidor_id = $request->input('distribuidorID');
      
      
      $this->AnaliticoParticipante($mes, $ano, $trimestreID, $distribuidor_id, $request);
      
      
      $fullData = session('relatorio_analiticoparticipante');

      $dados[] = ['Razão Social','CNPJ','Status do Distribuidor','Nome GRV','CPF GRV', 'Email GRV' ,'Nome Assitente', 'CPF Assistente','Email Assistente', 'Trimestre','Ano','Mês da Compra','Meta (R$)','Grupo A (R$)','Grupo B (R$)', 'Grupo C (R$)' , 'Grupo D (R$)', 'Grupo E (R$)', 'Grupo F (R$)' , 'Grupo G (R$)', 'Total (R$)','% Atingimento Meta','Pontos Conquistados', 'Pontos Represados','Liberados?','Data da Distribuição','Status da Distribuição'];  
    
      
      
      foreach ($fullData as $data) {
        $item = [];
        foreach($data as $key=>$value){
          array_push($item, $value);
        }

          array_push($dados, $item);
      }


// Data is OK here to generate Excel !!!

//        print_r(json_encode($dados));
//        die;
//-------------------------------------------------


      Excel::create('relatorio_analiticodoparticipante', function($excel) use ($dados) {

          // Set the spreadsheet title, creator, and description
          $excel->setTitle('Relatorio Analítico dos Participantes');
          $excel->setCreator('IPremi')->setCompany('IPremi');
          $excel->setDescription('Lista Analítica dos Participantes');

          // Build the spreadsheet, passing in the users array
          $excel->sheet('sheet1', function($sheet) use ($dados) {
              $sheet->fromArray($dados, null, 'A1', false, false);
              $sheet->setOrientation('landscape');
              $sheet->setfitToPage(true);
          });

      })->download('xlsx');;
      
      
  }
  
 
 
 
I don't know the reason apparently the Excel sheet is been printed at the screen?!

img001

Thanks in advance!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros