@@ -116,7 +116,7 @@ public function writeToFile($filename)
116116 $ zip ->close ();
117117 }
118118
119- protected function initializeSheet ($ sheet_name , $ col_widths =array () )
119+ protected function initializeSheet ($ sheet_name , $ col_widths =array (), $ auto_filter = false )
120120 {
121121 //if already initialized
122122 if ($ this ->current_sheet ==$ sheet_name || isset ($ this ->sheets [$ sheet_name ]))
@@ -134,6 +134,7 @@ protected function initializeSheet($sheet_name, $col_widths=array() )
134134 'merge_cells ' => array (),
135135 'max_cell_tag_start ' => 0 ,
136136 'max_cell_tag_end ' => 0 ,
137+ 'auto_filter ' => $ auto_filter ,
137138 'finalized ' => false ,
138139 );
139140 $ sheet = &$ this ->sheets [$ sheet_name ];
@@ -203,7 +204,8 @@ public function writeSheetHeader($sheet_name, array $header_types, $col_options
203204 $ style = &$ col_options ;
204205
205206 $ col_widths = isset ($ col_options ['widths ' ]) ? (array )$ col_options ['widths ' ] : array ();
206- self ::initializeSheet ($ sheet_name , $ col_widths );
207+ $ auto_filter = isset ($ col_options ['auto_filter ' ]) ? intval ($ col_options ['auto_filter ' ]) : false ;
208+ self ::initializeSheet ($ sheet_name , $ col_widths , $ auto_filter );
207209 $ sheet = &$ this ->sheets [$ sheet_name ];
208210 $ sheet ->columns = $ this ->initializeColumnTypes ($ header_types );
209211 if (!$ suppress_row )
@@ -283,6 +285,12 @@ protected function finalizeSheet($sheet_name)
283285 $ sheet ->file_writer ->write ( '</mergeCells> ' );
284286 }
285287
288+ $ max_cell = self ::xlsCell ($ sheet ->row_count - 1 , count ($ sheet ->columns ) - 1 );
289+
290+ if ($ sheet ->auto_filter ) {
291+ $ sheet ->file_writer ->write ( '<autoFilter ref="A1: ' . $ max_cell . '"/> ' );
292+ }
293+
286294 $ sheet ->file_writer ->write ( '<printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" verticalCentered="false"/> ' );
287295 $ sheet ->file_writer ->write ( '<pageMargins left="0.5" right="0.5" top="1.0" bottom="1.0" header="0.5" footer="0.5"/> ' );
288296 $ sheet ->file_writer ->write ( '<pageSetup blackAndWhite="false" cellComments="none" copies="1" draft="false" firstPageNumber="1" fitToHeight="1" fitToWidth="1" horizontalDpi="300" orientation="portrait" pageOrder="downThenOver" paperSize="1" scale="100" useFirstPageNumber="true" usePrinterDefaults="false" verticalDpi="300"/> ' );
@@ -292,7 +300,6 @@ protected function finalizeSheet($sheet_name)
292300 $ sheet ->file_writer ->write ( '</headerFooter> ' );
293301 $ sheet ->file_writer ->write ('</worksheet> ' );
294302
295- $ max_cell = self ::xlsCell ($ sheet ->row_count - 1 , count ($ sheet ->columns ) - 1 );
296303 $ max_cell_tag = '<dimension ref="A1: ' . $ max_cell . '"/> ' ;
297304 $ padding_length = $ sheet ->max_cell_tag_end - $ sheet ->max_cell_tag_start - strlen ($ max_cell_tag );
298305 $ sheet ->file_writer ->fseek ($ sheet ->max_cell_tag_start );
@@ -635,6 +642,15 @@ protected function buildWorkbookXML()
635642 $ i ++;
636643 }
637644 $ workbook_xml .='</sheets> ' ;
645+ $ workbook_xml .='<definedNames> ' ;
646+ foreach ($ this ->sheets as $ sheet_name =>$ sheet ) {
647+ if ($ sheet ->auto_filter ) {
648+ $ sheetname = self ::sanitize_sheetname ($ sheet ->sheetname );
649+ $ workbook_xml .='<definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1"> \'' .self ::xmlspecialchars ($ sheetname ).'\'!$A$1: ' . self ::xlsCell ($ sheet ->row_count - 1 , count ($ sheet ->columns ) - 1 , true ) . '</definedName> ' ;
650+ $ i ++;
651+ }
652+ }
653+ $ workbook_xml .='</definedNames> ' ;
638654 $ workbook_xml .='<calcPr iterateCount="100" refMode="A1" iterate="false" iterateDelta="0.001"/></workbook> ' ;
639655 return $ workbook_xml ;
640656 }
@@ -678,14 +694,18 @@ protected function buildContentTypesXML()
678694 /*
679695 * @param $row_number int, zero based
680696 * @param $column_number int, zero based
681- * @return Cell label/coordinates, ex: A1, C3, AA42
697+ * @param $absolute bool
698+ * @return Cell label/coordinates, ex: A1, C3, AA42 (or if $absolute==true: $A$1, $C$3, $AA$42)
682699 * */
683- public static function xlsCell ($ row_number , $ column_number )
700+ public static function xlsCell ($ row_number , $ column_number, $ absolute = false )
684701 {
685702 $ n = $ column_number ;
686703 for ($ r = "" ; $ n >= 0 ; $ n = intval ($ n / 26 ) - 1 ) {
687704 $ r = chr ($ n %26 + 0x41 ) . $ r ;
688705 }
706+ if ($ absolute ) {
707+ return '$ ' . $ r . '$ ' . ($ row_number +1 );
708+ }
689709 return $ r . ($ row_number +1 );
690710 }
691711 //------------------------------------------------------------------
0 commit comments