Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable AutoFilter for top row in Excel sheet?

Here is my little test application that produces an Excel 2007 XLSX file:

uses
  Excel2007;

function CreateExportExcelWorkbook(AApp: ExcelApplication; ALCID: Integer): ExcelWorkbook;
var
  OldDefaultSaveFormat: XlFileFormat;
begin
  OldDefaultSaveFormat := AApp.DefaultSaveFormat;
  AApp.DefaultSaveFormat := xlOpenXMLWorkbook;
  try
    Result := AApp.Workbooks.Add(xlWBATWorksheet, ALCID);
  finally
    AApp.DefaultSaveFormat := OldDefaultSaveFormat;
  end;
end;

procedure FixTopRows(AApp: ExcelApplication; ARowCount: Integer);
var
  ActiveWindow: Window;
begin
  ActiveWindow := AApp.ActiveWindow;
  ActiveWindow.SplitColumn := 0;
  ActiveWindow.SplitRow := ARowCount;
  ActiveWindow.FreezePanes := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  cRowCount = 200;
  cColCount = 10;
var
  LCID: Integer;
  ExcelApp: ExcelApplication;
  Workbook: ExcelWorkbook;
  Worksheet: ExcelWorksheet;
  i, j: Integer;
  FVarArray: Variant;
  Cell1, Range: ExcelRange;
begin
  LCID := GetUserDefaultLCID;

  ExcelApp := CoExcelApplication.Create;
  try
    ExcelApp.Visible[LCID] := False;
    ExcelApp.UserControl := False;
    ExcelApp.DisplayAlerts[LCID] := False;

    Workbook := CreateExportExcelWorkbook(ExcelApp, LCID);
    Worksheet := Workbook.Worksheets.Item[1] as ExcelWorksheet;

    FVarArray := VarArrayCreate([0, cRowCount - 1, 0, cColCount - 1], varVariant);

    for j := 0 to cColCount - 1 do
      FVarArray[0, j] := Format('Column %d', [j]);


    for i := 1 to cRowCount - 1 do
      for j := 0 to cColCount - 1 do
        FVarArray[i, j] := 100 * i + j;

    Cell1 := Worksheet.Cells.Range['A1', 'A1'];
    Range := Worksheet.Range[Cell1, Cell1.Offset[cRowCount - 1, cColCount - 1]];
    Range.Value[EmptyParam] := FVarArray;
    VarClear(FVarArray);
    Range.EntireColumn.AutoFit;

    FixTopRows(ExcelApp, 1);

    Range := Worksheet.Range[Cell1, Cell1.Offset[0, cColCount - 1]];
    //Range := Cell1.EntireRow;
    //Range.AutoFilter(1, 'All', EmptyParam, EmptyParam, True);

    Workbook.SaveAs(ExpandFileName('test.xlsx'), EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
      xlNoChange, EmptyParam, False, EmptyParam, EmptyParam, EmptyParam, LCID);
  finally
    ExcelApp.Quit;
  end;
end;

Unit Excel2007 is the imported Excel 2007 type library. This works fine so far. However I would like to make the header row auto-filtered like in this screenshot:screenshot of AutoFilter Unfortunately all my attempts to do this via OLE automation resulted in OLE or Variant exceptions. Any idea how I should proceed?

NB: The data shouldn't be filtered - I just want the drop-down buttons.

like image 675
Uli Gerhardt Avatar asked Oct 21 '25 05:10

Uli Gerhardt


1 Answers

You need to specify the operator, which you failed to do. For example:

Worksheet.Cells.AutoFilter(1, EmptyParam, xlAnd, EmptyParam, True);

Note that the Criteria1 parameter is optional, can be omitted, and defaults to 'All'.

like image 134
David Heffernan Avatar answered Oct 23 '25 19:10

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!