Home >Backend Development >PHP Tutorial >PHP development: Use PHPExcel to process Excel files
With the advent of the digital age, data has become the most important part of our daily lives and work, and Excel files have become one of the important tools for data processing. I believe that many PHP developers will often encounter the use of Excel files for data processing and operations in their work. This article will introduce you to the methods and precautions for using the PHPExcel library to process Excel files.
What is PHPExcel?
PHPExcel is a PHP class library that can be used to read and write Excel files. It supports multiple Excel file types, including .xls (Excel 97-2003), .xlsx (Excel 2007), and .ods (OpenOffice/LibreOffice). PHPExcel can read, modify, create and write Excel files, and has better performance optimization for processing larger Excel files.
How to use PHPExcel?
Before using PHPExcel, we need to download the corresponding library file first. You can download the compressed package from the official website (https://phpoffice.github.io/PHPExcel/index.html), or install it directly using Composer on the command line. After the installation is successful, we need to import the library files.
require_once 'PHPExcel.php';
Next, we can read the Excel file through the following code:
//Set the file name and path
$excel_file = './data.xlsx';
//Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
//Read the file
$objReader = PHPExcel_IOFactory ::createReaderForFile($excel_file);
$objPHPExcel = $objReader->load($excel_file);
//Select the active worksheet
$objPHPExcel->setActiveSheetIndex(0);
//Get the number of rows in the current worksheet
$row_count = $objPHPExcel->getActiveSheet()->getHighestRow();
The above code demonstrates how to read Excel file and get the row number of the current worksheet. Next, we can write data to the Excel file through the following code:
//Set the file name
$excel_file = 'data.xlsx';
//Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
//Set file properties
$objPHPExcel->getProperties()->setCreator('PHPExcel')
->setLastModifiedBy( 'PHPExcel')
->setTitle('PHP Excel Document')
->setSubject('PHPExcel Document')
->setDescription('PHPExcel document for PHP, generated using PHP classes. ')
->setKeywords('office PHPExcel php')
->setCategory('Example PHP Excel Document');
//Add data
$objPHPExcel-> setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Name')->setCellValue('B1', 'Age');
$objPHPExcel-> ;getActiveSheet()->setCellValue('A2', 'Amy')->setCellValue('B2', 18);
$objPHPExcel->getActiveSheet()->setCellValue('A3', ' Bob')->setCellValue('B3', 22);
//Set column width
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth (15);
//Save Excel file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($excel_file);
The above code demonstrates how to create a new Excel file and write data into it. We can set file properties, add data, and set column widths. When finally saving the Excel file, you need to specify the saved file format.
Summary
This article mainly introduces how to use the PHPExcel library to process Excel files, including reading, modifying, creating and writing Excel files. In actual development, we need to choose the corresponding operation method according to specific needs, such as getting the number of rows, adding data, and setting column widths. At the same time, we also need to pay attention to code optimization and error handling to ensure system stability and performance.
The above is the detailed content of PHP development: Use PHPExcel to process Excel files. For more information, please follow other related articles on the PHP Chinese website!