一、PHPEXECL下载地址
Git地址:https://github.com/PHPOffice/PHPExcel
把对应的class文件改名成PHPExcel,放到vender文件中
二、导入前端
三、导入控制器
public function import(){
//import('phpexcel.PHPExcel', EXTEND_PATH);//方法二
vendor("PHPExcel.PHPExcel"); //方法一
$objPHPExcel = new \PHPExcel();
//获取表单上传文件
$file = request()->file('excel');
$info = $file->validate(['ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
if($info){
$exclePath = $info->getSaveName(); //获取文件名
$file_types = explode(".", $exclePath);
$file_type = $file_types [count($file_types) - 1];//xls后缀
$file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath; //上传文件的地址
if($file_type == 'xls'){
$objReader =\PHPExcel_IOFactory::createReader('Excel5');
}else{
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
}
$obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
echo "";
$excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
$data = [];
$i=0;
foreach($excel_array as $k=>$v) {
$data[$k]['title'] = $v[0];
$data[$k]['key'] = $v[1];
$data[$k]['desc'] = $v[2];
$data[$k]['author'] = $v[3];
$data[$k]['content'] = $v[4];
$data[$k]['source'] = $v[5];
$data[$k]['click'] = '50';
$data[$k]['cateid'] = $v[7];
$data[$k]['sort'] = '0';
$data[$k]['headline'] = '0';
$data[$k]['status'] = '1';
$data[$k]['create_time'] = time();
$i++;
}
$success=db('article')->insertAll($data); //批量插入数据 这里的数据表改为你需要的。
//$i=
$error=$i-$success;
$this->success("导入成功.'$success.'条记录,失败.'$error.'条记录",'execl');
}else{
// 上传失败获取错误信息
echo $file->getError();
}
}
四、导入前端
五、导入控制器
public function export(){
$path = dirname(__FILE__); //找到当前脚本所在路径
Loader::import("PHPExcel.PHPExcel");
Loader::import("PHPExcel.PHPExcel.Writer.IWriter");
Loader::import("PHPExcel.PHPExcel.Writer.Abstract");
Loader::import("PHPExcel.PHPExcel.Writer.Excel5");
Loader::import("PHPExcel.PHPExcel.Writer.Excel2007");
Loader::import("PHPExcel.PHPExcel.IOFactory");
$objPHPExcel = new PHPExcel();
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
// 实例化完了之后就先把数据库里面的数据查出来
$sql = db('article')->select();
// 设置表头信息
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'id')
->setCellValue('B1', 'title')
->setCellValue('C1', 'key')
->setCellValue('D1', 'desc')
->setCellValue('E1', 'author')
->setCellValue('F1', 'thumb')
->setCellValue('G1', 'content')
->setCellValue('H1', 'source')
->setCellValue('I1', 'click')
->setCellValue('J1', 'cateid')
->setCellValue('K1', 'sort')
->setCellValue('L1', 'headline')
->setCellValue('M1', 'status');
$i=2; //定义一个i变量,目的是在循环输出数据是控制行数
$count = count($sql); //计算有多少条数据
for ($i = 2; $i <= $count+1; $i++) {
$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $sql[$i-2]['id']);
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $sql[$i-2]['title']);
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $sql[$i-2]['key']);
$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $sql[$i-2]['desc']);
$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $sql[$i-2]['author']);
$objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $sql[$i-2]['thumb']);
$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $sql[$i-2]['content']);
$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $sql[$i-2]['source']);
$objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $sql[$i-2]['click']);
$objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $sql[$i-2]['cateid']);
$objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $sql[$i-2]['sort']);
$objPHPExcel->getActiveSheet()->setCellValue('L' . $i, $sql[$i-2]['headline']);
$objPHPExcel->getActiveSheet()->setCellValue('M' . $i, $sql[$i-2]['status']);
}
$objPHPExcel->getActiveSheet()->setTitle('article'); //设置sheet的名称
$objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来
$PHPWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
header('Content-Disposition: attachment;filename="article.xlsx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
}
六、遇到的问题
这个问题主要是.xls表格导入时出现的问题,这个需要使用Excel5而不能用Excel2007
所以需要判断
$file_types = explode(".", $exclePath);
$file_type = $file_types [count($file_types) - 1];//xls后缀
if($file_type == 'xls'){
$objReader =\PHPExcel_IOFactory::createReader('Excel5');
}else{
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
}