本文介绍了PHPExcel插入数组公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要插入此数组公式:
{=SUM(IF(FREQUENCY(IF(T9:T977=1,MATCH(U9:U977,U9:U977,0)),ROW(U9:U977)-ROW(U9)+1),1))}
但是当我使用时:
$sheet->getCell("C1")->setValue("{=SUM(IF(FREQUENCY(IF(T9:T977=1,MATCH(U9:U977,U9:U977,0)),ROW(U9:U977)-ROW(U9)+1),1))}");
它不起作用,我检查了文档,但仍然没找到任何东西.
It doesn't work, I've checked the documentation, but still haven't found anything.
推荐答案
我找不到任何答案,所以我通过了PHPExcel并提出了自己的解决方案:
I couldn't find any answer so I went through PHPExcel and made myself a solution:
in /PHPExcel/Cell.php
上添加以下内容:
case PHPExcel_Cell_DataType::TYPE_FORMULA_ARRAY:
$this->_value = (string)$pValue;
break;
in /PHPExcel/Cell/DataType.php
添加此常量:
const TYPE_FORMULA_ARRAY = 't';
最后在/PHPExcel/Writer/Excel2007/Worksheet.php
中,我已在1095行开始的开关中添加了它:
At last in /PHPExcel/Writer/Excel2007/Worksheet.php
I've added this in the switch beginning at row 1095:
case 't': // Array Formulae
$objWriter->startElement('f');
$objWriter->writeAttribute('t', 'array');
$objWriter->writeAttribute('ref', $pCellAddress);
$objWriter->writeAttribute('aca', '1');
$objWriter->writeAttribute('ca', '1');
$objWriter->text($cellValue);
$objWriter->endElement();
if ($this->getParentWriter()->getOffice2003Compatibility() === false) {
if ($this->getParentWriter()->getPreCalculateFormulas()) {
$calculatedValue = $pCell->getCalculatedValue();
if (!is_array($calculatedValue) && substr($calculatedValue, 0, 1) != '#') {
$objWriter->writeElement('v', PHPExcel_Shared_String::FormatNumber($calculatedValue));
} else {
$objWriter->writeElement('v', '0');
}
} else {
$objWriter->writeElement('v', '0');
}
}
break;
然后我使用了这样的函数:
Then I used the function like this:
$sheet->getCell("C1")->setValueExplicit("=SUM(IF(FREQUENCY(IF(T9:T977=1,MATCH(U9:U977,U9:U977,0)),ROW(U9:U977)-ROW(U9)+1),1))", PHPExcel_Cell_DataType::TYPE_FORMULA_ARRAY);
当我创建一个excel文件时,一切正常!
And it works all good when I'm creating a excel file!
这篇关于PHPExcel插入数组公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!