style_demo.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * 样式设置demo
  4. */
  5. require_once(__DIR__ . '/../vendor/autoload.php');
  6. use Pxlswrite\DB\DB;
  7. use Pxlswrite\Pxlswrite;
  8. $fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
  9. $field = [
  10. 'id' => ['name' => 'title'],
  11. 'c1' => ['name' => 'age'],
  12. 'c2' => ['name' => 'year'],
  13. 'c3' => ['name' => 'kk'],
  14. 'c4' => ['name' => 'll'],
  15. 'c5' => ['name' => 'aa']//callback 回调处理格式化值 可以是函数/对象方法
  16. ];
  17. $filePath = $fileObj->fileName('style.xlsx', 'sheet1')
  18. ->field($field)//设置字段&表格头
  19. ->setGeneralData('generateData')//设置数据
  20. ->setRow('A1:A3', 80, ['bold' => true]) //设置单元行样式 A1:A3 单元格范围 80行高 ['blod'=>true] 加粗
  21. ->setColumn('A:F', 20, ['background' => Pxlswrite::COLOR_GRAY, 'align' => [Pxlswrite::FORMAT_ALIGN_CENTER, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]]) //设置单元列样式
  22. ->output();//输出excel文件到磁盘
  23. function generateData()
  24. {
  25. $db = DB::getInstance();
  26. $step = 10000;
  27. for ($i = 0; $i < 10000; $i = $i + 1) {
  28. yield [
  29. [
  30. 'id' => $i + 1,
  31. 'c1' => $i,
  32. 'c2' => $i,
  33. 'c3' => $i,
  34. 'c4' => $i,
  35. 'c5' => $i
  36. ]
  37. ];
  38. }
  39. }