1. 程式人生 > >PHPExcel讀取csv格式檔案,解決中文讀取為空問題

PHPExcel讀取csv格式檔案,解決中文讀取為空問題

PHPExcel讀取csv格式檔案

$file = "test.csv";  
$type = strtolower( pathinfo($file, PATHINFO_EXTENSION) );  
$path = __YOUR_FILE_PATH__.'/'.$file;
if (!file_exists($path)) { die('no file!'); }//根據不同型別分別操作

 if( $type=='xlsx'||$type=='xls' ){ 
 		$objPHPExcel = PHPExcel_IOFactory::load($path);
  }else if( $type=='csv' ){ 
  		$objReader = PHPExcel_IOFactory::createReader('CSV')
  		 ->setDelimiter(',')  
  		 ->setInputEncoding('GBK') //不設定將導致中文列內容返回boolean(false)或亂碼   
  		 ->setEnclosure('"')  
  		 ->setLineEnding("\r\n")  //新版本可刪除 
  		 ->setSheetIndex(0); 
		$objPHPExcel = $objReader->load($path); 
}else{ 
		die('Not supported file types!'); 
}