ThinkPHP6 data import and export: realizing batch processing of data
In actual development, we often encounter the need to import and export data in batches, such as importing Excel tables to the database, or export the data in the database to an Excel file. Such operations can improve development efficiency and reduce the workload of manual data entry. This article will introduce how to use the ThinkPHP6 framework to implement batch processing of data, including specific steps and code examples for data import and export.
1. Data import
First, you need to install the PHPExcel library in the project to process Excel files. You can use Composer to install PHPExcel and execute the following command:
1 |
|
After the installation is completed, a vendor directory will be generated, which contains the relevant files of the PHPExcel library.
In ThinkPHP6, you can use the request()
function to obtain files uploaded by users. First, create a method in the controller to handle the import operation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
In the above code, the uploaded file is first obtained through the request()
function, and the validity is verified. , limits file size to 1MB, and only allows uploading files in .xls and .xlsx formats. Then use the move()
method to move the file to the uploads directory of the framework, and save the file name to the $filename
variable.
Next, you can use the PHPExcel library in the import logic to read and process the Excel file. The following is a simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
In the above code, we use the PHPExcel library to create a PHPExcel object and use the createReader()
method to read the Excel file. Then use the getSheet()
method to get the object of the first worksheet, and use the getHighestRow()
method to get the total number of rows.
Next, by traversing the data of each row, use the getCell()
method to obtain the value of the specified cell, and insert the data into the database to complete the import operation.
2. Data export
First, create a method in the controller to handle the export operation:
1 2 3 4 5 6 7 8 |
|
In the above code, use the query constructor of ThinkPHP6Db::name('user')->select()
to query user data in the database.
Next, we use the PHPExcel library to export the data to an Excel file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
In the above code, we created A PHPExcel object and use the setTitle()
method to set the title of the worksheet. Then use the setCellValue()
method to set the header and data content.
Finally, send the exported Excel file to the browser for download by setting the response header.
Summary
This article introduces how to use the ThinkPHP6 framework to implement batch processing of data, including specific steps and code examples for data import and export. By using the PHPExcel library, we can easily process Excel files, improve development efficiency and reduce the workload of manual data entry. I hope this article is helpful to you and can play a role in actual development.
The above is the detailed content of ThinkPHP6 data import and export: realize data batch processing. For more information, please follow other related articles on the PHP Chinese website!