问题
$mysqli = new mysqli("localhost", "root", "", "wolly");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM action";
if ($result = $mysqli->query($query))
{
while ($row = $result->fetch_assoc())
{
$objPHPExcel->getActiveSheet()->setCellValue('A1', $row["ActionId"]);
$objPHPExcel->getActiveSheet()->setCellValue('B1', $row["ActionName"]);
}
$result->free();
}
$mysqli->close();
By the above query it will print only in the A1, B1 (the last data of the table). But How can i make it dynamic. So that in the foreach loop it should print
A1|B1
1 |Ram
2 |Raj
3 |Adam
However A1,B1, A2,B2 ... Should be generated dynamically inside the foreach loop. How can i do this.
I mean what should i give in the place of A1, B1 in the below code to add it dynamically
$objPHPExcel->getActiveSheet()->setCellValue('A1', $row["ActionId"]);
$objPHPExcel->getActiveSheet()->setCellValue('B1', $row["ActionName"]);
回答1:
Simple PHP string concatenation, with a simple PHP incrementing number, to give you a cell address that increments with each row:
$rowNumber = 1;
while ($row = $result->fetch_assoc())
{
$objPHPExcel->getActiveSheet()
->setCellValue('A' . $rowNumber, $row["ActionId"])
->setCellValue('B' . $rowNumber, $row["ActionName"]);
$rowNumber++;
}
来源:https://stackoverflow.com/questions/27784737/phpexcel-add-column-dynamically