PHPExcel:无法设置索引号和&最后一列值为“日期”

时间:2015-04-13 05:55:04

标签: phpexcel

我是最近两天学习PHPExcel的新手,我正在为表单输入数据生成一个报告。我在excel报告中生成了动态列,但无法将第一列设置为Index&最后一列为日期。 我的代码是:

// 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!

提前感谢您的回复。

1 个答案:

答案 0 :(得分:0)

你已经取得了最大的成就。我刚刚更新了你的代码。现在它会像你希望的那样工作。

// 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");