title: Import CSV file, remove empty rows and export it immediately without storing it into database - laravel excel
body: <p>I am trying to remove all the empty rows from a csv and make it downloadable. In this process, there is no involvement of database/model.</p>
  
  <p>My flow looks like:</p>
  
  <p>1) Import csv file.
  2) Filter empty rows.
  3) Export the data after all the empty rows are removed.</p>
  
  <p>My code looks like:</p>
  
  <h1>Controller</h1>
  
  <pre><code>    public function formatCSV()
      {
          $path = storage_path('app/files/') . 'example.csv';
          Excel::import(new FormatCSV, $path);
      }
  </code></pre>
  
  <h1>app/Imports/FormatCSV</h1>
  
  <pre><code>&lt;?php
  
  
  namespace App\Imports;
  
  
  use App\Exports\ExportFormattedCSV;
  use App\Http\Services\AmenityService;
  use Maatwebsite\Excel\Concerns\ToArray;
  use Maatwebsite\Excel\Concerns\WithChunkReading;
  use Excel;
  
  class FormatCSV implements ToArray, WithChunkReading
  {
      private $table,$service,$model;
      public function __construct()
      {
          $this-&gt;service = new AmenityService();
      }
      public function array(Array $rows)
      {   $rec_arr = array();
          foreach ($rows as $row)
          {
              $rec_arr[] = array_values($row);
          }
          $records_arr = $this-&gt;service-&gt;trimArray($rec_arr);
  
          $export = new ExportFormattedCSV($records_arr);
          //print_r($export);
          return Excel::download($export, 'csv.csv');
      }
  
      public function chunkSize(): int
      {
          return 10;
      }
  
  }
  
  </code></pre>
  
  <p>trimArray function</p>
  
  <pre><code>    public function trimArray($arr)
      {
          $final = array();
  
          foreach($arr as $k =&gt; $v)
          {
              if(array_filter($v)) {
                  $final[] = $v;
              }
          }
  
          return $final;
  
      }
  </code></pre>
  
  <h1>app/Exports/ExportFormattedCSV</h1>
  
  <pre><code>&lt;?php
  
  
  namespace App\Exports;
  
  use Maatwebsite\Excel\Concerns\FromArray;
  
  class ExportFormattedCSV implements FromArray
  {
      protected $data;
  
      public function __construct(array $data)
      {
          $this-&gt;data = $data;
      }
      public function array(): array
      {
          return $this-&gt;data;
      }
  
  }
  
  </code></pre>
  
  <p>With this code it does nothing, shows blank at the end. </p>
  
  <p>However, if I uncomment the line <code>print_r($export)</code></p>
  
  <p>It shows data as:</p>
  
  <pre><code>App\Exports\ExportFormattedCSV Object
  (
      [data:protected] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [0] =&gt; First Name
                      [1] =&gt; Last Name
                      [2] =&gt;  Roll No
                  )
  
              [1] =&gt; Array
                  (
                      [0] =&gt; Ram
                      [1] =&gt; Patel
                      [2] =&gt; 1
                  )
  
              [2] =&gt; Array
                  (
                      [0] =&gt; Rajuv
                      [1] =&gt; Roy
                      [2] =&gt; 2
                  )
  
              [3] =&gt; Array
                  (
                      [0] =&gt; Sunny
                      [1] =&gt; Deol
                      [2] =&gt; 5
                  )
  
              [4] =&gt; Array
                  (
                      [0] =&gt; Akshya
                      [1] =&gt; Kumar
                      [2] =&gt; 6
                  )
  
              [5] =&gt; Array
                  (
                      [0] =&gt; Amir Khan
                      [1] =&gt; 7
                      [2] =&gt; 
                  )
  
              [6] =&gt; Array
                  (
                      [0] =&gt; Salman
                      [1] =&gt; Khan
                      [2] =&gt; 9
                  )
  
              [7] =&gt; Array
                  (
                      [0] =&gt; Bobby
                      [1] =&gt; Deol
                      [2] =&gt; 10
                  )
  
          )
  
  )
  </code></pre>
  
  <p>The File I am testing is <code>example.csv</code></p>
  
  <pre><code>First Name,Last Name, Roll No
  Ram,Patel,1
  Rajuv,Roy,2
  ,,
  Sunny,Deol,5
  Akshya,Kumar,6
  Amir Khan,7
  ,,
  Salman,Khan,9
  Bobby,Deol,10,
  Barun,Dhawan,11
  ,,
  Virat,Kohli,13
  Rohit,Sharma,14
  </code></pre>