Php reads EXCEL files php excelreader reads excel files

Source: Internet
Author: User

Php development will definitely encounter the need to import the excel file content to the database. php-excel-reader is a class for reading excel files, which can be easily used to read excel files.

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

I downloaded the php-excel-reader-2.21 version, when using it also encountered a few small problems, and then I will elaborate on the first php instance:

My excel files are as follows:

The php code is as follows:
Copy codeThe Code is as follows:
<? Php
/* By www.phpddt.com */
Header ("Content-Type: text/html; charset = UTF-8 ");
Require_once 'excel _ reader2.php ';
// Create an object
$ Data = new Spreadsheet_Excel_Reader ();
// Sets the text output encoding.
$ Data-> setOutputEncoding ('utf-8 ');
// Read the Excel file
$ Data-> read ("example.xls ");
// $ Data-> sheets [0] ['numrows '] indicates the number of rows in Excel
For ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++ ){
// $ Data-> sheets [0] ['numcols'] indicates the number of columns in Excel.
For ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++ ){
// Display the content of each cell
Echo $ data-> sheets [0] ['cells '] [$ I] [$ j]. '';
}
Echo '<br> ';
}
?>

The read result is as follows::

Let's talk about the small issues of this class:

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

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

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

Solution: remove the $ this-> _ ole = & new OLERead () in the excel_reader2.php source code, because the = & Symbol in php5.3 is directly referenced by =.

(3) garbled problem solving:

The constructor is function Spreadsheet_Excel_Reader ($ file = '', $ store_extended_info = true, $ outputEncoding =''). The default encoding is UTF-8. If this parameter is not specified, garbled characters may occur. You can use $ data-> setOutputEncoding ('gbk'); specify, and if you use the dump () function, dump () function will excel content an html format output, using htmlentities to convert characters to html, it uses ISO8559-1 encoding by default, so you want to excel_reader2.php source code htmlentities ($ val) change the function to htmlentities ($ val, ENT_COMPAT, "GB2312.

Finally, php-excel-reader has two important operations in excel.:

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

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

2. Save the excel data to the array and use $ data-> Sheet to print the following:
Copy codeThe Code is as follows:
Array
(
[0] => Array
(
[Maxrow] => 0
[Maxcol] => 0
[NumRows] => 5
[NumCols] => 4
[Cells] => Array
(
[1] => Array
(
[1] => NO.
[2] => name
[3] => Age
[4] => Student ID
)
[2] => Array
(
[1] => 1
[2] => Xiaohong
[3] => 22
[4] => a1000
)
[3] => Array
(
[1] => 2
[2] => Xiao Wang
[3] => 33
[4] => a1001
)
[4] => Array
(
[1] => 3
[2] => Xiao Hei
[3] => 44
[4] => a1002
)
[5] => Array
(
[2] =>
[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
)
)

In this way, you should know how to obtain the data in excel. Well, it is so easy to use php-excel-reader to read the excel file.

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.