我正在使用Maatwebsite的库来读取和创建excel文件。创作很简单,而且有点直观,但是当谈到阅读时,我根本无法理解。
我想出了如何循环遍历行,但让我感到困惑的是我的示例中的$ row实际上是字符串的类型,所以我不能做$ row->某事;
在我的控制器中,我想这样读:
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);
});
});
});
这给了我输出
[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
让我感到困惑的是输出的顺序不正确。
让我感到困惑的是我的行是字符串的类型。为什么呢?
是否可以像在矩阵中那样访问特定单元格:$ table [$ row] [$ column]; ?
如果你无法回答我的问题,那么如果你有任何工作代码,我会真的很赞成,因为网上没有很多文档。
编辑:我发现我的标题上有多个单词,所以不知怎的,它们都混淆了。
编辑:因为网上的文档不多,我觉得我应该分享解决它的问题。
解决方案代码:
$rows = Excel::load('storage\\exports\\'. $fName)->get();
Log::warning($rows);
打印出来:
[{"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}]
答案 0 :(得分:8)
也许这对你有用
$rows = Excel::load('storage\\exports\\'. $fName)->get();
答案 1 :(得分:1)
//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'));
}