Use php-excel-reader to read 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-reade

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://code.google.com/p/php-excel-reader/downloads/list

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:


 SetOutputEncoding ('utf-8'); // read the Excel file $ data-> read ("example.xls "); // $ data-> sheets [0] ['numrows '] is the number of rows in Excel for ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {// $ data-> sheets [0] ['numcols'] indicates 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 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. Click here for details.

(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:


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] => small red [3] => 22 [4] => a1000) [3] => Array ([1] => 2 [2] => John [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 ))

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.