PHP: PHP reads records from Excel files

Because the work needs to read out the Excel file records and then write them into the database. Before, VBS was used, but it is not a formal application. It is also OK to use PowerBuilder, but PowerBuilder is only used temporarily after all. C# it has not been used for a long time, so I intend to use the recently learned PHP to operate.

After work, I began to work. After tossing for nearly 4 hours, I finally got it. I met a lot of pits and was about to collapse. Think about it. It's hard for programmers to be tired of these superficial codes!

To use PHP to read Excel file records, we checked the data and used PHPExcel package, and all of them had to be installed with Composer. As a result, we encountered the first pit: Composer could not install phoffice / PHPExcel.

First install Composer.

Composer requires phtraffic / phpspreadsheet, which can be followed by version number or not.

It may be that we can't install it all the time because we use the Internet agent. It's not easy to install it. We can't install PHPOffice/PHPExcel using Composer. All kinds of inexplicable error prompts.

Next, I downloaded JetBrains PhpStorm and installed it. The error prompt changed a little, but it still didn't work. Opening OPENSSL and downloading certificates still didn't work.

I spent 3 hours on this. I really broke down. Think about why others can?

Why must I install Composer? In fact, installation is just to write phoffice information to the configuration file to facilitate programmers to conduct code prompt, error detection and later operation prompt in the programming environment. What if it is not installed?

Download a paid phcoffee online, unpack it to a directory, quote it, read it online, and the result is dozens of lines of error prompt:

Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\PHP2021\HTAPP\phpexcel\Classes\PHPExcel\Shared\String.php on line 526
......
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\PHP2021\HTAPP\phpexcel\Classes\PHPExcel\Reader\Excel5.php on line 1928
......
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\PHP2021\HTAPP\phpexcel\Classes\PHPExcel\Calculation.php on line 2551
......
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\PHP2021\HTAPP\phpexcel\Classes\PHPExcel\Worksheet\AutoFilter.php on line 720
......
Deprecated: Array and string offset access syntax with curly braces is deprecated in G:\PHP2021\HTAPP\phpexcel\Classes\PHPExcel\Cell.php on line 812

This means: not recommended: it is not recommended to use the array with braces and string offset access syntax. This display has dozens of lines, which is frustrating, because the class library was written by others and was intended to give up. When the web page was pulled to the end, there were data records to read. It is really a surprise, which means that as long as this error is solved.

Open the files with error prompts one by one, and change the "{}" access mode of the array to "[]". There are too many errors. It took half an hour to change, and the problem was solved.

Correct code:

<?php
error_reporting(E_ALL);//Report all errors
date_default_timezone_set('PRC');   //Set time zone to China

/** PHPExcel_IOFactory */
require_once './Classes/PHPExcel/IOFactory.php';
$excelFilename="Installation record 111.xlsx";

$ext=substr($excelFilename,strripos($excelFilename,'.'));
$dataSheet=[];

if($ext=='.xlsx'){
	//Read in Excel 2007 file format
	$reader = PHPExcel_IOFactory::createReader('Excel2007');
	$reader->setReadDataOnly(true);
	$spreadSheet=$reader->load($excelFilename);
	$workSheet=$spreadSheet->getActiveSheet();
	foreach($workSheet->getRowIterator() as $key=>$row){
		$cellIterator=$row->getCellIterator();
		$cellIterator->setIterateOnlyExistingCells(false);
		foreach($cellIterator as $k=>$cell){
			$dataSheet[$key][$k]=$cell->getValue();
		}
	}
}elseif($ext=='.xls'){
	//Read in Excel 5 (Excel 97-2003 Workbook)
	$reader = PHPExcel_IOFactory::createReader('Excel5');
	//Load excel file
	$PHPExcel = $reader->load("WPS Installation record.xls");
	//Read first sheet
	$sheet = $PHPExcel->getSheet(0);
	//Get total rows
	$highestRow = $sheet->getHighestRow();
	//Get the total number of columns
	$highestColumm = $sheet->getHighestColumn();

	//Read the data of the cell (starting from the first row, column A, first and then columns)
	for ($row = 1; $row <= $highestRow; $row++){
		for ($column = 'A'; $column <= $highestColumm; $column++) {
			$dataSheet[] = $sheet->getCell($column.$row)->getValue();
		}
	}
}else{
	exit("Unrecognizable{$excelFilename}.\n");
}

print_r($dataSheet);

?>

Done! All file records are displayed. You can read both xls and xlsx files.

Then write to the database.

Keywords: PHP Excel

Added by cwheel3915 on Wed, 08 Dec 2021 07:03:52 +0200