Import.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Excel\Lib;
  3. use Dever;
  4. Dever::apply('autoload', 'excel', 'vendor');
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  7. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  8. class Import extends Core
  9. {
  10. /**
  11. * 流式读取 Excel,每次返回一行
  12. * @param string $file Excel 文件路径
  13. * @param int $sheetIndex sheet 下标(默认 0)
  14. * @param int $offset 跳过前几行
  15. * @return \Generator
  16. */
  17. public function act($file = '', $sheetIndex = 0, $offset = 0, $head = [])
  18. {
  19. $file = mb_convert_encoding($file, "UTF-8", "gbk");
  20. if (empty($file) || !file_exists($file)) {
  21. Dever::error('file not exists!');
  22. }
  23. try {
  24. $type = IOFactory::identify($file);
  25. $reader = IOFactory::createReader($type);
  26. //$reader->setReadDataOnly(true);
  27. $spreadsheet = $reader->load($file);
  28. } catch (\Exception $e) {
  29. Dever::error('加载文件发生错误:"'.$file.'": ' . $e->getMessage());
  30. }
  31. $sheet = $spreadsheet->getSheet($sheetIndex);
  32. $highestRow = $sheet->getHighestRow();
  33. $highestCol = $sheet->getHighestColumn();
  34. $columnCnt = array_search($highestCol, $this->cell) ?: 0;
  35. $draws = $sheet->getDrawingCollection();
  36. $pictures = [];
  37. foreach ($draws as $img) {
  38. list($col, $row) = Coordinate::coordinateFromString($img->getCoordinates());
  39. $pictures[$row][$col][] = Dever::load('save', 'upload')->init(1)->act($img->getPath())['url'];
  40. }
  41. for ($row = $offset + 1; $row <= $highestRow; $row++) {
  42. $rowData = [];
  43. for ($col = 0; $col <= $columnCnt; $col++) {
  44. $key = $this->cell[$col];
  45. if (isset($pictures[$row][$key])) {
  46. $cellValue = $pictures[$row][$key];
  47. } else {
  48. $cellId = $key . $row;
  49. $cellValue = $sheet->getCell($cellId)->getCalculatedValue();
  50. if ($cellValue instanceof RichText) {
  51. $cellValue = $cellValue->__toString();
  52. }
  53. }
  54. if ($head && $key = Dever::issets($head, $col)) {
  55. if (is_array($key)) {
  56. $rowData[$key[0]][$key[1]] = $cellValue;
  57. } else {
  58. $rowData[$key] = $cellValue;
  59. }
  60. } else {
  61. $rowData[$key] = $cellValue;
  62. }
  63. }
  64. yield $rowData;
  65. }
  66. }
  67. }
  68. /*
  69. <?php namespace Excel\Lib;
  70. use Dever;
  71. Dever::apply('autoload', 'excel', 'vendor');
  72. class Import extends Core
  73. {
  74. public function act($file = '', $sheet = 0, $offset = 0)
  75. {
  76. $file = mb_convert_encoding($file, "UTF-8", "gbk");
  77. if(empty($file) OR !file_exists($file)) {
  78. Dever::error('file not exists!');
  79. }
  80. try {
  81. $type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file);
  82. $read = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($type);
  83. //$read->setReadDataOnly(true);
  84. $excel = $read->load($file);
  85. } catch (\Exception $e) {
  86. Dever::error('加载文件发生错误:"'.$file.'": '. $e->getMessage());
  87. }
  88. $sheet = $excel->getSheet($sheet);
  89. $columnH = $sheet->getHighestColumn();
  90. $columnCnt = array_search($columnH, $this->cell);
  91. if (!$columnCnt) {
  92. $columnCnt = $offset - 1;
  93. }
  94. if ($columnCnt < 0) {
  95. $columnCnt = 0;
  96. }
  97. $rowCnt = $sheet->getHighestRow();
  98. $data = array();
  99. for ($_row = 1; $_row <= $rowCnt; $_row++) {
  100. for ($_column = 0; $_column <= $columnCnt; $_column++) {
  101. $cellId = $this->cell[$_column].$_row;
  102. //$cellValue = $sheet->getCell($cellId)->getValue();
  103. $cellValue = $sheet->getCell($cellId)->getCalculatedValue();
  104. if ($cellValue instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText) {
  105. $cellValue = $cellValue->__toString();
  106. }
  107. $data[$_row][$this->cell[$_column]] = $cellValue;
  108. }
  109. }
  110. $draws = $sheet->getDrawingCollection();
  111. //处理图片
  112. foreach ($draws as $img) {
  113. list($startColumn, $startRow) = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::coordinateFromString($img->getCoordinates());//获取图片所在行和列
  114. $pic = Dever::load('save', 'upload')->act(1, $img->getPath());
  115. //插入代码
  116. //$startColumn = $this->decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字
  117. $data[$startRow][$startColumn] = $pic['url'];
  118. }
  119. return $data;
  120. }
  121. }
  122. */