2

I am able to choose an Excel file, but after I clicked on Open, the excel file doesn't appear. What should I do? I'm still new with OpenFileDialog, it will be good if anyone can tell what I should add to make the excel file appear after clicking on Open.

Modified from http://www.c-sharpcorner.com/uploadfile/mahesh/openfiledialog-in-c-sharp/

This is my code:

private void BrowseButton_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = @"C:\";
        openFileDialog1.Title = "Browse Text Files";

        openFileDialog1.CheckFileExists = true;
        openFileDialog1.CheckPathExists = true;

        openFileDialog1.DefaultExt = "txt";
        openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        openFileDialog1.ReadOnlyChecked = true;
        openFileDialog1.ShowReadOnly = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string file = openFileDialog1.FileName;
            try
            {
                string text = File.ReadAllText(file);
                int size = text.Length;
            }
            catch (IOException)
            {
            }

        }
    }


public bool ThumbnailCallback()
   {
     return false;
   }

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    {

    }

After I clicked Open, only the file name appear, but not the excel file - https://i.sstatic.net/GXToy.jpg

3
  • What do you mean by "make the excel file appear"? Do you want to open the Microsoft Excel Application, to open the selected Workbook? In that case you need to add some code to actually open ("execute") the file, since the OpenFileDialog is only used to select a file (basically what you're doing with the selected file is up to you and your C# code). Commented Jul 5, 2017 at 11:01
  • Yes, I want to open the Microsoft Excel App after I select which workbook I want to open. Can you give me an example of the coding how to execute the excel file? Commented Jul 6, 2017 at 1:26
  • There is a old post, FYR stackoverflow.com/questions/464902/… Commented Jul 6, 2017 at 4:00

2 Answers 2

3

You need to set the filter to select excel files.

openFileDialog1.Filter = "Excel Worksheets|*.xls";

You can refer the documentation here.

Sign up to request clarification or add additional context in comments.

1 Comment

What I mean is, after I choose the excel file, I need it to pop up after I clicked on Open. I want the coding to open the excel file. Is it possible?
0

If you only want to open the Excel file using the default application that is associated to *.xlsx files (which usually is MS Excel when it is installed), then you can simply use the Process.Start(string) method. In you case it may look something like this:

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        Process.Start(openFileDialog1.FileName);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.