📌  相关文章
📜  maatwebsite excel 包 5.2 laravel - PHP (1)

📅  最后修改于: 2023-12-03 14:44:05.554000             🧑  作者: Mango

Maatwebsite/Excel Package 5.2 Laravel - PHP

Introduction

Maatwebsite/Excel is a PHP package that provides a simple and elegant way to import and export Excel and CSV files. It is built to work seamlessly with Laravel, making it easy to integrate into your Laravel project.

Features
  • Reading and Writing Excel files
  • Reading and Writing CSV files
  • Importing and Exporting large datasets
  • Multiple file formats (XLS, XLSX, CSV)
  • Easy integration with Laravel
  • Automatic chunking for large datasets
  • Comes with an intuitive API for data manipulation
Installation

To install the package, you can simply run the following Composer command:

composer require maatwebsite/excel:"^3.1"

Once installed, you can start using the package in your Laravel application.

Basic Usage
Importing

To import data from an Excel or CSV file, you can use the import() method on the Excel facade. For example, let's say we have a CSV file with customer data. We can import the data using the following code:

use Maatwebsite\Excel\Facades\Excel;
use App\Imports\CustomersImport;

Excel::import(new CustomersImport, 'customers.csv');

Here, we are using the import() method to import the data from the customers.csv file using the CustomersImport class.

Exporting

To export data to an Excel or CSV file, you can use the download() method on the Excel facade. For example, let's say we have a collection of customer data that we want to export to Excel. We can use the following code:

use Maatwebsite\Excel\Facades\Excel;
use App\Exports\CustomersExport;

return Excel::download(new CustomersExport, 'customers.xlsx');

Here, we are using the download() method to export the customer data to an Excel file named customers.xlsx using the CustomersExport class.

Advanced Usage
Working with Sheets

If your Excel file has multiple sheets, you can work with a specific sheet by passing the sheet name or index to the sheet() method. For example, let's say we have an Excel file with two sheets named Sheet1 and Sheet2. We can work with Sheet1 using the following code:

use Maatwebsite\Excel\Facades\Excel;

// Working with a Sheet by name
Excel::selectSheets('Sheet1')->load('file.xlsx')->get();

// Working with a Sheet by index
Excel::selectSheetsByIndex(0)->load('file.xlsx')->get();
Chunking

If you are working with large datasets, you can use the automatic chunking feature to process the data in smaller chunks. To enable chunking, you can simply pass the chunk size as the second parameter to the import() or load() method. For example, to process the data in chunks of 100 rows, you can use the following code:

use Maatwebsite\Excel\Facades\Excel;
use App\Imports\CustomersImport;

Excel::filter('chunk')->load('customers.csv')->chunk(100, function($results) {
    foreach($results->toArray() as $row) {
        // Process the row
    }
});

Here, we are using the chunk() method to process the data in chunks of 100 rows.

Data Manipulation

The Maatwebsite/Excel package comes with an intuitive API for data manipulation. You can use this API to modify the data before importing or after exporting. For example, let's say we want to change the format of a column while importing the data. We can use the beforeImport() method to apply a callback function to the data before it is imported. For example:

use Maatwebsite\Excel\Facades\Excel;

Excel::filter('chunk')->load('customers.csv')->beforeImport(function($results) {
    $results->transform(function($row) {
        $row['dob'] = \Carbon\Carbon::parse($row['dob'])->format('Y-m-d');
        return $row;
    });
})
->chunk(100, function($results) {
    foreach($results->toArray() as $row) {
        // Process the row
    }
});

Here, we are using the beforeImport() method to apply a callback function to the data before it is imported. The callback function transforms the dob column to the Y-m-d format.

Conclusion

The Maatwebsite/Excel package is a powerful tool for working with Excel and CSV files in Laravel. With its intuitive API, automatic chunking, and easy integration with Laravel, it provides a simple and elegant way to import and export large datasets.