You can set the Filter
property of the OpenFileDialog
to restrict the file types that the dialog box displays. Here’s how you can do it for .xls and .xlsx files:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
In this code, "Excel Files|*.xls;*.xlsx"
is a filter string. It consists of a description (“Excel Files”), a vertical bar (|), and one or more file type patterns separated by semicolons (;). The patterns “.xls” and “.xlsx” match any file with a .xls or .xlsx extension, respectively.