import_demo.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. /**
  3. * 游标读取Excel
  4. * 分段插入数据库
  5. */
  6. require_once(__DIR__.'/../vendor/autoload.php');
  7. use Pxlswrite\DB\DB;
  8. use Pxlswrite\Pxlswrite;
  9. use Pxlswrite\WebSocket\WebSocketClient;
  10. $time = time();
  11. $fileObj = new Pxlswrite(['path' => dirname($_GET['file'])]);
  12. $fileInfo = explode('/', $_GET['file']);
  13. $fileName = end($fileInfo);
  14. //实例化WebSocketClient
  15. $pushHandle = new WebSocketClient('ws://192.168.18.192:9502',$_GET['fd']);
  16. //打开excel文件 setSkipRows(1)跳过一行数据
  17. $fileObj->openFile($fileName)->openSheet()->setSkipRows(1)->importDataByCursor('insert_data',$pushHandle);
  18. //数据插入逻辑
  19. function insert_data($data)
  20. {
  21. $db = DB::getInstance();
  22. $sql = '';
  23. foreach ($data as $v) {
  24. $sql .= "(" . implode(",", $v) . "),";
  25. }
  26. $sql = trim($sql, ',');
  27. $db->execute("insert into sheet2 (id,c1,c2,c3,c4) values " . $sql);
  28. }
  29. $memory = floor((memory_get_peak_usage()) / 1024 / 1024) . "MB";#10M 12S
  30. $execute_time = time() - $time . 's';
  31. echo json_encode(['code' => 1, 'data' => ['memory' => $memory, 'execute_time' => $execute_time], 'msg' => '导入完毕']);
  32. #5MB 57s