【问题标题】:openFileDialog Filter not showing any option and showing all the files in the folder (i.e. *.jpeg and *.xlsx)openFileDialog 过滤器不显示任何选项并显示文件夹中的所有文件(即 *.jpeg 和 *.xlsx)
【发布时间】:2015-06-07 02:18:43
【问题描述】:
string file = "";
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
openFileDialog1.Filter = "Excel |*.xlsx"; //"Excel Files|(*.xlsx, *.xls)|*.xlsx;*.xls";
openFileDialog1.FilterIndex = 1;

if(result == DialogResult.OK) // Test result.
{
    file = openFileDialog1.FileName;
    try
    {
        string text = File.ReadAllText(file);
        size = text.Length;
    }
    catch(System.IO.IOException)
    {
    }
}

tempLBL.Text = file;

我还测试了上面评论的过滤器。 当我浏览时它没有显示任何过滤器......并显示所有文件。我需要的是,当点击浏览按钮时,只向用户显示 XLSX 或 XLS 文件。

提前致谢

【问题讨论】:

    标签: c# excel openfiledialog


    【解决方案1】:

    那是因为您在设置过滤器和 FilterIndex 之前显示对话框。

    你的代码应该是这样的

    openFileDialog1.Filter = "Excel |*.xlsx"; //"Excel Files|(*.xlsx, *.xls)|*.xlsx;*.xls";
    openFileDialog1.FilterIndex = 1;
    DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
    

    【讨论】:

    • @OP:不要忘记将他的帖子标记为正确答案。 :)
    猜你喜欢
    • 1970-01-01
    • 2011-01-29
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多