I got the following code which works fine, but it does not bring the database field names. How can I bring the field names, or even bring some title names at the first row?
while($row = mysql_fetch_array($result)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['target_id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['form_id']);
$objPHPExcel->getActiveSheet()->SetCellValue('f'.$rowCount, $row['update_date']);
$objPHPExcel->getActiveSheet()->SetCellValue('g'.$rowCount, $row['user_id']);
$rowCount++;
echo "<br>New record created successfully";
}
Add a separate SQL query to fetch your column headers and insert those separately, for example:
$sql = 'SELECT DISTINCT column_name FROM database.table WHERE column_name = column_name;';
And then insert the headers into your excel worksheet, something like:
$res = mysqli_query($sql);
$heads = mysqli_fetch($res);
$headers = array();
$loop = 0;
while($row = mysqli_fetch_assoc($res)){
$headers[$loop] = $row;
$loop ++;
}
$r = 0;
$c = 1;
foreach ($headers as $k => $v) {
if(isset($v['column_name']) && '' != $v['column_name']) {
$sheet->setCellValueByColumnAndRow($c, $r, $v['column_name']);
}
$c++;
}