PHPExcel.Class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/10
  6. * Time: 5:10 PM
  7. */
  8. namespace Util\PHPExcel;
  9. use Util\PHPExcel\Style;
  10. use Util\PHPExcel\Worksheet;
  11. use Util\PHPExcel\Calculation;
  12. use Util\PHPExcel\NamedRange;
  13. use Util\PHPExcel\DocumentProperties;
  14. class PHPExcel
  15. {
  16. /**
  17. * Unique ID
  18. *
  19. * @var string
  20. */
  21. private $uniqueID;
  22. /**
  23. * Collection of Worksheet objects
  24. *
  25. * @var Worksheet[]
  26. */
  27. private $workSheetCollection = array();
  28. /**
  29. * CellStyleXf collection
  30. *
  31. * @var Style[]
  32. */
  33. private $cellStyleXfCollection = array();
  34. /**
  35. * CellXf collection
  36. *
  37. * @var Style[]
  38. */
  39. private $cellXfCollection = array();
  40. /**
  41. * Active sheet index
  42. *
  43. * @var integer
  44. */
  45. private $activeSheetIndex = 0;
  46. /**
  47. * Calculation Engine
  48. *
  49. * @var Calculation
  50. */
  51. private $calculationEngine;
  52. /**
  53. * Named ranges
  54. *
  55. * @var NamedRanges[]
  56. */
  57. private $namedRanges = array();
  58. /**
  59. * Document properties
  60. *
  61. * @var DocumentProperties
  62. */
  63. private $properties;
  64. /**
  65. * Create a new PHPExcel with one Worksheet
  66. */
  67. public function __construct()
  68. {
  69. $this->uniqueID = uniqid();
  70. $this->calculationEngine = new Calculation($this);
  71. // Initialise worksheet collection and add one worksheet
  72. $this->workSheetCollection = array();
  73. $this->workSheetCollection[] = new Worksheet($this);
  74. $this->activeSheetIndex = 0;
  75. // Set named ranges
  76. $this->namedRanges = array();
  77. // Create document properties
  78. $this->properties = new DocumentProperties();
  79. /*
  80. // Create document security
  81. $this->security = new PHPExcel_DocumentSecurity();
  82. // Create the cellXf supervisor
  83. $this->cellXfSupervisor = new PHPExcel_Style(true);
  84. $this->cellXfSupervisor->bindParent($this); */
  85. // Create the default style
  86. $this->addCellXf(new Style);
  87. $this->addCellStyleXf(new Style);
  88. }
  89. /**
  90. * Add a cellXf to the workbook
  91. *
  92. * @param PHPExcel_Style $style
  93. */
  94. public function addCellXf(Style $style)
  95. {
  96. $this->cellXfCollection[] = $style;
  97. $style->setIndex(count($this->cellXfCollection) - 1);
  98. }
  99. /**
  100. * Add a cellStyleXf to the workbook
  101. *
  102. * @param PHPExcel_Style $pStyle
  103. */
  104. public function addCellStyleXf(Style $pStyle)
  105. {
  106. $this->cellStyleXfCollection[] = $pStyle;
  107. $pStyle->setIndex(count($this->cellStyleXfCollection) - 1);
  108. }
  109. /**
  110. * Remove sheet by index
  111. *
  112. * @param int $pIndex Active sheet index
  113. * @throws Exception
  114. */
  115. public function removeSheetByIndex($pIndex = 0)
  116. {
  117. $numSheets = count($this->workSheetCollection);
  118. if ($pIndex > $numSheets - 1) {
  119. throw new \Exception(
  120. "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  121. );
  122. } else {
  123. array_splice($this->workSheetCollection, $pIndex, 1);
  124. }
  125. // Adjust active sheet index if necessary
  126. if (($this->activeSheetIndex >= $pIndex) &&
  127. ($pIndex > count($this->workSheetCollection) - 1)) {
  128. --$this->activeSheetIndex;
  129. }
  130. }
  131. /**
  132. * Remove cellStyleXf by index
  133. *
  134. * @param integer $pIndex Index to cellXf
  135. * @throws Exception
  136. */
  137. public function removeCellStyleXfByIndex($pIndex = 0)
  138. {
  139. if ($pIndex > count($this->cellStyleXfCollection) - 1) {
  140. throw new \Exception("CellStyleXf index is out of bounds.");
  141. } else {
  142. array_splice($this->cellStyleXfCollection, $pIndex, 1);
  143. }
  144. }
  145. /**
  146. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
  147. *
  148. * @param integer $pIndex Index to cellXf
  149. */
  150. public function removeCellXfByIndex($pIndex = 0)
  151. {
  152. if ($pIndex > count($this->cellXfCollection) - 1) {
  153. throw new \Exception("CellXf index is out of bounds.");
  154. } else {
  155. // first remove the cellXf
  156. array_splice($this->cellXfCollection, $pIndex, 1);
  157. // then update cellXf indexes for cells
  158. foreach ($this->workSheetCollection as $worksheet) {
  159. foreach ($worksheet->getCellCollection(false) as $cellID) {
  160. $cell = $worksheet->getCell($cellID);
  161. $xfIndex = $cell->getXfIndex();
  162. if ($xfIndex > $pIndex) {
  163. // decrease xf index by 1
  164. $cell->setXfIndex($xfIndex - 1);
  165. } elseif ($xfIndex == $pIndex) {
  166. // set to default xf index 0
  167. $cell->setXfIndex(0);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. /**
  174. * Create sheet and add it to this workbook
  175. *
  176. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  177. * @return PHPExcel_Worksheet
  178. * @throws PHPExcel_Exception
  179. */
  180. public function createSheet($iSheetIndex = null)
  181. {
  182. $newSheet = new Worksheet($this);
  183. $this->addSheet($newSheet, $iSheetIndex);
  184. return $newSheet;
  185. }
  186. /**
  187. * Add sheet
  188. *
  189. * @param Worksheet $pSheet
  190. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  191. * @return Worksheet
  192. * @throws
  193. */
  194. public function addSheet(Worksheet $pSheet, $iSheetIndex = null)
  195. {
  196. if ($this->sheetNameExists($pSheet->getTitle())) {
  197. throw new \Exception(
  198. "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
  199. );
  200. }
  201. if ($iSheetIndex === null) {
  202. if ($this->activeSheetIndex < 0) {
  203. $this->activeSheetIndex = 0;
  204. }
  205. $this->workSheetCollection[] = $pSheet;
  206. } else {
  207. // Insert the sheet at the requested index
  208. array_splice(
  209. $this->workSheetCollection,
  210. $iSheetIndex,
  211. 0,
  212. array($pSheet)
  213. );
  214. // Adjust active sheet index if necessary
  215. if ($this->activeSheetIndex >= $iSheetIndex) {
  216. ++$this->activeSheetIndex;
  217. }
  218. }
  219. if ($pSheet->getParent() === null) {
  220. $pSheet->rebindParent($this);
  221. }
  222. return $pSheet;
  223. }
  224. /**
  225. * Check if a sheet with a specified name already exists
  226. *
  227. * @param string $pSheetName Name of the worksheet to check
  228. * @return boolean
  229. */
  230. public function sheetNameExists($pSheetName)
  231. {
  232. return ($this->getSheetByName($pSheetName) !== null);
  233. }
  234. /**
  235. * Get sheet by name
  236. *
  237. * @param string $pName Sheet name
  238. * @return PHPExcel_Worksheet
  239. */
  240. public function getSheetByName($pName = '')
  241. {
  242. $worksheetCount = count($this->workSheetCollection);
  243. for ($i = 0; $i < $worksheetCount; ++$i) {
  244. if ($this->workSheetCollection[$i]->getTitle() === $pName) {
  245. return $this->workSheetCollection[$i];
  246. }
  247. }
  248. return null;
  249. }
  250. /**
  251. * Return the calculation engine for this worksheet
  252. *
  253. * @return PHPExcel_Calculation
  254. */
  255. public function getCalculationEngine()
  256. {
  257. return $this->calculationEngine;
  258. }
  259. /**
  260. * Code to execute when this worksheet is unset()
  261. *
  262. */
  263. public function __destruct()
  264. {
  265. $this->calculationEngine = null;
  266. $this->disconnectWorksheets();
  267. }
  268. /**
  269. * Disconnect all worksheets from this PHPExcel workbook object,
  270. * typically so that the PHPExcel object can be unset
  271. *
  272. */
  273. public function disconnectWorksheets()
  274. {
  275. $worksheet = null;
  276. foreach ($this->workSheetCollection as $k => &$worksheet) {
  277. $worksheet->disconnectCells();
  278. $this->workSheetCollection[$k] = null;
  279. }
  280. unset($worksheet);
  281. $this->workSheetCollection = array();
  282. }
  283. /**
  284. * Get named ranges
  285. *
  286. * @return PHPExcel_NamedRange[]
  287. */
  288. public function getNamedRanges()
  289. {
  290. return $this->namedRanges;
  291. }
  292. /**
  293. * Get named range
  294. *
  295. * @param string $namedRange
  296. * @param Worksheet|null $pSheet Scope. Use null for global scope
  297. * @return NamedRange|null
  298. */
  299. public function getNamedRange($namedRange, Worksheet $pSheet = null)
  300. {
  301. $returnValue = null;
  302. if ($namedRange != '' && ($namedRange !== null)) {
  303. // first look for global defined name
  304. if (isset($this->namedRanges[$namedRange])) {
  305. $returnValue = $this->namedRanges[$namedRange];
  306. }
  307. // then look for local defined name (has priority over global defined name if both names exist)
  308. if (($pSheet !== null) && isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  309. $returnValue = $this->namedRanges[$pSheet->getTitle() . '!' . $namedRange];
  310. }
  311. }
  312. return $returnValue;
  313. }
  314. /**
  315. * Add named range
  316. *
  317. * @param PHPExcel_NamedRange $namedRange
  318. * @return boolean
  319. */
  320. public function addNamedRange(NamedRange $namedRange)
  321. {
  322. if ($namedRange->getScope() == null) {
  323. // global scope
  324. $this->namedRanges[$namedRange->getName()] = $namedRange;
  325. } else {
  326. // local scope
  327. $this->namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange;
  328. }
  329. return true;
  330. }
  331. /**
  332. * Get index for sheet
  333. *
  334. * @param Worksheet $pSheet
  335. * @return int Sheet index
  336. * @throws
  337. */
  338. public function getIndex(Worksheet $pSheet)
  339. {
  340. foreach ($this->workSheetCollection as $key => $value) {
  341. if ($value->getHashCode() == $pSheet->getHashCode()) {
  342. return $key;
  343. }
  344. }
  345. throw new \Exception("Sheet does not exist.");
  346. }
  347. /**
  348. * Check if a sheet with a specified code name already exists
  349. *
  350. * @param string $pSheetCodeName Name of the worksheet to check
  351. * @return boolean
  352. */
  353. public function sheetCodeNameExists($pSheetCodeName)
  354. {
  355. return ($this->getSheetByCodeName($pSheetCodeName) !== null);
  356. }
  357. /**
  358. * Get sheet by code name. Warning : sheet don't have always a code name !
  359. *
  360. * @param string $pName Sheet name
  361. * @return PHPExcel_Worksheet
  362. */
  363. public function getSheetByCodeName($pName = '')
  364. {
  365. $worksheetCount = count($this->workSheetCollection);
  366. for ($i = 0; $i < $worksheetCount; ++$i) {
  367. if ($this->workSheetCollection[$i]->getCodeName() == $pName) {
  368. return $this->workSheetCollection[$i];
  369. }
  370. }
  371. return null;
  372. }
  373. /**
  374. * Get properties
  375. *
  376. * @return PHPExcel_DocumentProperties
  377. */
  378. public function getProperties()
  379. {
  380. return $this->properties;
  381. }
  382. /**
  383. * Set properties
  384. *
  385. * @param PHPExcel_DocumentProperties $pValue
  386. */
  387. public function setProperties(DocumentProperties $pValue)
  388. {
  389. $this->properties = $pValue;
  390. }
  391. /**
  392. * 获取指定工作表
  393. *
  394. * @param int $pIndex Sheet index
  395. * @return Worksheet
  396. * @throws
  397. */
  398. public function getSheet($pIndex = 0)
  399. {
  400. if (!isset($this->workSheetCollection[$pIndex])) {
  401. $numSheets = $this->getSheetCount();
  402. throw new \Exception(
  403. "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
  404. );
  405. }
  406. return $this->workSheetCollection[$pIndex];
  407. }
  408. /**
  409. * Get sheet count
  410. *
  411. * @return int
  412. */
  413. public function getSheetCount()
  414. {
  415. return count($this->workSheetCollection);
  416. }
  417. /**
  418. * 指定激活状态的工作表
  419. *
  420. * @param int $pIndex Active sheet index
  421. * @throws
  422. * @return PHPExcel_Worksheet
  423. */
  424. public function setActiveSheetIndex($pIndex = 0)
  425. {
  426. $numSheets = count($this->workSheetCollection);
  427. if ($pIndex > $numSheets - 1) {
  428. throw new \Exception(
  429. "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  430. );
  431. } else {
  432. $this->activeSheetIndex = $pIndex;
  433. }
  434. return $this->getActiveSheet();
  435. }
  436. /**
  437. * 获取激活状态工作表
  438. *
  439. * @return Worksheet
  440. *
  441. * @throws
  442. */
  443. public function getActiveSheet()
  444. {
  445. return $this->getSheet($this->activeSheetIndex);
  446. }
  447. }