ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
08conditionalformatting.php
Go to the documentation of this file.
1 <?php
29 error_reporting(E_ALL);
30 ini_set('display_errors', TRUE);
31 ini_set('display_startup_errors', TRUE);
32 
33 define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
34 
35 date_default_timezone_set('Europe/London');
36 
38 require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
39 
40 
41 // Create new PHPExcel object
42 echo date('H:i:s') , " Create new PHPExcel object" , EOL;
44 
45 // Set document properties
46 echo date('H:i:s') , " Set document properties" , EOL;
47 $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
48  ->setLastModifiedBy("Maarten Balliauw")
49  ->setTitle("Office 2007 XLSX Test Document")
50  ->setSubject("Office 2007 XLSX Test Document")
51  ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
52  ->setKeywords("office 2007 openxml php")
53  ->setCategory("Test result file");
54 
55 
56 // Create a first sheet, representing sales data
57 echo date('H:i:s') , " Add some data" , EOL;
58 $objPHPExcel->setActiveSheetIndex(0);
59 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Description')
60  ->setCellValue('B1', 'Amount');
61 
62 $objPHPExcel->getActiveSheet()->setCellValue('A2', 'Paycheck received')
63  ->setCellValue('B2', 100);
64 
65 $objPHPExcel->getActiveSheet()->setCellValue('A3', 'Cup of coffee bought')
66  ->setCellValue('B3', -1.5);
67 
68 $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Cup of coffee bought')
69  ->setCellValue('B4', -1.5);
70 
71 $objPHPExcel->getActiveSheet()->setCellValue('A5', 'Cup of tea bought')
72  ->setCellValue('B5', -1.2);
73 
74 $objPHPExcel->getActiveSheet()->setCellValue('A6', 'Found some money')
75  ->setCellValue('B6', 8);
76 
77 $objPHPExcel->getActiveSheet()->setCellValue('A7', 'Total:')
78  ->setCellValue('B7', '=SUM(B2:B6)');
79 
80 
81 // Set column widths
82 echo date('H:i:s') , " Set column widths" , EOL;
83 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
84 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
85 
86 
87 // Add conditional formatting
88 echo date('H:i:s') , " Add conditional formatting" , EOL;
92  ->addCondition('200')
93  ->addCondition('400');
94 $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_YELLOW);
95 $objConditional1->getStyle()->getFont()->setBold(true);
96 $objConditional1->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
97 
101  ->addCondition('0');
102 $objConditional2->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
103 $objConditional2->getStyle()->getFont()->setItalic(true);
104 $objConditional2->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
105 
109  ->addCondition('0');
110 $objConditional3->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
111 $objConditional3->getStyle()->getFont()->setItalic(true);
112 $objConditional3->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
113 
114 $conditionalStyles = $objPHPExcel->getActiveSheet()->getStyle('B2')->getConditionalStyles();
118 $objPHPExcel->getActiveSheet()->getStyle('B2')->setConditionalStyles($conditionalStyles);
119 
120 
121 // duplicate the conditional styles across a range of cells
122 echo date('H:i:s') , " Duplicate the conditional formatting across a range of cells" , EOL;
123 $objPHPExcel->getActiveSheet()->duplicateConditionalStyle(
124  $objPHPExcel->getActiveSheet()->getStyle('B2')->getConditionalStyles(),
125  'B3:B7'
126  );
127 
128 
129 // Set fonts
130 echo date('H:i:s') , " Set fonts" , EOL;
131 $objPHPExcel->getActiveSheet()->getStyle('A1:B1')->getFont()->setBold(true);
132 //$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
133 $objPHPExcel->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true);
134 //$objPHPExcel->getActiveSheet()->getStyle('B7')->getFont()->setBold(true);
135 
136 
137 // Set header and footer. When no different headers for odd/even are used, odd header is assumed.
138 echo date('H:i:s') , " Set header/footer" , EOL;
139 $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
140 $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
141 
142 
143 // Set page orientation and size
144 echo date('H:i:s') , " Set page orientation and size" , EOL;
145 $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
146 $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
147 
148 
149 // Rename worksheet
150 echo date('H:i:s') , " Rename worksheet" , EOL;
151 $objPHPExcel->getActiveSheet()->setTitle('Invoice');
152 
153 
154 // Set active sheet index to the first sheet, so Excel opens this as the first sheet
155 $objPHPExcel->setActiveSheetIndex(0);
156 
157 
158 // Save Excel 2007 file
159 echo date('H:i:s') , " Write to Excel2007 format" , EOL;
160 $callStartTime = microtime(true);
161 
163 $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
164 $callEndTime = microtime(true);
166 
167 echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
168 echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
169 // Save Excel5 file
170 echo date('H:i:s') , " Write to Excel5 format" , EOL;
171 $callStartTime = microtime(true);
172 
174 $objWriter->save(str_replace('.php', '.xls', __FILE__));
175 $callEndTime = microtime(true);
177 
178 echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
179 echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
180 // Echo memory usage
181 echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
182 
183 
184 // Echo memory peak usage
185 echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
186 
187 // Echo done
188 echo date('H:i:s') , " Done writing file" , EOL;
189 echo 'File has been created in ' , getcwd() , EOL;
const COLOR_YELLOW
Definition: Color.php:47
static createWriter(PHPExcel $phpExcel, $writerType='')
Create PHPExcel_Writer_IWriter.
Definition: IOFactory.php:132
if($is_dev) echo "Review changes write something in WHATSNEW and and then commit with log PHP_EOL
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())