PHPexcel(2)

时间:2021-03-28 17:00:39

//导出
public function export(){
$xlsData = Db('ent_apply')->select();
Vendor('PHPExcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
$objExcel = new \PHPExcel();
//set document Property
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

$objActSheet = $objExcel->getActiveSheet();
$key = ord("A");
$letter =explode(',',"A,B,C,D,E,F,G");
$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['a_company']);
$objActSheet->setCellValue('B'.$k, $v['a_trade']);
$objActSheet->setCellValue('C'.$k, $v['a_city']);
$objActSheet->setCellValue('D'.$k, $v['a_name']);
$objActSheet->setCellValue('E'.$k, $v['a_phone']);
$objActSheet->setCellValue('F'.$k, $v['a_content']);
$objActSheet->setCellValue('G'.$k, $v['a_time']);
// $objActSheet->setCellValue('H'.$k, $v['role_id'] == 2?'正式会员':'普通会员');
// $objActSheet->setCellValue('I'.$k, $v['active'] == 1?'是':'否');
// 表格高度
$objActSheet->getRowDimension($k)->setRowHeight(20);
}

$width = array(10,15,20,25,30);
//设置表格的宽度
$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[1]);
$objActSheet->getColumnDimension('F')->setWidth($width[1]);
$objActSheet->getColumnDimension('G')->setWidth($width[1]);
$objActSheet->getColumnDimension('H')->setWidth($width[1]);
$objActSheet->getColumnDimension('I')->setWidth($width[1]);

$outfile = "企服申请信息列表.xlsx";
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');
}

安全起见 在extend  和vendor这里面都放上 PHPExcel扩展库