gpt4 book ai didi

c# - 在列上添加特定的自动过滤器

转载 作者:太空狗 更新时间:2023-10-29 17:49:17 27 4
gpt4 key购买 nike

我正在尝试对列设置过滤器。这是我在 Interop 中的做法:

private void CheckMasterFile(string path) {
var xlApp = new Excel.Application();
var xlWorkbook = xlApp.Workbooks.Open(path);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];

foreach (var project in projects) {
if (string.IsNullOrEmpty(project.ProjectID.Value)) {
continue;
}

var xlRange = xlWorksheet.UsedRange;
if (xlWorksheet.AutoFilter != null) {
xlWorksheet.AutoFilterMode = false;
}
xlRange.AutoFilter(Field: 2, Criteria1: project.ProjectID.Value);
var result = xlRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);//only shows filtered values
double sum = 0;

foreach (Excel.Range row in result.Rows) {
if (row.Cells[2, 2].Value2() != null) {
if (!NOT_ALLOWED_RUBRIQUES.Contains((string)row.Cells[2, 8].Value2())) {//check if rubrique is allowed or not
//finish method
}
}
}
}
xlWorkbook.Close(SaveChanges: false);
xlApp.Quit();
}

但是我可以在 EPPlus 中使用自动过滤器做一些事情的唯一方法是启用或禁用它,而不是根据特定值过滤列。像这样:

sheet.Cells["A RANGE HERE"].AutoFilter = true;

所以我在这里想要实现的是根据特定值过滤一个巨大的 Excel 文件,然后只显示具有这些值的行。

最佳答案

如果我明白你在问什么,那不是 EPPlus 的意图。它旨在生成 excel 文件作为端点。您正在尝试做的事情似乎实际上是在使用 Excel 作为您的分析工具。

如果您真的想对输出列应用过滤器,那么您不必手动进行,因为 EPPlus 本身并不应用过滤器。所以像这样的事情(必须自己处理)。但是您(生成器)仍有责任实际执行分析 - 在这种情况下最后一个 linq 查询:

[TestMethod]
public void AutoFilter_Test()
{
//http://stackoverflow.com/questions/32723483/adding-a-specific-autofilter-on-a-column

//Throw in some data
var datatable = new DataTable("tblData");
datatable.Columns.AddRange(new[] { new DataColumn("Col1", typeof(int)), new DataColumn("Col2", typeof(int)), new DataColumn("Col3", typeof(object)) });

for (var i = 0; i < 10; i++)
{
var row = datatable.NewRow(); row[0] = i; row[1] = i * 10;row[2] = Path.GetRandomFileName();
datatable.Rows.Add(row);
}

//Create a test file
var fi = new FileInfo(@"c:\temp\autofilter.xlsx");
if (fi.Exists)
fi.Delete();

using (var pck = new ExcelPackage(fi))
{
var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells.LoadFromDataTable(datatable, true);

var range = worksheet.Cells["A1:C10"];
range.AutoFilter = true;

pck.Save();
}

//Needed prior save in order for the XML to be generated
using (var pck = new ExcelPackage(fi))
{
var worksheet = pck.Workbook.Worksheets.First();

//Get reference to the worksheet xml for proper namespace
var xdoc = worksheet.WorksheetXml;
var nsm = new XmlNamespaceManager(xdoc.NameTable);
nsm.AddNamespace("default", xdoc.DocumentElement.NamespaceURI);

//Create the filters themselves
var filter1 = xdoc.CreateNode(XmlNodeType.Element, "filter", xdoc.DocumentElement.NamespaceURI);
var att = xdoc.CreateAttribute("val");
att.Value = "40";
filter1.Attributes.Append(att);

var filter2 = xdoc.CreateNode(XmlNodeType.Element, "filter", xdoc.DocumentElement.NamespaceURI);
att = xdoc.CreateAttribute("val");
att.Value = "50";
filter2.Attributes.Append(att);

//Add filters to the collection
var filters = xdoc.CreateNode(XmlNodeType.Element, "filters", xdoc.DocumentElement.NamespaceURI);
filters.AppendChild(filter1);
filters.AppendChild(filter2);

//Create the parent filter container
var filterColumn = xdoc.CreateNode(XmlNodeType.Element, "filterColumn", xdoc.DocumentElement.NamespaceURI);
att = xdoc.CreateAttribute("colId");
att.Value = "1";
filterColumn.Attributes.Append(att);
filterColumn.AppendChild(filters);

//Now add it to the autoFilters node
var autoFilter = xdoc.SelectSingleNode("/default:worksheet/default:autoFilter", nsm);
autoFilter.AppendChild(filterColumn);

//Have to manually hide rows based on criteria
worksheet.Cells
.Where(cell =>
cell.Address.StartsWith("B")
&& cell.Value is double
&& (double) cell.Value != 40d
&& (double) cell.Value != 50d)
.Select(cell => cell.Start.Row)
.ToList()
.ForEach(r => worksheet.Row(r).Hidden = true);

pck.Save();
}
}

输出

enter image description here

关于c# - 在列上添加特定的自动过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32723483/

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