How can PHP extract data from an Excel file?

PHP can utilize the PHPExcel library to retrieve data from Excel files. Below is a simple example code:

require 'PHPExcel/IOFactory.php';

$inputFileName = 'example.xlsx';

// 读取Excel文件
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

// 获取第一个工作表
$sheet = $objPHPExcel->getSheet(0);

// 获取最大行数和列数
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();

// 逐行读取数据
for ($row = 1; $row <= $highestRow; $row++) {
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
    print_r($rowData);
}

In the example above, “example.xlsx” is the name of the Excel file to be read. Start by using the PHPExcel_IOFactory::load method to load the Excel file, then retrieve the first worksheet and get the maximum number of rows and columns. Finally, use the rangeToArray method to read the data row by row and output it to the console. You can further process and manipulate the Excel data as needed.

bannerAds