我希望标题中的每个单元格都包含一个自动过滤器。下面是我尝试使用的代码,但autofilter
仅在指定的最后一个单元格上设置。
例如,如果我为autofilter
注释掉K1
命令,则会创建电子表格,其中C1
是唯一具有自动过滤器的单元格。
//Headers
ws.Cells["A1"].Value = "ChannelCode";
ws.Cells["A1"].AutoFilter = true;
ws.Cells["B1"].Value = "DrmTerrDesc";
ws.Cells["B1"].AutoFilter = true;
ws.Cells["C1"].Value = "IndDistrnId";
ws.Cells["C1"].AutoFilter = true;
ws.Cells["D1"].Value = "StateCode";
ws.Cells["D1"].AutoFilter = true;
ws.Cells["E1"].Value = "ZipCode";
ws.Cells["E1"].AutoFilter = true;
ws.Cells["F1"].Value = "EndDate";
ws.Cells["F1"].AutoFilter = true;
ws.Cells["G1"].Value = "EffectiveDate";
ws.Cells["G1"].AutoFilter = true;
ws.Cells["H1"].Value = "LastUpdateId";
ws.Cells["H1"].AutoFilter = true;
ws.Cells["I1"].Value = "ErrorCodes";
ws.Cells["I1"].AutoFilter = true;
ws.Cells["J1"].Value = "Status";
ws.Cells["J1"].AutoFilter = true;
ws.Cells["K1"].Value = "Id";
ws.Cells["K1"].AutoFilter = true;
答案 0 :(得分:26)
EPPlus .AutoFilter
有点儿错误...我建议使用这样的范围来做:
ws.Cells["A1:K1"].AutoFilter = true;
答案 1 :(得分:16)
适用于所有工作表数据范围
worksheet.Cells[worksheet.Dimension.Address].AutoFilter=true;
答案 2 :(得分:4)
如果您有一组动态列,或者它们是在数组或列表中定义的:
string[] columns = new string[] { "Name", "City", "Street" };
worksheet.Cells[1, 1, 1, columns.Length].AutoFilter = true;
使用的重载如下:
public ExcelRange this[int FromRow, int FromCol, int ToRow, int ToCol] { get; }
我个人更喜欢这个而不是将整数(columns.Length)转换为带有字符的Excel范围(例如" A1:C1")。
答案 3 :(得分:-1)
此代码应该有所帮助。我试过并测试了它。
ws.AutoFilterAddress = new ExcelAddressBase(ws.Dimension.Address);