在 Laravel 中读取 excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35048872/
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
Reading excel file in Laravel
提问by Andrej
I'm using Maatwebsite's library for reading and creating excel file. The creating was easy and somehow intuitive, but when it comes to reading one, I simply can't figure it out.
我正在使用Maatwebsite的库来读取和创建 excel 文件。创作很简单,而且在某种程度上很直观,但是当谈到阅读时,我就是想不通。
This is how my excel file looks like:
I figured out how to loop through rows, but what confused me that $row in my example is really type of string, so I can't do $row->something;
我想出了如何遍历行,但令我困惑的是,我的示例中的 $row 实际上是字符串类型,所以我不能做 $row->something;
In my controller I want to read it like this:
在我的控制器中,我想这样阅读:
Excel::load('storage\exports\'. $fName, function($reader) {
$reader->each(function($sheet) {
Log::warning("sheet happens");
// Loop through rows
$sheet->each(function($row) {
Log::warning($row);
});
});
});
This gives me output
这给了我输出
[2016-01-28 06:42:05] local.WARNING: sheet happens
[2016-01-28 06:42:05] local.WARNING:
[2016-01-28 06:42:05] local.WARNING:
[2016-01-28 06:42:05] local.WARNING:
[2016-01-28 06:42:05] local.WARNING:
[2016-01-28 06:42:05] local.WARNING:
[2016-01-28 06:42:05] local.WARNING:
[2016-01-28 06:42:05] local.WARNING: sheet happens
[2016-01-28 06:42:05] local.WARNING: ocjena
[2016-01-28 06:42:05] local.WARNING: Kolokvijum 1 (%)
[2016-01-28 06:42:05] local.WARNING: Kolokvijum 2
[2016-01-28 06:42:05] local.WARNING: Kolokvijum 2 (%)
[2016-01-28 06:42:05] local.WARNING: Zavr?ni ispit (%)
[2016-01-28 06:42:05] local.WARNING: Zavr?ni ispit
It confused me that output is not in the right order.
让我感到困惑的是输出的顺序不正确。
It confused me that my row is type of string. Why?
让我感到困惑的是我的行是字符串类型。为什么?
Is it possible to access specific cells like we do in matrix: $table[$row][$column]; ?
是否可以像我们在矩阵中那样访问特定的单元格:$table[$row][$column]; ?
If you can't answer my question I would really apreciate if you have any piece of working code since there is not very much docs online.
如果您不能回答我的问题,我真的很感激您是否有任何工作代码,因为在线文档并不多。
EDIT: I found out that I have multiple words at my headers, so somehow it all gets mixed up.
编辑:我发现我的标题中有多个单词,所以不知何故它们都被混淆了。
EDIT: Cause there is not much docs online I feel like I should share what solved it.
编辑:因为网上没有太多文档,我觉得我应该分享解决它的方法。
The solution code:
解决代码:
$rows = Excel::load('storage\exports\'. $fName)->get();
Log::warning($rows);
Prints out:
打印出来:
[{"ime_studenta":"andrej","broj_indeksa":4,"kolokvijum_1":4,"kolokvijum_2":4,"zavrsni_ispit":44,"ukupno":4,"ocjena":4},{"ime_studenta":"as","broj_indeksa":342,"kolokvijum_1":123,"kolokvijum_2":57,"zavrsni_ispit":56,"ukupno":5656,"ocjena":56}]
回答by thgs
Maybe this is useful for you
也许这对你有用
$rows = Excel::load('storage\exports\'. $fName)->get();
回答by Jatin Saradgikar
//Using PHPExcel IOFactory
public function index()
{
$inputFileName = 'example.xlsx';
$spreadsheet = IOFactory::load($inputFileName);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
//dd($sheetData);
foreach ($sheetData as $rows=>$k) {
$num = $rows;
foreach ($k as $key=>$value) {
$excel = new Excel;
$excel->cell_number = $num;
$excel->cell_letter = $key;
$excel->cell_value = $value;
$excel->save();
}
}
return view('excel', compact('sheetData'));
}