S
S
Sergey Khlopov2018-02-09 14:56:10
PHP
Sergey Khlopov, 2018-02-09 14:56:10

How to populate multiple columns in PHPExcel?

Hello, please tell me, I am generating an Excel file, data from the database should be inserted into it, and the fact is that this Excel should have 291 columns. Here is an option to do this:

$active_sheet2->setCellValue('A1',"yellowline1");
  $active_sheet2->setCellValue('B1',"yellowline2");
  $active_sheet2->setCellValue('C1',"yellowline4");
  $active_sheet2->setCellValue('D1',"yellowline6");

In manual 219 columns, so creating it is a long time. Can you please tell me how to do this in a loop? You just need to insert the name of each column in the first line.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Osadchy, 2018-02-09
@waspmax1

You have received an array of data, written it into a variable, and then:

for($i = 1; $i < count($yourData); $i++){
$active_sheet2->setCellValue('A' . $i,"yellowline1");
  $active_sheet2->setCellValue('B' . $i,"yellowline2");
  $active_sheet2->setCellValue('C' . $i,"yellowline4");
  $active_sheet2->setCellValue('D' . $i,"yellowline6");
}

S
Stimulate, 2018-02-09
@Stimulate

$it = 1;

foreach ($results as $result) {
    $active_sheet2->setCellValue('A'.$it, $result->yellowline1);
    $active_sheet2->setCellValue('B'.$it, $result->yellowline2);
    $active_sheet2->setCellValue('C'.$it, $result->yellowline3);
    $active_sheet2->setCellValue('D'.$it, $result->yellowline4);

    $it++;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question