3

So i work with Laravel, and i use Laravel excel to load excel/csv files, but my files contains empty rows and i want to delete every empty row.

this is my code :

Excel::selectSheetsByIndex(0)->load($path, function($reader){
       $results = $reader->noHeading()->toArray();
         foreach ($results as $row) {
            //my code
         }
       }, 'UTF-8');

So please if someone has any idea how i can do that i will be very appreciative

2
  • Good question. Did you found an answer? I would like to know it ;) Commented Jul 2, 2015 at 8:14
  • still no answer :( Commented Aug 26, 2016 at 17:13

2 Answers 2

2

I think you can do it in this way

/** @var LaravelExcelReader $data */
        $data = \Excel::load('file.xls', function ($reader) {
            $reader->limitRows(20);
            $reader->ignoreEmpty();
        })->get()->toArray();

        # remove empty rows
        $data = array_filter($data);
Sign up to request clarification or add additional context in comments.

Comments

0

use ToCollection method the wrap everything within if($row->filter()->isNotEmpty())

    public function collection(Collection $rows)
       {
          foreach($rows as $row) {
            if($row->filter()->isNotEmpty()){
                // you logic can go here

                $user = User::create([
                    'name' => ucwords($row['name']),
                    'class' => $row['class'],
                    ...
                ]);
            }
        }   
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.