Datafiltrering

Autofiltrera data

Autofiltrering är det snabbaste sättet att bara välja de objekt från kalkylbladet som du vill visa i en lista. Autofiltreringsfunktionen tillåter användare att filtrera objekt i en lista enligt ett bestämt kriterium. Filtrera baserat på text, siffror eller datum.

Autofilter i Microsoft Excel

Så här aktiverar du autofilterfunktionen i Microsoft Excel:

  1. Klicka på rubrikraden i ett kalkylblad.
  2. FrånData menyn, väljFiltrera och dåAutoFilter.

När du använder ett autofilter på ett kalkylblad visas filteromkopplare (svarta pilar) till höger om kolumnrubrikerna.

  1. Klicka på en filterpil för att se en lista med filteralternativ.

Några av autofilteralternativen är:

alternativ Beskrivning
Allt Visa alla objekt i listan en gång.
Beställnings Anpassa filterkriterier som innehåller/innehåller inte
Filtrera efter färg Filter baserat på fylld färg
Datumfilter Filtrerar rader baserat på olika kriterier på datum
Nummerfilter Olika typer av filter på siffror som jämförelse, medelvärden och Top 10 etc.
Textfilter Olika filter som börjar med, slutar med, innehåller etc,
Blanks/Non Blanks Dessa filter kan implementeras genom Text Filter Blank

Användare filtrerar sina kalkylbladsdata manuellt i Microsoft Excel med dessa alternativ.

Autofilter med Aspose.Cells

Aspose.Cells tillhandahåller en klass, arbetsbok som representerar en Excel-fil. Klassen Workbook innehåller en kalkylbladssamling som ger åtkomst till varje kalkylblad i Excel-filen.

Ett kalkylblad representeras av klassen Worksheet. Klassen Worksheet tillhandahåller ett brett utbud av egenskaper och metoder för att hantera kalkylblad. För att skapa ett autofilter, använd egenskapen AutoFilter för klassen Worksheet. AutoFilter-egenskapen är ett objekt i klassen AutoFilter, som tillhandahåller Range-egenskapen för att ange intervallet av celler som utgör en rubrikrad. Ett autofilter tillämpas på cellintervallet som är rubrikraden.

I varje kalkylblad kan du bara ange ett filterintervall. Detta begränsas av Microsoft Excel. För anpassad datafiltrering, använd metoden AutoFilter.Custom.

I exemplet nedan har vi skapat samma autofilter med Aspose.Cells som vi skapade med Microsoft Excel i avsnittet ovan.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range of the heading row
worksheet.AutoFilter.Range = "A1:B1";
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");

Olika typer av filter

Aspose.Cells ger flera alternativ för att använda olika typer av filter som färgfilter, datumfilter, nummerfilter, textfilter, tomma filter och inga tomma filter.

Fyllnadsfärg

Aspose.Cells tillhandahåller en funktion AddFillColorFilter för att filtrera data baserat på fyllningsfärgsegenskapen för cellerna. I exemplet nedan används en mallfil med olika fyllningsfärger i den första kolumnen på arket för att testa färgfiltreringsfunktionen. Exempelfiler kan laddas ner från följande länkar.

  1. ColouredCells.xlsx
  2. FilteredColouredCells.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "ColouredCells.xlsx");
// Instantiating a CellsColor object for foreground color
CellsColor clrForeground = workbook.CreateCellsColor();
clrForeground.Color = Color.Red;
// Instantiating a CellsColor object for background color
CellsColor clrBackground = workbook.CreateCellsColor();
clrBackground.Color = Color.White;
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call AddFillColorFilter function to apply the filter
worksheet.AutoFilter.AddFillColorFilter(0, BackgroundType.Solid, clrForeground, clrBackground);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredColouredCells.xlsx");
Datum

Olika typer av datumfilter kan implementeras som att filtrera alla rader med datum i januari 2018. Följande exempelkod visar detta filter med AddDateFilter-funktionen. Exempelfiler ges nedan.

  1. Datum.xlsx
  2. FilteredDate.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call AddDateFilter function to apply the filter
worksheet.AutoFilter.AddDateFilter(0, DateTimeGroupingType.Month, 2018, 1, 0, 0, 0, 0);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredDate.xlsx");
Dynamiskt datum

Ibland krävs dynamiska filter baserat på datum som att alla celler har datum i januari, oavsett år. I det här fallet används DynamicFilter-funktionen som anges i följande exempelkod. Exempelfiler ges nedan.

  1. Datum.xlsx
  2. FilteredDynamicDate.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call DynamicFilter function to apply the filter
worksheet.AutoFilter.DynamicFilter(0, DynamicFilterType.January);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredDynamicDate.xlsx");
siffra

Anpassade filter kan tillämpas med Aspose.Cells som att markera celler med nummer mellan ett givet intervall. Följande exempel visar användningen av Custom()-funktionen för att filtrera siffror. Exempelfiler ges nedan.

  1. Antal.xlsx
  2. FilteredNumber.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Number.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call Custom function to apply the filter
worksheet.AutoFilter.Custom(0, FilterOperatorType.GreaterOrEqual, 5, true, FilterOperatorType.LessOrEqual, 10);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredNumber.xlsx");
Text

Om en kolumn innehåller text och celler ska väljas som innehåller viss text, kan Filter()-funktionen användas. I följande exempel innehåller mallfilen en lista över länder och en rad ska väljas med ett visst landsnamn. Följande kod visar filtrering av text. Exempelfiler ges nedan.

  1. Text.xlsx
  2. FilteredText.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Text.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call Filter function to apply the filter
worksheet.AutoFilter.Filter(0, "Angola");
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredText.xlsx");
Blanks

Om en kolumn innehåller text så att få celler är tomma, och filter krävs för att välja de rader endast där tomma celler finns, kan MatchBlanks()-funktionen användas som visas nedan. Exempelfiler ges nedan.

  1. Blank.xlsx
  2. FilteredBlank.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Blank.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call MatchBlanks function to apply the filter
worksheet.AutoFilter.MatchBlanks(0);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredBlank.xlsx");
Icke Blanks

När celler som har någon text ska filtreras, använd MatchNonBlanks filterfunktion som visas nedan. Exempelfiler ges nedan.

  1. Blank.xlsx
  2. FilteredNonBlank.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(sourceDir + "Blank.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call MatchNonBlanks function to apply the filter
worksheet.AutoFilter.MatchNonBlanks(0);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "FilteredNonBlank.xlsx");
Anpassat filter med Innehåller

Excel tillhandahåller anpassade filter som filterrader som innehåller en viss sträng. Den här funktionen är tillgänglig i Aspose.Cells och visas nedan genom att filtrera namnen i exempelfilen. Exempelfiler ges nedan.

  1. sourseSampleCountryNames.xlsx
  2. outSourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows containing string "Ba"
worksheet.AutoFilter.Custom(0, FilterOperatorType.Contains, "Ba");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save("outSourseSampleCountryNames.xlsx");
Anpassat filter med NotContains

Excel tillhandahåller anpassade filter som filterrader som inte innehåller någon specifik sträng. Den här funktionen är tillgänglig i Aspose.Cells och visas nedan genom att filtrera namnen i exempelfilen nedan.

  1. sourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows containing string "Ba"
worksheet.AutoFilter.Custom(0, FilterOperatorType.NotContains, "Be");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save("outSourseSampleCountryNames.xlsx");
Anpassat filter med BeginsWith

Excel tillhandahåller anpassade filter som filterrader som börjar med en viss sträng. Den här funktionen är tillgänglig i Aspose.Cells och visas nedan genom att filtrera namnen i exempelfilen nedan.

  1. sourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook(sourceDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows starting with string "Ba"
worksheet.AutoFilter.Custom(0, FilterOperatorType.BeginsWith, "Ba");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "outSourseSampleCountryNames.xlsx");
Anpassat filter med EndsWith

Excel tillhandahåller anpassade filter som filterrader som slutar med någon specifik sträng. Den här funktionen är tillgänglig i Aspose.Cells och visas nedan genom att filtrera namnen i exempelfilen nedan.

  1. sourseSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook(sourceDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows end with string "ia"
worksheet.AutoFilter.Custom(0, FilterOperatorType.BeginsWith, "ia");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(outputDir + "outSourseSampleCountryNames.xlsx");

Förhandsämnen