[c#] Open File Dialog, One Filter for Multiple Excel Extensions?

I want to use an OpenFileDialog object to browse to an excel file. I would like to set the filter to open files with different types of excel extensions like: .xls, .xlsm, .xlsx and so on.

what I am using is this:

OpenFileDialog of = new OpenFileDialog();
of.Filter = "Excel Files(.xls)|*.xls| 
    Excel Files(.xlsx)|*.xlsx| Excel Files(*.xlsm)|*.xlsm";

This works, but the user must select the correct excel file type from the dropdown in the OpenFileDialog.

Does anyone know if there a way to apply one filter for all types of Excel extensions?

Something like: "...Excel Files (.xls, .xlsx, .xlxm)|*.xls, *.xlsx, *.xlsm;"

Thanks in advance for any replies.

This question is related to c# winforms openfiledialog

The answer is


Use a semicolon

OpenFileDialog of = new OpenFileDialog();
of.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";

If you want to merge the filters (eg. CSV and Excel files), use this formula:

OpenFileDialog of = new OpenFileDialog();
of.Filter = "CSV files (*.csv)|*.csv|Excel Files|*.xls;*.xlsx";

Or if you want to see XML or PDF files in one time use this:

of.Filter = @" XML or PDF |*.xml;*.pdf";

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to openfiledialog

Open a workbook using FileDialog and manipulate it in Excel VBA How to get file path from OpenFileDialog and FolderBrowserDialog? Open File Dialog, One Filter for Multiple Excel Extensions? Reading a text file using OpenFileDialog in windows forms How to use OpenFileDialog to select a folder? Open file dialog and select a file using WPF controls and C# Quick and easy file dialog in Python? Obtaining only the filename when using OpenFileDialog property "FileName" Load a bitmap image into Windows Forms using open file dialog Multiple file extensions in OpenFileDialog