excel的导出

public function excel()
{
$PHPExcel = new \PHPExcel();
$PHPSheet = $PHPExcel->getActiveSheet();
// 设置个表格标题
$PHPSheet->setTitle("代理商");
$PHPSheet->setCellValue("A1", "ID");
$PHPSheet->setCellValue("B1", "分类名称");
$PHPSheet->setCellValue("C1", "图片");
$PHPSheet->setCellValue("D1", "状态");

//设置表格宽度
$PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);
$i = 2;
$list = \app\admin\model\Category::select();
$list = (new \think\Collection($list))->toArray();
$list = $this->get_cate_list($list);
foreach ($list as $key => $value) {
$PHPSheet->setCellValue('A' . $i, '' . $value['id']);
$PHPSheet->setCellValue('B' . $i, '' . $value['cate_name']);

$PHPSheet->setCellValue('D' . $i, '' . $value['is_show']);
$img = Db::table('pyg_category')->where('id', $value['id'])->field('image_url')->find();
$objDrawing[$key] = new \PHPExcel_Worksheet_Drawing();
$objDrawing[$key]->setPath('.' . $img['image_url']); //这里拼接 . 是因为要在根目录下获取

$objDrawing[$key]->setHeight(20);//照片高度
$objDrawing[$key]->setWidth(70); //照片宽度

$objDrawing[$key]->setCoordinates('C' . $key);

// 图片偏移距离
$objDrawing[$key]->setOffsetX(12);
$objDrawing[$key]->setOffsetY(12);
$objDrawing[$key]->setWorksheet($PHPExcel->getActiveSheet());
// $PHPSheet->setCellValue('D'.$i,''.$value['image_input']);
$i++;

}

$PHPExcel->setActiveSheetIndex(0);
$file_name = date('Y-m-d_H:i:s') . '表格' . '.xls';
$objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
header('Content-Disposition: attachment;filename=' . $file_name);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");


}

function get_cate_list($data,$pid=0,$level=0){
$info=[];
foreach ($data as $v){
if($v['pid']==$pid){
$v['level']= $level;
$info[]=$v;
$this->get_cate_list($data,$v['id'],$level+1);

}
}
return $info;
}
上一篇:PHPExcel多表生成及保存


下一篇:解决:PHPExcel 读取带公式的表格数据 以及时间转换问题