首页 > 解决方案 > PHP Com Excel 应用过滤器

问题描述

我正在用 com 打开 excel,它工作正常,但是我无法同时将两个过滤器应用于同一列。这是代码:

$excel = new COM("excel.application") or die("Unable to instanciate excel");
$excel->Visible = 1;
$excel->DisplayAlerts = 1;

$wb = $excel->Workbooks->Open($dataFile);
$sheet = $wb->Worksheets(1);

// apply filters
$sheet->range("AS1")->AutoFilter(45, '<>'); // works with single filter

但是,当我想同时将两个过滤器应用于同一列时,它不起作用:

$sheet->range("AS1")->AutoFilter(45, '<> AND > 0'); // DOES NOT WORK

我的猜测是AND,我应该使用实际的 excel 常量,而不是在上面的语句中使用文字字符串,Excel.XlAutoFilterOperator.xlAnd但我无法得到它。

任何帮助将不胜感激。

标签: phpexcelwebms-office

解决方案


好吧想通了,我必须传递额外的参数:

define('xlAnd', 1);

$sheet->range("AS1")->AutoFilter(45, '<>', xlAnd, '> 0');

推荐阅读