I am using PHPExcel in my php code to generate an excel report with a number to spreadsheets but i want to apply formula complete columns in those spreadsheets and i am using the following code but its just applying the same values to the complete column but i want the formula to run and give values depending on the each cell column G of my worksheet, and i want these resulting values in column Q. Following is the complete code that i am using:
<?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');
Wading through your wall of code
=IF(column="SOS",15,0)
Doesn't look like a valid Excel formula to me. What is column? If it was an Excel named range, or a cell reference, then it would work; but as it stands it's nothing that would be valid in an Excel formula.
Your formula must be a valid Excel formula
EDIT
If you want to set a single cell to a value of 15 or 0 depending on whether any cell in column G contains the value "SOS", then an appropriate MS Excel formula for that would be
=IF(ISERROR(MATCH("SOS",G:G, 0)), 0, 15)
If you want to do this check on a row by row basis, then you'd set your formula to something like:
$objPHPExcel->getActiveSheet()
->setCellValue($column.$rowCount,'=IF(G'.$rowCount.'="SOS",15,0)');
EDIT #2
As a way of debugging, for row #2 (ie cell Q2) that formula should be
=IF(G2="SOS",15,0)
for row #3 (cell Q3) it should be
=IF(G3="SOS",15,0)
etc
Do some basic debugging to be sure that your PHP is concatenating the formula value correctly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.