export_general_demo.php 884 B

123456789101112131415161718192021222324252627
  1. <?php
  2. /**
  3. * 一般通用数据自动合并导出demo
  4. */
  5. require_once(__DIR__ . '/../vendor/autoload.php');
  6. use Pxlswrite\Pxlswrite;
  7. //定义字段
  8. $field = [
  9. 'id' => ['name' => 'title'],
  10. 'c1' => ['name' => 'age'],
  11. 'c2' => ['name' => 'year'],
  12. 'c3' => ['name' => 'kk'],
  13. ];
  14. $fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads' ]);
  15. $filePath = $fileObj->fileName('general.xlsx')
  16. ->field($field)//设置字段&表格头
  17. ->setGeneralData('generateData', ['c1', 'c2'], ['align' => [Pxlswrite::FORMAT_ALIGN_CENTER, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]])//设置数据 并自动合并单元格
  18. ->output();//输出excel文件到磁盘
  19. //数据生成器
  20. function generateData(){
  21. for($i=0;$i<10000;$i++){
  22. yield [
  23. ['id'=>$i,'c1'=>$i+1,'c2'=>$i+2,'c3'=>$i+3],
  24. ['id'=>$i,'c1'=>$i+1,'c2'=>$i+2,'c3'=>$i+3],
  25. ];
  26. }
  27. }