简体   繁体   English

如何返回流而不是写入磁盘?

[英]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.

相关问题 如何直接在 WCF Response 对象中返回多个字段,而不是包装在另一个对象中? - How can i return several fields directly in a WCF Response object, rather than wrapped in another object? 如何在不写入磁盘的情况下读取流? - How to read stream without writing to disk? 如何从异步 function C# 返回字符串而不是 SYSTEM.THREADING.TASK.TASK`1[SYSTEM.STRING] - How can I return a string rather than SYSTEM.THREADING.TASK.TASK`1[SYSTEM.STRING] from an async function C# 如何为应用程序而非用户授权 REST API? - How can I authorize REST API for an application rather than for a user? 如何在EPPlus中过滤列(而不是行)? - How can I filter columns (rather than rows) in EPPlus? 如何缓冲BCP输出而不是写入文件 - How to buffer BCP output rather than writing to a file 在C#中,如何从字符串创建TextReader对象(无需写入磁盘) - In C#, how can I create a TextReader object from a string (without writing to disk) 如何在不写入磁盘的情况下将XML从Delphi传递到C#? - How can I pass XML from Delphi to C# without writing to disk? 在ASP.NET中的分段文件上传,可以将数据写入磁盘而不是读取到RAM吗? - Multipart File Uploads in ASP.NET, can the data be written to disk rather than read into RAM? 如何从字节数组创建文件并将其作为文件发送到流中,而无需在磁盘上创建此文件? - How can I create a file from a byte array and send it to the stream as a file without creating this file on the disk?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM