代码之家  ›  专栏  ›  技术社区  ›  ForgivenIT

PHP使用xlsxwriter向excel中的列添加公式

  •  1
  • ForgivenIT  · 技术社区  · 7 年前

    我可以用xlsxwriter将=MAX(K2,L2,M2,O2,P2,Q2,N2)放入单元格F2中,这没有问题,但我无法为每行更改它(即:单元格F2中的=MAX(K2,L2,M2,O2,P2,Q2,N2))

    因此,如果我有1000行,它将转到单元格F1000中的(即=MAX(K1000,L1000,M1000,O1000,P10002,Q2,N1000)(BestPrice是F行)

    这是我在PHP 5.3.24中的代码

    $resulted[$y] = array('ISBN' => $isbn,
                      'Quantity' => $qty,
                      'Title' => $shortTitle,
                      'ItemAmount'=>$amount,
                      'Best'=>$best,
                      'BestPrice'=> '=MAX(K2,L2,M2,O2,P2,Q2,N2)',
                      'Difference'=>'=E-H',
                      'AvaQty'=>$AvaQty,
                      'LastDate'=>$LastDate,
                      'LastPrice'=>$LastPrice,
                                             );
    $y++;
    }
    
    
    $header =  array('ISBN' => 'integer',
                      'Quantity' => 'integer',
                      'Title' => 'string',
                      'Item_Amount'=>'price',
                      'Best' =>'string',
                      'BestPrice'=> 'price',
    
                      'Difference'=>'price',
    
                      'AvaQty'=>'integer',
                      'LastDate'=>'date',
                      'LastPrice'=>'price',
    
    
                       );
    
    $col_options=array('widths'=> array(40, 10,40,10));
    $writer = new XLSXWriter();
    
    $writer->writeSheetHeader('Name', $header, $col_options);
    foreach($resulted as $row)
    $writer->writeSheetRow('Name', $row);
    $writer->writeToFile("../My".$tracking .".xlsx");
    

    有什么想法吗

    1 回复  |  直到 7 年前
        1
  •  1
  •   TheGentleman    7 年前

    $y 是的,你可以使用它。如果没有,这样的方法会奏效:

    $y = 0;
    $row = 2;
    foreach($book_list as $book){ //Assuming you have some kind of book list you're iterating over...
        //Doing some stuff (e.g. setting those variables you use in the array)
    
        $resulted[$y] = array('ISBN' => $isbn,
          'Quantity' => $qty,
          'Title' => $shortTitle,
          'ItemAmount'=>$amount,
          'Best'=>$best,
          'BestPrice'=> "=MAX(K{$row},L{$row},M{$row},O{$row},P{$row},Q{$row},N{$row})", //Note the change in this line.
          'Difference'=>'=E-H',
          'AvaQty'=>$AvaQty,
          'LastDate'=>$LastDate,
          'LastPrice'=>$LastPrice,
        );
        $y++;
        $row++;
    }
    

    我想你可以使用 为此,假设它只是一个0索引计数器,而不是一个单独的变量,您可以执行以下操作: $row = $y+2 在循环迭代开始时。无论如何,这应该为你指明正确的方向