-4

I am making a program to open an Excel file in C# and get the data from it.

private void XLConnect_Click(object sender, EventArgs e)
{
    type = 1;
    open.Filter = "Excel|.xlsx";
    open.ShowDialog();
}

public void readFile()
{
    if (type == 1)
    {
        MessageBox.Show("Success");
    }
}

My problem with the code is that when the open dialog appears, it doesnt show any file to select. What is wrong in the code?

4

3 回答 3

2
  1. 在过滤器中使用星号。
  2. 可能您的机器只包含旧式 Excel 文件?

使用此过滤器:

open.Filter = "Excel (*.xls, *.xlsx)|*.xls;*.xlsx";
于 2013-05-06T07:42:24.240 回答
0

您必须使用通配符,在本例中为星号 (*):

private void XLConnect_Click(object sender, EventArgs e)
{ 
    type = 1;
    open.Filter = "Excel|*.xlsx";
    open.ShowDialog();
}
于 2013-05-06T07:41:21.213 回答
0

你声明了open吗?

OpenFileDialog open = new OpenFileDialog();

至于过滤器,请使用*.

open.Filter = "Excel|*.xlsx";
于 2013-05-06T07:41:55.487 回答