firezihai / z-excel
通过注解解析和导出excel表格
v1.0.0
2024-11-26 07:31 UTC
Requires
- php: >=8.0
- phpoffice/phpspreadsheet: ^1.25
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpro/grumphp: ^1.16
- phpstan/phpstan: ^1.12
This package is auto-updated.
Last update: 2025-03-26 08:59:33 UTC
README
功能
- 用于解析和导出表格
- 支持phpoffice、xlswriter
安装
composer require firezihai/z-excel
使用
定义DTO
//index按表头索引解析表格,name:按表头名称解析表格,默认name #[ExcelDto(type:"index")] class UserDto implements ExcelDtoInterface { // 导出表格无index时,属性在类中的顺序,即为表头的顺序 #[ExcelHeader(name:"用户名",index:0)] public string $username; #[ExcelHeader(name:"昵称",index:1)] public string $nickname; #[ExcelHeader(name:"出生日期",index:3)] public string $birthday; #[ExcelHeader(name:"性别",index:2,formatter:true)] public string $gender; // 将性别转成中文 public function formatterGender($value) { $gender = ['未知','男','女']; return $gender[$value]??''; } }
解析表格
$excelFactory = new ExcelFactory(); $excel = $excelFactory->get('phpOffice'); $data = $excel->parse('./test.xlsx', UserDto::class);
注意
xlswriter
解析表格时,需安装 xlswriter 扩展