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

PHP to add a formula to a column in excel with xlsxwriter

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

所以如果我有一千行,它将转到(即 =MAX(K1000,L1000,M1000,O1000,P10002,Q2,N1000) 在单元格 F1000 中)(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");

有什么想法

您只需要使用一个变量来跟踪您的行号。根据 $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++;
}

我怀疑您可以为此使用 $y 而不是单独的变量,假设它只是一个 0 索引的计数器,您可以在开头做类似 $row = $y+2 的事情循环迭代。无论如何,这应该为您指明正确的方向