我想将公式设置为单元格,但我在途中遇到错误。这是一个代码:
$objReader = PHPExcel_IOFactory::load($path.$filename);
$objReader->setActiveSheetIndex(0);
$objReader->getActiveSheet()
->setCellValue('C2','=VLOOKUP(A9;A3:B32;2)');
$objWriter = PHPExcel_IOFactory::createWriter($objReader, 'Excel5');
$objWriter->save($path.'plik.xls');
公式是从普通表中复制的,所以她是怀特。我得到这个消息:feuil1!C2 -> Formula Error: An unexpected error occured
。
答案 0 :(得分:0)
除非您启用了使用;
作为参数分隔符的语言环境,否则必须使用英语(en_us)参数分隔符(即逗号,
)
从手册中引用:
Inside the Excel file, formulas are always stored as they would appear in an English version of Microsoft Office Excel, and PHPExcel handles all formulae internally in this format. This means that the following rules hold:
• Decimal separator is '.' (period)
• Function argument separator is ',' (comma)
• Matrix row separator is ';' (semicolon)
• English function names must be used
This is regardless of which language version of Microsoft Office Excel may have been used to create the Excel file.
这样:
$objReader = PHPExcel_IOFactory::load($path.$filename);
$objReader->setActiveSheetIndex(0);
$objReader->getActiveSheet()
->setCellValue('C2','=VLOOKUP(A9,A3:B32,2)');
$objWriter = PHPExcel_IOFactory::createWriter($objReader, 'Excel5');
$objWriter->save($path.'plik.xls');
修改强>
如果您想使用法语公式,则需要启用语言环境,并在法语和英语之间转换公式:
$objReader = PHPExcel_IOFactory::load($path.$filename);
$objReader->setActiveSheetIndex(0);
$locale = 'fr';
$validLocale = PHPExcel_Settings::setLocale($locale);
if (!$validLocale) {
echo 'Unable to set locale to '.$locale." - reverting to en_us<br />\n";
$internalFormula = '=VLOOKUP(A9,A3:B32,2)';
} else {
$formula = '=RECHERCHEV(A9;A3:B32;2)';
$internalFormula =
PHPExcel_Calculation::getInstance()->translateFormulaToEnglish($formula);
}
$objReader->getActiveSheet()
->setCellValue('C2', $internalFormula);
$objWriter = PHPExcel_IOFactory::createWriter($objReader, 'Excel5');
$objWriter->save($path.'plik.xls');