public function addall(){ $Water = M('Waterrate'); $config = array( 'maxSize' => 0, 'rootPath' => 'Public', 'savePath' => '/Home/excel/', 'saveName' => array('uniqid',''), 'exts' => array('xlsx','xls'), ' autoSub' => false, 'subName' => '' ); $upload = new ThinkUpload($config); $excel = $upload->upload(); $xls = $excel['excel']['savename']; $uploadfile = "Public/Home/excel/".$xls; vendor('phpexcel.Classes.PHPExcel'); //导入thinkphp第三方类库 vendor('phpexcel.Classes.PHPExcel.IOFactory'); //导入thinkphp第三方类库
if($excel[0]['extension'] == 'xlsx'){
$objReader = PHPExcel_IOFactory::createReader('Excel2007');//use excel2007 for 2007 format
}else if($excel[0]['extension'] == 'xls'){
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
} $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format $objPHPExcel = $objReader->load($uploadfile); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 //循环读取excel文件,读取一条,插入一条 for($j=1;$j<=$highestRow;$j++) { for($k='A';$k<=$highestColumn;$k++) { $str .= iconv('utf-8','gbk',$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue()).'\';//读取单元格 } //explode:函数把字符串分割为数组。 $strs = explode("\",$str); // $sql = "INSERT INTO tab_zgjx_award (jx_name,jx_degree,jx_item_name,jx_unit,dy_originator,de_originator,xm_intro,hj_item_id) VALUES('". // $strs[0]."','". //奖项名称 // $strs[1]."','". //奖项届次 // $strs[2]."','". //获奖项目名 // $strs[3]."','". //获奖单位 // $strs[4]."','". //第一发明人 // $strs[5]."','". //第二发明人 // $strs[6]."','". //项目简介 // $strs[7]."')"; //获奖项目编号 $data['name'] = $strs[0]; //奖项名称 $data['waterrate'] = $strs[1]; //奖项届次 $res = $Water->add($data); $str = ""; } // unlink($uploadfile); //删除上传的excel文件,导入成功 if($res){ $this->success('提交成功'); }else{ $this->error("提交失败"); } }