如何在 Laravel Excel 中检查 Excel 文件的第一行或第一列是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26032014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if the first row or columns of Excel file is valid in Laravel Excel?
提问by christianleroy
I'm trying to check if the excel file a user has uploaded has the correct columns/first row/attributes before reading into its other rows. I'm using Laravel 4 with MaatWebsite'sLaravel Excel.
在读取其他行之前,我试图检查用户上传的 excel 文件是否具有正确的列/第一行/属性。我将 Laravel 4 与MaatWebsite 的Laravel Excel 一起使用。
I have created a function which allows users to update the employee table of the system's database by importing an excel file.
我创建了一个函数,它允许用户通过导入一个 excel 文件来更新系统数据库的员工表。
Now the correct excel file, has, let's say, 3 columns: firstname
, lastname
, and username
. If the excel file has all these attributes/columns, I will now read each of the following rows and validate each row, which isn't really a part of my problem as of now.
现在正确的Excel文件,有,比方说,3列:firstname
,lastname
,和username
。如果 excel 文件具有所有这些属性/列,我现在将读取以下每一行并验证每一行,这实际上不是我现在问题的一部分。
But if any of the 3 attribute/column is not present in the excel file, I'd ignore the request and return an error.
但是,如果 excel 文件中不存在 3 个属性/列中的任何一个,我将忽略该请求并返回错误。
I tried using this, which gets lastname
attribute:
我尝试使用它,它获取lastname
属性:
$sample = Excel::selectSheetsByIndex(0)->load($readFile, function($reader){})->get(array("lastname"));
But even though lastname
hasn't been found, $sample is still not null so I won't be able to check if lastname
is present or not.
但即使lastname
没有找到, $sample 仍然不为空,所以我将无法检查是否lastname
存在。
How can I check if the attributes/columns are present/not?
如何检查属性/列是否存在?
UPDATE:
更新:
The answer that I selected would work perfectly if the first row after the attributes row has all the needed attributes. For example: If it has values for firstname
, lastname
, and username
.
如果属性行之后的第一行具有所有需要的属性,那么我选择的答案将非常有效。例如:如果有值firstname
,lastname
和username
。
But if in cases where first name
value (or any attritbute value for that matter) of the first non-attribute row (attribute row referring to the column names) is missing, then the provided script would return false, even if the excel file has all the firstname
, lastname
, and username
attributes.
但是,如果first name
第一个非属性行(引用列名的属性行)的值(或任何属性值)丢失,则提供的脚本将返回 false,即使 excel 文件具有所有firstname
、lastname
、 和username
属性。
So I modified the script to this:
所以我将脚本修改为:
- First, I read the excel file. I also declared a Boolean variable to mark if the excel file is valid or not.
- 首先,我阅读了excel文件。我还声明了一个布尔变量来标记 excel 文件是否有效。
$excelChecker = Excel::selectSheetsByIndex(0)->load('path/to/file', function($reader){})->get()->toArray();
$excelChecker = Excel::selectSheetsByIndex(0)->load('path/to/file', function($reader){})->get()->toArray();
$excelIsValid = false;
Then I loop through all the results and check if at least once, all the values required (
firstname
,lastname
, andusername
) have been set or are present.foreach($excelChecker as $ex){ if(isset($ex["lastname"]) && isset($ex["firstname"]) && isset($ex["username"])){ $excelIsValid = true; } }
通过所有的结果然后我循环,并检查是否至少一次,需要(所有值
firstname
,lastname
和username
)已经被设置或都存在。foreach($excelChecker as $ex){ if(isset($ex["lastname"]) && isset($ex["firstname"]) && isset($ex["username"])){ $excelIsValid = true; } }
Explanation:
解释:
If $excelIsValid
is still false
at the end of the loop, then not once did the file had all the attributes required. This either means the file is empty, has the wrong attribute names, or does not have any valid row. In short, the file is invalid and should not be in the system.
如果$excelIsValid
仍false
处于循环末尾,则该文件没有一次具有所需的所有属性。这要么意味着文件是空的,有错误的属性名称,或者没有任何有效的行。简而言之,该文件无效,不应在系统中。
回答by Marcin Nabia?ek
I prepared this sample script:
我准备了这个示例脚本:
Route::get('/', function() {
$isError = false;
Excel::load('file.xls', function($reader) use (&$isError) {
$firstrow = $reader->first()->toArray();
if (isset($firstrow['firstname']) && isset($firstrow['lastname']) && isset($firstrow['username'])) {
$rows = $reader->all();
foreach ($rows as $row) {
echo $row->firstname.' '.$row->lastname.' '.$row->username."<br />";
}
}
else {
$isError = true;
}
});
if ($isError) {
return View::make('error');
}
});
The script loads file and check if there are columns you want. It doesn't check if there are more columns - of course if you want you can add exta check - count($firstow) == 3
. If there is error it sets $isError
to true
and in route it displays a template.
脚本加载文件并检查是否有您想要的列。它不会检查是否有更多的列 - 当然,如果你愿意,你可以添加额外的检查 - count($firstow) == 3
。如果有错误,它会设置$isError
为true
并在路由中显示一个模板。