Datafiltrering

Autofilterdata

Autofiltrering är det snabbaste sättet att välja endast de poster från arbetsbladet som du vill visa i en lista. Autofilterfunktionen gör det möjligt för användare att filtrera poster i en lista enligt ett angivet kriterium. Filtrera baserat på text, nummer eller datum.

Autofilter i Microsoft Excel

För att aktivera autofilterfunktionen i Microsoft Excel:

  1. Klicka på den rubrikraden i en arbetsbok.
  2. Från menyn Data, välj Filter och sedan AutoFilter.

När du tillämpar ett autofilter på en arbetsbok visas filteromkopplare (svarta pilar) till höger om kolumnrubrikerna.

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

Några av autofilteralternativen är:

Alternativ Beskrivning
All Visa alla poster i listan en gång.
Custom Anpassa filterkriterier som innehåller/inte innehåller
Filter by Color Filter baserat på fyllningsfärg
Date Filters Filtrera rader baserat på olika kriterier på datum
Number Filters Olika typer av filter på nummer som jämförelse, medeltal och Topp 10 etc.
Text Filters Olika filter som börjar med, slutar med, innehåller osv.
Blanks/Non Blanks Dessa filter kan implementeras genom textfilter Tom

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

Autofilter med Aspose.Cells

Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Excel-fil. Workbook-klassen innehåller en Worksheets-samling som ger åtkomst till varje arbetsbok i Excel-filen.

En arbetsbok representeras av klassen Worksheet. Worksheet-klassen tillhandahåller ett brett utbud av egenskaper och metoder för att hantera arbetsböcker. För att skapa ett autofilter, använd AutoFilter-egenskapen i Worksheet-klassen. AutoFilter-egenskapen är en instans av klassen AutoFilter, som ger Range-egenskapen för att ange den rad med celler som utgör en rubrikrad. Ett autofilter appliceras på cellområdet som är rubrikraden.

I varje arbetsbok kan du endast ange ett filterområde. Detta är begränsat av Microsoft Excel. För anpassad datafiltrering, använd AutoFilter.Custom-metoden.

I det angivna 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 tillhandahåller flera alternativ för att använda olika typer av filter som färgfilter, datumfilter, nummerfilter, textfilter, blanka filter och icke-blanka filter.

Fyllfärg

Aspose.Cells tillhandahåller en funktion AddFillColorFilter för att filtrera data baserat på fyllfärgsegenskapen hos cellerna. I det angivna exemplet nedan används en mallfil med olika fyllfärger i den första kolumnen i 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 datumsfilter kan implementeras för att filtrera alla rader med datum i januari 2018. Följande exempelkod demonstrerar detta filter using AddDateFilter-funktionen. Exempelfiler ges nedan.

  1. Date.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 alla celler med datum i januari oavsett år. I detta fall används DynamicFilter-funktionen enligt det följande exempelkodet. Exempelfiler ges nedan.

  1. Date.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");
Nummer

Anpassade filter kan appliceras med Aspose.Cells som att välja celler med nummer mellan ett givet intervall. Följande exempel demonstrerar användningen av Custom-funktionen för att filtrera nummer. Exempelfiler ges nedan.

  1. Number.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 särskild text, kan Filter-funktionen användas. I följande exempel, innehåller mallfilen en lista över länder och en rad ska väljas som innehåller ett visst landsnamn. Följande kod demonstrerar 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");
Tomma

Om en kolumn innehåller text så att några celler är tomma och det krävs ett filter för att välja de rader där tomma celler finns, kan MatchBlanks-funktionen användas enligt nedan demonstrerat. Exempelfiler ges nedan.

  1. Tomma.xlsx
  2. FiltreradeTomma.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");
Ej tomma

När celler med text ska filtreras, använd MatchNonBlanks filterfunktion enligt nedan. Exempelfiler ges nedan.

  1. Tomma.xlsx
  2. FiltreradeEjTomma.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 erbjuder anpassade filter som filtrerar rader som innehåller en specifik sträng. Denna funktion är tillgänglig i Aspose.Cells och demonstreras nedan genom att filtrera namnen i provfilen. 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 EjInnehåller

Excel tillhandahåller anpassade filter för att filtrera rader som inte innehåller en specifik sträng. Denna funktion finns 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 BörjaMed

Excel tillhandahåller anpassade filter för att filtrera rader som börjar med en specifik sträng. Denna funktion finns 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 SlutaMed

Excel erbjuder anpassade filter som filtrerar rader som slutar med en specifik sträng. Denna funktion är tillgänglig i Aspose.Cells och demonstreras nedan genom att filtrera namnen i provfilen 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");

Fortsatta ämnen