PHPExcel reads the implementation code for the Excel file

  • 2020-05-10 17:47:29
  • OfStack

Knowledge points:

php reads the excel file in a loop

php encodes the character ascii and converts the character to a decimal number

php reads the excel date format and converts the display

php encodes and converts chaotic Chinese characters

 
<?php 

require_once 'PHPExcel.php'; 

/** right excel The date in the format conversion */ 
function GetData($val){ 
$jd = GregorianToJD(1, 1, 1970); 
$gregorian = JDToGregorian($jd+intval($val)-25569); 
return $gregorian;/** Display format is   "Month / day / In"  */ 
} 

$filePath = 'test.xlsx'; 

$PHPExcel = new PHPExcel(); 

/** The default with excel2007 read excel , if the format is not correct, the previous version will be used 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); 
/** read excel No. 1 A worksheet */ 
$currentSheet = $PHPExcel->getSheet(0); 
/** Gets the largest column number */ 
$allColumn = $currentSheet->getHighestColumn(); 
/** achieve 1 How many rows */ 
$allRow = $currentSheet->getHighestRow(); 
/** From the first 2 The line begins to output because excel The first in the table 1 Behavior of the column */ 
for($currentRow = 2;$currentRow <= $allRow;$currentRow++){ 
/** From the first A Column start output */ 
for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){ 
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord() Convert the character to 10 Hexadecimal number */ 
if($currentColumn == 'A') 
{ 
echo GetData($val)."\t"; 
}else{ 
//echo $val; 
/** If the output of Chinese characters are garbled, the output content should be used iconv The function is encoded as follows gb2312 Coding to utf-8 Coding the output */ 
echo iconv('utf-8','gb2312', $val)."\t"; 
} 
} 
echo "</br>"; 
} 
echo "\n"; 
?> 

Related articles: