版本
PHP版本:7.2
Laravel 版本:5.7
包版本:3.1
我收到错误消息:来自 /Users/administrator/Sites/blog/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/RowIterator.php 的“起始行 (2) 超出最高行 (1)”
电子表格在第一行有标题,是一张单张。 第二行是空白的,但这不应该吗? 我不确定如何解释这种情况。 任何帮助表示赞赏。 谢谢你。
App\Imports\PoliciesImport.php
<?php
namespace App\Imports;
use App\Policy;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Auth;
class PoliciesImport implements ToModel, WithValidation, WithHeadingRow, WithMultipleSheets
{
use Importable;
/**
* <strong i="14">@param</strong> array $row
*
* <strong i="15">@return</strong> \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Policy([
'user_id' => Auth::user()->id,
'policy_name' => $row['name'],
'phone' => $row['phone'],
'street' => $row['address'],
'city' => $row['city'],
'state' => $row['state'],
'postal_code' => $row['postal_code'],
]);
}
public function sheets(): array
{
return [
// Select by sheet index
0 => new PoliciesImport(),
];
}
public function rules(): array
{
return [
'policy_name' => 'required|string',
'*.policy_name' => 'required|string',
];
}
}
PolicyController.php
public function import(Request $request)
{
if ($request->hasFile('file')) {
// validate incoming request
$this->validate($request, [
'file' => 'required|file|mimes:xls,xlsx,csv|max:10240', //max 10Mb
]);
if ($request->file('file')->isValid()) {
$file = $request->file('file');
$path = $file->store('uploads/policy');
$fileHeaders = current((new HeadingRowImport)->toArray($path)[0]);
$validHeaders = [
'name','phone','address','state','city','postal_code'
];
sort($fileHeaders);
sort($validHeaders);
// Check the File Headers
if ($fileHeaders == $validHeaders) {
// Import the saved excel file
(new PoliciesImport)->import($path);
}
Storage::delete($path);
}
}
return back();
}
您可以发布完整的堆栈跟踪吗? 现在很难看出是哪个进口导致的。
/Users/administrator/Sites/blog/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/RowIterator.php
*
* <strong i="8">@param</strong> int $startRow The row number at which to start iterating
*
* <strong i="9">@throws</strong> PhpSpreadsheetException
*
* <strong i="10">@return</strong> RowIterator
*/
public function resetStart($startRow = 1)
{
if ($startRow > $this->subject->getHighestRow()) {
throw new PhpSpreadsheetException("Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})");
}
$this->startRow = $startRow;
if ($this->endRow < $this->startRow) {
$this->endRow = $this->startRow;
}
$this->seek($startRow);
return $this;
}
/**
* (Re)Set the end row.
*
对于那个很抱歉。 这有帮助吗?
谢谢!
这里有同样的问题!
根据堆栈跟踪,您的文件只有 1 行(标题行),当尝试导入每一行时,PhpSpreadsheet 抱怨没有第二行(并且没有比第二行更远的行)
@patrickbrouwers对于我的情况,肯定有超过第 1 行的行和值。看来我必须确保我实现了WithChunkReading
和WithCustomChunkSize
的关注点才能通过问题。 话虽如此,我还创建了一个新的 Excel 文件并复制了旧文件值。
听起来您的文件可能有问题,如果它在将值复制到新文件后有效。
如果它是 CSV,也许没有使用正确的分隔符?
是的。 很有可能。 如果它再次弹出,我会回到这里,因为我很快就会处理几个不同的文件。 它们都是 XLSX 文件。
当然,让我知道!
@patrickbrouwers是的,这是真的。 在这种情况下,我试图防止服务器错误,并且我正在努力如何捕获它并停止执行导入模型方法。
通过在 App\Imports\PoliciesImport.php 中使用 RegistersEventListeners,我是否走在正确的轨道上? 我想出了如何获得最高行,但我不知道如何传递这个值或从这里去哪里。
public static function beforeImport(BeforeImport $event)
{
$worksheet = $event->reader->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
dd($highestRow);
}
@abbylovesdon最好是在 beforeImport 中抛出异常并在控制器中尝试捕获它。 您可以通过异常传递最高行。
@patrickbrouwers 非常感谢您的帮助! 我是 PHP/Laravel/This Package 的新手,所以如果其他人在我的鞋子里,这就是我所做的:
在我的导入类中:App\Imports\PoliciesImport.php
<?php
namespace App\Imports;
use App\Policy;
use Auth;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Validators\ValidationException;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Events\BeforeImport;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class PoliciesImport implements ToModel, WithValidation, WithHeadingRow, WithMultipleSheets, WithEvents
{
use Importable, RegistersEventListeners;
public static function beforeImport(BeforeImport $event)
{
$worksheet = $event->reader->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
if ($highestRow < 2) {
$error = \Illuminate\Validation\ValidationException::withMessages([]);
$failure = new Failure(1, 'rows', [0 => 'Now enough rows!']);
$failures = [0 => $failure];
throw new ValidationException($error, $failures);
}
}
/**
* <strong i="8">@param</strong> array $row
*
* <strong i="9">@return</strong> \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Policy([
'user_id' => Auth::user()->id,
'policy_name' => $row['name'],
'phone' => $row['phone'],
'street' => $row['address'],
'city' => $row['city'],
'state' => $row['state'],
'postal_code' => $row['postal_code'],
]);
}
public function sheets(): array
{
return [
// Select by sheet index
0 => new PoliciesImport(),
];
}
public function rules(): array
{
return [
'name' => 'required|string',
'*.name' => 'required|string',
];
}
}
现在在我的控制器中我可以使用:
try {
(new PoliciesImport)->import($path);
} catch (\Exception $e) {
$failures = $e->failures();
dd($failures[0]);
}
这里有同样的问题! :((
这里有同样的问题! :((
如果您无法使用上面的建议和示例解决问题,我会要求您使用问题模板为其打开一个新问题。 谢谢!
我有同样的错误!
但删除表 2 后, 3 解决了!
对于这种情况,您需要定义工作表索引
public function sheets(): array
{
return [
// Select by sheet index
0 => new pricelist_items(),
];
}
同样的情况发生在我身上,但我意识到这是由于 Excel 文件中存在多个工作表所致
我只是有同样的问题。 在一些挫折之后,我的潜在怀疑被证明是正确的。 这是由电子表格中的拆分引起的。 在“拆分”第一行之后,它起作用了。
根据堆栈跟踪,您的文件只有 1 行(标题行),当尝试导入每一行时,PhpSpreadsheet 抱怨没有第二行(并且没有比第二行更远的行)
如果excel只有标题行,如何处理。 我想向客户显示正确的错误消息
您好,我知道该线程已关闭,但我在这里遇到了同样的问题,并且该线程中的任何内容都无法解决我的问题。
我同时上传 10k 行,我收到了这个错误Start row (2) is beyond highest row (1)
。 但是当我把它分成 5k 时,它就像魔术一样工作。
这里似乎有什么问题?
提前致谢!
您好,我知道该线程已关闭,但我在这里遇到了同样的问题,并且该线程中的任何内容都无法解决我的问题。
我同时上传 10k 行,我收到了这个错误
Start row (2) is beyond highest row (1)
。 但是当我把它分成 5k 时,它就像魔术一样工作。这里似乎有什么问题?
提前致谢!
原始文件中可能存在错误,您可以通过拆分它来解决。 这也可能是一个性能问题,但如果不查看您的代码和服务器规格就很难判断。 我猜这是一个文件错误。
刚发生在我身上。 当文件大于 PHP 的 'upload_max_filesize' 指令时发生。 我对 5mb 的文件大小和 40k 行使用了 500 的分块,而我在 AWS AMI 上的最大“upload_max_filesize”为 2MB。 我可以建议作者检查最大上传大小,并打印此错误而不是混淆Start row (2) is beyond highest row (1)
。 当我增加“upload_max_filesize”限制时,错误消失了,文件上传成功。
phpspreadsheet 可能引发该错误的原因很可能有更多。 你总是可以在那里尝试公关。
@GlennM感谢您的回复!
我已经为它做了一个解决方法,它现在已经很适合我的项目的需要了。 如果我将来遇到这个问题,会考虑你的建议。
@patrickbrouwers对于我的情况,肯定有超过第 1 行的行和值。看来我必须确保我实现了
WithChunkReading
和WithCustomChunkSize
的关注点才能通过问题。 话虽如此,我还创建了一个新的 Excel 文件并复制了旧文件值。
它对我有帮助。 仍然有同样的问题
删除工作表为空
删除工作表为空
这是我的情况,如果您的文件有空工作表并且您使用“WithHeadingRow”,则会出现此问题。
最有用的评论
我有同样的错误!
但删除表 2 后, 3 解决了!
对于这种情况,您需要定义工作表索引