Xls.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Xls Reader
  4. *
  5. * @author Janson
  6. * @create 2017-11-23
  7. */
  8. namespace Asan\PHPExcel\Reader;
  9. use Asan\PHPExcel\Parser\Excel5;
  10. use Asan\PHPExcel\Parser\Excel5\OLERead;
  11. class Xls extends BaseReader {
  12. /**
  13. * Xls parser
  14. *
  15. * @var Excel5
  16. */
  17. protected $parser;
  18. /**
  19. * File row、column count
  20. *
  21. * @var array|int
  22. */
  23. protected $count;
  24. public function __construct() {
  25. $this->parser = new Excel5();
  26. }
  27. /**
  28. * Loads Excel from file
  29. *
  30. * @param string $file
  31. *
  32. * @return $this
  33. */
  34. public function load($file) {
  35. $this->parser->loadOLE($file);
  36. $this->generator = $this->makeGenerator();
  37. return $this;
  38. }
  39. /**
  40. * Count elements of the selected sheet
  41. *
  42. * @param bool $all
  43. * @return int|array
  44. */
  45. public function count($all = false) {
  46. if ($this->count === null) {
  47. $row = $column = 0;
  48. if ($sheet = $this->sheets($this->parser->getSheetIndex())) {
  49. $row = $sheet['totalRows'] ?? 0;
  50. $column = $sheet['totalColumns'] ?? 0;
  51. }
  52. $this->count = [
  53. $this->rowLimit > 0 ? min($row, $this->rowLimit) : $row,
  54. $this->columnLimit > 0 ? min($column, $this->columnLimit) : $column
  55. ];
  56. }
  57. return $all ? $this->count : $this->count[0];
  58. }
  59. /**
  60. * Get the work sheets info
  61. *
  62. * @param int $index
  63. * @return array
  64. */
  65. public function sheets($index = null) {
  66. $sheets = $this->parser->parseWorksheetInfo();
  67. if ($index !== null) {
  68. return $sheets[$index] ?? [];
  69. }
  70. return $sheets;
  71. }
  72. /**
  73. * Make the generator
  74. *
  75. * @return \Generator
  76. */
  77. protected function makeGenerator() {
  78. list($rowLimit, $columnLimit) = $this->count(true);
  79. $line = $finish = 0;
  80. while ($finish < $rowLimit && ($row = $this->parser->getRow($line++, $columnLimit)) !== false) {
  81. if ($this->parser->isIgnoreEmptyRow() && trim(implode('', $row)) === '') {
  82. continue;
  83. }
  84. $finish++;
  85. yield $row;
  86. }
  87. }
  88. /**
  89. * Ignore empty row
  90. *
  91. * @param bool $ignoreEmpty
  92. *
  93. * @return $this
  94. */
  95. public function ignoreEmptyRow($ignoreEmpty = false) {
  96. $this->parser->ignoreEmptyRow($ignoreEmpty);
  97. return $this;
  98. }
  99. /**
  100. * Set sheet index
  101. *
  102. * @param int $index
  103. * @return $this
  104. */
  105. public function setSheetIndex($index) {
  106. if ($index != $this->parser->getSheetIndex()) {
  107. $this->parser->setSheetIndex($index);
  108. $this->count = null;
  109. $this->rewind();
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Can the current Reader read the file?
  115. *
  116. * @param string $file
  117. *
  118. * @return bool
  119. */
  120. public function canRead($file) {
  121. try {
  122. // Use ParseXL for the hard work.
  123. $ole = new OLERead();
  124. // open file
  125. $ole->openFile($file);
  126. } catch (\Exception $e) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. /**
  132. * Release parser and generator
  133. */
  134. public function __destruct() {
  135. $this->parser = null;
  136. $this->generator = null;
  137. }
  138. }