php reads EXCEL files php excelreader reads excel files_PHP tutorial

WBOY
Release: 2016-07-21 15:14:32
Original
1069 people have browsed it

In PHP development, you will definitely encounter the need to import the contents of excel files into the database. php-excel-reader is a class for reading excel. It can be easily used to read excel files. It is very convenient.

php-excel-reader download address: http://www.jb51.net/codes/67223.html

I downloaded the php-excel-reader-2.21 version. I encountered a few minor problems when using it. I will elaborate on it later. Here is a php example:

The excel I use is as shown below:

php-excel-readerThe php code is as follows:

Copy the code The code is as follows:

/ *by www.phpddt.com*/
header("Content-Type:text/html;charset=utf-8");
require_once 'excel_reader2.php';
//Create object
$data = new Spreadsheet_Excel_Reader();
//Set text output encoding
$data->setOutputEncoding('UTF-8');
//Read Excel file
$data- >read("example.xls");
//$data->sheets[0]['numRows'] is the number of Excel rows
for ($i = 1; $i <= $ data->sheets[0]['numRows']; $i++) {
//$data->sheets[0]['numCols'] is the number of Excel columns
for ($j = 1 ; $j <= $data->sheets[0]['numCols']; $j++) {
//Display the content of each cell
echo $data->sheets[0][ 'cells'][$i][$j].' ';
}
echo '
';
}
?>

The screenshot of the reading result is as follows:

php-excel-reader读取excel文件 Let’s talk about this small problem:

(1)Deprecated appears: Function split() is deprecated in. . . Error

Solution: Change split in excel_reader2.php source code to explode. For details, click on the difference between explode and split in php

(2) Deprecated: Assigning the return value of new by reference is deprecated in error

Solution: Remove & from $this->_ole =& new OLERead() in the excel_reader2.php source code, because the =& symbol is abolished in php5.3 and the = reference is used directly

(3) Solution to garbled code problem:

The constructor is function Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding=''). Its default encoding is utf-8. If not specified, garbled characters may occur. You can pass $data ->setOutputEncoding('GBK'); specifies, and if you use the dump() function, the dump() function will output the excel content in html format, use htmlentities to convert the characters into html, it uses ISO8559-1 encoding by default Yes, so you have to change the htmlentities($val) function in the excel_reader2.php source code to htmlentities($val,ENT_COMPAT,"GB2312");.

Finally, php-excel-reader has two important methods for operating excel :

1.dump(), which can output excel content in html format:

echo $data->dump(true,true);

2. Store the excel data in the array, use $data->sheets, print it as follows:

Copy the code The code is as follows:

Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 5
[numCols] => 4
[cells] => Array
(
[1] => Array
(
[1] => 编号
[2] => 姓名
[3] => 年龄
[4] => 学号
)
[2] => Array
(
[1] => 1
[2] => 小红
[3] => 22
[4] => a1000
)
[3] => Array
(
[1] => 2
[2] => 小王
[3] => 33
[4] => a1001
)
[4] => Array
(
[1] => 3
[2] => 小黑
[3] => 44
[4] => a1002
)
[5] => Array
(
[2] => by
[3] => www.phpddt.com
)
)
[cellsInfo] => Array
(
[1] => Array
(
[1] => Array
(
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[2] => Array
(
[1] => Array
(
[string] => 1
[raw] => 1
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 22
[raw] => 22
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[3] => Array
(
[1] => Array
(
[string] => 2
[raw] => 2
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[2] => Array
(
[xfIndex] => 23
)
[3] => Array
(
[string] => 33
[raw] => 33
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[4] => Array
(
[xfIndex] => 23
)
)
[4] => Array
(
[1] => Array
(
[string] => 3
[raw] => 3
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 44
[raw] => 44
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[5] => Array
(
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 24
[hyperlink] => Array
(
[flags] => 23
[desc] => www.phpddt.com
[link] => http://www.phpddt.co
)
)
)
)
)
[1] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[numCols] => 0
)
[2] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[numCols] => 0
)
)

这样你应该知道怎么取excel中的数据了,好了,使用php-excel-reader读取excel文件就是这么简单

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/326250.htmlTechArticlephp开发中肯定会遇到将excel文件内容导入到数据库的需要,php-excel-reader是一个读取excel的类,可以很轻松的使用它读取excel文件非常方便。...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!