69

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.

netcat
  • 1,105
  • 1
  • 8
  • 17
  • 1
    The MSDN documentation has all the details: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filter.aspx – David Heffernan Jun 14 '13 at 19:42
  • Thanks David and odyodyodys. Yes the example there on the page and I did read the page, but somehow I somehow missed the section. Thanks. – netcat Jun 14 '13 at 20:07

2 Answers2

139

Use a semicolon

OpenFileDialog of = new OpenFileDialog();
of.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
Odys
  • 8,319
  • 8
  • 63
  • 105
  • Awesome! Needed it for audio files `dialog.Filter = "Audio Files|*.mp3;*.wav;*.wmp";` – Andrew Grinder Aug 05 '14 at 20:11
  • Thanks! Saved me some time searching on net – Allen Linatoc May 07 '15 at 08:57
  • 1
    For two filters - in my case CSV and Excel files - I used : `CSV files (*.csv)|*.csv|Excel Files|*.xls;*.xlsx;*.xlsm` – Ashok Aug 15 '17 at 12:00
  • 2
    ```Excel Files|*.xlsx;*.xlsm;*.xlsb;*.xltx;*.xltm;*.xls;*.xlt;*.xls;*.xml;*.xml;*.xlam;*.xla;*.xlw;*.xlr;``` extensions from: https://support.office.com/en-us/article/File-formats-that-are-supported-in-Excel-0943ff2c-6014-4e8d-aaea-b83d51d46247 – aeroson Sep 30 '17 at 12:58
  • you can use a * to refer more than one type, example: `of.Filter = "Excel Files|*.xls*";` – Celso Lívero Feb 14 '19 at 10:41
4

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";
Bence Végert
  • 589
  • 5
  • 12