phpexcel.class.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * ZenTaoPMS - Open-source project management system.
  4. *
  5. * @copyright Copyright 2009-2025 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.chandao.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. *
  8. * A third-party license is embedded for some of the code in this file:
  9. * The use of the source code of this file is also subject to the terms
  10. * and consitions of the license of "PhpSpreadsheet" (LGPL, see
  11. * </lib/vendor/phpoffice/phpspreadsheet/LICENSE>).
  12. */
  13. use PhpOffice\PhpSpreadsheet\IOFactory;
  14. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  15. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  16. use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
  17. use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
  18. require_once dirname(__FILE__, 2) . '/base/delegate/delegate.class.php';
  19. class phpExcel extends baseDelegate
  20. {
  21. protected static $className = 'PhpOffice\PhpSpreadsheet\Spreadsheet';
  22. public function __construct()
  23. {
  24. $this->instance = new static::$className();
  25. }
  26. public static function load($file)
  27. {
  28. return IOFactory::load($file);
  29. }
  30. public function createReader($type = 'Xlsx')
  31. {
  32. try
  33. {
  34. return IOFactory::createReader($type);
  35. }
  36. catch (ReaderException $e)
  37. {
  38. throw new Exception('Create reader error, the error message is ' . $e->getMessage());
  39. }
  40. }
  41. public function createWriter($type = 'Xlsx')
  42. {
  43. try
  44. {
  45. return IOFactory::createWriter($this->instance, ucfirst($type));
  46. }
  47. catch (WriterException $e)
  48. {
  49. throw new Exception('Create writer error, the error message is ' . $e->getMessage());
  50. }
  51. }
  52. public static function canRead($file)
  53. {
  54. try
  55. {
  56. $fileType = IOFactory::identify($file);
  57. $reader = IOFactory::createReader($fileType);
  58. return $reader instanceof Xls || $reader instanceof Xlsx;
  59. }
  60. catch (ReaderException $e)
  61. {
  62. return false;
  63. }
  64. }
  65. }