简体   繁体   English

将 Excel 工作簿写入 memoryStream

[英]Writing an excel workbook to a memoryStream

I would like save an newly created Excel in a MemoryStream.我想在 MemoryStream 中保存一个新创建的 Excel。 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).问题是当我在xlWorkBook.SaveAs(ms)之后断点时, ms 为空(长度为 0)。 Moreover, when saving, a file "System.IO.MemoryStream" is created on the PC (in the folder Documents ).此外,保存时,会在 PC 上创建一个文件“System.IO.MemoryStream”(在文件夹Documents 中)。 Plus, I tried ExportAsFixedFormat and it worked well (but the document is saved on the PC).另外,我尝试了ExportAsFixedFormat并且效果很好(但文档保存在 PC 上)。 Then, SaveToStream doesn't exist here.那么, SaveToStream在这里不存在。

I am developing an application Web ASP.NET MVC4 in Visual Studio 2012 and Dotnet 4.5.我正在 Visual Studio 2012 和 Dotnet 4.5 中开发应用程序 Web ASP.NET MVC4。 The language is C#.语言是 C#。 It is possible to save a workBook in a memoryStream without saving it on the PC?是否可以将工作簿保存在 memoryStream 中而不将其保存在 PC 上?

Thanks,谢谢,

您是否尝试过以下选项?

xlWorkSheet.UsedRange.Copy(Type.Missing);

Processor is Running in background you have tried Try this to terminate the process "Excel.exe"处理器正在后台运行您尝试过尝试此操作以终止进程“Excel.exe”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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