ISAPI(Internet Server Application Program Interface)是微软提供的一套面向 Internet 服务的 API 接口,一个 ISAPI 的 DLL,可以在被用户请求激活后长驻内存,等待用户的另一个请求,还可以在一个 DLL 里设置多个用户请求处理函数,此外,ISAPI 的 DLL 应用程序和 WEB 服务器处于同一个进程中,效率要显著高于 CGI。由于微软的排他性,只能运行于 Windows 环境。
nginx: [warn] the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/nginx/conf/nginx.conf:2
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function export()
{
return Excel::download(new UsersExport, 'users.xlsx');
}
}
2.存储到磁盘
public function storeExcel()
{
// Store on default disk
Excel::store(new InvoicesExport(2018), 'invoices.xlsx');
// Store on a different disk (e.g. s3)
Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3');
// Store on a different disk with a defined writer type.
Excel::store(new InvoicesExport(2018), 'invoices.xlsx', 's3', Excel::XLSX);
}
参数1为导出类
参数2为文件名
参数3为laravel存储的磁盘
参数4为导出格式 详情看配置文件
从集合导出
<?php
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
public function collection()
{
return User::all();
}
}
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class InvoicesExport implements FromQuery
{
use Exportable;
public function query()
{
return Invoice::query();
}
}
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Contracts\Queue\ShouldQueue;
class UsersExport implements FromCollection, ShouldQueue
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return User::all();
}
}
队列直接下载导出 控制器代码
public function export(Request $request, Excel $excel)
{
return $excel->download(new UsersExport(), 'users.xlsx');
}
队列导出存储到磁盘
public function store(Request $request)
{
ini_set('memory_limit', '1024M');
$result = Excel::store(new UsersExport(), 'users_store'.date('YmdHis').'.xlsx', 'public');
}
数据量大时,有时会超出内存限制需要设置内存,后来没复现,没找到原因
从 collection导出的方式,成功后没有发现导出excel文件,后来没复现,没有找到原因
设置表头
class InvoicesExport implements WithHeadings
{
public function headings(): array
{
return [
'对应ExcelA列',
'对应ExcelB列',
...
];
}
}
namespace App\Exports;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class InvoicesExport implements ShouldAutoSize
{
...
}
自定义宽度
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
class InvoicesExport implements WithColumnWidths
{
public function columnWidths(): array
{
//A B 是列的标识头
return [
'A' => 55,
'B' => 45,
];
}
}
常用配置修改
/*
|--------------------------------------------------------------------------
Heading Row Formatter
|--------------------------------------------------------------------------
|
| Configure the heading row formatter.
| Available options: none|slug|custom
|
*/
'heading_row' => [
'formatter' => 'none',//不格式化,这样可以导入中文标题头excel
],
/*
|--------------------------------------------------------------------------
| Transaction Handler
|--------------------------------------------------------------------------
|
| By default the import is wrapped in a transaction. This is useful
| for when an import may fail and you want to retry it. With the
| transactions, the previous import gets rolled-back.
|
| You can disable the transaction handler by setting this to null.
| Or you can choose a custom made transaction handler here.
|
| Supported handlers: null|db
|
*/
'transactions' => [
'handler' => 'null', //取消事物,不回滚,成功导入的数据不会被回滚
],
如有索引 (a, b, c, d),查询条件 a = 1 and b = 2 and c = 3 and d > 4,则会在每个节点依次命中 a、b、c,无法命中 d。也就是最左前缀匹配原则。
不需要考虑 =、in 等的顺序,MySQL 会自动优化这些条件的顺序,以匹配尽可能多的索引列。
如有索引 (a, b, c, d),查询条件 c > 3 and b = 2 and a = 1 and d < 4 与 a = 1 and c > 3 and b = 2 and d < 4 等顺序都是可以的,MySQL 会自动优化为 a = 1 and b = 2 and c > 3 and d < 4,依次命中 a、b。