Home > PHP Framework > Laravel > Laravel Development: How to use Laravel Excel to process Excel files?

Laravel Development: How to use Laravel Excel to process Excel files?

WBOY
Release: 2023-06-13 11:36:33
Original
2735 people have browsed it

Laravel Development: How to use Laravel Excel to process Excel files?

Laravel Excel is an open source Laravel extension package, which can help us process Excel files more conveniently. Using Laravel Excel in Laravel projects allows us to read, write and modify Excel files more quickly and flexibly.

This article will introduce the installation and use of Laravel Excel, and how to use Laravel Excel to process Excel files in Laravel projects.

1. Installation of Laravel Excel

Before installing Laravel Excel, make sure that the Laravel framework has been installed.

  1. Install Laravel Excel

Use composer to install Laravel Excel, the command is as follows:

composer require maatwebsite/excel
Copy after login
  1. Configuration

After the installation is complete, you need to add Laravel Excel's ServiceProvider and Facades to the config/app.php configuration file.

Please copy the following code to the providers array of config/app.php:

MaatwebsiteExcelExcelServiceProvider::class,
Copy after login

At the same time, copy the following code to the aliases array of config/app.php:

'Excel' => MaatwebsiteExcelFacadesExcel::class,
Copy after login

This completes the installation and configuration of Laravel Excel.

2. How to use Laravel Excel

The following will introduce how to use Laravel Excel.

  1. Reading Excel Files

Using Laravel Excel makes it easy to read Excel files just like reading a database. The following code can be used to read the Excel file:

use MaatwebsiteExcelFacadesExcel;

Excel::load('文件路径', function($reader) {
    // 获取Excel文件中的所有数据
    $results = $reader->get();

    // 遍历每一行数据
    foreach ($results as $row) {
        // 处理每一行数据
    }
});
Copy after login

After reading the Excel file, each row of data can be processed. If there are multiple tables in the Excel file, you can use the sheet() method to specify the table to be read, as shown below:

Excel::selectSheets('表格名称')->load('文件路径', function($reader) {
    // 获取表格中的所有数据
    $results = $reader->get();

    // 遍历每一行数据
    foreach ($results as $row) {
        // 处理每一行数据
    }
});
Copy after login
  1. Write Excel file

Use Laravel Excel can easily write data into Excel files. You only need to format the data into a format supported by Excel. You can use the following code to write data to an Excel file:

use MaatwebsiteExcelFacadesExcel;

Excel::create('文件名', function($excel) {

    // 创建一个工作表
    $excel->sheet('Sheet1', function($sheet) {

        // 写入数据
        $sheet->row(1, array(
            '姓名', '年龄', '性别'
        ));
        $sheet->row(2, array(
            '小明', 20, '男'
        ));
        $sheet->row(3, array(
            '小红', 18, '女'
        ));

    });

})->store('xls', '文件路径');
Copy after login

After executing the store() method, the Excel file will be saved in the specified path.

  1. Modify Excel files

Using Laravel Excel, you can also easily modify the data in Excel files. You can use the following code to modify the data in the Excel file:

use MaatwebsiteExcelFacadesExcel;

Excel::load('文件路径', function($reader) {

    // 获取Excel文件中的所有数据
    $results = $reader->get();

    // 遍历每一行数据
    foreach ($results as $row) {

        // 判断是否需要修改该行数据
        if (...) {
            // 修改数据
            $row->column1 = '新的值';
            $row->column2 = '新的值';
            // ...
        }

    }

    // 将修改后的数据保存到原Excel文件中
    $reader->export('xls');

});
Copy after login

After modifying the data, use the export() method to save the modified data to the original Excel file.

Conclusion

This article introduces how to install and use Laravel Excel to process Excel files. By using Laravel Excel, we can read, write and modify Excel files more conveniently, improve development efficiency and reduce tedious Excel file processing work.

The above is the detailed content of Laravel Development: How to use Laravel Excel to process Excel files?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template