thinkphp 5.0 excel导出
2021/11/30 17:09:00
本文主要是介绍thinkphp 5.0 excel导出,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public function excel() { //查找要导出的数据表 $list = Db::table('eb_brand')->select(); //导出的文件名称 $file_name = date('Y-m-d_H:i:s') . '表格' . '.xls'; dirname(__FILE__); $PHPExcel=new \PHPExcel(); $objWriter = new \PHPExcel_Writer_Excel2007($PHPExcel); $PHPSheet = $PHPExcel->getActiveSheet(); //设置水平居中 $PHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $PHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $PHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $PHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 设置个表格标题 $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); // 设置垂直居中 $PHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $PHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $PHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $PHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $i = 2; foreach ($list as $key => $value) { $PHPSheet->setCellValue('A' . $i, '' . $value['id']); $PHPSheet->setCellValue('B' . $i, '' . $value['bname']); $PHPSheet->setCellValue('C' . $i, '' . $value['sort']); //获取到图片信息 $img = DB::table('eb_brand')->where('id', $value['id'])->field('image_input')->find(); $objDrawing[$key] = new \PHPExcel_Worksheet_Drawing(); $objDrawing[$key]->setPath('.' . $img['image_input']); //这里拼接 . 是因为要在根目录下获取 $objDrawing[$key]->setHeight(20);//照片高度 $objDrawing[$key]->setWidth(70); //照片宽度 $objDrawing[$key]->setCoordinates('D' . $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); $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"); //文件通过浏览器下载 } //具体参数根据实际需求
这篇关于thinkphp 5.0 excel导出的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-11开源 PHP 商城项目 CRMEB 二次开发和部署教程
- 2024-11-09怎么使用php在kaufland平台刊登商品?-icode9专业技术文章分享
- 2024-11-05PHP的抽象类和接口是什么,有什么区别-icode9专业技术文章分享
- 2024-11-01开源 PHP 商城项目 CRMEB 安装和使用教程
- 2024-11-01用php和mysql写无限分类,有哪几种方法-icode9专业技术文章分享
- 2024-10-31php数据分表导出时部分数据无法导出什么原因-icode9专业技术文章分享
- 2024-10-30有经验的 PHP 开发者学习一门新的编程语言,有哪些推荐的有前景的语言-icode9专业技术文章分享
- 2024-10-21php 检测图片是否篡改过-icode9专业技术文章分享
- 2024-10-20fruitcake/php-cors 该怎么使用-icode9专业技术文章分享
- 2024-10-18PHP7.1可以使用哪个版本的swoole-icode9专业技术文章分享