dongqiao1151 2016-01-27 21:58
浏览 885
已采纳

在Laravel中读取excel文件

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.

This is how my excel file looks like: enter image description here

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;

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]; ?

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}] 
  • 写回答

2条回答 默认 最新

  • duanqiang6501 2016-01-27 23:33
    关注

    Maybe this is useful for you

    $rows = Excel::load('storage\\exports\\'. $fName)->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!