Excel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * PHP Excel
  4. *
  5. * @author Janson
  6. * @create 2017-11-23
  7. */
  8. namespace Asan\PHPExcel;
  9. use Asan\PHPExcel\Exception\ReaderException;
  10. class Excel {
  11. /**
  12. * Load a file
  13. *
  14. * @param string $file
  15. * @param callback|null $callback
  16. * @param string|null $encoding
  17. * @param string $ext
  18. * @param string $logPath
  19. *
  20. * @throws ReaderException
  21. * @return \Asan\PHPExcel\Reader\BaseReader
  22. */
  23. public static function load($file, $callback = null, $encoding = null, $ext = '', $logPath = '') {
  24. set_error_handler(function($errorNo, $errorMsg, $errorFile, $errorLine) use ($logPath) {
  25. if ($logPath) {
  26. if (!file_exists($logPath)) {
  27. mkdir($logPath, 0755, true);
  28. }
  29. $content = sprintf(
  30. "%s\t%s.%s\t%s\t%s", date("Y-m-d H:i:s"), self::class, 'ERROR',
  31. "[$errorNo]$errorMsg in $errorFile:$errorLine", PHP_EOL
  32. );
  33. file_put_contents("$logPath/excel-" . date('Y-m-d'). '.log', $content, FILE_APPEND);
  34. }
  35. }, E_ALL ^ E_ERROR);
  36. $ext = $ext ?: strtolower(pathinfo($file, PATHINFO_EXTENSION));
  37. $format = self::getFormatByExtension($ext);
  38. if (empty($format)) {
  39. throw new ReaderException("Could not identify file format for file [$file] with extension [$ext]");
  40. }
  41. $class = __NAMESPACE__ . '\\Reader\\' . $format;
  42. $reader = new $class;
  43. if ($callback) {
  44. if ($callback instanceof \Closure) {
  45. // Do the callback
  46. call_user_func($callback, $reader);
  47. } elseif (is_string($callback)) {
  48. // Set the encoding
  49. $encoding = $callback;
  50. }
  51. }
  52. if ($encoding && method_exists($reader, 'setInputEncoding')) {
  53. $reader->setInputEncoding($encoding);
  54. }
  55. return $reader->load($file);
  56. }
  57. /**
  58. * Identify file format
  59. *
  60. * @param string $ext
  61. * @return string
  62. */
  63. protected static function getFormatByExtension($ext) {
  64. $formart = '';
  65. switch ($ext) {
  66. /*
  67. |--------------------------------------------------------------------------
  68. | Excel 2007
  69. |--------------------------------------------------------------------------
  70. */
  71. case 'xlsx':
  72. case 'xlsm':
  73. case 'xltx':
  74. case 'xltm':
  75. $formart = 'Xlsx';
  76. break;
  77. /*
  78. |--------------------------------------------------------------------------
  79. | Excel5
  80. |--------------------------------------------------------------------------
  81. */
  82. case 'xls':
  83. case 'xlt':
  84. $formart = 'Xls';
  85. break;
  86. /*
  87. |--------------------------------------------------------------------------
  88. | CSV
  89. |--------------------------------------------------------------------------
  90. */
  91. case 'csv':
  92. case 'txt':
  93. $formart = 'Csv';
  94. break;
  95. }
  96. return $formart;
  97. }
  98. }