简体   繁体   中英

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 )

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.

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);
   }
}

您可能正在寻找像这样的保存/打开文件对话框: http : //msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog%28v=VS.85%29.aspx他们会打开一个对话框,询问您将文件保存在何处,一旦选择位置,该信息就会返回到程序中,以便您可以在此处保存文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM