Reputation: 1240
Edited:
I want to set fields name before values?
->setCellValue('A1','ID'); ->setCellValue('B1','Email'); ->setCellValue('C1','Phone'); ....
I have many fields
Codes:
$rowNumber = 1;
// Loop through the result set
while ($row = mysql_fetch_object($result)) {
$objPHPExcel->getActiveSheet()->setCellValue('A1','ID')
->setCellValue('B1','Email')
..... etc
->setCellValue('A'.$rowNumber,$row->id)
->setCellValue('B'.$rowNumber,$row->email);
...... etc
$rowNumber++;
}
Is there another option?
Upvotes: 1
Views: 2415
Reputation: 49
use this code to use auto header print excel head from data base
$headerArr=array("id","Email","Phone");
$newsheet = $objPHPExcel->createSheet();
$colH = 'A';
$rowNumberH=1;
foreach ($headerArr as $h) {
$newsheet->setCellValue($colH . $rowNumberH, $h);
$colH++;
}
Upvotes: 0
Reputation: 212402
How would you do this if you were outputting it to a simple HTML table? Outputting to Excel isn't much different.
$rowNumber = 1;
// Loop through the result set
while ($row = mysql_fetch_row($result)) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowNumber,$row['id'].' Mr '.$row['name']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$rowNumber,$row['email']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$rowNumber,$row['title']);
$rowNumber++;
}
or even...
$rowNumber = 1;
// Loop through the result set
while ($row = mysql_fetch_row($result)) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowNumber,$row['id'].' Mr '.$row['name'])
->setCellValue('B'.$rowNumber,$row['email'])
->setCellValue('C'.$rowNumber,$row['title']);
$rowNumber++;
}
EDIT
Another option, based on your edit of the question:
Modify your SQL query to do the concatenation and adding 'Mr' for you, so that the returned row contains only the actual values that you want to write in each cell of the Excel row, then use the fromArray() method to write each row to the worksheet.
Upvotes: 1