PHP Excel implementation code for reading Excel files

Source: Internet
Author: User
Tags php excel

Knowledge points:

Php reads excel files cyclically

Php encodes the characters in ascii format and converts the characters into decimal numbers.

Php reads the excel Date Format and converts it to display

Php encodes Chinese characters with garbled characters

Copy codeThe Code is as follows:
<? Php

Require_once 'phpexcel. php ';

/** Convert the date format in excel */
Function GetData ($ val ){
$ Jd = GregorianToJD (1, 1, 1970 );
$ Gregorian = JDToGregorian ($ jd + intval ($ val)-25569 );
Return $ gregorian;/** the display format is "month/day/year "*/
}

$ FilePath = 'test.xlsx ';

$ PHPExcel = new PHPExcel ();

/** Excel is read using excel2007 by default. If the format is incorrect, it will be read using the previous version */
$ PHPReader = new PHPExcel_Reader_Excel2007 ();
If (! $ PHPReader-> canRead ($ filePath )){
$ PHPReader = new PHPExcel_Reader_Excel5 ();
If (! $ PHPReader-> canRead ($ filePath )){
Echo 'no Excel ';
Return;
}
}

$ PHPExcel = $ PHPReader-> load ($ filePath );
/** Read the first worksheet in an excel file */
$ CurrentSheet = $ PHPExcel-> getSheet (0 );
/** Obtain the largest column number */
$ AllColumn = $ currentSheet-> getHighestColumn ();
/** Obtain the total number of rows */
$ AllRow = $ currentSheet-> getHighestRow ();
/** Output from the second row because the first row column name in the excel table */
For ($ currentRow = 2; $ currentRow <= $ allRow; $ currentRow ++ ){
/** Output from column */
For ($ currentColumn = 'a'; $ currentColumn <= $ allColumn; $ currentColumn ++ ){
$ Val = $ currentSheet-> getCellByColumnAndRow (ord ($ currentColumn)-65, $ currentRow)-> getValue ();/** ord () convert characters into decimal numbers */
If ($ currentColumn = 'A ')
{
Echo GetData ($ val). "\ t ";
} Else {
// Echo $ val;
/** If garbled characters are output, use the iconv function to convert the output content. Convert gb2312 encoding to UTF-8 encoding as follows */
Echo iconv ('utf-8', 'gb2312', $ val). "\ t ";
}
}
Echo "</br> ";
}
Echo "\ n ";
?>

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.