/**
* 导出execl(会员级别)
*/
public function execlLevel(){
// 实例化商品对象
$xlsModel = M('user');
// 判断商品状态,导出对应的商品数据
$url = $_SERVER['HTTP_REFERER'];
// 查找对应的数据
$xlsData = $xlsModel ->select(); //查询
foreach($xlsData as $k=>$v){
//循环体
}
// 引入PHPExcel
vendor("PHPExcel.PHPExcel");
// Create new PHPExcel object
$objPHPExcel = new \PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("ctos")
->setLastModifiedBy("ctos")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
//set width 设置宽
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
//设置行高度
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(25);
//set font size bold
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
//设置水平居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//合并cell
$objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
// set table header content
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '会员级别 时间:'.date('Y-m-d H:i:s'))
->setCellValue('A2', '排序')
->setCellValue('B2', '用户')
->setCellValue('C2', '类型')
->setCellValue('D2', '级别')
->setCellValue('E2', '单位名称')
->setCellValue('F2', '联系人')
->setCellValue('G2', '手机号')
->setCellValue('H2', '时间');
// Miscellaneous glyphs, UTF-8
for($i=0;$i<count($xlsData);$i++){
$objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3), $i+1);
$objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), $xlsData[$i]['username']);
$objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), $xlsData[$i]['groups']);
$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), $xlsData[$i]['level']); //这里调用了common.php的时间戳转换函数
$objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+3), $xlsData[$i]['companyname']);
$objPHPExcel->getActiveSheet(0)->setCellValue('F'.($i+3), $xlsData[$i]['contactman']);
$objPHPExcel->getActiveSheet(0)->setCellValue('G'.($i+3), $xlsData[$i]['uphone']);
$objPHPExcel->getActiveSheet(0)->setCellValue('H'.($i+3), $xlsData[$i]['time'].'天');
$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':H'.($i+3))->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':H'.($i+3))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
}
// sheet命名
$objPHPExcel->getActiveSheet()->setTitle('会员级别');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// excel头参数
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="会员级别('.date('Ymd-His').').xls"'); //日期为文件名后缀
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel5为xls格式,excel2007为xlsx格式
$objWriter->save('php://output');
exit;
}
PHP 导出execl
最新推荐文章于 2024-09-02 10:23:11 发布