C# 打开文件对话框,多个 Excel 扩展的一个过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17116045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:38:33  来源:igfitidea点击:

Open File Dialog, One Filter for Multiple Excel Extensions?

c#winformsopenfiledialog

提问by netcat

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.

我想使用 OpenFileDialog 对象浏览到一个 excel 文件。我想设置过滤器以打开具有不同类型的 excel 扩展名的文件,例如:.xls、.xlsm、.xlsx 等。

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.

这有效,但用户必须从 OpenFileDialog 的下拉列表中选择正确的 Excel 文件类型。

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

有谁知道是否有办法对所有类型的 Excel 扩展应用一个过滤器?

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

类似于:“...Excel 文件(.xls、.xlsx、.xlxm)|*.xls、*.xlsx、*.xlsm;”

Thanks in advance for any replies.

提前感谢您的任何答复。

采纳答案by Odys

Use a semicolon

使用分号

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

回答by Bence Végert

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

如果要合并过滤器(例如 CSV 和 Excel 文件),请使用以下公式:

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