涉及知識(shí)點(diǎn):
php對(duì)excel文件進(jìn)行循環(huán)讀取 php對(duì)字符進(jìn)行ascii編碼轉(zhuǎn)化,將字符轉(zhuǎn)為十進(jìn)制數(shù) php對(duì)excel日期格式讀取,并進(jìn)行顯示轉(zhuǎn)化 php對(duì)漢字亂碼進(jìn)行編碼轉(zhuǎn)化 復(fù)制代碼代碼如下: <?php require_once 'PHPExcel.php'; /**對(duì)excel里的日期進(jìn)行格式轉(zhuǎn)化*/ function GetData($val){ $jd = GregorianToJD(1, 1, 1970); $gregorian = JDToGregorian($jd+intval($val)-25569); return $gregorian;/**顯示格式為 “月/日/年” */ } $filePath = 'test.xlsx'; $PHPExcel = new PHPExcel(); /**默認(rèn)用excel2007讀取excel,若格式不對(duì),則用之前的版本進(jìn)行讀取*/ $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); /**讀取excel文件中的第一個(gè)工作表*/ $currentSheet = $PHPExcel->getSheet(0); /**取得最大的列號(hào)*/ $allColumn = $currentSheet->getHighestColumn(); /**取得一共有多少行*/ $allRow = $currentSheet->getHighestRow(); /**從第二行開(kāi)始輸出,因?yàn)閑xcel表中第一行為列名*/ for($currentRow = 2;$currentRow <= $allRow;$currentRow++){ /**從第A列開(kāi)始輸出*/ for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){ $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()將字符轉(zhuǎn)為十進(jìn)制數(shù)*/ if($currentColumn == 'A') { echo GetData($val)."\t"; }else{ //echo $val; /**如果輸出漢字有亂碼,則需將輸出內(nèi)容用iconv函數(shù)進(jìn)行編碼轉(zhuǎn)換,如下將gb2312編碼轉(zhuǎn)為utf-8編碼輸出*/ echo iconv('utf-8','gb2312', $val)."\t"; } } echo "</br>"; } echo "\n"; ?> |
|