ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
10autofilter-selection-display.php
Go to the documentation of this file.
1<?php
29error_reporting(E_ALL);
30ini_set('display_errors', TRUE);
31ini_set('display_startup_errors', TRUE);
32date_default_timezone_set('Europe/London');
33
34define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
35
37require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
38
39
40// Create new PHPExcel object
41echo date('H:i:s').' Create new PHPExcel object'.EOL;
43
44// Set document properties
45echo date('H:i:s').' Set document properties'.EOL;
46$objPHPExcel->getProperties()->setCreator('Maarten Balliauw')
47 ->setLastModifiedBy('Maarten Balliauw')
48 ->setTitle('PHPExcel Test Document')
49 ->setSubject('PHPExcel Test Document')
50 ->setDescription('Test document for PHPExcel, generated using PHP classes.')
51 ->setKeywords('office PHPExcel php')
52 ->setCategory('Test result file');
53
54// Create the worksheet
55echo date('H:i:s').' Add data'.EOL;
56$objPHPExcel->setActiveSheetIndex(0);
57$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Financial Year')
58 ->setCellValue('B1', 'Financial Period')
59 ->setCellValue('C1', 'Country')
60 ->setCellValue('D1', 'Date')
61 ->setCellValue('E1', 'Sales Value')
62 ->setCellValue('F1', 'Expenditure')
63 ;
64$startYear = $endYear = $currentYear = date('Y');
67
69$periods = range(1,12);
70$countries = array( 'United States', 'UK', 'France', 'Germany',
71 'Italy', 'Spain', 'Portugal', 'Japan'
72 );
73
74$row = 2;
75foreach($years as $year) {
76 foreach($periods as $period) {
77 foreach($countries as $country) {
78 $endDays = date('t',mktime(0,0,0,$period,1,$year));
79 for($i = 1; $i <= $endDays; ++$i) {
81 $year,
82 $period,
83 $i
84 );
85 $value = rand(500,1000) * (1 + rand(-0.25,+0.25));
86 $salesValue = $invoiceValue = NULL;
87 $incomeOrExpenditure = rand(-1,1);
88 if ($incomeOrExpenditure == -1) {
89 $expenditure = rand(-500,-1000) * (1 + rand(-0.25,+0.25));
90 $income = NULL;
91 } elseif ($incomeOrExpenditure == 1) {
92 $expenditure = rand(-500,-1000) * (1 + rand(-0.25,+0.25));
93 $income = rand(500,1000) * (1 + rand(-0.25,+0.25));;
94 } else {
95 $expenditure = NULL;
96 $income = rand(500,1000) * (1 + rand(-0.25,+0.25));;
97 }
98 $dataArray = array( $year,
99 $period,
100 $country,
101 $eDate,
102 $income,
103 $expenditure,
104 );
105 $objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A'.$row++);
106 }
107 }
108 }
109}
110$row--;
111
112
113// Set styling
114echo date('H:i:s').' Set styling'.EOL;
115$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(true);
116$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setWrapText(TRUE);
117$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(12.5);
118$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10.5);
119$objPHPExcel->getActiveSheet()->getStyle('D2:D'.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
120$objPHPExcel->getActiveSheet()->getStyle('E2:F'.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
121$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(14);
122$objPHPExcel->getActiveSheet()->freezePane('A2');
123
124
125
126// Set autofilter range
127echo date('H:i:s').' Set autofilter range'.EOL;
128// Always include the complete filter range!
129// Excel does support setting only the caption
130// row, but that's not a best practise...
131$objPHPExcel->getActiveSheet()->setAutoFilter($objPHPExcel->getActiveSheet()->calculateWorksheetDimension());
132
133// Set active filters
134$autoFilter = $objPHPExcel->getActiveSheet()->getAutoFilter();
135echo date('H:i:s').' Set active filters'.EOL;
136// Filter the Country column on a filter value of countries beginning with the letter U (or Japan)
137// We use * as a wildcard, so specify as U* and using a wildcard requires customFilter
138$autoFilter->getColumn('C')
140 ->createRule()
141 ->setRule(
143 'u*'
144 )
146$autoFilter->getColumn('C')
147 ->createRule()
148 ->setRule(
150 'japan'
151 )
153// Filter the Date column on a filter value of the first day of every period of the current year
154// We us a dateGroup ruletype for this, although it is still a standard filter
155foreach($periods as $period) {
156 $endDate = date('t',mktime(0,0,0,$period,1,$currentYear));
157
158 $autoFilter->getColumn('D')
160 ->createRule()
161 ->setRule(
163 array(
164 'year' => $currentYear,
165 'month' => $period,
166 'day' => $endDate
167 )
168 )
170}
171// Display only sales values that are blank
172// Standard filter, operator equals, and value of NULL
173$autoFilter->getColumn('E')
175 ->createRule()
176 ->setRule(
178 ''
179 );
180
181// Execute filtering
182echo date('H:i:s').' Execute filtering'.EOL;
183$autoFilter->showHideRows();
184
185// Set active sheet index to the first sheet, so Excel opens this as the first sheet
186$objPHPExcel->setActiveSheetIndex(0);
187
188
189// Display Results of filtering
190echo date('H:i:s').' Display filtered rows'.EOL;
191foreach ($objPHPExcel->getActiveSheet()->getRowIterator() as $row) {
192 if ($objPHPExcel->getActiveSheet()->getRowDimension($row->getRowIndex())->getVisible()) {
193 echo ' Row number - ' , $row->getRowIndex() , ' ';
194 echo $objPHPExcel->getActiveSheet()->getCell('C'.$row->getRowIndex())->getValue(), ' ';
195 echo $objPHPExcel->getActiveSheet()->getCell('D'.$row->getRowIndex())->getFormattedValue(), ' ';
196 echo EOL;
197 }
198}
$objPHPExcel
Include PHPExcel.
$dataArray
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
An exception for terminatinating execution or to throw for unit testing.
static FormattedPHPToExcel($year, $month, $day, $hours=0, $minutes=0, $seconds=0)
FormattedPHPToExcel.
Definition: Date.php:215
$i
Definition: disco.tpl.php:19