在Laravel 5.7和Excel 3.1中导出之前如何操作数据

时间:2019-01-14 09:11:52

标签: laravel customization maatwebsite-excel

我想自定义列名称,将数据合并为一列。可以对数据施加一些条件。

下面是示例图像,将以格式显示数据。如何做到这一点-

enter image description here

1 个答案:

答案 0 :(得分:1)

在这里您可以找到如何操作列名的方法: https://laravel-excel.maatwebsite.nl/3.1/exports/mapping.html

下面是我的项目中如何使用它的示例。享受吧!

namespace App\Exports;
use App\Aluno;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class AlunosExport implements FromCollection, WithStrictNullComparison, WithHeadings, ShouldAutoSize
{
  /**
  * @return \Illuminate\Support\Collection
  */
  public function collection()
  {
      $alunos = Aluno::all();
      $dadosAluno = [];
      foreach ($alunos as $aluno) {
          $textoEtapas = '';
          foreach ($aluno->etapas as $etapa) {
              $textoEtapas .= "{$etapa->nome}";
              if ($etapa->concluido) {
                  $textoEtapas .= ' (Concluído)';
              }
              $textoEtapas .= "\n";
          }
          $dadosAluno[] = [
              'nome' => $aluno->cliente->nome,
              'telefone' => formatarTelefone($aluno->cliente->telefone),
              'instituicao' => $aluno->cliente->turma->unidade->instituicao->nome,
              'turma' => $aluno->cliente->turma->nome,
              'programa' => $aluno->programa->nome,
              'etapas' => $textoEtapas,
              'valor' => $aluno->valor,
              'orientador' => !is_null($aluno->orientador) ? $aluno->orientador->nome : '(Excluído)',
              'status' => $aluno->cliente->status
          ];
      }
      $alunoColection = collect($dadosAluno);
      return $alunoColection;
  }
  public function headings(): array
  {
      return [
          'Aluno',
          'Telefone',
          'Instituição',
          'Turma',
          'Programa',
          'Etapas',
          'Valor',
          'Orientador',
          'Status'
      ];
  }
}