如何将 Excel 工作簿写入 .NET 中的 MemoryStream?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/156500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:10:50  来源:igfitidea点击:

How to write an Excel workbook to a MemoryStream in .NET?

.netexcel

提问by Jon Cahill

How do I write an Excel workbook to a MemoryStreamwithout first saving it to the file system?

如何将 Excel 工作簿写入 aMemoryStream而不先将其保存到文件系统?

All options within the Microsoft.Office.Interop.Excel.WorkBooksave options take a filename.

Microsoft.Office.Interop.Excel.WorkBook保存选项中的所有选项都采用文件名。

回答by Charles Graham

I have done extensive work with the PIA and with storing Excel files in a document repository and streaming it out to the browser, and I have not been able to find a solution to using the PIA without first writing the contents to the file system first.

我已经使用 PIA 完成了大量工作,并将 Excel 文件存储在文档存储库中并将其流式传输到浏览器,如果不先将内容写入文件系统,我一直无法找到使用 PIA 的解决方案。

I think that you are going to have to swallow the bullet and deal with the file system as an intermediary. The good news is that you can just give the file a unique name like a guid or use some other tempfilename method (not sure if one exists in .net) and just delete the contents when you are done.

我认为您将不得不吞下子弹并将文件系统作为中介来处理。好消息是您可以给文件一个唯一的名称,如 guid 或使用其他一些 tempfilename 方法(不确定 .net 中是否存在),并在完成后删除内容。

回答by Todd Main

This can be done with Open XML SDK 2.0. Eric White has a great article on how to work with OOXML files in stream at Working with In-Memory Open XML Documents

这可以通过 Open XML SDK 2.0 完成。Eric White 在Working with In-Memory Open XML Documents有一篇关于如何在流中处理 OOXML 文件的很棒的文章

回答by GvS

If you only need basic functionality from Excel, you might want to create the Workbook as an Html stream. There is another question on SO that handless this.

如果您只需要 Excel 的基本功能,您可能希望将工作簿创建为 Html 流。SO 上还有另一个问题无法解决这个问题

This also solves some problems (you might not have yet) concerning the scalability of your solution.

这也解决了一些有关解决方案可扩展性的问题(您可能还没有)。

回答by KevB

Another option would be to use thisfree library for exporting the workbook. The save method on the workbook class can either take a file name or a stream.

另一种选择是使用这个免费库来导出工作簿。工作簿类上的 save 方法可以采用文件名或流。

回答by Anonymous Type

The only way you could do this is if you were prepared to create a custom object that allowed you to store all the various bits and pieces of data/formulas/vba/links/ole objects that you wanted to keep, copy from your workbook to the object, and then persist that object to a memory stream. In affect using your proxy object as a go between.

您可以这样做的唯一方法是,如果您准备创建一个自定义对象,该对象允许您存储所有您想要保留的数据/公式/vba/links/ole 对象的各种零碎,从您的工作簿复制到对象,然后将该对象持久化到内存流中。在影响使用您的代理对象作为中间。

There is no way (as others have said) of writing an Excel file straight to memory.

没有办法(正如其他人所说)将 Excel 文件直接写入内存。

With Excel 2007 OpenXML format, you could use the BeforeSave event of the workbook to have a custom method that first sets the Cancel parameter of the BeforeSave event to True, and then instead serialises the resultant xml package (that represents the file) into an object in memory.

对于 Excel 2007 OpenXML 格式,您可以使用工作簿的 BeforeSave 事件来拥有一个自定义方法,该方法首先将 BeforeSave 事件的 Cancel 参数设置为 True,然后将生成的 xml 包(表示文件)序列化为一个对象在记忆中。