Create Excel files using PHP and PHPExcel

WBOY
Release: 2023-05-11 09:50:02
Original
1478 people have browsed it

In today's era of rapid information transmission, data processing and storage are becoming more and more important. The use of Excel tables is the first choice for many people because Excel tables can integrate various data and can be easily analyzed and processed. In order to complete the creation of Excel tables more efficiently, we can use two powerful tools, PHP and PHPExcel. In this article, we will introduce how to create Excel files using PHP and PHPExcel.

1. Install PHPExcel

First, we need to download PHPExcel locally, and then copy the PHPExcel folder to our own PHP project. In the PHP project, we can use the following code to load the PHPExcel library:

require_once '/path/to/PHPExcel/Classes/PHPExcel.php';

2. Create an Excel file

Next, we will create an Excel file and add a table using the following code:

$objPHPExcel = new PHPExcel();

$objPHPExcel->getActiveSheet()- >setCellValue('A1', 'Name');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Gender');
$objPHPExcel->getActiveSheet()- >setCellValue('C1', 'Age');

$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Zhang San');
$objPHPExcel-> getActiveSheet()->setCellValue('B2', 'Male');
$objPHPExcel->getActiveSheet()->setCellValue('C2', '25');

$objPHPExcel ->getActiveSheet()->setCellValue('A3', '李思');
$objPHPExcel->getActiveSheet()->setCellValue('B3', 'Female');
$ objPHPExcel->getActiveSheet()->setCellValue('C3', '23');

$file_path = 'test.xlsx';

$objWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel, 'Excel2007');
$objWriter->save($file_path);

The above code will create an Excel file containing a table containing name, gender and age data. The $file_path variable in the code specifies the saving path of the Excel file.

3. Add formats and styles

You can also use PHPExcel to add various formats and styles to Excel tables. For example, the following code will add a bold font to the table, center align the cells in the name column, and set the row height to 20 pixels:

$objPHPExcel->getActiveSheet()->getStyle(' A1:C1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->getAlignment()-> setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);

The above code can control the font in the table , alignment and row height to make it more beautiful and easier to see.

4. Read Excel files

In Excel files, we can also read and process data. The following code will read data from an Excel file and output it to a web page:

$objPHPExcel = PHPExcel_IOFactory::load($file_path);

$sheet = $objPHPExcel-> ;getActiveSheet();

$max_col = $sheet->getHighestColumn();
$max_row = $sheet->getHighestRow();

for($row=1 ; $row<=$max_row; $row ){

for($col='A'; $col<=$max_col; $col++){
    $cell = $sheet->getCell($col.$row);
    echo $cell->getValue() . " ";
}
echo "<br>";
Copy after login

}

The above code will read the data in the Excel file and output it line by line to the web page. The $max_col and $max_row variables in the code represent the maximum column and maximum row of the table respectively. The data in the Excel file is read row by row through a loop and output to the web page.

Summary:

In this article, we learned how to create and process Excel files using PHP and the PHPExcel library. PHPExcel provides a large number of functions and options that can make the operation of Excel files easier and more efficient. Whether you are dealing with data analysis or creating reports, PHPExcel is a very good PHP library choice.

The above is the detailed content of Create Excel files using PHP and PHPExcel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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