PHP读取CSV文件
我之前用的那种笨办法,结果碰到阿里云下载的csv,压根不好使
要么用csv格式无法读取,要么就是强制按照xlsx 读取了吧,有只能读第一列
$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!');
}
然后一搜,看到如下精简写法,好使!还是大牛厉害,大牛就是大牛啊
1 2 3 4 5 6 7 8 |
|