1.官网下载PHPExcel库地址:
https://github.com/PHPOffice/PHPExcel
2导入账号控制器中
/**
* 导入员工账号
*/
public function fabu1(){
Loader::import('PHPExcel.PHPExcel');
Loader::import('PHPExcel.PHPExcel.PHPExcel_IOFactory');
Loader::import('PHPExcel.PHPExcel.PHPExcel_Cell'); //实例化PHPExcel
$objPHPExcel = new \PHPExcel();
$file = request()->file('excel');
if ($file) {
$file_types = explode(".", $_FILES ['excel'] ['name']); // ["name"] => string(25) "excel文件名.xls"
$file_type = $file_types [count($file_types) - 1];//xls后缀
$file_name = $file_types [count($file_types) - 2];//xls去后缀的文件名 /*判别是不是.xls文件,判别是不是excel文件*/
if (strtolower($file_type) != "xls" && strtolower($file_type) != "xlsx") { echo '不是Excel文件,重新上传'; die;
}
$info = $file->move(ROOT_PATH . 'public' . DS . 'excel');//上传位置
$path = ROOT_PATH . 'public' . DS . 'excel' . DS; $file_path = $path . $info->getSaveName();//上传后的EXCEL路径
$re = $this->actionRead($file_path, 'utf-8');
array_splice($re, 1, 0);
unset($re[0]);
/*将数组的键改为自定义名称*/
// print_r($re);die;
$keys = array('name','password','number'); //键名按顺序
foreach ($re as $i => $vals) {
$re[$i] = array_combine($keys, $vals);
}
// print_r($re);die;
for ($i = 1; $i <= count($re); $i++) {
$data['name'] = $re[$i]['name'];
$data['password'] = $re[$i]['password'];
$data['number'] = $re[$i]['number'];
// print_r($data);die;
$arr = Db::name('user')->where('name',$re[$i]['name'])->find();
if(!$arr){
$res = Db::name('user')->insert($data);
}
}
}
return $this->redirect('xfabu');
}
3.
public function actionRead($filename,$encode='utf-8'){
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); //return $highestRow;
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
//var_dump($highestColumnIndex);
$excelData = array();
for($row = 1; $row <= $highestRow; $row++) {
for ($col = 0; $col < $highestColumnIndex; $col++) {
$excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
return $excelData;
}
4.文件中
5.数据库中
6导出到Excel
7代码
/*导出到Excel*/
public function phpExcelList($field, $list, $title='文件')
{
Loader::import('PHPExcel.PHPExcel');
Loader::import('PHPExcel.PHPExcel.PHPExcel_IOFactory');
Loader::import('PHPExcel.PHPExcel.PHPExcel_Cell'); //实例化PHPExcel
$objPHPExcel = new \PHPExcel();
// vendor('phpExcel.PHPExcel');
// $objPHPExcel = new \PHPExcel();
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //设置保存版本格式
foreach ($list as $key => $value) {
foreach ($field as $k => $v) {
if ($key == 0) {
$objPHPExcel->getActiveSheet()->setCellValue($k . '1', $v[1]);
}
$i = $key + 2; //表格是从2开始的
$objPHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
}
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename='.$title.'.xls');
header("Content-Transfer-Encoding:binary");
// $objWriter->save($title.'.xls');
$objWriter->save('php://output');
}
public function outExcelRecharge() {
//手机号
$data = Db::name('phone')
->alias('a')
->field(['a.id','a.phone','a.beizhu','a.status','a.time','a.type','a.user_id'])
->join('admin_chudan u','u.phone_id=a.id','left')
->field('u.user_bianhao ,u.name as uname,u.phone as uphone,u.address,u.money,u.time as utime,u.status as ustatus')
->join('admin_user s','s.id=a.user_id','left')
->field('s.name as sname,s.number as snumber')
->order('u.user_id desc')
->select();
foreach ($data as $key => $value) {
if($data[$key]['type'] == 0){ $data[$key]['type'] ="无" ;} else if($data[$key]['type'] == 1){ $data[$key]['type'] ="审核中" ;}else if($data[$key]['type'] == 2){ $data[$key]['type'] ="已出单" ;}
if($data[$key]['status'] == 0){ $data[$key]['status'] ="未拨打" ;} else if($data[$key]['status'] == 1){ $data[$key]['status'] ="已拨打" ;}
}
$field = array(
'A' => array('id', 'ID'),
'B' => array('user_id', '员工ID'),
'C' => array('sname', '员工账号'),
'D' => array('snumber', '员工编号'),
'E' => array('phone', '手机号'),
'F' => array('beizhu', '备注'),
'J' => array('type', '出单状态'),
'H' => array('status', '拨打状态'),
'I' => array('utime', '出单时间'),
'G' => array('uname', '用户姓名'),
'K' => array('address', '用户地址'),
'L' => array('money', '出单金额'),
);
$this->phpExcelList($field, $data, '手机号' . date('Y-m-d'));
}
最后结果