Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get excel to array in maatwebsite

I am trying to convert an Excel file to an array using the latest version of Laravel-Excel (3.1.9)

The code below will download the file:

return Excel::download(new SalesOrderExport('columns'),'test.xlsx')

But I need to convert it to get an array only. I don't want to store this Excel data in a database at this point.

I tried with the code below but it did not work as the load method is not available in version 3.

Excel::load($request->file('sampledata'), function ($reader) {
    return response()->json($reader);
});

Please share your thoughts on how to get an array from Excel.

like image 656
Niklesh Raut Avatar asked Feb 27 '19 10:02

Niklesh Raut


People also ask

What is maatwebsite/Excel used for?

The package I intend to use is called Maatwebsite/excel. This package allows us to easily import data to a collection / model, insert imports in batches and even export the collections to excel / csv (among other features). You can read more about the package here .

How to import Excel data into maatwebsite using HTTP controller?

Route::post ( 'importExcel', 'MaatwebsiteController@importExcel' ); Now create MaatwebsiteController.php file in app/Http/Controllers folders look like.

How to use maatwebsite/Excel package in Laravel?

First, we need to have maatwebsite/excel package included in our Laravel Application. This will download the package and also phpoffice/phpspreadsheet package on which this package depends on. This package comes along with a configuration file so that you can override the default configuration provided by the package.

How to install maatwebsite/excel in composer?

In this step we need to install maatwebsite/excel package via the Composer package manager, so one your terminal and fire bellow command: Now open config/app.php file and add service provider and aliase. .... .... In this step, we need to create route of import export file. so open your "routes/web.php" file and add following route.


1 Answers

I and @narayan tried hard to make requested excel file into array. Now I am able to get array properly with below code

$rows = Excel::toArray(new SalesOrderImport, $request->file('sampledata')); 

In my SalesOrderExport class I have default function only, which is required as abstract method.

namespace App\Exports;

use App\SalesOrder;
use Maatwebsite\Excel\Concerns\FromCollection;

class SalesOrderExport implements FromCollection
{
    public function collection()
    {   
        return SalesOrder::all();
    }
}

My Controller code

public function importTest(Request $request)
{
    $rows = Excel::toArray(new SalesOrderImport, $request->file('sampledata'));
    return response()->json(["rows"=>$rows]);
}

And in HTML

<input class="" type="file" name="sampledata" id="sampledata">

I already created this export by

php artisan make:import SalesOrder

Attaching related images

enter image description hereenter image description here

like image 55
Niklesh Raut Avatar answered Sep 19 '22 12:09

Niklesh Raut