Hi @Alvord, Timothy , Welcome to Microsoft Q&A.
You can refer to my code below:
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace _8_xx_xx
{
internal class Program
{
// Helper method to release COM objects.
private static void ReleaseObject(object obj)
{
try
{
if (obj != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
}
catch (Exception ex)
{
obj = null;
Console.WriteLine($"Error releasing COM object: {ex.Message}");
}
finally
{
GC.Collect();
}
}
static void Main(string[] args)
{
//Your own file path
string filePath = @"C:\Users\Administrator\Desktop\xxx.xlsx";
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = null;
Excel.Worksheet worksheet = null;
try
{
workbook = excelApp.Workbooks.Open(filePath);
worksheet = workbook.Worksheets[1]; // Assuming you want to work with the first sheet.
worksheet.AutoFilterMode = false;
//How to open the filter of all columns of the worksheet
worksheet.Range["A1", "D1"].AutoFilter(1, "<>0", Excel.XlAutoFilterOperator.xlAnd, "<>0", true); ;
// Save the changes (applying the AutoFilter).
workbook.Save();
}
catch (Exception ex)
{
// Handle the exception or print the error message.
Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
// Clean up.
if (workbook != null)
{
workbook.Close();
}
excelApp.Quit();
ReleaseObject(worksheet);
ReleaseObject(workbook);
ReleaseObject(excelApp);
}
//Just to keep the console open.
System.Console.ReadLine();
}
}
}
Choose your own rules for your screening.
Mine is to filter the data in the range "A1" to "D1" of the worksheet according to the condition that the value of the first column (column A) is not equal to 0, and apply the filter results to the entire worksheet.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.