I want to have my excel file filled with some data which I get from my database, for example the name and age of someone.
Say there are 10 people in my database. I want those 10 people in my Excel file.
So basically, you would get:
NAME AGE
Person1 20 years
Person2 25 years
And so on. I know how to set the NAME and AGE stuff, but how would I go about looping the data and writing it inside the excel file? I couldn't find anything about it in PHPExcel's documentation.
This is my MySQL:
$query = "SELECT * FROM bestelling";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$name = $row['name'];
$age = $row['age'];
}
I'm assuming you already have the excel object created. I'll call it $objPHPExcel to conform to their examples. In that case you can loop your result set and populate the spreadsheet this way:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowCount = 1;
while($row = mysql_fetch_array($result)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['age']);
$rowCount++;
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file.xlsx');
EDIT: I have updated the example to provide a complete solution.