export_demo.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. require_once(__DIR__ . '/../vendor/autoload.php');
  3. use Pxlswrite\DB\DB;
  4. use Pxlswrite\Pxlswrite;
  5. use Pxlswrite\WebSocket\WebSocketClient;
  6. $time = time();
  7. //实例化pxlswrite
  8. $fileObj = new Pxlswrite(['path' => __DIR__ . '/uploads']);
  9. //实例化WebSocketClient--需要推送进度才实例化
  10. $pushHandle = new WebSocketClient('ws://192.168.18.192:9502', $_GET['fd']);
  11. //创建excel文件
  12. $fileObj->fileName('123.xlsx');
  13. //定义样式
  14. $style = [
  15. 'align' => [Pxlswrite::FORMAT_ALIGN_CENTER, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER],//对齐 [x,y]
  16. 'border' => Pxlswrite::BORDER_SLANT_DASH_DOT,//单元格边框
  17. 'background' => Pxlswrite::COLOR_RED,//单元格背景色
  18. 'fontColor' => Pxlswrite::COLOR_BLUE,//字体颜色
  19. 'fontSize' => 30,//字体大小
  20. 'font' => 'FontName',//设置字体 字体名称,字体必须存在于本机
  21. 'number' => '#,##0',//数字格式化
  22. 'bold' => true,//粗题
  23. 'strikeout' => false,//文本删除线
  24. 'wrap' => true,//文本换行
  25. 'italic' => true,//斜体
  26. ];
  27. //定义字段
  28. $field = [
  29. 'id' => ['name' => 'title'],
  30. 'c1' => ['name' => 'age'],
  31. 'c2' => ['name' => 'year'],
  32. 'c3' => ['name' => 'kk'],
  33. 'c4' => ['name' => 'll'],
  34. 'c5' => ['name' => 'aa', 'callback' => 'myFormat']//callback 回调处理格式化值 可以是函数/对象方法
  35. ];
  36. //注意:设置行与行/列与列样式 交集范围会覆盖;行样式优先于列样式
  37. $filePath = $fileObj->field($field)//设置字段&表格头
  38. ->setDataByGenerator('generateData', $pushHandle)//设置数据 回调生成器方法获取数据,$pushHandle 用于推送,可不传
  39. ->setRow('A1:A3', 80, $style)//设置范围行样式 80行高
  40. ->setColumn('A:F', 20, ['background' => Pxlswrite::COLOR_GRAY])//设置范围列样式 20列宽
  41. ->setRow('A1', 50, ['background' => Pxlswrite::COLOR_PINK, 'align' => [Pxlswrite::FORMAT_ALIGN_CENTER, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]])//设置指定某一行样式
  42. ->setColumn('F:F', 60, ['background' => Pxlswrite::COLOR_YELLOW])//指定某一列样式
  43. ->defaultFormat(['background' => Pxlswrite::COLOR_GREEN])//全局默认样式
  44. ->mergeCells('A1:C1', 'Merge cells', ['align' => [Pxlswrite::FORMAT_ALIGN_CENTER, Pxlswrite::FORMAT_ALIGN_VERTICAL_CENTER]])//合并单元格
  45. ->output();//输出excel文件到磁盘
  46. //单元格插入文本
  47. //for ($index = 0; $index < 10; $index++) {
  48. // $fileObj->insertText($index, 0, 'viest');
  49. // $fileObj->insertText($index, 1, 10000, '#,##0');
  50. //}
  51. //$filePath = $fileObj->output();
  52. $memory = floor((memory_get_peak_usage()) / 1024 / 1024) . "MB";#10M 22S
  53. $execute_time = time() - $time . 's';
  54. //同步下载
  55. //$fileObj->download($filePath);
  56. //ajax请求返回下载地址
  57. echo json_encode(['code' => 1, 'msg' => '导出完毕', 'url' => '/download.php?file=' . $filePath, 'data' => ['memory' => $memory, 'excute_time' => $execute_time]]);
  58. //数据生成器--封装模拟数据获取的方法
  59. function generateData()
  60. {
  61. $db = DB::getInstance();
  62. $step = 10000;
  63. for ($i = 0; $i < 100000; $i = $i + $step) {
  64. yield $db->get_records_sql("select * from sheet1 limit {$i},{$step}", null, PDO::FETCH_ASSOC);
  65. }
  66. }
  67. //格式化字段值
  68. function myFormat($v, $values)
  69. {
  70. return $v . '自定义格式化-' . $values['id'];
  71. }