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

时间:2015-07-06 19:05:10

标签: php 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');

1 个答案:

答案 0 :(得分:0)

趟过你的代码墙

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

对我来说,它看起来不是一个有效的Excel公式。什么是专栏?如果它是Excel命名范围或单元格引用,那么它将起作用;但是因为它在Excel公式中没有任何效果。

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

修改

如果要将单个单元格设置为值15或0,具体取决于G列中的任何单元格是否包含值&#34; SOS&#34;,那么适当的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正确连接公式值