Home > Backend Development > PHP Tutorial > How to read EXCEL with PHP

How to read EXCEL with PHP

WBOY
Release: 2016-08-08 09:31:06
Original
1528 people have browsed it

There are three commonly used methods to read EXCEL with PHP, each with its own advantages and disadvantages. I personally recommend the third method because it can be used across platforms.

1. Read in .csv format
Convert .xls to .csv text format, and then use PHP to analyze the file, which is no different from PHP analyzing text.
Advantages: cross-platform, relatively high efficiency, and can read and write.
Disadvantages: You can only use .csv files directly. If you often accept .xls binary files, you need to convert them manually and cannot be automated. There is only one SHEET for a file.

PHP has its own analysis.csv function: fgetcsv

array fgetcsv ( int $handle [, int $length [, string $delimiter [, string $enclosure]]] )
handle A valid file pointer generated by fopen(), popen() or fsockopen().
length (optional) must be greater than the longest line in the CVS file. This parameter is optional in PHP 5. If this parameter is omitted (set to 0 in PHP 5.0.4 and later versions), there is no limit to the length, but it may affect execution efficiency.
delimiter (optional) sets the field delimiter (only one character allowed), the default value is comma.
enclosure (optional) sets the field surround character (only one character allowed), the default value is double quotes. This parameter was added in PHP 4.3.0. Similar to fgets() , except fgetcsv() parses the read line and finds the fields in CSV format and returns an array containing those fields.
fgetcsv() returns FALSE on error, including when the end of file is encountered.
Note: Empty lines in the CSV file will be returned as an array containing a single null field and will not be treated as an error.

Of course, you can also manually analyze the string yourself.

Example:

<?php
$row = 1;
echo "<pre class="brush:php;toolbar:false"><table border=1>";
$handle = fopen("three.csv","r");
while ($data = fgetcsv($handle, 1000, ",")) {
	$num = count($data);
	$row++;
	echo "<tr align=center>";
	for ($c=0; $c < $num; $c++) {
		echo "<td>".$data[$c]."</td>";
	}
	echo "</tr>";
}
echo "</table>
"; fclose($handle); ?>
Copy after login

You can also use the fputcsv function to format the rows into CSV and write the file pointer.

2. ODBC linked data source

Advantages: Supports multiple formats, cvs, xls, etc. Supports reading and writing, uses standard SQL language, and is almost identical to SQLSERVER and MYSQL databases.
Disadvantage: only supports windows server

3. PHP custom class (the file is a bit big, so I put it in the next chapter)

Advantages: Cross-platform. Some classes support write operations. Supports .xls binary files

test.php in
Modify $data->setOutputEncoding('CP1251');

is $data->setOutputEncoding('CP936');

Simplified Chinese cp936, Traditional Chinese can be modified to CP950, Japanese is CP932, etc.

Modify $data->read('three.xls') to your own excel file name.

The above introduces the method of reading EXCEL with PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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