PHPExcel implements the code for reading Excel files. Knowledge points involved: php reads excel files cyclically. php converts ascii code to the characters, converts the characters to decimal numbers. php reads the excel date format and converts the characters to Chinese. knowledge points involved:
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
The code is as follows:
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"
";
}
Echo "\ n ";
?>
Php cyclically reads the excel file. php converts the characters into ascii code, converts the characters into decimal digits, and php reads the excel date format. then, it converts the characters into Chinese characters...