框架Thinkphp中使用Excel
1 .查询导出的数据和开始方法体
public function download(){//导出Excel表数据整理
$xlsData = M('Manager')->select();//查找需要导出的数据
$xlsCell = array(//设置excel文档的格式第一行就相当于标题
'管理ID号',
'管理员用户名',
'管理员密码',
'头像',
'状态',
'手机号',
'添加时间',
'最后登录时间',
'最后登录IP'
);
$newArray = array();//自定义数组
foreach ($xlsData as $k => $v)
{//然后把所有查找到的数据根据设置第一行的标题相对应放进数组里面
$newArray[$k]['mid'] = $v['mid'];
$newArray[$k]['admin'] = $v['admin'];
$newArray[$k]['password'] = $v['password'];
$newArray[$k]['avatar'] = $v['avatar'];
$newArray[$k]['type'] = $v['type'];
$newArray[$k]['phone'] = $v['phone'];
$newArray[$k]['add_time'] = $v['add_time'];
$newArray[$k]['last_login_time'] = $v['last_login_time'];
$newArray[$k]['last_login_ip'] = $v['last_login_ip'];
}
$xlsName = $_SESSION['admin'].'导出Excel'.date('Y-m-d H:i:s');//设置Excel表文件名称
$this->exportExcel($xlsName,$xlsCell,$newArray);//调用PHPExcel插件,这步的函数也需要自定义
}
2. Excel的写入步骤与单元格的设置
public function exportExcel($xlsName,$xlsCell,$newArray){
$xlsTitle = iconv('utf-8', 'gb2312', $xlsName);//文件名称需要转码避免乱码出错
$xlsCell_num = count($xlsCell);
$newArray_num = count($newArray);
vendor("PHPExcel.PHPExcel");//关键,利用thinkphp内置函数嵌套PHPExcel插件,如果在第二步没有改文件夹名称就这样:vendor("Classes.PHPExcel");点之前表示插件文件夹,点之后的表示PHPExcel.php文件的名称不要后缀名
$objPHPExcel = new \PHPExcel();//实例化PHPExcel
//合并单元做标题名
$objPHPExcel->getActiveSheet()->mergeCells('B1:J1');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1','会员信息表');
//循环写入标题
$key_num = 'B';
for ($i=0; $i < $xlsCell_num; $i++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($key_num.'2',$xlsCell[$i]);
$key_num++;
}
//从第三个单元格开始写入 ,当获取的数据条数数组+1时,则将变量$num+1从而将单元格坐标下移
foreach($newArray as $key => $value){
$num=$key+3;
$objPHPExcel->setActiveSheetIndex(0)
//Excel的第A列,uid是你查出数组的键值,下面以此类推
->setCellValue('B'.$num,$value['mid'])
->setCellValue('C'.$num,$value['admin'])
->setCellValue('D'.$num,$value['password'])
->setCellValue('E'.$num,$value['avatar'])
->setCellValue('F'.$num,$value['type'])
->setCellValue('G'.$num,$value['phone'])
->setCellValue('H'.$num,$value['add_time'])
->setCellValue('I'.$num,$value['last_login_time'])
->setCellValue('J'.$num,$value['last_login_ip']);
}
//设置每列单元格的宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(9);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(32);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(42);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(13);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(13);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);
//生成Excel文件
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
header("Content-Disposition:attachment;filename=$xlsTitle.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//Excel5为xls格式,excel2007为xlsx格式
$objWriter->save('php://output');
}
网友评论