2014-09-02 52 views
10

我使用maatwebsite/EXCEL库创建Excel文件,然后下载我的文件如何在Laravel下载后重定向?

在我的控制器我这样做:

public function todas() 
    { 
     $input = Input::all(); 

     if(isset($input['todos'])&&($input['todos']!=0)) 
     { 
      set_time_limit(0); 
      $datainicio = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_inicio'); 
      $datafinal = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_fim'); 
      $mes = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('mes_referencia'); 

      $horarioQuery = $this->horario->with(array('funcionario', 'item_contabil')) 
           ->whereBetween('data', array($datainicio, $datafinal)) 
           ->whereNull('deleted_at') 
           ->orderBy('cod_funcionario') 
           ->orderBy('data', 'ASC') 
           ->get(); 

      $horarios = reset($horarioQuery); 

      $count = count($horarios); 

      if($horarios==null) 
       return Redirect::route('relatorios.todas')->withErrors(['não existem marcações para este período']); 

      $funcionarios = $this->funcionario->all(); 

      $datainicio = Carbon::createFromFormat('Y-m-d H:i:s', $datainicio); 
      $datafinal = Carbon::createFromFormat('Y-m-d H:i:s', $datafinal); 

      $nome = 'Marcações'.$mes.'-'.Carbon::now()->year; 
      $this->horario->allToExcel($nome, $horarios); 

      return View::make('relatorios.todashow', compact('funcionarios', 'datainicio', 'datafinal', 'horarios', 'count', 'nome')); 
     } 
     else 
     { 
      return Redirect::route('relatorios.todas')->withErrors(['Selecione um período!']); 
     } 
    } 

这是我的函数生成Excel文件:

public static function allToExcel($nome, $horarios) 
    { 
      Excel::create($nome , function ($excel) use ($horarios) { 

      $excel->sheet('Marcações', function ($sheet) use ($horarios) { 

       $sheet->row(1, array(

          'Unidade', 
          'Nome', 
          'Função', 
          'Data', 
          'Hora Faturada', 
          'Hora Contratada', 
          'Horas Trabalhadas', 
          'Horas Extras', 
          'Tempo Exposicao', 
          'Atividade', 
          'Item Contabil', 
          'Observacoes' 
         )); 

       $sheet->row(1, function($row) { 
        $row->setBackground('#2A8005'); 
        $row->setFontColor('#ffffff'); 
        $row->setFontWeight('bold'); 
       }); 

       $i = 2; 
       foreach ($horarios as $horario) { 

         if($horario->funcionario->funcao_qt != null) 
          $funcao = $horario->funcionario->funcao_qt; 
         else 
          $funcao = $horario->funcionario->funcao_a5; 

         $sheet->row($i, array(
          $horario->unidade, 
          $horario->funcionario->nome, 
          $funcao, 
          $horario->data->format('d/m/Y'), 
          $horario->hora_faturada->format('H:i'), 
          $horario->hora_contratada, 
          $horario->getWorkedHours()->format('H:i'), 
          $horario->getExtraHours()->format('H:i'), 
          $horario->tempo_exposicao ?: "-", 
          $horario->atividade, 
          $horario->item_contabil->CTD_DESC01, 
          $horario->observacoes 
         )); 
         if($i % 2 != 0) 
         { 
         $sheet->row($i, function($row) { 
          $row->setBackground('#D1D1D1'); 
          }); 
         }  

         $i++; 
        } 
      }); 

      })->download('xls'); 
    } 

但我下载excel文件后,我无法重定向到路由或视图,我也试过使用:

routes:

Route::post('relatorios/todas', array('as' => 'relatorios.todas', 'after' => 'voltar', 'uses' => '[email protected]')); 

过滤器:

Route::filter('voltar', function() 
{ 
    return Redirect::back()->with('message', '<p class="bg-success"><b>Relatório gerado com sucesso</b></p>'); 
}); 

,但无论如何没有工作,有另一种方式后重定向下载我的文件??!

thx提前!

回答

10

不能这样做。问题在于,如果您向用户浏览器发送下载指令,则事实上,您发送响应并且只能发回一个响应。

你可以做的是,首先将用户重定向到“最终”页面,然后在该页面中开始下载。该代码会是这样的:

Session::flash('download.in.the.next.request', 'filetodownload.pdf'); 

return Redirect::to('/whatever/page'); 

然后在你的新页面,你就会有一些选择:

所以,你可以在你的布局做这样的事情:

<html> 
    <head> 
     @if(Session::has('download.in.the.next.request')) 
     <meta http-equiv="refresh" content="5;url={{ Session::get('download.in.the.next.request') }}"> 
     @endif 
    <head> 

    <body> 
     ... 
    </body> 
</html> 

而且,看看这个答案:PHP generate file for download then redirect

+0

我会尝试thx! – 2014-09-02 15:27:25

0

奏效

我使用AJAX放弃了,只是试图航线

Route::get('relatorios/exportar/{cod}', array('as' => 'relatorios.exportar', 'uses' => '[email protected]')); 

我控制器

public function exportar($cod) 
    { 
     set_time_limit(0); 
     $datainicio = DB::table('tb_periodo')->where('cod', $cod)->pluck('periodo_inicio'); 
     $datafinal = DB::table('tb_periodo')->where('cod', $cod)->pluck('periodo_fim'); 
     $mes = DB::table('tb_periodo')->where('cod', $cod)->pluck('mes_referencia'); 

     $horarioQuery = $this->horario->with(array('funcionario', 'item_contabil')) 
          ->whereBetween('data', array($datainicio, $datafinal)) 
          ->whereNull('deleted_at') 
          ->orderBy('cod_funcionario') 
          ->orderBy('data', 'ASC') 
          ->get(); 

     $horarios = reset($horarioQuery); 

     $nome = 'Marcações'.$mes.'-'.Carbon::now()->year; 

     $this->horario->allToExcel($nome, $horarios); 
    } 

观点:

{{ link_to_route('relatorios.exportar', 'Exportar para excel', array($cod), array('class' => 'btn btn-success')) }} 

多数民众赞成解决了我,因为不要加载其他页面并下载正确的文件。 thx的帮助!

+0

我有同样的问题,但不明白你的答案,你能否详细说明.. – Arshi 2016-10-05 06:02:57