user726895
user726895

Reputation: 11

How to display a 'Save as' dialog box for an Excel file generated through C#

Rather than saving a file silently to a default path, I want to give the user the option to save the excel file. How can I do that?

Upvotes: 0

Views: 4320

Answers (3)

Alexander Prokofyev
Alexander Prokofyev

Reputation: 34523

I believe it's not really ASP.NET MVC question, but anyway:

Excel.Application excel = new Excel.Application();
Excel.Workbook workbook = excel.Workbooks.Add(Missing.Value) as Excel.Workbook;

...

object filename = excel.GetSaveAsFilename("DefaultName.xls", 
    "Excel 2000-2003 Workbook (*.xls), *.xls", Missing.Value, 
    Missing.Value, Missing.Value);

if (!(filename is bool))
{
    workbook.SaveAs(filename, Excel.XlFileFormat.xlWorkbookNormal,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value,
        Missing.Value, Missing.Value, Missing.Value);
        excel.Quit();
}

Upvotes: 2

davidsleeps
davidsleeps

Reputation: 9513

This question deals with downloading a file in asp.net MVC (by using the FileResultType)...your simply choosing to send an excel file which should still apply.

Upvotes: 0

Rhapsody
Rhapsody

Reputation: 6077

You need to create a custom ActionResult class which will display the Save As dialog. Phil Haack has an excellent tutorial about that.

Upvotes: 0

Related Questions