write to excel cell using the namedrange instead of cell notation like (A1,A2) using PHP

I am writing to excel using PHP_Excel. I am using

$object->getActiveSheet()->setCellValue("A1", utf8_encode("$var"));

Is there any way that instead of using the Cell (A1) itself i can use the namedrange. I already name the cell in excel file ?

1 answer

  • answered 2018-03-13 22:05 Tommaso Belluzzo

    This is what I suggest you:

    1) Create a multidimensional array by repeating your variable...

    $var = utf8_encode("MyString");
    $arr = array();
    for ($i = 0; $i < 10; ++$i)
      for ($j = 0; $j < 10; ++$j)
        $arr[$i][] = $var;

    2) Populate the Excel spreadsheet using fromArray (where A1 is the top left coordinate of your final data)...

    $object->getActiveSheet()->fromArray($arr, NULL, 'A1');

    One way or another, even if you don't want to use setCellValueByColumnAndRow (which would save you one step with a little drawback on the performance), you will have to put your code into an iteration.