我查询了数据库里面就只有6000多条数据,我用的是web方式去请求下载的。
执行结果却是
这是我控制器方法:
这个是我的Export类:
<?php
namespace App\Exports;
use App\Helper\Utils;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class CopyRightExport implements WithHeadings, WithMapping, WithTitle, ShouldAutoSize, WithEvents, FromQuery
{
use Exportable;
protected $months = [];//2.0月份
protected $months3 = [];//3.0月份
public function __construct()
{
$startMonth = "2018-8";//2.0开始月份
$startMonth3 = "2019-8";//3.0开始月份
//获取当前月份
$thisMonth = date("Y-m");
$months = [$startMonth];
$months3 = [$startMonth3];
while (true) {
if ($startMonth == $thisMonth) {
break;
}
$nextMonth = Utils::getMonth(0, $startMonth);
$months[] = $nextMonth;
$startMonth = $nextMonth;
}
while (true) {
if ($startMonth3 == $thisMonth) {
break;
}
$nextMonth3 = Utils::getMonth(0, $startMonth3);
$months3[] = $nextMonth3;
$startMonth3 = $nextMonth3;
}
$this->months = $months;
$this->months3 = $months3;
}
public function query()
{
// 查询版权方
return Novel::where("platform_id", "!=", Novel::MY_PLATFORM)
->with(['platform'])
->select(['id', 'name', 'author', 'platform_id']);
}
public function map($novel): array
{
$money = [];
foreach ($this->months as $month) {
//起止时间
$days = date('t', strtotime($month));
//计算起始时间
$startTime = $month . "-1 00:00:00";
$endTime = $month . "-" . $days . " 23:59:59";
$money[] = ChannelIncomeLog::where('platform_id', $novel->platform_id)
->where("novel_id", $novel->id)
->whereBetween('created_at', [$startTime, $endTime])
->sum("money");
}
foreach ($this->months3 as $month3){
//起止时间
$days = date('t', strtotime($month3));
//计算起始时间
$startTime3 = $month3 . "-1 00:00:00";
$endTime3 = $month3 . "-" . $days . " 23:59:59";
$money[] = ChannelIncomeXFLog::where('platform_id', $novel->platform_id)
->where("novel_id", $novel->id)
->whereBetween('created_at', [$startTime3, $endTime3])
->sum("money");
}
$novel->data = $money;
$arr1 = [
$novel->id,
$novel->name,
$novel->author,
$novel->platform->name ?? "未知",
];
return array_merge($arr1, $novel->data);
}
public function headings(): array
{
$startMonth = "2018-8";
//获取当前月份
$thisMonth = date("Y-m");
$month = [];
$month[] = $startMonth . " 收入(2.0)";
while (true) {
if ($startMonth == $thisMonth) {
break;
}
$nextMonth = Utils::getMonth(0, $startMonth);
$month[] = $nextMonth . " 收入(2.0)";
$startMonth = $nextMonth;
}
foreach ($this->months3 as $month3){
$month[] = $month3 . " 收入(3.0)";
}
$arr = [
"书本id",
"书本名称",
"书本作者",
"版权方"
];
return array_merge($arr, $month);
}
public function title(): string
{
return 'novel';
}
public function sheets(): array
{
return [
(new self()),
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getColumnDimension('A')->setAutoSize(false)->setWidth(15);
$event->sheet->getColumnDimension('B')->setAutoSize(false)->setWidth(25);
$event->sheet->getColumnDimension('C')->setAutoSize(false)->setWidth(25);
$event->sheet->getColumnDimension('D')->setAutoSize(false)->setWidth(20);
}
];
}
}
虽然原数据只有6000多条,但是每条原数据会去查询数据库和原数据相关的数据,所以是不是这里导致的啊?
这个问题是什么原因造成的啊?
有什么优化方案吗?
关于导出excel,结合自身经验,有以下一些优化的思路:
关于这方面写过一遍博客,地址:https://segmentfault.com/a/11...
博客原文地址:https://tsmliyun.github.io/ph...