5

I am using Angular 4 in my Front end and Laravel 5.2 in my backend.I want to import an Excel sheet from UI, store the values in DB, retrieve the values and show them in UI using Angular.

I am using Maatwebsite/Excel package in laravel for this. My excel sheet has the following structure.

Image Link

name | details | price
expertphp | Online Tutorials | 100
hello | code | 200

My laravel code is as below: Here myFile is the key which has the file path and name as its value.

    $File = $request->file('myFile');
    $path = $request->file('myFile')->getRealPath();
    $real_name = $File->getClientOriginalName();
    $data = Excel::load($path)->get();

    if($data->count()){
        foreach($data as $key => $value){
            $arr[] = ['name' => $value->name, 'details' => $value->details, 'price' => $value->price];
        }
    }

    return response()->success($data);

I get my reponse as below:

     {
        "errors":false,
        "data":[
                {
                   "namedetailsprice":"expertphp\tOnline tutorials\t100"
                },
                {
                   "namedetailsprice":"hello\tfirst code\t2000"
                }
               ]
      }

I am not understanding why is the headers becoming one field. Appreciate any help.

Thanks.

Anamik Adhikary
  • 401
  • 1
  • 8
  • 27

1 Answers1

0

I think if you change your code to this code it will work or it will get you closer to your solution:

$File = $request->file('myFile');
$path = $request->file('myFile')->getRealPath();
$real_name = $File->getClientOriginalName();
$data = Excel::load($path)->get();
$returnData = [];

if($data->count()){
    foreach($data as $key => $value){
        $arr = ['name' => $value->name, 'details' => $value->details, 'price' => $value->price];
        array_push($returnData,$arr);
    }
}

return response()->success($returnData);

If I understand you correctly, then you would want this new $returnData to be returned instead of the raw loaded excel file. One other reason that I changed your code was that you were saving only one row of your data and replacing it with another row in the same array $arr. Now it is pushing new rows every time and saving all.

Ali Mrj
  • 71
  • 6
  • There is no need for an if statement in front of an foreach loop. If the array is empty, the loop doesn’t loop ;-) – common sense Jan 10 '19 at 11:38
  • @ali... the problem is not in the pushing of values in the array to overcome the overriding issue. The problem is that it is treating the entire first row as one field instead of treating it column wise as a key value pair. I get this response if i use the code suggested by you. {"errors":false,"data":[[{"name":null,"details":null,"price":null}],[{"name":null,"details":null,"price":null},{"name":null,"details":null,"price":null}]]} – Anamik Adhikary Jan 10 '19 at 11:57
  • Can you publish your excel file as well? something doesn't look right about it. does it contain any data? – Ali Mrj Jan 10 '19 at 12:51
  • Although this looks not so clever, try changing $data to $rows and change the code like this: foreach($rows as $row => $value) – Ali Mrj Jan 10 '19 at 12:55
  • @AliMrj.. i updated my question. I added the excel image. plz have a look. – Anamik Adhikary Jan 10 '19 at 12:57
  • i just figured out that this is working fine with .xls extension files but not with .csv extension files... is .csv file not supported? @AliMrj – Anamik Adhikary Jan 10 '19 at 13:02
  • I checked the Maatwebsite/Excel github and it was written that files can be exported to .csv format. However the reverse might not be working. Moreover the bug you have faced might be due to capital letter CSV format. – Ali Mrj Jan 10 '19 at 14:00
  • 1
    I happened to check the config/excel.php - here for csv files, the delimiter is "," I tried adding a "," after each of my values. It's working fine. – Anamik Adhikary Jan 10 '19 at 17:12