if (!empty($_FILES)) {
$config = array(
'exts' => array('xlsx','xls'),
'maxSize' => 134217728,
'rootPath' =>"./Public/",
'savePath' => 'Uploads/',
'subName' => array('date','Ymd'),
);
$upload = new \Think\Upload($config);
if (!$info = $upload->upload()) {
$this->error($upload->getError());
}
vendor("PHPExcel.PHPExcel");
$file_name=$upload->rootPath.$info['photo']['savepath'].$info['photo']['savename'];
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表格后缀格式
if ($extension == 'xlsx') {
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
} else if ($extension == 'xls'){
$objReader =\PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
}
$sheet =$objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //取得总行数
$highestColumn =$sheet->getHighestColumn(); //取得总列数
//sql拼接
$sql = "INSERT INTO `song_sheet` (`song_no`, `song_name`, `singer_name`, `language`, `film_classification`, `dance_category`, `music_classification`, `epidemic_classification`, `picture_type`, `max_volume`, `song_count`, `backup_information`, `picture`, `retain`, `retain_date`) VALUES ";
for ($i = 2; $i <= $highestRow; $i++) {
//拼接
$sql .= "('".addslashes($objPHPExcel->getActiveSheet()->getCell("B" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("C" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("D" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("E" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("F" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("G" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("H" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("I" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("J" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("K" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("L" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("M" .$i)->getValue())."',"
. "'".addslashes($objPHPExcel->getActiveSheet()->getCell("N" .$i)->getValue())."',"
. "'".$objPHPExcel->getActiveSheet()->getCell("O" .$i)->getValue()."',"
. "'".$objPHPExcel->getActiveSheet()->getCell("P" .$i)->getValue()."'"
. "),";
}
$sql = substr($sql,0,strlen($sql)-1);
$sql .=";";
// print_r(array($sql));exit;
$Model = new \Think\Model();
$Model->execute($sql);
$this->success('导入成功!',U('Songsheet/index'));die;
}else{
$this->success('请选择上传的文件!');die;
}
参照http://www.imooc.com/video/8527