user3013853
user3013853

Reputation:

Writing an excel workbook to a memoryStream

I would like save an newly created Excel in a MemoryStream. Here my code:

using Excel = Microsoft.Office.Interop.Excel;
...

public void Create(){
  Excel.Application xlApp;
  Excel.Workbook xlWorkBook;
  Excel.Worksheet xlWorkSheet;

  //Complete xlWorkSheet

  MemoryStream ms = new MemoryStream();
  xlWorkBook.SaveAs(ms);

  xlWorkBook.Close(false, misValue, misValue);
  xlApp.Quit();
}

The problem is when I breakpoint just after xlWorkBook.SaveAs(ms), ms is empty (the length is 0). Moreover, when saving, a file "System.IO.MemoryStream" is created on the PC (in the folder Documents). Plus, I tried ExportAsFixedFormat and it worked well (but the document is saved on the PC). Then, SaveToStream doesn't exist here.

I am developing an application Web ASP.NET MVC4 in Visual Studio 2012 and Dotnet 4.5. The language is C#. It is possible to save a workBook in a memoryStream without saving it on the PC?

Thanks,

Upvotes: 2

Views: 11089

Answers (2)

Ayad Mihidabi Khan
Ayad Mihidabi Khan

Reputation: 1

Processor is Running in background you have tried Try this to terminate the process "Excel.exe"

'foreach (Process proc in System.Diagnostics.Process.GetProcessesByName("Excel"))
           {
           proc.Kill();
           }'

Upvotes: 0

Nil23
Nil23

Reputation: 377

have you tried following option?

xlWorkSheet.UsedRange.Copy(Type.Missing);

Upvotes: 2

Related Questions