静态数据导出
匿名 · 更新于 2021/6/28
使用自定义结构
如果您没有使用 Eloquent 或有其他数据源(例如 API、MongoDB、Cache 等),您还可以返回自定义集合:
namespace App\Exports;
use App</span>Invoice;
use Maatwebsite</span>Excel</span>Concerns</span>FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
return new Collection([
[1, 2, 3],
[4, 5, 6]
]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#使用数组
如果您更喜欢使用普通数组而不是集合,则可以使用FromArray关注点:
namespace App\Exports;
use App</span>Invoice;
use Maatwebsite</span>Excel</span>Concerns</span>FromArray;
class InvoicesExport implements FromArray
{
public function array(): array
{
return [
[1, 2, 3],
[4, 5, 6]
];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
如果需要将数据从控制器传递到导出,可以使用构造函数来执行此操作:
namespace App\Exports;
use App</span>Invoice;
use Maatwebsite</span>Excel</span>Concerns</span>FromArray;
class InvoicesExport implements FromArray
{
protected $invoices;
<span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">public</span> <span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">function</span> <span class="token function" style="color: rgb(240, 141, 73);">__construct</span><span class="token punctuation" style="color: rgb(204, 204, 204);">(</span><span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">array</span> <span class="token variable" style="color: rgb(126, 198, 153);">$invoices</span><span class="token punctuation" style="color: rgb(204, 204, 204);">)</span>
<span class="token punctuation" style="color: rgb(204, 204, 204);">{</span>
<span class="token variable" style="color: rgb(126, 198, 153);">$this</span><span class="token operator" style="color: rgb(103, 205, 204);">-</span><span class="token operator" style="color: rgb(103, 205, 204);">></span><span class="token property" style="color: rgb(248, 197, 85); font-style: italic !important;">invoices</span> <span class="token operator" style="color: rgb(103, 205, 204);">=</span> <span class="token variable" style="color: rgb(126, 198, 153);">$invoices</span><span class="token punctuation" style="color: rgb(204, 204, 204);">;</span>
<span class="token punctuation" style="color: rgb(204, 204, 204);">}</span>
<span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">public</span> <span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">function</span> <span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">array</span><span class="token punctuation" style="color: rgb(204, 204, 204);">(</span><span class="token punctuation" style="color: rgb(204, 204, 204);">)</span><span class="token punctuation" style="color: rgb(204, 204, 204);">:</span> <span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">array</span>
<span class="token punctuation" style="color: rgb(204, 204, 204);">{</span>
<span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">return</span> <span class="token variable" style="color: rgb(126, 198, 153);">$this</span><span class="token operator" style="color: rgb(103, 205, 204);">-</span><span class="token operator" style="color: rgb(103, 205, 204);">></span><span class="token property" style="color: rgb(248, 197, 85); font-style: italic !important;">invoices</span><span class="token punctuation" style="color: rgb(204, 204, 204);">;</span>
<span class="token punctuation" style="color: rgb(204, 204, 204);">}</span>
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在您的控制器中,您现在可以使用导出类的构造函数:
public function export()
{
$export = new InvoicesExport([
[1, 2, 3],
[4, 5, 6]
]);
<span class="token keyword" style="color: rgb(204, 153, 205); font-style: italic !important;">return</span> Excel<span class="token punctuation" style="color: rgb(204, 204, 204);">:</span><span class="token punctuation" style="color: rgb(204, 204, 204);">:</span><span class="token function" style="color: rgb(240, 141, 73);">download</span><span class="token punctuation" style="color: rgb(204, 204, 204);">(</span><span class="token variable" style="color: rgb(126, 198, 153);">$export</span><span class="token punctuation" style="color: rgb(204, 204, 204);">,</span> <span class="token single-quoted-string string" style="color: rgb(126, 198, 153);">'invoices.xlsx'</span><span class="token punctuation" style="color: rgb(204, 204, 204);">)</span><span class="token punctuation" style="color: rgb(204, 204, 204);">;</span>
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
