无法使用 phpexcel 将公式应用于 excel 电子表格?

Not able to apply formula to the excel spreadsheet using phpexcel?

我在我的 php 代码中使用 PHPExcel 生成一个 excel 报告,其中包含一个数字到电子表格,但我想在这些电子表格中应用公式完整列,我使用以下代码但是它只是将相同的值应用于整个列,但我希望公式为 运行 并根据我的工作表的每个单元格列 G 给出值,我希望这些结果值在 Q 列中。 以下是我使用的完整代码:

<?php 
$dbhost= "localhost"; //your MySQL Server 
$dbuser = "root"; //your MySQL User Name 
$dbpass = "root"; //your MySQL Password 
$dbname = "pyramid"; 
//your MySQL Database Name of which database to use this 
$tablename = "peachtree"; //your MySQL Table Name which one you have to create excel file 
// your mysql query here , we can edit this for your requirement 

$tablename1 = "shipping";
$tablename2 = "embassies";
$tablename3 = "documentprocesses";
$tablename4 = "documentdetails";
$tablename5 = "documents";
$sql = "Select * from $tablename "; 
$sql1= "Select * from $tablename1";
$sql2= "Select * from $tablename2";
$sql3= "Select filter1.docID,
  filter1.salesOrderID,
  IF(filter1.docSource='shipping', 'Shipping', 'Non-Shipping') AS 'Doc Type',
  filter1.countryID,
  DATE(filter1.docTimeStamp) AS 'Date Received',
  IF(filter2.processStartDate IS NULL, 'OPEN',
   DATE(filter2.processStartDate)) AS 'Date Shipped',
  $tablename3.processCode,
  $tablename3.destination,
  $tablename3.processStartDate,
  $tablename4.*,
  filter1.docValue
FROM ( SELECT docID,
    salesOrderID,
    docSource,
    docValue,
    countryID,
    docTimeStamp
  FROM $tablename5
  WHERE DATE(docTimeStamp) <= '2015-03-31' ) AS filter1
JOIN (
SELECT docID,
   processStartDate
FROM $tablename3
WHERE processCode = 'SHPD'
  AND( processStatus <> 'completed'
   OR DATE(processStartDate) > '2015-03-31')
) AS filter2
ON filter1.docID = filter2.docID
LEFT JOIN (
SELECT *
FROM $tablename3
WHERE DATE(processStartDate) <= '2015-03-31'
AND  processStartDate IS NOT NULL) AS $tablename3
ON $tablename3.docID = filter2.docID
LEFT JOIN $tablename4 ON $tablename4.docID = filter2.docID";
//create  code for connecting to mysql 
$Connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno()); 
//select database 
$Db = @mysql_select_db($dbname, $Connect) 
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno()); 
//execute query 
$result = @mysql_query($sql,$Connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno()); 
$result1 = @mysql_query($sql1,$Connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno()); 
$result2 = @mysql_query($sql2,$Connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
$result3 = @mysql_query($sql3,$Connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());  

error_reporting(E_ALL);
 set_time_limit(0);
 ini_set('memory_limit','2500M');
 require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
 $objPHPExcel = new PHPExcel();
 // Set the active Excel worksheet to sheet 0 

$objPHPExcel->setActiveSheetIndex(0);  

// Initialise the Excel row number 

$rowCount = 1;  


//start of printing column names as names of MySQL fields  

 $column = 'A';

for ($i = 0; $i < mysql_num_fields($result); $i++)  

{
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result,$i));
    $column++;
}

//end of adding column names  
//start while loop to get data  

$rowCount = 2;  

while($row = mysql_fetch_row($result))  

{  
    $column = 'A';

   for($j=0; $j<mysql_num_fields($result);$j++)  
    {  
        if(!isset($row[$j]))  

            $value = NULL;  

        elseif ($row[$j] != "")  

            $value = strip_tags($row[$j]);  

        else  

            $value = "";  


        $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
        $column++;
    }  

    $rowCount++;
} 
$objPHPExcel->getActiveSheet()->setTitle('peachtree');

$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$rowCount = 1;  


//start of printing column names as names of MySQL fields  

 $column = 'A';

for ($i = 0; $i < mysql_num_fields($result1); $i++)  

{
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result1,$i));
    $column++;
}

//end of adding column names  
//start while loop to get data  

$rowCount = 2;  

while($row = mysql_fetch_row($result1))  

{  
    $column = 'A';

   for($j=0; $j<mysql_num_fields($result1);$j++)  
    {  
        if(!isset($row[$j]))  

            $value = NULL;  

        elseif ($row[$j] != "")  

            $value = strip_tags($row[$j]);  

        else  

            $value = "";  


        $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
        $column++;
    }  

    $rowCount++;
} 
$objPHPExcel->getActiveSheet()->setTitle('shipping');

$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(2);
$rowCount = 1;  


//start of printing column names as names of MySQL fields  

 $column = 'A';

for ($i = 0; $i < mysql_num_fields($result2); $i++)  

{
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result2,$i));
    $column++;
}

//end of adding column names  
//start while loop to get data  

$rowCount = 2;  

while($row = mysql_fetch_row($result2))  

{  
    $column = 'A';

   for($j=0; $j<mysql_num_fields($result2);$j++)  
    {  
        if(!isset($row[$j]))  

            $value = NULL;  

        elseif ($row[$j] != "")  

            $value = strip_tags($row[$j]);  

        else  

            $value = "";  


        $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
        $column++;
    }  

    $rowCount++;
} 
$objPHPExcel->getActiveSheet()->setTitle('embassies');

$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(3);
$rowCount = 1;  


//start of printing column names as names of MySQL fields  

 $column = 'A';

for ($i = 0; $i < mysql_num_fields($result3); $i++)  

{
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result3,$i));
    $column++;
}
$column = 'Q';
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount,"SOS");
//end of adding column names  
//start while loop to get data  

$rowCount = 2;  

while($row = mysql_fetch_row($result3))  

{  
    $column = 'A';

   for($j=0; $j<mysql_num_fields($result3);$j++)  
    {  
        if(!isset($row[$j]))  

            $value = NULL;  

        elseif ($row[$j] != "")  

            $value = strip_tags($row[$j]);  

        else  

            $value = "";  


        $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
        $column++;

    }
    ***$column = 'Q';

    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount,'=IF(column="SOS",15,0)');
    $rowCount++;***

}      


$objPHPExcel->getActiveSheet()->setTitle('documentdata');


// Redirect output to a client’s web browser (Excel5) 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="results.xls"'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output');

趟过你的代码墙

=IF(column="SOS",15,0)

在我看来这不是一个有效的 Excel 公式。什么是专栏?如果它是一个 Excel 命名范围,或者一个单元格引用,那么它就可以工作;但就目前而言,在 Excel 公式中没有什么是有效的。

您的公式必须是有效的Excel公式

编辑

如果您想根据 G 列中的任何单元格是否包含值 "SOS" 将单个单元格的值设置为 15 或 0,那么适用的 MS Excel 公式将是

=IF(ISERROR(MATCH("SOS",G:G, 0)), 0, 15)

如果您想逐行进行此检查,则可以将公式设置为:

$objPHPExcel->getActiveSheet()
    ->setCellValue($column.$rowCount,'=IF(G'.$rowCount.'="SOS",15,0)');

编辑 #2

作为一种调试方式,对于第 2 行(即单元格 Q2),该公式应为

=IF(G2="SOS",15,0)

第 3 行(单元格 Q3)应该是

=IF(G3="SOS",15,0)

等等

进行一些基本调试以确保您的 PHP 正确连接公式值