G
G
gomer17262018-01-17 23:03:48
phpspreadsheet
gomer1726, 2018-01-17 23:03:48

How to fill cells dynamically in PHPExcel?

Hello
I have this problem using PHPExcel

$sheet->setCellValue("A1", 'ФИО');
    $sheet->setCellValue("B1", 'Компания');
    $sheet->setCellValue("C1", 'Тест');
    $sheet->setCellValue("D1", 'Результат');
    $sheet->setCellValue("E1", 'Балл');
    
    $itr = 0;
    foreach($report as $key){
      $sheet->setCellValue("A".($itr + 2), $key["user_name"]);
      $sheet->setCellValue("B".($itr + 2), $key["company_title"]);
      $sheet->setCellValue("C".($itr + 2), $key["test"]);
      $sheet->setCellValue("D".($itr + 2), $key["word"]);
      $sheet->setCellValue("E".($itr + 2), $key["score"]);
      $itr++;
    }

Here there are cells, that is, A1, etc., but the problem is that these very cells should be generated automatically and I don’t know what letters will be there. How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shamanov, 2018-01-17
@SilenceOfWinter

everything seems to be simple - we add 2 counters for the data array (1 - a letter, 2 - a number), well, we do a cycle ala

for ($i = 1, $i < count($data), $i++) {
  for ($j = 1, $j < count($data[$i]), $j++) {
    eсho 'Ячейка ' . $i . 'x' . $j . ': ' . $data[$i][$j];
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question