Error with the formulas

你。 提交于 2019-12-24 21:41:07

问题


I want to set formula to cell but im getting error on my way. Theres a code:

  $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'); 

Formula is copied from normal sheet so she's wright. Im getting this msg: feuil1!C2 -> Formula Error: An unexpected error occured.


回答1:


Unless you've enabled a locale that uses ; as an argument separator, then you must use an English (en_us) argument separator (ie, a comma ,)

Quoting from the manual:

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.

so:

$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'); 

EDIT

If you want to use French language formulae, then you need to enable the locale, and convert the formulae between French and English:

$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'); 


来源:https://stackoverflow.com/questions/18212115/error-with-the-formulas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!