0

I am working on Upload excel sheet. I need to count records of the excel sheet before upload to use as condition. I am using this package. My code is like below.

public function import() 

    {
        Excel::import(new UsersImport,request()->file('file'));

        return back();

    }

How can I count record before upload ?

abu abu
  • 6,599
  • 19
  • 74
  • 131

1 Answers1

1

I don't know the package you are using. I use PhpSpreadsheet. The source code to count the number of rows of a file (xls, xlsx, csv ...) is below:

$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("05featuredemo.xlsx");
$highestRow = $this->spreadsheet->getActiveSheet()->getHighestRow();
echo $highestRow;

In your case probably:

$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(request()->file('file'));
$highestRow = $this->spreadsheet->getActiveSheet()->getHighestRow();
if ($highestRow < 10) {
    // Error
}
return back();
mspiderv
  • 509
  • 7
  • 15