直截了当的问题:如何用laravel-excel得到电子表格的总行数?
现在我有了一个工作计数器,它显示已经处理了多少行(在CompanyImport
文件中),但是在开始向数据库添加行之前,我需要知道总行数。
我导入的工作表几乎有1 M行,所以我尝试创建一个进度条。
我的导入:
public function model(array $row)
{
# Counter
++$this->currentRow;
# Dont create or validate on empty rows
# Bad workaround
# TODO: better solution
if (!array_filter($row)) {
return null;
}
# Create company
$company = new Company;
$company->crn = $row['crn'];
$company->name = $row['name'];
$company->email = $row['email'];
$company->phone = $row['phone'];
$company->website = (!empty($row['website'])) ? Helper::addScheme($row['website']) : '';
$company->save();
# Everything empty.. delete address
if (!empty($row['country']) || !empty($row['state']) || !empty($row['postal']) || !empty($row['address']) || !empty($row['zip'])) {
# Create address
$address = new CompanyAddress;
$address->company_id = $company->id;
$address->country = $row['country'];
$address->state = $row['state'];
$address->postal = $row['postal'];
$address->address = $row['address'];
$address->zip = $row['zip'];
$address->save();
# Attach
$company->addresses()->save($address);
}
# Update session counter
Session::put('importCurrentRow', $this->currentRow);
return $company;
}
我的控制器:
public function postImport(Import $request)
{
# Import
$import = new CompaniesImport;
# Todo
# Total number of rows in the sheet to session
Session::put('importTotalRows');
#
Excel::import($import, $request->file('file')->getPathname());
return response()->json([
'success' => true
]);
}
6条答案
按热度按时间cbjzeqam1#
在Laravel Excel 3.1中,您可以通过实现
WithEvents
并侦听beforeImport
事件来获取总行数。u7up0aaq2#
您可以使用下面的代码来计算行数
您可以查看单据
更新
上述方法用于计算到目前为止已导入的行数。为了获得工作表中的行数,需要使用
getHighestRow
插件的作者已在此处引用了该内容。
smtd7mpg3#
1.-生成要导入的文件
2.-您的文件导入
3.-您的控制器
本文档:https://docs.laravel-excel.com/3.1/imports/importables.html
ldxq2e6h4#
您可以使用下面的代码在导入前获取行数
代码取自Laravel Excel库
laximzn55#
检查以下示例:
通过使用**getHighestRow()**方法,你可以获取总行数。在上面的代码示例中,我将字体作为粗体应用于第一列的第二个单元格,直到相同的第一列的最大行数。
另一个示例的详细代码段:
bd1hkmkf6#
通过此链接的文档,您可以计算受影响的行数
https://docs.laravel-excel.com/3.1/architecture/objects.html