IOFactory.Class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace util\PHPExcel;
  3. use Mall\Framework\Core\ResultWrapper;
  4. use Mall\Framework\Core\ErrorCode;
  5. /**
  6. * Created by PhpStorm.
  7. * User: phperstar
  8. * Date: 2020/8/10
  9. * Time: 3:32 PM
  10. */
  11. class IOFactory
  12. {
  13. /**
  14. * Search locations
  15. *
  16. * @var array
  17. * @access private
  18. * @static
  19. */
  20. private static $searchLocations = array(
  21. array( 'type' => 'IWriter', 'path' => '\Util\PHPExcel\Writer\{0}', 'class' => '{0}' ),
  22. array( 'type' => 'IReader', 'path' => '\Util\PHPExcel\Reader\{0}', 'class' => '{0}' )
  23. );
  24. /**
  25. * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  26. *
  27. * @static
  28. * @access public
  29. * @param string $pFilename The name of the spreadsheet file
  30. * @return ResultWrapper
  31. * @throws PHPExcel_Reader_Exception
  32. */
  33. public static function load($pFilename)
  34. {
  35. $result = self::createReaderForFile($pFilename);
  36. if(!$result->isSuccess()){
  37. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  38. }
  39. $reader = $result->getData();
  40. $reader->setReadDataOnly(true); // 只读取数据
  41. return ResultWrapper::success($reader->load($pFilename));
  42. }
  43. /**
  44. * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
  45. *
  46. * @static
  47. * @access public
  48. * @param string $pFilename The name of the spreadsheet file
  49. * @return ResultWrapper
  50. */
  51. public static function createReaderForFile($pFilename)
  52. {
  53. $pathinfo = pathinfo($pFilename);
  54. $extensionType = null;
  55. if (isset($pathinfo['extension'])) {
  56. switch (strtolower($pathinfo['extension'])) {
  57. case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet
  58. case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
  59. case 'xltx': // Excel (OfficeOpenXML) Template
  60. case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded)
  61. $extensionType = 'Excel2007';
  62. break;
  63. case 'xls': // Excel (BIFF) Spreadsheet
  64. case 'xlt': // Excel (BIFF) Template
  65. $extensionType = 'Excel5';
  66. break;
  67. case 'ods': // Open/Libre Offic Calc
  68. case 'ots': // Open/Libre Offic Calc Template
  69. $extensionType = 'OOCalc';
  70. break;
  71. case 'slk':
  72. $extensionType = 'SYLK';
  73. break;
  74. case 'xml': // Excel 2003 SpreadSheetML
  75. $extensionType = 'Excel2003XML';
  76. break;
  77. case 'gnumeric':
  78. $extensionType = 'Gnumeric';
  79. break;
  80. case 'htm':
  81. case 'html':
  82. $extensionType = 'HTML';
  83. break;
  84. case 'csv':
  85. // Do nothing
  86. // We must not try to use CSV reader since it loads
  87. // all files including Excel files etc.
  88. break;
  89. default:
  90. break;
  91. }
  92. if ($extensionType !== null) {
  93. $result = self::createReader($extensionType);
  94. if(!$result->isSuccess()){
  95. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  96. }
  97. $reader = $result->getData();
  98. if (isset($reader) && $reader->canRead($pFilename)) {
  99. return ResultWrapper::success($reader);
  100. }
  101. }
  102. }
  103. // If we reach here then "lucky guess" didn't give any result
  104. // Try walking through all the options in self::$autoResolveClasses
  105. /*foreach (self::$autoResolveClasses as $autoResolveClass) {
  106. // Ignore our original guess, we know that won't work
  107. if ($autoResolveClass !== $extensionType) {
  108. $reader = self::createReader($autoResolveClass);
  109. if ($reader->canRead($pFilename)) {
  110. return $reader;
  111. }
  112. }
  113. }*/
  114. return ResultWrapper::fail('当前文件类型无法读取', ErrorCode::$notAllowAccess);
  115. }
  116. /**
  117. * Create PHPExcel_Reader_IReader
  118. *
  119. * @static
  120. * @access public
  121. * @param string $readerType Example: Excel2007
  122. * @return ResultWrapper
  123. */
  124. public static function createReader($readerType = '')
  125. {
  126. // Search type
  127. $searchType = 'IReader';
  128. // Include class
  129. foreach (self::$searchLocations as $searchLocation) {
  130. if ($searchLocation['type'] == $searchType) {
  131. $className = str_replace('{0}', $readerType, $searchLocation['path']);
  132. $instance = new $className();
  133. if ($instance !== null) {
  134. return ResultWrapper::success($instance);
  135. }
  136. }
  137. }
  138. return ResultWrapper::fail('没有对应的解析解析类', ErrorCode::$notAllowAccess);
  139. }
  140. }