2

I am using PHPExcel to generate an excel sheet using the database data. I wrote the following code till now.

I don't know how to write inside while loop to write the records for A2, B2, C2 and A3, B3, C3 etc.

<?php
include('PHPExcel/Classes/PHPExcel.php');
include('PHPExcel/Classes/PHPExcel/Writer/Excel2007.php');
include('config/db.php');

$stmt = $pdo->prepare("SELECT * FROM students");
$stmt-> execute();

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Name');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Type');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'DOB');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'DOJ');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Gender');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'City');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Location');

while($f = $stmt->fetch()){
  $objPHPExcel->getActiveSheet()->SetCellValue('A2', '');
}

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

echo " Click here for gerate xlsx file <a href='test.xlsx'>clicking me</a>";
?>

Please help me further on this on what to do next here.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Side note: The prepare/execute is pointless. You can just use `query()` since you're not using a prepared statement, per se. – Funk Forty Niner Sep 06 '19 at 16:35
  • Instead of hard coding the cell values as A1, B1, C1... Make that a variable consisting of two parts, the row and columns. For example: `$row` will contain the number and `$col` will contain the letter. Then use `SetCellValue($col . $row, 'Name');` Increment the values for `$row` and `$col` in the loop. – dazed-and-confused Sep 06 '19 at 16:39
  • @dazed-and-confused I am really confused. Can you please write a code answer? –  Sep 06 '19 at 16:42
  • An answer has been posted which should give you enough to work with. Good luck – dazed-and-confused Sep 06 '19 at 16:56
  • @dazed-and-confused can you also tell me how to make the text bold for A1, B1 etc., as they are the headings? –  Sep 06 '19 at 17:13

0 Answers0