The PHP method for reading EXCEL

Source: Internet
Author: User
Tags php excel
: This article mainly introduces the php excel reading method. if you are interested in the PHP Tutorial, refer to it. 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) sets the field delimiter (only one character is allowed). The default value is comma.
Enclosure (optional) sets 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.

Example:

 
 
 
  
  ";$handle = fopen("three.csv","r");while ($data = fgetcsv($handle, 1000, ",")) {$num = count($data);$row++;echo "
  
  
     ";for ($c=0; $c < $num; $c++) {echo " 
    ";}echo " 
   ";}echo " 
  
".$data[$c]."
"; 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: only windows servers are supported.

3. PHP user-defined class (I put some large files in the next chapter)

Advantage: cross-platform. Some classes support write operations. Supports. xls binary files

In test. php
Modify $ data-> setOutputEncoding ('cp1251 ');

$ Data-> setOutputEncoding ('cp936 ');

For simplified Chinese cp936 and traditional Chinese, you can change it to CP950 or CP932 in Japanese.

Change $ data-> read('three.xls ') to your excel file name.

The above introduces the php excel reading method, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.