Home > Article > PHP Framework > Laravel development: How to process Excel files using Laravel Excel and Spout?
Laravel is a popular PHP development framework that provides many features and extensions to help developers quickly build web applications. One of the common tasks is processing Excel files. In this article, we will learn how to use Laravel Excel and Spout to process Excel files.
Laravel Excel is a popular extension for importing and exporting Excel files. It provides a simple and easy-to-use interface to read, write and manipulate Excel files. Spout is a faster PHP library designed for reading and writing large Excel files.
First, we need to install these extensions. In Laravel 5.5 and above, Laravel Excel can be installed with the following command:
composer require maatwebsite/excel
For Spout, it can be installed with the following command:
composer require box/spout
Once these extensions are installed, we You can start using them to process Excel files.
To read Excel files, we need to use the Excel class provided by Laravel Excel. The following is a simple example that will read all cell data in an Excel file and return a two-dimensional array:
use MaatwebsiteExcelFacadesExcel; $rows = Excel::load('example.xlsx')->all()->toArray();
In this example, the all()
method will return A Collection
object that contains all row and column data in the Excel file. toArray()
The method converts the Collection object into a simple two-dimensional array, for example:
[ [ "Name" => "John Doe", "Email" => "johndoe@example.com", "Age" => 30 ], [ "Name" => "Jane Doe", "Email" => "janedoe@example.com", "Age" => 28 ], // ... ]
We can use the get()
method to get a specific row or List. For example, to get the first column of data, you can use the following code:
$column = Excel::load('example.xlsx')->get()[0];
To write data to an Excel file, we need to use Laravel Excel's writer class. Here is a simple example that will create an Excel file containing data:
use MaatwebsiteExcelFacadesExcel; use MaatwebsiteExcelWriter; use MaatwebsiteExcelClassesPHPExcel; Excel::create('example', function(Writer $writer) { $writer->setTitle('My First Excel'); $writer->setCreator('John Doe'); $writer->setDescription('A demonstration of using Laravel Excel'); $writer->sheet('Sheet1', function($sheet) { $sheet->fromArray([ ['Name', 'Email', 'Age'], ['John Doe', 'johndoe@example.com', 30], ['Jane Doe', 'janedoe@example.com', 28] ]); }); })->download('xlsx');
In this example, we will use the create()
method to create a new Excel file. In the callback function, we can set the title, creator and description of the Excel file. sheet()
The method will create a new worksheet and write data into it. Finally, the download()
method downloads the Excel file as a response. In this case, the file will be downloaded in XLSX format.
Spout is a lightweight PHP library designed for reading and writing large Excel files. Here is a simple example that will read data from an Excel file using Spout:
use BoxSpoutReaderReaderFactory; use BoxSpoutCommonType; $reader = ReaderFactory::create(Type::XLSX); $reader->open('example.xlsx'); foreach ($reader->getSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { $data[] = $row->toArray(); } } $reader->close();
In this example, we have created a $reader# through the
ReaderFactory class ##Object, used to read Excel files. Through the
getSheetIterator() and
getRowIterator() methods, we can get the iterator of the worksheet and rows, and can use the
toArray() method to get the cell data .
$reader->close()The method is used to close the Excel file.
WriterFactory class and
Sheet class to write data to Excel files.
use BoxSpoutWriterWriterFactory; use BoxSpoutCommonType; $writer = WriterFactory::create(Type::XLSX); $writer->openToFile('example.xlsx'); $writer->addRow(['Name', 'Email', 'Age']); $writer->addRow(['John Doe', 'johndoe@example.com', 30]); $writer->addRow(['Jane Doe', 'janedoe@example.com', 28]); $writer->close();In this example, we use the
WriterFactory class to create a
$writer object and open the Excel file through the
openToFile() method. Use the
addRow() method to add data to the worksheet, and finally use the
close() method to close the Excel file.
The above is the detailed content of Laravel development: How to process Excel files using Laravel Excel and Spout?. For more information, please follow other related articles on the PHP Chinese website!