phpexcel add column dynamically

╄→гoц情女王★ 提交于 2020-07-22 21:35:40

问题


$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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!