Open File Dialog, One Filter for Multiple Excel Extensions?

C#WinformsOpenfiledialog

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

Use a semicolon

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

Solution 2 - C#

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";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnetcatView Question on Stackoverflow
Solution 1 - C#OdysView Answer on Stackoverflow
Solution 2 - C#Bence VégertView Answer on Stackoverflow