Gegevens in Excel-tabel filteren met C#

In dit onderwerp vindt u de vereiste informatie over het filteren van gegevens in de Excel-tabel met C#. De voorbeeldcode laat zien hoe u filters in Excel kunt toevoegen met C# in het XLSX-bestand. Bovendien kunt u in een paar eenvoudige stappen een dynamisch filter maken in een Excel-spreadsheet met C#.

Stappen om gegevens in Excel-tabel te filteren met C#

  1. Verkrijg Aspose.Cells for .NET pakket van NuGet.org
  2. Importeer Aspose.Cells naamruimte naar het project
  3. Instantieer en stel Licentie-object in met de SetLicense-methode
  4. Maak een Workbook helemaal opnieuw
  5. Gegevens invoegen in het eerste werkblad in de werkmap
  6. Stel AutoFilter bereik in en voeg aangepaste filtering toe
  7. Bewaar gefilterde gegevens in XLSX-bestand

Gegevens dynamisch filteren betekent dat alleen die records worden weergegeven die aan bepaalde criteria voldoen. Als u alleen records voor specifiek fruit (druiven in dit geval) wilt krijgen, demonstreert dit voorbeeld de autofilterfunctie van Excel en helpt u te begrijpen hoe u een aangepast filter in Excel in C# kunt toepassen.

Voorbeeldcode om gegevens in Excel-tabel te filteren met C#

using System;
using Aspose.Cells;
namespace FilterDataInExcelTable
{
class Program
{
static void Main(string[] args)
{
//Create License object and set it at the start before using any other APIs
Aspose.Cells.License Aspose_Cells_lic = new Aspose.Cells.License();
Aspose_Cells_lic.SetLicense("Aspose.Cells.lic");
//Create an empty Excel workbook
Workbook FilteringDataWorkbook = new Workbook();
//Get the worksheet at first indexed position in the workbook - default worksheet
Worksheet FilteringDataSheet = FilteringDataWorkbook.Worksheets[0];
//Obtain the cells collection from the first sheet
Cells FilteringDataCells = FilteringDataSheet.Cells;
//Put data/values into the cells for the table
FilteringDataCells["A1"].PutValue("Fruits");
FilteringDataCells["B1"].PutValue("Total");
FilteringDataCells["A2"].PutValue("Blueberries");
FilteringDataCells["B2"].PutValue(2500);
FilteringDataCells["A3"].PutValue("Apples");
FilteringDataCells["B3"].PutValue(1100);
FilteringDataCells["A4"].PutValue("Mangoes");
FilteringDataCells["B4"].PutValue(1500);
FilteringDataCells["A5"].PutValue("Grapes");
FilteringDataCells["B5"].PutValue(1200);
FilteringDataCells["A6"].PutValue("Oranges");
FilteringDataCells["B6"].PutValue(3000);
FilteringDataCells["D1"].PutValue("Count:");
//Specify formula to E1 cell - this formula would give you count
FilteringDataCells["E1"].Formula = "=SUBTOTAL(2,B1:B6)";
//Set the range to which the specified autofilters would be applied
FilteringDataSheet.AutoFilter.Range = "A1:B6";
//Now add your desired filter to first column to select your desired data
FilteringDataSheet.AutoFilter.AddFilter(0, "Grapes");
FilteringDataSheet.AutoFilter.Refresh();
//Save Excel XLSX file
FilteringDataWorkbook.Save("FilteredData.xlsx");
}
}
}

Het codesegment (hierboven) maakt een lege werkmap aan en voegt vervolgens gegevens in een celbereik in het eerste werkblad in. De C#-code stelt het Excel-autofilterbereik in; het voegt een aangepast filter toe of past het toe in Excel-spreadsheet. Ten slotte slaat het gefilterde gegevens op in het XLSX-bestand op de schijf. Mogelijk bent u ook geïnteresseerd in het onderwerp hoe maak je een Excel-cirkeldiagram in C#.

 Nederlands