field_demo.php 906 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. /**
  3. * 字段定义demo
  4. */
  5. require_once(__DIR__ . '/../vendor/autoload.php');
  6. use Pxlswrite\Pxlswrite;
  7. //定义字段
  8. $field = [
  9. 'name' => ['name' => '姓名'],
  10. 'year' => ['name' => '出生年份'],
  11. 'age' => ['name' => '年龄','callback'=>'ageFormat']//callback 回调方法处理格式化值 可以是函数/对象方法
  12. ];
  13. $fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads' ]);
  14. $fileObj->fileName('field.xlsx')
  15. ->field($field)//设置字段&表格头
  16. ->setGeneralData(function(){
  17. yield [
  18. ['name'=>'Rent', 'year'=>1999,'age'=>0],
  19. ['name'=>'Gas', 'year'=>1996,'age'=>0],
  20. ['name'=>'Food', 'year'=>1998,'age'=>0],
  21. ['name'=>'Gym', 'year'=>1995,'age'=>0],
  22. ];
  23. }
  24. )
  25. ->output();//输出excel文件到磁盘
  26. //格式化字段值
  27. function ageFormat($v, $values)
  28. {
  29. return date('Y') - $values['year'];
  30. }