我使用库
PHPExcel 1.7.9来处理Excel文件.首先,我创建一个模板,风格和抛光它.然后,为避免样式硬编码,使用上面提到的库我打开该模板,更改一些值并将其另存为新的.xlsx文件.
首先,我们从单元格中获取该样式.
$this->styles = array() ; $this->styles['category'] = $sheet->getStyle("A4"); $this->styles['subcategory'] = $sheet->getStyle("A5");
private function displayCategories($categories,&$row,$level = 0){ $sheet = $this->content ; foreach($categories as $category){ if ($category->hasChildren() || $category->hasItems()){ //Check if the row has changed. $sheet->getRowDimension($row)->setRowHeight(20); $sheet->mergeCells(Cell::NUMBER . $row . ":" . Cell::TOTAL . $row) ; $name = ($level == 0) ? strtoupper($category->name) : str_repeat(" ",$level*6) ."- {$category->name}" ; $sheet->setCellValue(Cell::NUMBER . $row,$name) ; $sheet->duplicateStyle((($level == 0) ? $this->styles['category'] : $this->styles['subcategory']),Cell::NUMBER . $row); $row++ ; if ($category->hasChildren()){ $this->displayCategories($category->children,$row,$level+1); } } } }
问题
如果使用$sheet-> duplicateStyle(),由于无限递归,将无法保存文档.
Maximum function nesting level of ‘200’ reached,aborting! <- FATAL ERROR
问题出在下一段代码中,在PHPExcel_Style_Fill类中,一个对象反复引用自己.
public function getHashCode() { //class PHPExcel_Style_Fill if ($this->_isSupervisor) { var_dump($this === $this->getSharedComponent()); //Always true 200 times return $this->getSharedComponent()->getHashCode(); } return md5( $this->getFillType() . $this->getRotation() . $this->getStartColor()->getHashCode() . $this->getEndColor()->getHashCode() . __CLASS__ ); }
有没有解决方法可以解决这个问题?我也接受任何有关如何将一个完整的单元格应用于另一个单元格的想法.
解:
正如@MarkBaker在评论中所说,GitHub上的分支开发确实包含对bug的修复.
编辑我添加了一个带有修复的拉取请求:
https://github.com/PHPOffice/PHPExcel/pull/251
原文链接:https://www.f2er.com/php/137188.html当您尝试将单元格的样式复制到同一单元格时会发生这种情况;看看这个:
$PHPe = new PHPExcel(); $sheet = $PHPe->createSheet(); $sheet->setCellValue('A1','hi there') ; $sheet->setCellValue('A2','hi again') ; $sheet->duplicateStyle($sheet->getStyle('A1'),'A2'); $writer = new PHPExcel_Writer_Excel2007($PHPe); $writer->save('./test.xlsx');
它会工作得很好.但如果我添加另一行这样:
$sheet->duplicateStyle($sheet->getStyle('A1'),'A1');
$sheet->duplicateStyle((($level == 0) ? $this->styles['category'] : $this->styles['subcategory']),Cell::NUMBER . $row);
对于以下内容:
$style = ($level == 0) ? $this->styles['category'] : $this->styles['subcategory']; $targetCoords = Cell::NUMBER . $row; if($style->getActiveCell() != $targetCoords) { $sheet->duplicateStyle($style,$targetCoords); }