thinkphp6 整合PhpSpreadsheet 导出数据到excel,加边框
taotaoit ThinkPHP6 2022-11-24 229 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

3714920 2275 32
最新评论
<script>alert(11)</script>
评 留言
:kaixin: :haha:
评 phpExcel列数据自动换行的方法->getAlignment()->setWrapText(true);
666 :kaixin:
评 return和return false的区别
楼主很给力,请教了有一个问题,很细心给我解答,还帮我解决了问题。 :zhenbang: :zhenbang: :zhenbang:
评 留言
厉害 :zhenbang: :zhenbang: :zhenbang:
评 留言
文章标签更多
ThinkPHP (240)
Mysql (48)
DedeCms (33)
jQuery (58)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)

thinkphp6 整合PhpSpreadsheet 导出数据到excel,加边框

效果图

from clipboard

完整代码:

<?php
namespace app\admin\model;

use think\facade\Db;
use app\common\model\Base;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;
/**
 * Excel模型
 */
class Excel extends Base{
	// 主键
	protected $pk = 'excelId';	
	// 禁用软删除
	protected $deleteTime = false;
	/**
	 * 导出
	 */
	public function export(){
		$data = $this->select();
		
		$spreadsheet = new Spreadsheet();
		$sheet = $spreadsheet->getActiveSheet();
		$sheet->setCellValue('A1', '标题');
		$sheet->setCellValue('B1', '内容');
		
		/*--------------开始从数据库提取信息插入Excel表中------------------*/
		$i=2;  //定义一个i变量,目的是在循环输出数据是控制行数
		 /*$i = 2,因为第一行是表头,所以写到表格时候只能从第二行开始写。*/
		$count = count($data);  //计算有多少条数据
		for ($i = 2; $i <= $count+1; $i++) {
			$sheet->setCellValue('A' . $i, $data[$i-2]['excelTitle']); 
			$sheet->setCellValue('B' . $i, $data[$i-2]['excelContent']);
		}		
		//样式设置 - 边框
// $styleArray = [
// 	'borders' => [
// 			'outline' => [
// 					'borderStyle' => Border::BORDER_THICK,
// 					'color' => ['argb' => 'FFFF0000'],
// 			],
// 			'inside' => [
// 					'borderStyle' => Border::BORDER_THIN,
// 			]
// 	],
// ];
// 或者
$styleArray = [
	'borders' => [
			'allBorders' => [ // allBorders可以替换成top,left,right,bottom
					'borderStyle' => Border::BORDER_THICK,
					'color' => ['argb' => 'ffff0000'],
			],
	],
];

	$sheet->getStyle('A1:D1')->applyFromArray($styleArray);
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.'信息'.'.xlsx"');
		header('Cache-Control: max-age=0');
		$writer = new Xlsx($spreadsheet);
		$writer->save('php://output');
		exit;
	}

composer命令安装phpspreadsheet

composer require phpoffice/phpspreadsheet

版权声明:本站原创文章,允许自由转载。

相关推荐
tp6控制器不存在的解决方法:控制器不存在:app\controller\Index
ThinkPHP6 | 2021-12-01 3806
当我们下载tp6,应用 多应用模式的时候,提示错误:控制器不存在:app\controller\Index,如图: 原因 多应用没有配置 解决方法 步骤如下: (1)需要安装多应用模式扩展think-multi-app 进入项目根目录。我的路径是(切记改为自己的项目路径):D:\phpStudy\PHPTutorial\...
thinkphp6 整合PhpSpreadsheet 导出数据到excel
ThinkPHP6 | 2022-01-15 2777
thinkphp6 整合PhpSpreadsheet 导出数据到excel use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; /**根据提交的时间导出数据**/ public function outda...
评论:0条
评论加载中...
发表评论