0

I'm trying to export my dataTable as an Excel file. I can do this from (https://stackoverflow.com/a/13973274), but I'd like the user to be able to input the location the file is saved to. It seems like the SaveFileDialog class would work well.

My issue stems from the fact that I can't figure out where to find the filepath that has been specified by the user after hitting "save". I need that filepath to input into the ExportToExcel(ExcelFilePath) when I actually do the export of my dataTable.

SaveFileDialog Class

Sorry if I'm unclear, this is my first time asking a question here.

Mk668
  • 3
  • 1
  • 1
    SaveFileDialog derives from [FileDialog](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog). See its properties (FileName, for example). Btw, you don't actually need to use Interop to save an Excel File. If you want to use Interop, then make sure the users of you program also have Excel installed. – Jimi Jun 05 '20 at 16:46
  • Extract the path with Path.GetDirectoryName from the FileName property – Steve Jun 05 '20 at 16:49
  • @Jimi Thank you! I appreciate it! – Mk668 Jun 05 '20 at 17:09
  • See an implementation [here](https://stackoverflow.com/a/62206805/7444103) (last piece of code). Note that some properties are there to clarify an *intention* rather than to determine a functionality. You'll have to read more to understand why. – Jimi Jun 05 '20 at 17:11

1 Answers1

0

As you've noted, the SaveFileDialog class is documented here.

If we look at the top of that page, it shows the inheritance hierarchy:

Inheritance → ObjectMarshalByRefObjectComponentCommonDialogFileDialog → SaveFileDialog

Now if we follow the link to the FileDialog class and go to Properties, we see:

FileDialog.FileName Property
Property Value
String
The file name selected in the file dialog box. The default value is an empty string ("").

Remarks
The file name includes both the file path and the extension. If no files are selected, this method returns an empty string ("").

Rufus L
  • 36,127
  • 5
  • 30
  • 43