phpexcel动态添加列 [英] phpexcel add column dynamically

查看:84
本文介绍了phpexcel动态添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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();

通过上面的查询,它只会在A1,B1(表的最后一个数据)中打印.但是我怎样才能让它动态.这样在 foreach 循环中它应该打印

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

但是 A1,B1, A2,B2 ... 应该在 foreach 循环内动态生成.我该怎么做.

However A1,B1, A2,B2 ... Should be generated dynamically inside the foreach loop. How can i do this.

我的意思是我应该在下面的代码中用什么代替A1,B1来动态添加它

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"]); 

推荐答案

简单的 PHP 字符串连接,用一个简单的 PHP 递增数字,给你一个每行递增的单元格地址:

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++;
}

这篇关于phpexcel动态添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆