涉及知識點:
php對excel文件進行循環讀取
php對字符進行ascii編碼轉化,將字符轉為十進制數
php對excel日期格式讀取,並進行顯示轉化
php對漢字亂碼進行編碼轉化
1 <?php 2 3 require_once 'PHPExcel.php'; 4 5 /**對excel里的日期進行格式轉化*/ 6 function GetData($val){ 7 $jd = GregorianToJD(1, 1, 1970); 8 $gregorian = JDToGregorian($jd+intval($val)-25569); 9 return $gregorian;/**顯示格式為 “月/日/年” */ 10 } 11 12 $filePath = 'test.xlsx'; 13 14 $PHPExcel = new PHPExcel(); 15 16 /**默認用excel2007讀取excel,若格式不對,則用之前的版本進行讀取*/ 17 $PHPReader = new PHPExcel_Reader_Excel2007(); 18 if(!$PHPReader->canRead($filePath)){ 19 $PHPReader = new PHPExcel_Reader_Excel5(); 20 if(!$PHPReader->canRead($filePath)){ 21 echo 'no Excel'; 22 return ; 23 } 24 } 25 26 $PHPExcel = $PHPReader->load($filePath); 27 /**讀取excel文件中的第一個工作表*/ 28 $currentSheet = $PHPExcel->getSheet(0); 29 /**取得最大的列號*/ 30 $allColumn = $currentSheet->getHighestColumn(); 31 /**取得一共有多少行*/ 32 $allRow = $currentSheet->getHighestRow(); 33 /**從第二行開始輸出,因為excel表中第一行為列名*/ 34 for($currentRow = 2;$currentRow <= $allRow;$currentRow++){ 35 /**從第A列開始輸出*/ 36 for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){ 37 $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()將字符轉為十進制數*/ 38 if($currentColumn == 'A') 39 { 40 echo GetData($val)."\t"; 41 }else{ 42 //echo $val; 43 /**如果輸出漢字有亂碼,則需將輸出內容用iconv函數進行編碼轉換,如下將gb2312編碼轉為utf-8編碼輸出*/ 44 echo iconv('utf-8','gb2312', $val)."\t"; 45 } 46 } 47 echo "</br>"; 48 } 49 echo "\n"; 50 ?>