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

“最难不过坚持”

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

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

6282558 2594 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (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 6200
当我们下载tp6,应用 多应用模式的时候,提示错误:控制器不存在:app\controller\Index,如图: 原因 多应用没有配置 解决方法 步骤如下: (1)需要安装多应用模式扩展think-multi-app 进入项目根目录。我的路径是(切记改为自己的项目路径):D:\phpStudy\PHPTutorial\...
tp6获取当前域名
ThinkPHP6 | 2022-02-22 4011
tp6获取当前域名 use think\facade\Request; Request::instance()->domain(); 结果为http://tp6.cc 后面不带/
评论:0条
评论加载中...
发表评论