0

我想将公式设置为单元格,但我在路上遇到错误。有一段代码:

  $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

4

1 回答 1

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'); 
于 2013-08-13T19:23:42.070 回答