如何在MaatWebsite中获得Excel的数组 [英] How to get excel to array in maatwebsite

查看:824
本文介绍了如何在MaatWebsite中获得Excel的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用最新版本的 Laravel-Excel 将Excel文件转换为数组. a>(3.1.9)

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')

但是我需要将其转换为仅获取数组.我现在不希望将此Excel数据存储在数据库中.

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.

我尝试使用下面的代码,但是由于load方法在版本3中不可用,因此无法正常工作.

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);
});

请分享您对如何从Excel中获取数组的想法.

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

推荐答案

我和@narayan努力将请求的excel文件放入数组.现在,我可以使用以下代码正确获取数组

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')); 

在我的SalesOrderExport类中,我仅具有默认功能,这是抽象方法所必需的.

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();
    }
}

我的控制器代码

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

以HTML

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

我已经通过

php artisan make:import SalesOrder

附加相关图片

这篇关于如何在MaatWebsite中获得Excel的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆