php execl导出
/**
* 导出 是把数表中的数据添加到execl表中
*/
public function export(){
$xlsData = Db('user')->select();
Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
$objExcel = new \PHPExcel();//引用根目录下的phpexcel;
//set document Property
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
//如果报错Class 'XMLWriter' not found,则用下面一行
//$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objActSheet = $objExcel->getActiveSheet();
$letter =explode(',',"A,B,C,D,E,F");
$arrHeader = array('姓名','公司','职位','邮箱','状态');
//填充表头信息
$lenth = count($arrHeader);
for($i = 0;$i < $lenth;$i++) {
$objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
};
//填充表格信息
foreach($xlsData as $k=>$v){
$k +=2;
$objActSheet->setCellValue('A'.$k,$v['username']);
$objActSheet->setCellValue('B'.$k, $v['company']);
// // 图片生成
// $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
// $objDrawing[$k]->setPath('public/static/admin/images/profile_small.jpg');
// // 设置宽度高度
// $objDrawing[$k]->setHeight(40);//照片高度
// $objDrawing[$k]->setWidth(40); //照片宽度
// /*设置图片要插入的单元格*/
// $objDrawing[$k]->setCoordinates('C'.$k);
// // 图片偏移距离
// $objDrawing[$k]->setOffsetX(30);
// $objDrawing[$k]->setOffsetY(12);
// $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
// 表格内容
$objActSheet->setCellValue('C'.$k, $v['position']);
$objActSheet->setCellValue('D'.$k, $v['email']);
$objActSheet->setCellValue('E'.$k, $v['statuid'] == 1?'正常':'失效');
// 表格高度
$objActSheet->getRowDimension($k)->setRowHeight(20);
}
$width = array(20,20,15,10,10,30,10,15);
//设置表格的宽度
$objActSheet->getColumnDimension('A')->setWidth($width[5]);
$objActSheet->getColumnDimension('B')->setWidth($width[1]);
$objActSheet->getColumnDimension('C')->setWidth($width[0]);
$objActSheet->getColumnDimension('D')->setWidth($width[5]);
$objActSheet->getColumnDimension('E')->setWidth($width[5]);
$outfile = "人员表.xls";
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.'"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
}