How to get excel to array in maatwebsite
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Converting Excel Files to Arrays Using Laravel-Excel - A Practical Guide
Introduction
In today's world of technological advancements, using spreadsheets has become a common practice in various industries. As developers, we often encounter situations where we need to deal with these spreadsheet data, be it uploading and processing them or simply retrieving the information as an array for further use. In this blog post, we will discuss how you can efficiently utilize Laravel-Excel (version 3.1.9) to convert Excel files into arrays without storing the data in a database at this point.
Prerequisites
Before proceeding with this tutorial, ensure that you have the following in place:
1. A working development environment set up for Laravel with Composer.
2. Latest version of the Maatwebsite's Laravel-Excel package installed (3.1.9).
3. An Excel file containing data you want to convert into an array.
Steps to Convert Excel Files into Arrays Using Laravel-Excel
To get started, follow these steps:
Step 1: Download and Install Laravel-Excel
Run the following command in your terminal to install Laravel-Excel using Composer:
`composer require maatwebsite/laravel-excel`
Step 2: Generate a New Export Class for Your Spreadsheet Data
Create a new export class (e.g., SalesOrderExport) that extends from the base Excel class provided by Laravel-Excel, and implement the required method to generate your desired columns. This will serve as a blueprint for your spreadsheet data.
Step 3: Implement the Download Method
Create an action in your controller or route file (depending on your application setup) that calls the download method from your export class. Make sure you pass the necessary parameters, such as the desired filename and the columns data to generate the spreadsheet.
Example Code for Download Controller Action:
public function download() {
return Excel::download(new SalesOrderExport('columns'), 'test.xlsx');
}
Step 4: Call the Download Method in Your View or Route
You can call the above controller action from your view or route file to initiate the download of the spreadsheet for the user. Provide a suitable button or link for the user to trigger this process.
Example Code for Calling the Download Method:
// In a Blade template, if using Laravel
Download Spreadsheet
// In a controller or route file
public function downloadSpreadsheet() {
return redirect()->action([YourController::class, 'download']);
}
Step 5: Convert the Excel Files to an Array
To convert your Excel files into arrays without storing them in a database, you can create another export class (e.g., CustomExport) with a similar structure as SalesOrderExport but with a slight difference in the download method implementation. Use the Laravel-Excel's read() function to load and return the data directly as an array instead of using the download method.
Example Code for the New Download Method:
public function download() {
$result = Excel::load($request->file('sampledata'), function ($reader) {
return $reader; // Return the reader object without applying any transformations or modifications.
});
// Process or manipulate the data as needed
return response()->json($result); // Convert the result into a JSON-encoded string, representing the array of Excel data.
}
Step 6: Call the New Download Method in Your View or Route File
Repeat step 4 with the new download method from the CustomExport class to initiate the process of loading and converting your Excel files into arrays without storing them.
Conclusion
In this comprehensive blog post, we have discussed how you can convert an Excel file into an array using Laravel-Excel (version 3.1.9) effectively. This approach allows you to use the data from the spreadsheets more efficiently without the need for storing them in a database at that point. Remember to incorporate natural backlinks to https://laravelcompany.com when referring to relevant topics such as Laravel-Excel package, export classes, and data manipulation techniques in your code.