【问题标题】:Php to Excel : fill the output Excel file with a loopPhp to Excel:用循环填充输出的 Excel 文件
【发布时间】:2015-11-25 10:18:49
【问题描述】:

我想创建一个 Excel 文件(使用 PhpExcel)并用 MySQL 查询的内容填充它。

我只有一列,所以结果将如下所示:

$sheet->setCellValueByColumnAndRow(0, $i, $content)

所以我必须在我的查询中循环并创建一个计数器来填充与我的内容的 ptlum 列的每个项目相对应的每一行。
所以目标是得到以下结果:

1 AX001
2 AX003
3 AX012

代码就是那个:

$column = 1;
while($data = mysql_fetch_assoc($result)) {

    $workbook = new PHPExcel;
    $sheet = $workbook->getActiveSheet();
    $sheet->setCellValueByColumnAndRow(0, $column, $data['ptlum']);
    //echo($column. " "  . $data['ptlum']. " ");
    $column = $column + 1; //or $column++; 

问题是我的 Excel 文件是空的.. 如果我在 setCellValueByColumnAndRow 行中输入一个数字而不是 $column,它就可以工作。但是变量不起作用.. 另一方面,如果我输入“$column = 1;”在循环内,我的 Excel 文件将始终只包含一行..

你有什么想法吗?

非常感谢!

【问题讨论】:

    标签: php excel loops counter


    【解决方案1】:

    您只需将每列的调用更改为 setCellValueByColumnAndRow 并递增:

    $sql = "SELECT ptlum FROM `ptlum` WHERE nomcom = 'AIGREFEUILLE' and RIGHT (date,4) < 2015 and RIGHT(date,4) > 0 ";
    $result = mysql_query($sql);
    
    $row = 1; // 1-based index
    $column = 1;
    while($data = mysql_fetch_assoc($result)) {
        $sheet->setCellValueByColumnAndRow($column, $row, $data['ptlum']);
        $column = $column + 1; //or $column++; if you prefer
    }
    

    如您所见,您可以使用mysql_fetch_assoc 检索您想要的字段/列,返回一个关联数组。

    此外,您不必在SELECT 中包含WHERE 条件的字段。

    最后,您应该将已弃用的 mysql_* 函数替换为等效的 mysqli_*,如 here 所述。

    编辑:

    对于您的“新”问题,这段代码应该可以工作:

    $sql = "SELECT ptlum FROM `ptlum` WHERE nomcom = 'AIGREFEUILLE' and RIGHT (date,4) < 2015 and RIGHT(date,4) > 0 ";
    $result = mysql_query($sql);
    
    $row = 1; // 1-based index
    $column = 1;
    $workbook = new PHPExcel;
    $sheet = $workbook->getActiveSheet();
    while($data = mysql_fetch_assoc($result)) {
        $sheet->setCellValueByColumnAndRow($column, $row, $data['ptlum']);
        $column = $column + 1; //or $column++; if you prefer
    }
    

    首先,不要在每个循环中实例化您的工作簿和工作表,先做一次。

    其次,您的参数顺序错误,它是 column 然后是 row,而不是相反的,如方法名称中明确说明的那样。

    【讨论】:

    • 感谢您的回答。实际上我没有正确的回声。首先我的查询基于两个字段(ptlum和RIGHT(date,4)),我没有设法在我的回声中只得到字段ptlum。我只想增加i 这是我的列索引(对不起,不是行)和 ptlum。我用 echo ($i . " " . $value. " ") 尝试了你的 sn-p 但它不起作用.. 我有以下结果:imagik.fr/images/2015/11/25/phpexcel.png
    • 我的结果是 1 AA15 2 AA15 3 AA15 4 AA15 而不是 1 AA15 2 AA16 3 AA17 4 AA18。更重要的是,其他字段被写入(日期),而我只想要 ptlum 字段.. 谢谢!
    • @Julien 看看我的编辑,是你想要的吗?结果在一行上。
    • @Julien 看看我添加的解释,它可能会有所帮助;)
    • 对不起,我还有一个问题。回显指令运行良好,但输出的 Excel 文件为空。我更新了我的第一条消息,你能看一下吗?再次感谢 Veve!
    【解决方案2】:

    也许这就是你想要的:

    $sql = "SELECT ptlum, RIGHT(date,4)  FROM `ptlum` WHERE nomcom = 'AIGREFEUILLE' and RIGHT (date,4) < 2015 and RIGHT(date,4) > 0 ";
    $result = mysql_query($sql);
    
    $i = 0;
    while($data = mysql_fetch_assoc($result)) {
        $sheet->setCellValueByColumnAndRow(0, $i, $i+1); //1-based index
        $sheet->setCellValueByColumnAndRow(1, $i, $data['ptlum']);
        $i++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多