【问题标题】:PHPExcel set column value and column quantityPHPExcel设置列值和列数
【发布时间】:2018-01-25 14:44:37
【问题描述】:

我有一个 select 语句,其结果如下:

  79927
  79927
  79927
  79927
  79928
  79928
  79928
  79928
  79928

然后当我导出到 excel 时,我有以下屏幕:enter image description here

但我真正想要的是这个结果:enter image description here

我想将最大列设置为 D,这意味着每次结果达到 D 列时,下一个将移动到下一行。

如果结果达到D1,接下来将进入A2,依此类推。

这是我的代码:

        $rowCount=1;
        while ( $row1 = mysql_fetch_array ($itens_spec_comb)) {

        $objPHPExcel->getActiveSheet()->SetCellValue ('A'.$rowCount, $row1 ['id']);



        $rowCount++;
        }

【问题讨论】:

    标签: php mysql excel select phpexcel


    【解决方案1】:

    如果我理解正确,请使用我的示例:

        $rowCount=1;
        $col = array("A","B","C","D");
        $i=0;
    
         while ( $row1 = mysql_fetch_array ($itens_spec_comb)) {
    
                $objPHPExcel->getActiveSheet()->SetCellValue ($col[$i].$rowCount, $row1 ['id']);
                $i++;
                if($i==3){
                     $i=0;
                     $rowCount++;
                }
           }
    

    【讨论】:

    • 没错!我刚刚将 if($i==3){ 更改为 if($i==4){ 非常感谢!
    【解决方案2】:
        $rowCount=1;
        while ( $row1 = mysql_fetch_array ($itens_spec_comb)) {
    
            for($col=0; $col<4; $col++){
    
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ($col, $rowCount, $row1 ['id']);
            }
    
            $rowCount++;
        }
    

    相关:PHPExcel setCellValueByColumnAndRow not writing data to spreadsheet

    【讨论】:

    • 使用你的想法,我在每一列中都得到了重复的所有结果。
    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多