1Mayur
1Mayur

Reputation: 3485

Saving excel in clients Machine using Interop

I'm using Interop Excel to automate excel in my C# code, but at the time of saving it I want that it should give user a popup(just like we get when we download a file from internet with option of open with and save)

I've tried the following but it only saves it on server's HDD

xlWorksheet.Save();
xlWorksheet.SaveAs(parameters);

anyone can sugget what can be done (please see: I don't want to use filestream)

Upvotes: 1

Views: 2133

Answers (3)

Eduardo Molteni
Eduardo Molteni

Reputation: 39413

You need to initiate a file download after saving the file in the server

public FilePathResult GetFile()
{
    string name = System.IO.Path.GetTempPath()+Guid.NewGuid().ToString()+".xls";
    // do the work
    xlWorksheet.Save(name);
    return File(name, "Application/x-msexcel");
}

Edited to save the file in the temp folder. Beware that this not guarantees the clean up of the file.

Upvotes: 1

Alex
Alex

Reputation: 618

you're probably looking for a save/open file dialog such as this one: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog%28v=VS.85%29.aspx they open a dialog asking you where to save the file and once you select the location returns that information to your program so you can save the file there.

Upvotes: 0

kyjan
kyjan

Reputation: 321

I don't know if this will help you. But i once wrote a WebService that returns my xls as a bytearray and my client App parses it back to a xls

public byte[] doSomething() { 
   try {
      var xl = new ExcelWorksheet();
      //...blablabla...
      xl.SaveAs(parameters...);
      if (File.Exists(pathOfXlsDoc)) return File.ReadAllBytes(pathOfXlsDoc);
      return null;
   } finally {
      if (File.Exists) File.Delete(pathOfXlsDoc);
   }
}


----------

In clients App it looks like this:

private void BuildXls() {
   try {
      File.WriteAllBytes(clientsXlsPath, doSomething());
   } catch (Exception e) {
      throw new Exception("Error!", e);
   }
}

Upvotes: 0

Related Questions