laravel 如何在maatwebsite中获取excel到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54902966/
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
How to get excel to array in maatwebsite
提问by Niklesh Raut
I am trying to convert an Excel file to an array using the latest version of Laravel-Excel(3.1.9)
我正在尝试使用最新版本的Laravel-Excel(3.1.9)将 Excel 文件转换为数组
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.
但我需要将其转换为仅获取数组。此时我不想将此 Excel 数据存储在数据库中。
I tried with the code below but it did not work as the load
method is not available in version 3.
我尝试使用下面的代码,但它不起作用,因为该load
方法在版本 3 中不可用。
Excel::load($request->file('sampledata'), function ($reader) {
return response()->json($reader);
});
Please share your thoughts on how to get an array from Excel.
请分享您对如何从 Excel 获取数组的想法。
采纳答案by Niklesh Raut
I and @narayan tried hard to make requested excel file into array. Now I am able to get array properly with below code
我和@narayan 努力将请求的 excel 文件放入数组。现在我可以使用以下代码正确获取数组
$rows = Excel::toArray(new SalesOrderImport, $request->file('sampledata'));
In my SalesOrderExport class I have default function only, which is required as abstract method.
在我的 SalesOrderExport 类中,我只有默认函数,这是抽象方法所必需的。
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
在 HTML 中
<input class="" type="file" name="sampledata" id="sampledata">
I already created this export by
我已经创建了这个导出
php artisan make:import SalesOrder
Attaching related images
附上相关图片
回答by Niklesh Raut
Here is how I achieved this:
这是我实现这一目标的方法:
In my UsersImport class: namespace App\Imports;
在我的 UsersImport 类中: namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
class UsersImport implements ToModel
{
use Importable;
public function model(array $row)
{
return new User([
'id' => $row[0]
]);
}
}
And in controller:
在控制器中:
$imported_users = (new UsersImport)->toArray(request()->file('excel_file'))[0];
I hope it helps you.
我希望它能帮助你。
回答by narayansharma91
1) Create Import File using below command.
1) 使用以下命令创建导入文件。
php artisan make:import TestImport
2) Inside TestImport make changes like this:
2) 在 TestImport 中进行如下更改:
namespace App\Imports;
use App\User;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class TestImport implements ToCollection
{
public function collection(Collection $rows)
{
return $rows;
}
}
3) In Controller make changes like this:
3)在控制器中进行如下更改:
$rows = Excel::import(new TestImport, 'test.xlsx');
回答by AddWeb Solution Pvt Ltd
You should try this:
你应该试试这个:
$data = Excel::load($request->file('sampledata'), function ($reader) use($table) {
$data = $reader->toArray();
//here data has all excel data in array.
});