EasyXLS Excel library can be used to export Excel files with PHP on Windows, Linux, Mac or other operating systems. The integration vary depending on the operating system or if .NET Framework or Java is chosen:
Step 4: Run PHP code that creates conditional formatting in Excel cells
Execute the following PHP code that exports an Excel file and sets conditional formatting to a range. The first step is to set the range where the conditional formatting will be applied, then set the formatting options.
<?php/*=============================================================
| Tutorial 14
|
| This tutorial shows how to create an Excel file in PHP having
| a sheet and conditional formatting for cell ranges.
* ===========================================================*/include("DataType.inc");
include("ConditionalFormatting.inc");
include("FontSettings.inc");
include("Border.inc");
include("Color.inc");
header("Content-Type: text/html");
echo"Tutorial 14<br>";
echo"----------<br>";
// Create an instance of the class that exports Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Create a sheet
$workbook->easy_addWorksheet_2("Sheet1");
// Get the table of data for the first worksheet
$xlsTab = $workbook->easy_getSheet("Sheet1");
$xlsTable = $xlsTab->easy_getExcelTable();
// Add data in cellsfor ($i=0; $i<6; $i++)
{
for ($j=0; $j<4; $j++)
{
if(($i<2)&&($j<2))
$xlsTable->easy_getCell($i, $j)->setValue("12");
elseif(($j==2)&&($i<2))
$xlsTable->easy_getCell($i, $j)->setValue("1000");
else
$xlsTable->easy_getCell($i, $j)->setValue("9");
$xlsTable->easy_getCell($i, $j)->setDataType($DATATYPE_NUMERIC);
}
}
// Set conditional formatting
$xlsTab->easy_addConditionalFormatting_5("A1:C3", $CONDITIONALFORMATTING_OPERATOR_BETWEEN,
"=9", "=11", true, true, (int)$COLOR_RED);
// Set another conditional formatting
$xlsTab->easy_addConditionalFormatting_9("A6:C6", $CONDITIONALFORMATTING_OPERATOR_BETWEEN,
"=COS(PI())+2", "", (int)$COLOR_BISQUE);
$xlsTab->easy_getConditionalFormattingAt_2("A6:C6")->getConditionAt(0)->setConditionType(
$CONDITIONALFORMATTING_CONDITIONAL_FORMATTING_TYPE_EVALUATE_FORMULA);
// Export Excel fileecho"Writing file: C:\Samples\Tutorial14 - conditional formatting in Excel.xlsx<br>";
$workbook->easy_WriteXLSXFile("C:\Samples\Tutorial14 - conditional formatting in Excel.xlsx");
// Confirm export of Excel fileif ($workbook->easy_getError() == "")
echo"File successfully created.";
elseecho"Error encountered: " . $workbook->easy_getError();
// Dispose memory
$workbook->Dispose();
$workbook = null;
?>
Overloaded methods For methods with same name but different parameters, only the first method overload retains the original name. Subsequent overloads are uniquely renamed by appending to the method name '_2', '_3', etc (method, method_2, method_3), an integer that corresponds to the order of declaration that can be found in EasyXLS.h, a file that comes with EasyXLS installation.
EasyXLS on Linux, Mac, Windows using Java with PHP
If you opt for the Java version of EasyXLS, a similar code as above requires PHP/Java Bridge between PHP and Java.
Copy EasyXLS.jar into Tomcat installation path, lib folder.
Step 4: Run PHP code that creates conditional formatting in Excel cells
Execute the following PHP code that exports an Excel file and sets conditional formatting to a range. The first step is to set the range where the conditional formatting will be applied, then set the formatting options.