How to view Excel Files with OpenFileDialog?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    How to view Excel Files with OpenFileDialog?

    I am coding a textbox and button to select a Excel File. When I run the application and the file browser appears, non of the files appear in the browser. Here is the code that I wrote for the button
    Code:
     
    private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog browsedFile = new OpenFileDialog();
                browsedFile.Filter = "XML Files (*.xml,*.xls,*.xlsx,*.xlsm,*.xlsb)"+
                    "|*.xml*.xls,*.xlsx,*.xlsm,*.xlsb|All files (*.*)|*.*";
                browsedFile.ShowDialog();
    Also, why I am asking questions, when I want to uploaded the file selected from the browswer, how do I send it to the text box?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You need to get the selected file(s) from the OpenFileDialog. You can do this with the FileNames property.

    You also may want to consider doing a check on the return value from the ShowDialog method. It returns a DialogResult enum that tells you what was clicked. This way you can avoid trying to process a list of files that doesn't exist because your user clicked Cancel on the dialog :)

    From what you describe, it sounds like you know how make it open in Excel, do you still need any help with that? Finally, your final sentence there confuses me... what do you mean?

    Comment

    • Subin Ninan
      New Member
      • Sep 2010
      • 91

      #3
      There is an error in the value of your Filter property.
      No need to use + operator and , comma is missing.

      It should be like,

      Code:
      OpenFileDialog.Filter = "xml files (*.xml) |*.xml, *.xsl";

      To get selected file in textbox,


      Code:
      Textbox.Text = OpenFileDialog.Filename;

      Comment

      • Brian Connelly
        New Member
        • Jan 2011
        • 103

        #4
        So thanks guys, I have it somewhat working. When I try to filter the OpenFileDialog it filters out all files. If I use
        Code:
        browsedFile.Filter = "All files (*.*)|*.*";
        then all the files are present. Also I did
        Code:
        if (browsedFile.ShowDialog() == DialogResult.OK)
                    {
                        textBox1.Text = browsedFile.FileName;
                    }
        and it would make the dialogBox appear twice so I just got rid of it. How would you recommend I validate that the right file is selected...Coul d you provide a sample (Excel File Only). @Gary, forget that confusing question...It was me being stupid and learning...it was answered by what the OpenFileDialog Class does.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          It's probably showing twice because you added it to the end of what you had already. You only need one ShowDialog call and the one in the if statement where you validate the return is sufficient. If you're not comfortable putting the call directly in the if statement, you can always write it like this...

          Code:
          DialogResult dr = browsedFile.ShowDialog();
          if (dr == DialogResult.OK)
          {
            // do whatever
          }
          As for validating that the right file is selected, what do you mean? Like if your user picks a file that isn't openable by Excel? Well for starts, I would recommend you use your filter to specify the right files instead of *.* but it is true that they can always type *.* in the OpenFileDialog and get the same result.

          What are your qualifications for a "valid" file? Excel will open it, or is there more? Personally, I wouldn't try to validate it at all. I'd just let Excel try to open it and check to see if Excel failed. I haven't worked with it myself, but look into the objects you use to interact with Excel. I'd imagine there's some kind of error condition it gives you when the file can't open.

          Also look to see if it throws an exception when opening a bad file. If it does, you can put a try/catch around it and handle it more gracefully.

          Comment

          • Brian Connelly
            New Member
            • Jan 2011
            • 103

            #6
            Thanks! So my only problem is now is that if I filter using just Excel file types...when the dialog box opens there are no files with thos extensions. When I filter with the * key, there are lots of files and the Files I tried to filter for...Im so lost...

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Well, what files do you actually want? If you actually do want everything, go ahead and use *.* and that's fine, but you should probably ask yourself if you want users trying to open everything with excel. The filter is there to make it easier for the user to find the files they want (or can) open and to hide things that they shouldn't be concerned with.

              Excel only opens certain kinds of files. If you try to open a file, you can see the kinds of files it supports. It also has *.* on the list and the user can choose this. If your program is causing Excel to open files, you might want to consider cloning their filters.

              Or, if you're happy with *.*, just go with that. Ultimately it's up to you :D

              Comment

              • Brian Connelly
                New Member
                • Jan 2011
                • 103

                #8
                Thanks but let me expalin my problem a little clearer. Say I have a File called "Help.xls". When I use the openDialog function filtering for just Excel Files (I list them all file extensions) the dialog results with no excel files visible. If I filter for all file types, then the "Help.xls" file is visible in the openDialog. I can't figure out what is going on. What do you think?

                Comment

                • Joe Phillips
                  New Member
                  • Jun 2011
                  • 1

                  #9
                  You need to use semi-colons in the filter string:

                  Code:
                   browsedFile.Filter = "XML Files (*.xml;*.xls;*.xlsx;*.xlsm;*.xlsb)"+
                                      "|*.xml;*.xls;*.xlsx;*.xlsm;*.xlsb|All files (*.*)|*.*";
                  The part in parentheses is eye candy, I am referring to the part to the right of the "|" for each filter.
                  Last edited by Joe Phillips; Jun 6 '11, 08:58 PM. Reason: clarification

                  Comment

                  Working...