wefabric / wp-excel
WordPress后端使用Excel导出作为批量操作的实现。
dev-main
2022-04-21 08:52 UTC
Requires
- maatwebsite/excel: 3.0.10
This package is auto-updated.
Last update: 2024-09-21 14:33:50 UTC
README
WordPress后端使用Excel导出作为批量操作的实现。要与Wefabric themosis实现一起使用。它使用了 https://github.com/SpartnerNL/Laravel-Excel (v3.0) 包。
要求
- Wefabric WP 支持
- Themosis
安装
运行以下命令
composer require wefabric/wp-excel
发布配置文件
php console vendor:publish --tag=wp-excel
使用方法
在使用前,每个文章类型都需要注册导出。以下示例中,我们将为默认WordPress文章注册导出类。
- 首先,您需要创建一个将文章数据转换为Illuminate Collection的类。为此,请运行以下命令
php console make:export PostsExport
- 将导出WordPress文章的逻辑添加到Illuminate Collection中,例如
use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithHeadings; use Wefabric\WPExcel\Concerns\WpPostsExcellable; class PostsExport implements FromCollection, WithHeadings, WpPostsExcellable { protected array $postIds = []; public function headings(): array { return [ 'Id', 'Title', ]; } /** * @return \Illuminate\Support\Collection */ public function collection(): Collection { $args = [ 'post_type' => 'post', 'posts_per_page' => -1, ]; if($this->postIds) { $args['post__in'] = $this->postIds; } $posts = get_posts($args); $collection = new Collection(); foreach ($posts as $post) { $collection->push([ 'Id' => $post->ID, 'Title' => $post->post_title, ]); } return $collection; } public function setPostIds(array $postIds) { $this->postIds = $postIds; } }
- 将类添加到wp-excel配置文件(config/wp-excel.php)中。其中'post'是文章类型名称,第二个参数是导出类
return [ 'post_types' => [ 'post' => \App\Exports\PostsExport::class, ] ]
- 前往WordPress后端(wp-admin/edit.php),选择一篇文章,运行批量操作。
导出格式
默认导出格式为'XLSX'。要使用不同的导出格式,请将wp-excel配置文件(config/wp-excel.php)中的'default_format'更改为正确的格式。允许的导出格式在此处:https://docs.laravel-excel.com/3.0/exports/export-formats.html