1.下载PHPExcel:https://share.weiyun.com/55CpSL3
2.解压文件:
3.将classes改为PHPExcel,复制到根目录的vendor:
3.前端代码:
4控制器中导入方法:
//导入功能
public function savestudentImport(){
//import('phpexcel.PHPExcel', EXTEND_PATH);//方法二
vendor("PHPExcel.PHPExcel"); //方法一
$objPHPExcel = new \PHPExcel();
//获取表单上传文件
$file = request()->file('excel');
$info = $file->validate(['size'=>15678,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
if($info){
$exclePath = $info->getSaveName(); //获取文件名
$file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath; //上传文件的地址
$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]['name'] = $v[0];
$data[$k]['type'] = $v[1];
$i++;
}
$success=Db::name('fuzhuang')->insertAll($data); //批量插入数据 这里的数据表改为你需要的。
//$i=
$error=$i-$success;
$this->success('导入成功{$success}条记录,失败{$error}条记录','lan/index');
// echo "总{$i}条,成功{$success}条,失败{$error}条。";
// Db::name('t_station')->insertAll($city); //批量插入数据
}else{
// 上传失败获取错误信息
echo $file->getError();
}
}
5.我们在数据库创建fuzhuang表:
7.
8.再次查看数据库:
9导出功能:
代码:
$xlsData=Db::name('demo')->select();
Vendor('PHPEXCEL.PHPEXCEL');
Vendor('PHPEXCEL.PHPEXCEL.Worksheet.Drawing');
Vendor('PHPEXCEL.PHPEXCEL.Writer.Excel2007');
$objExcel=new \PHPEXCEL();
$objWriter=\PHPEXCEl_IOFactory::createWriter($objExcel,'Excel2007');
$objActSheet=$objExcel->getActiveSheet();
$key=ord("A");
$letter=explode(',',"A,B,C");
$arrHeader=array('ID','公司名称','创建时间','公司官网','后台','账户','密码','显示密码','完成周期','结束时间','源码地址');
$length=count($arrHeader);
for($i=0;$i<$length;$i++){
$objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
}
foreach($xlsData as $k =>$v){
$k+=2;
$objActSheet->setCellValue('A'.$k,$v['ID']);
$objActSheet->setCellValue('B'.$k,$v['name']);
$objActSheet->setCellValue('C'.$k,$v['time']);
$objActSheet->setCellValue('D'.$k,$v['address']);
$objActSheet->setCellValue('E'.$k,$v['houtai']);
$objActSheet->setCellValue('F'.$k,$v['username']);
$objActSheet->setCellValue('G'.$k,$v['password']);
$objActSheet->setCellValue('H'.$k,$v['pass']);
$objActSheet->setCellValue('I'.$k,$v['time']);
$objActSheet->getRowDimension($k)->setRowHeight(20);
}
$width=array(10,10,10,10,10,10,10,10,10,10);
$objActSheet->getColumnDimension('A')->setWidth($width[1]);
$objActSheet->getColumnDimension('B')->setWidth($width[2]);
$objActSheet->getColumnDimension('C')->setWidth($width[3]);
$objActSheet->getColumnDimension('D')->setWidth($width[4]);
$objActSheet->getColumnDimension('E')->setWidth($width[5]);
$objActSheet->getColumnDimension('F')->setWidth($width[6]);
$objActSheet->getColumnDimension('G')->setWidth($width[7]);
$objActSheet->getColumnDimension('H')->setWidth($width[8]);
$objActSheet->getColumnDimension('I')->setWidth($width[9]);
$outfile=date('Y-m-d h:i:sa');
ob_end_clean();
header("Content-Type:application/force-download");
header("content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:inline;filename="'.$outfile.'.xls"');
header('Content-Transfer-Encoding:binary');
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Pragma:no-cache');
$objWriter->save('php://output');