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.stack.imgur.com/GXToy.jpg

prestan
  • 63
  • 3
  • 13
  • 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). – bassfader Jul 05 '17 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? – prestan Jul 06 '17 at 01:26
  • There is a old post, FYR https://stackoverflow.com/questions/464902/how-to-open-an-excel-file-in-c – Huan Jiang Jul 06 '17 at 04:00

2 Answers2

3

You need to set the filter to select excel files.

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

You can refer the documentation here.

Harsh
  • 3,683
  • 2
  • 25
  • 41
  • 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? – prestan Jul 06 '17 at 01:13
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);
    }
bassfader
  • 1,346
  • 1
  • 11
  • 15