1

i am new to PHPExcel learning from last two days and i am generating one report for the form input data. I have generated dynamically columns in the excel report but not able to set the first column as Index & last column as Date. My code is:

// setting column names begin
$col = 1;
$row = 0;

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row, "Index   No.");

foreach ($formInfo['fields'] as $fields) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row,   $fields['grid-name']);
$col++;
}
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, 'Date & Time of Input');

// setting column names ends!

Thanks in advance for response.

Shaggie
  • 1,799
  • 7
  • 34
  • 63

1 Answers1

0

You have achieved the most. I just updated your code. Now it will work like as per you wish.

// Initializing last col variable
$endcolval = 0;
// Storing the value to First Column
$objPHPExcel->setActiveSheetIndex(0)
                          ->setCellValueByColumnAndRow(0, 1, "Index");
// Storing the rest of the values from array to the respect indexes
foreach ($formInfo['fields'] as $col=>$fields)
{
    $objPHPExcel->setActiveSheetIndex(0)
                          ->setCellValueByColumnAndRow($col+1, 1, $fields['grid-name']);
    // Using the above function you can able dynamically store the values to the cell.
    // setCellValueByColumnAndRow(Column_Number, Row_Number, Value_To_Save);
    $endcolval = $col+1;// Getting the last column number
}
// Assigning the Date to the Last Column.
$objPHPExcel->setActiveSheetIndex(0)
                          ->setCellValueByColumnAndRow($endcolval+1, 1, "Date");
Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66