gpt4 book ai didi

excel - 如何为 Excel 工作表中的顶行启用自动筛选?

转载 作者:行者123 更新时间:2023-12-03 02:52:46 24 4
gpt4 key购买 nike

这是我的小测试应用程序,它生成 Excel 2007 XLSX 文件:

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;

单元Excel2007是导入的Excel 2007类型库。到目前为止效果很好。不过,我想让标题行自动过滤,如下屏幕截图所示:screenshot of AutoFilter不幸的是,我通过 OLE 自动化执行此操作的所有尝试都导致了 OLE 或变体异常。知道我应该如何进行吗?

注意:不应过滤数据 - 我只想要下拉按钮。

最佳答案

您需要指定运算符(operator),但您未能执行此操作。例如:

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

请注意,Criteria1 参数是可选的,可以省略,默认为'All'

关于excel - 如何为 Excel 工作表中的顶行启用自动筛选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18589760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com