Viaches
Viaches

Reputation: 143

Using SaveFileDialog with ClosedXML

I try to tell you my problem. with ClosedXML i have SaveAs() method, but when i use SaveAs(string name), it saves my excel document to some strange folder with some strange path. so i've decide to use savefiledialog to give user posibility to select folder and name for document. how can i use savefiledialog with closedXML?

SaveAs() also have SaveAs(Path path). Can i use it?

Upvotes: 2

Views: 5633

Answers (2)

fra
fra

Reputation: 1

var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel files|*.xlsx";

//serialVal is name of a variable, not necessary at all if you don't need a specific file name

saveFileDialog.FileName = serialVal; 
if (saveFileDialog.ShowDialog() == true)
{
    workbook.SaveAs(saveFileDialog.FileName);
    workbook.Dispose();
    return;
}

Upvotes: 0

Manuel
Manuel

Reputation: 11469

The "strange" folder is the folder your application is running from (since you're not specifying a path).

If you want you can use the SaveFileDialog to get the path and pass it to the SaveAs method.

    var saveFileDialog = new SaveFileDialog
                             {
                                 Filter = "Excel files|*.xlsx", 
                                 Title = "Save an Excel File"
                             };

    saveFileDialog.ShowDialog();

    if (!String.IsNullOrWhiteSpace(saveFileDialog.FileName))
        workbook.SaveAs(saveFileDialog.FileName);

Upvotes: 6

Related Questions