Phpexcel reading an implementation code _php instance of an Excel file
Source: Internet
Author: User
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
Copy CodeThe code is as follows:
Require_once ' phpexcel.php ';
/** format conversions for dates in Excel */
function GetData ($val) {
$JD = GREGORIANTOJD (1, 1, 1970);
$gregorian = Jdtogregorian ($jd +intval ($val)-25569);
return $gregorian;/** display format is "month/day/year" */
}
$filePath = ' test.xlsx ';
$PHPExcel = new Phpexcel ();
/** default to read Excel with excel2007, if the format is not correct, then use the previous version to read */
$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);
/** reading the first worksheet in an Excel file */
$currentSheet = $PHPExcel->getsheet (0);
/** get the largest column number */
$allColumn = $currentSheet->gethighestcolumn ();
How many lines does the/** get altogether? */
$allRow = $currentSheet->gethighestrow ();
/** starts the output from the second line because the first behavior in the Excel table is listed in the name */
for ($currentRow = 2; $currentRow <= $allRow; $currentRow + +) {
/** start output from column A */
for ($currentColumn = ' A '; $currentColumn <= $allColumn; $currentColumn + +) {
$val = $currentSheet->getcellbycolumnandrow (ord ($currentColumn)-up, $currentRow)->getvalue ();/**ord () Convert character to decimal number */
if ($currentColumn = = ' A ')
{
echo GetData ($val). " \ t ";
}else{
Echo $val;
/** if the output character is garbled, the output will need to be encoded with the ICONV function to convert, as follows GB2312 encoding to UTF-8 encoded output */
echo iconv (' Utf-8 ', ' gb2312 ', $val). " \ t ";
}
}
echo "
";
}
echo "\ n";
?>
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.