Skip to content

Commit 13d65fe

Browse files
authored
Merge pull request mk-j#177 from andrewkesper/auto-filter
AutoFilter support
2 parents 8b01648 + 97e4026 commit 13d65fe

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

examples/ex09-autofilter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
set_include_path( get_include_path().PATH_SEPARATOR."..");
3+
include_once("xlsxwriter.class.php");
4+
5+
$chars = 'abcdefgh';
6+
7+
$writer = new XLSXWriter();
8+
$writer->writeSheetHeader('Sheet1', array('col-string'=>'string','col-numbers'=>'integer','col-timestamps'=>'datetime'), ['auto_filter'=>true, 'widths'=>[15,15,30]] );
9+
for($i=0; $i<1000; $i++)
10+
{
11+
$writer->writeSheetRow('Sheet1', array(
12+
str_shuffle($chars),
13+
rand()%10000,
14+
date('Y-m-d H:i:s',time()-(rand()%31536000))
15+
));
16+
}
17+
$writer->writeToFile('xlsx-autofilter.xlsx');
18+
echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";

xlsxwriter.class.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)