一、thinkphp引入PHPExcel到/Thinkphp/Library/Vendor/
二、在控制器中引用
public function get_detail()
{
Vendor("PHPExcel.PHPExcel",'','.php');
$obj = new \PHPExcel;
$filename = self::FILE_DIR.'my_data.xls';
try {
$inputFileType = \PHPExcel_IOFactory::identify($filename);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($filename);
} catch (Exception $e) {
die('加载文件发生错误:'.pathinfo($filename,PATHINFO_BASENAME).':'.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for ($i=1; $i <= $highestRow ; $i++) {
$rowData[] = $sheet->rangeToArray("A{$i}:{$highestColumn}{$i}",null,true,false);
}
echo json_encode($rowData);
}
第一次使用中,遇到了一些坑,分享下
1.Vendor("PHPExcel.PHPExcel",'','.php');可以Vendor("PHPExcel.PHPExcel"); 后缀名视实际情况而定
2.要非常重视命名空间问题,一定要加上反斜杠'\
'