(Practical article) Phpexcel reading the implementation code of an Excel file

Source: Internet
Author: User
Tags ord

Using Phpexcel to read Excel 2007 or Excel2003 files, you need a friend, you can refer to the next.

Knowledge points involved:

PHP to iterate through Excel files

PHP converts characters into decimal numbers by ASCII encoding

PHP reads the Excel date format and displays the conversion

PHP code Conversion of Chinese characters garbled

<?PHPrequire_once' Phpexcel.php '; /** Format conversions for dates in Excel*/ functionGetData ($val){ $JD=Gregoriantojd(1, 1, 1970); $gregorian=Jdtogregorian($JD+intval($val)-25569); return $gregorian;/** display format is "month/day/year"*/ } $filePath= ' test.xlsx '; $PHPExcel=NewPhpexcel ();/** Read Excel by default with excel2007, if the format is not correct, then use the previous version to read*/ $PHPReader=Newphpexcel_reader_excel2007 ();if(!$PHPReader->canread ($filePath)){ $PHPReader=NewPhpexcel_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); /** Get the maximum number of columns*/ $allColumn=$currentSheet-Gethighestcolumn ();/** Get a total number of lines*/ $allRow=$currentSheet-Gethighestrow ();/** Start output from the second line because the first behavior column name in the Excel table*/  for($currentRow= 2;$currentRow<=$allRow;$currentRow++){ /** Start output from column a*/  for($currentColumn= ' A ';$currentColumn<=$allColumn;$currentColumn++){ $val=$currentSheet->getcellbycolumnandrow (Ord($currentColumn)-65,$currentRow)->getvalue ();/**ord () converts a character to a decimal number*/ if($currentColumn= = ' A ') { EchoGetData ($val)." \ t "; }Else{ //echo $val;/** If the output character is garbled, the output must be encoded with the ICONV function to convert the GB2312 encoding to UTF-8 encoded output as follows*/ Echo Iconv(' Utf-8 ', ' gb2312 ',$val)." \ t "; } } Echo"</br>"; } Echo"\ n"; ?>

(Practical article) Phpexcel reading the implementation code of an Excel file

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.