[Into] PHP reads Excel content

Source: Internet
Author: User

From: http://apps.hi.baidu.com/share/detail/23157364

The following three methods are commonly used to read Excel using PHP, which have their respective advantages and disadvantages. I personally recommend the third method because it can be used across platforms.

1. Read data in .csv format
Convert .xlsinto A. CSV text format, and then use PHP to analyze the file. It is no different from PHP to analyze the text.
Advantages: cross-platform, highly efficient, and readable/writable.
Disadvantage: Only .csvfiles can be directly used. If .xls binary files are often accepted, they must be manually converted and cannot be automated. A file has only one sheet.

PHP self-contained analysis. CSV function: fgetcsv

array fgetcsv (INT $ handle [, int $ length [, string $ delimiter [, string $ enclosure])
handle is a valid file pointer generated by fopen (), popen (), or fsockopen.
length (optional) must be greater than the maximum row in the CVS file. This parameter is optional in PHP 5. If this parameter is ignored (set to 0 in PHP 5.0.4 and later versions), the length is not limited, but the execution efficiency may be affected.
delimiter (optional) set the field delimiter (only one character is allowed). The default value is comma.
enclosure (optional) set the field surround character (only one character is allowed). The default value is double quotation marks. This parameter is added in PHP 4.3.0. Similar to fgets (), only the rows read by fgetcsv () are parsed, fields in CSV format are found, and an array containing these fields is returned.
if an error occurs in fgetcsv (), false is returned, including when the file ends.
note: empty rows in the CSV file will be returned as an array containing a single null field and will not be treated as an error.

You can also manually analyze the string.

<? PHP  $ Row = 1 ;  $ Handle = Fopen ("Test.csv", "R" );  While ( $ Data = Fgetcsv ( $ Handle , 1000 ,"," )){  $ Num =Count ( $ Data  );  Echo " $ Num Fields in line $ Row : \ N" ;  $ Row ++ ;  For ( $ C = 0; $ C < $ Num ; $ C ++){  Echo   $ Data [ $ C ]. "\ N" ;}}  Fclose ( $ Handle  ); ?>

You can also use the fputcsv function to format rows into CSV and Write File pointers.

2. ODBC link data source

Advantages: supports multiple formats, such as CVS and XLS. Supports read/write and uses standard SQL languages, which are almost the same as SQL Server and MySQL databases.
Disadvantage: Windows servers are supported.

3. php custom class

Advantage: cross-platform. Some Classes support write operations. Supports. xls binary files
Common classes include phpexcelreader and phpexcel. The latter supports read/write, but php5.2 or above is required.

Phpexcelreader is used to read files. Returns an array containing all the contents of the table.
The usage of this class can refer to example. php In the compressed file downloaded from the website.
However, when I download it back (version), pay attention to the following two points:
Modify the following line in reader. php
Set require_once 'spreadsheet/EXCEL/Reader/oleread. php ';

Change to require_once 'oleread. inc ';

Example. php
Modify $ data-> setoutputencoding ('cp1251 ');

$ Data-> setoutputencoding ('cp936 ');

Modify nl2br (htmlentities ($ data-> sheets [$ sheet] ['cells '] [$ row] [$ Col]);

For $ table_output [$ sheet]. = nl2br (htmlspecialchars ($ data-> sheets [$ sheet] ['cells '] [$ row] [$ Col]);

Otherwise there will be problems with Chinese characters.
In traditional Chinese, you can change it to cp950 and the Japanese version to cp932. For details, refer to the codePage description.

Change $ data-> read('jxlrwtest.xls ') to your Excel file name. The jxlrwtest.xls file attached to the ZIP file should be broken.
This is:

Phpexcelreader: http://sourceforge.net/projects/phpexcelreader/
Phpexcel: http://www.codeplex.com/PHPExcel/Wiki/View.aspx? Title = documents & referringtitle = home

 

Additional information:

Baidu search

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.