paranoid
paranoid

Reputation: 7115

How to get excel sheet title in laravel 5.2

I want to get excel sheet title.
I have method controller like this:

use Maatwebsite\Excel\Facades\Excel;



public function importCSVEXCEl()
   {

    Excel::load('test.xls', function($reader) {
        foreach($reader as $sheet)
        {

         $temp= $sheet->getTitle();
        }
    });
}

after execute this method show me error

Call to undefined method PHPExcel::getTitle()

Upvotes: 0

Views: 3910

Answers (1)

KazikM
KazikM

Reputation: 1349

According to Issue #316 discussion on Github:

If you have only one sheet, then you don't have to loop through the sheets.

Excel::load(public_path().$destinationPath.'/'.$name, function($sheet)
{
      // reader methods
      $sheetTitle = $sheet->getTitle();  
}); 

To verify the injected value in the closure is indeed a Sheet, you can dump it and see if the class is called "RowCollection".

If you always want to loop through the sheets no matter what, you'll have to set the import.force_sheets_collection to true.

and second suggestion from the same source:

This is what I used and seems to work

 $sheetNames = Excel::load($file)->getSheetNames();

Upvotes: 2

Related Questions