[英]How can I return a stream rather than writing to disk?
I am using OpenXML SDK. 我正在使用OpenXML SDK。
The OpenXML SDK creates a method called CreatePackage as such: OpenXML SDK创建了一个名为CreatePackage的方法:
public void CreatePackage(string filePath)
{
using (SpreadsheetDocument package = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
{
CreateParts(package);
}
}
I call it from my program as follows which will create the Excel file to a given path: 我从我的程序中调用它如下,它将创建Excel文件到给定的路径:
gc.CreatePackage(excelFilePath);
Process.Start(_excelFilePath);
I am not sure how to tweak the code such that it gives back a Stream which shows the Excel file vs having it create the file on disk. 我不知道如何调整代码,以便它返回一个Stream,显示Excel文件与在磁盘上创建文件。
According to the documentation for SpreadsheetDocument.Create
there are multiple overloads, one of which takes a Stream
. 根据SpreadsheetDocument.Create
的文档,有多个重载,其中一个重载了Stream
。
so change your code to: 所以将您的代码更改为:
public void CreatePackage(Stream stream)
{
using (SpreadsheetDocument package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
{
CreateParts(package);
}
}
And then call it with any valid Stream
, for example: 然后使用任何有效的Stream
调用它,例如:
using(var memoryStream = new MemoryStream())
{
CreatePackage(memoryStream);
// do something with memoryStream
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.