PHP read Excel file php excelreader read Excel file _php instance

Source: Internet
Author: User
Tags deprecated explode php explode php source code

PHP Development will certainly encounter the need to import Excel file content into the database, Php-excel-reader is a class to read Excel, you can easily use it to read Excel files is very convenient.

Php-excel-reader Download Address: http://www.jb51.net/codes/67223.html

I downloaded the php-excel-reader-2.21 version, the use of the time also encountered a few small problems, and then elaborate, first on the PHP instance:

I use excel in the following figure:

The PHP code is as follows:

Copy Code code as follows:

<?php
/*by www.phpddt.com*/
Header ("Content-type:text/html;charset=utf-8");
Require_once ' excel_reader2.php ';
Creating objects
$data = new Spreadsheet_excel_reader ();
Set Text output encoding
$data->setoutputencoding (' UTF-8 ');
Reading Excel files
$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 contents of each cell
echo $data->sheets[0][' cells ' [$i] [$j]. ' ';
}
Echo ' <br> ';
}
?>

Read the result screenshot below

Again, the small problem of this class:

(1) appears deprecated:function split () is deprecated in ... Error

Resolution: The excel_reader2.php source in the split to explode, details click PHP explode and split the difference introduced

(2) appears deprecated:assigning the return value of the new by reference is deprecated in error

Resolve: excel_reader2.php Source code $this->_ole =& New Oleread () & removed, because php5.3 abolished the =& symbol directly with = reference

(3) Garbled problem solving:

The constructor is a function spreadsheet_excel_reader ($file = ', $store _extended_info=true, $outputEncoding = '), and its default encoding is Utf-8, If you do not specify, garbled problems may occur, via $data->setoutputencoding (' GBK '), and if you use the dump () function, the dump () function outputs an HTML format for Excel content. Using Htmlentities to convert characters to HTML, it defaults to iso8559-1 encoding, so you'll change the htmlentities ($val) function in excel_reader2.php source code to Htmlentities ($val, Ent_compat, "GB2312");

Finally, Php-excel-reader operates on two important methods in Excel :

1.dump (), which can output Excel content in HTML format:

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

2. Put the Excel data into the array, using $data->sheets, print the following:

Copy Code code as follows:

Array
(
[0] => Array
(
[MaxRow] => 0
[Maxcol] => 0
[NumRows] => 5
[Numcols] => 4
[Cells] => Array
(
[1] => Array
(
[1] => number
[2] => name
[3] => age
[4] =>
)
[2] => Array
(
[1] => 1
[2] => Xiao Hong
[3] => 22
[4] => a1000
)
[3] => Array
(
[1] => 2
[2] => Xiao Wang
[3] => 33
[4] => a1001
)
[4] => Array
(
[1] => 3
[2] => small black
[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
)
)

So you should know how to get the data in Excel, well, it's easy to read Excel files using Php-excel-reader.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.