Home > Backend Development > PHP Tutorial > PHP-ExcelReader: PHP class library for parsing excel files_PHP tutorial

PHP-ExcelReader: PHP class library for parsing excel files_PHP tutorial

WBOY
Release: 2016-07-13 10:10:06
Original
1250 people have browsed it

PHP-ExcelReader: PHP class library for parsing excel files

PHP-ExcelReader is an open source project based on PHP, its function is to parse excel files.

The official website of PHP-ExcelReader is as follows:

http://phpexcelreader.sourceforge.net/

The downloaded file structure is as shown below:

PHP-ExcelReader: PHP class library for parsing excel files_PHP tutorial

Among them, the two files reader.php and oleread.inc in the Excel directory are files that must be included in excel parsing. The classes and methods required for parsing are written in these two files respectively. Among others, the two files example.php and example2.php are sample programs, the jxlwtest.xls file is the file that the sample program needs to parse, and the remaining two files are readme files.

The usage of PHP-ExcelReader is relatively simple, the following four lines of code are necessary: ​​

[php]
  1. require_once('Excel/reader.php'); // Reference the Excel/reader.php file and load the class library $reader = new Spreadsheet_Excel_Reader(); // Instantiate the parsing class Spreadsheet_Excel_Reader
  2. $reader->setOutputEncoding(CP1251); // Set the encoding method $reader->read(jxlwtest.xls); // Read and parse the file (jxlwtest.xls is the file name)

    After the above four lines of code are executed, the excel file will be parsed. The parsed result is in the array $reader->sheets[0], which contains six items: maxrow, maxcol, numRows, numCols, cells, and cellsInfo. The useful ones are numRows (number of rows) and numCols (column). number), cells (cell content). To get the specific data information of the excel file, just traverse the $reader->sheets[0]["cells"] array. The array is of the form

    $reader->sheets[0]["cells"][][] =

    The two-dimensional array of

    has row and column indexes starting from 1.

    What needs to be explained is:

    1. The Excel/reader.php file has already included the oleread.inc file with require_once, so there is no need to load oleread.inc when calling.

    2. The official excel file jxlwtest.xls has been damaged and cannot be read and parsed.

    3. Line 31 of the original Excel/reader.php file (below) causes the error:

    require_once 'Spreadsheet/Excel/Reader/OLERead.php';

    The reason is that the Spreadsheet/Excel/Reader/OLERead.php file does not exist. In fact, the file required here is Excel/oleread.inc. Just change the file name after require_once to 'oleread.inc'.

    4. Line 261 of the original Excel/reader.php file will result in the following warning:

    Deprecated: Assigning thereturn value of new by reference is deprecated

    The reason is that the =& symbol in this line has been deprecated in PHP 5.3. According to the role of this symbol here, just change it to =.

    5. PHP-ExcelReader does not support the parsing of Excel 2007 documents, which means that Excel files with the extension xlsx cannot be parsed using this class library; it only targets Excel files with the extension xls .

    6. The setting encoding method of PHP-ExcelReader is based on the iconv command. The parameter of the setOutputEncoding method is the name of the character set that needs to be set. To make this method effective, you need to install the iconv extension to the local PHP environment; if the local PHP environment does not have the iconv extension, the encoding method of PHP-ExcelReader defaults to Unicode. For information about the iconv command, please refer to the following link:

    www.Bkjia.com

    7. PHP-ExceReader may have precision errors when parsing integers. For example, 58 is parsed as 57.5 for unknown reasons. All you can do is check the data after parsing it, and then round it (if necessary).

    8. PHP-ExcelReader will either directly skip the blank cells in the excel file and not store them in the result array, or save them as 0 or "" (empty string).

    PHP-ExcelReader is an open source project based on PHP, its function is to parse excel files.

    The official website of PHP-ExcelReader is as follows:

    http://phpexcelreader.sourceforge.net/

    The downloaded file structure is as shown below:

    PHP-ExcelReader: PHP class library for parsing excel files_PHP tutorial

    Among them, the two files reader.php and oleread.inc in the Excel directory are files that must be included in excel parsing. The classes and methods required for parsing are written in these two files respectively. Among others, the two files example.php and example2.php are sample programs, the jxlwtest.xls file is the file that the sample program needs to parse, and the remaining two files are readme files.

    The usage of PHP-ExcelReader is relatively simple, the following four lines of code are necessary: ​​

    [php]
    1. require_once('Excel/reader.php'); // Reference the Excel/reader.php file and load the class library $reader = new Spreadsheet_Excel_Reader(); // Instantiate the parsing class Spreadsheet_Excel_Reader
    2. $reader->setOutputEncoding(CP1251); // Set the encoding method $reader->read(jxlwtest.xls); // Read and parse the file (jxlwtest.xls is the file name)

      After the above four lines of code are executed, the excel file will be parsed. The parsed result is in the array $reader->sheets[0], which contains six items: maxrow, maxcol, numRows, numCols, cells, and cellsInfo. The useful ones are numRows (number of rows) and numCols (column). number), cells (cell content). To get the specific data information of the excel file, just traverse the $reader->sheets[0]["cells"] array. The array is of the form

      $reader->sheets[0]["cells"][][] =

      The two-dimensional array of

      has row and column indexes starting from 1.

      What needs to be explained is:

      1. The Excel/reader.php file has already included the oleread.inc file with require_once, so there is no need to load oleread.inc when calling.

      2. The official excel file jxlwtest.xls has been damaged and cannot be read and parsed.

      3. Line 31 of the original Excel/reader.php file (below) causes the error:

      require_once 'Spreadsheet/Excel/Reader/OLERead.php';

      The reason is that the Spreadsheet/Excel/Reader/OLERead.php file does not exist. In fact, the file required here is Excel/oleread.inc. Just change the file name after require_once to 'oleread.inc'.

      4. Line 261 of the original Excel/reader.php file will result in the following warning:

      Deprecated: Assigning thereturn value of new by reference is deprecated

      The reason is that the =& symbol in this line has been deprecated in PHP 5.3. According to the role of this symbol here, just change it to =.

      5. PHP-ExcelReader does not support the parsing of Excel 2007 documents, which means that Excel files with the extension xlsx cannot be parsed using this class library; it only targets Excel files with the extension xls .

      6. The setting encoding method of PHP-ExcelReader is based on the iconv command. The parameter of the setOutputEncoding method is the name of the character set that needs to be set. To make this method effective, you need to install the iconv extension to the local PHP environment; if the local PHP environment does not have the iconv extension, the encoding method of PHP-ExcelReader defaults to Unicode. For information about the iconv command, please refer to the following link:

      www.Bkjia.com

      7. PHP-ExceReader may have precision errors when parsing integers. For example, 58 is parsed as 57.5 for unknown reasons. All you can do is check the data after parsing it, and then round it (if necessary).

      8. PHP-ExcelReader will either directly skip the blank cells in the excel file and not store them in the result array, or save them as 0 or "" (empty string).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/938944.htmlTechArticlePHP-ExcelReader: PHP class library for parsing excel files PHP-ExcelReader is an open source project based on PHP. Its function is to parse excel files. The official website of PHP-ExcelReader is as follows:...
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