//phpexcel导出 包括导出图片,这边是完整的代码, 首先需要 下载 他的文档包 然后引入, 其次查询你所想导出的数据,多条就以循环遍历的的方式 把它写入到 excel文档, 这边需要注意的是图片导出,如果有图片则需要 添加 黄色背景部分,没有则去掉;
if($id==1){
$name='商业房产';
}else{
$name='住宅房产';
}
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
$objPHPExcel = new \PHPExcel();
$data=Service::where(['class_name'=>$id])->get();
$ii=count($data);
//设置格子宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(35);
$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(35);
/*以下就是对处理Excel里的数据, 横着取数据,主要是这一步,其他基本都不要改*/
$objPHPExcel->setActiveSheetIndex(0)
//Excel的第A列,uid是你查出数组的键值,下面以此类推
->setCellValue('A1', '标题')
->setCellValue('B1', '图片')
->setCellValue('C1', '房产类别')
->setCellValue('D1', '收益率')
->setCellValue('E1', '排序')
->setCellValue('F1', '地址')
->setCellValue('G1', '价格(单位(万)日元)')
->setCellValue('H1', '面积')
->setCellValue('I1', '楼层')
->setCellValue('J1', '建造时间')
->setCellValue('K1', '方向')
->setCellValue('L1', '是否出租')
->setCellValue('M1', '地址详情')
->setCellValue('N1', '房产户型')
->setCellValue('O1', '点击量')
->setCellValue('P1', '停车场')
->setCellValue('Q1', '车站距离');
foreach($data as $k => $v){
if($v->months==null || $v->months==""){
$address="";
}else{
$address=$v->months->address;
}
$num=$k+2;
if($v->direction=='r0'){
$direction= '未出租';
}else{
$direction= '已出租';
}
// //导出图片
$img_info = getimagesize($_SERVER['DOCUMENT_ROOT'].$v->avatar);
$objPHPExcel->getActiveSheet()->getRowDimension($num)->setRowHeight($img_info[1]/5);
$objDrawing[$num] = new \PHPExcel_Worksheet_Drawing();
$objDrawing[$num]->setName('Photo');
$objDrawing[$num]->setDescription('Photo');
$objDrawing[$num]->setPath($_SERVER['DOCUMENT_ROOT'].$v->avatar);
// 设置宽度高度
$objDrawing[$num]->setHeight(80);//照片高度
$objDrawing[$num]->setWidth(80); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing[$num]->setCoordinates('B'.$num);
// 图片偏移距离
$objDrawing[$num]->setOffsetX(12);
$objDrawing[$num]->setOffsetY(12);
$objDrawing[$num]->setWorksheet($objPHPExcel->getActiveSheet());
//导入除图片之外的文本数据
$objPHPExcel->setActiveSheetIndex(0)
//Excel的第A列,uid是你查出数组的键值,下面以此类推
->setCellValue('A'.$num, $v->title)
// ->setCellValue('B'.$num, $v->avatar)
->setCellValue('C'.$num, $name)
->setCellValue('D'.$num, $v->earnings)
->setCellValue('E'.$num, $v->sort)
->setCellValue('F'.$num, $address)
->setCellValue('G'.$num, $v->tota_price)
->setCellValue('H'.$num, $v->area)
->setCellValue('I'.$num, $v->layer)
->setCellValue('J'.$num, $v->time)
->setCellValue('K'.$num, $v->direction)
->setCellValue('L'.$num, $direction)
->setCellValue('M'.$num, $v->address_detail)
->setCellValue('N'.$num, $v->titles)
->setCellValue('O'.$num, $v->click)
->setCellValue('P'.$num, $v->distance)
->setCellValue('Q'.$num, $v->parking);
}
$objPHPExcel->getActiveSheet()->setTitle($name);
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
die();