Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从FileStreamResult获取内容_C#_Asp.net Mvc - Fatal编程技术网

C# 如何从FileStreamResult获取内容

C# 如何从FileStreamResult获取内容,c#,asp.net-mvc,C#,Asp.net Mvc,我正在使用第三方库,其中一个方法返回FileStreamResult public FileStreamResult GenerateFile(OutFormat format, dynamic params); 控制器中的操作调用此方法: public ActionResult GenerateExcel() { Utils.XCore core = new Utils.XCore(...); // where ... are contructor params // ...

我正在使用第三方库,其中一个方法返回
FileStreamResult

public FileStreamResult GenerateFile(OutFormat format, dynamic params);
控制器中的操作调用此方法:

public ActionResult GenerateExcel()
{
    Utils.XCore core = new Utils.XCore(...); // where ... are contructor params
    // ... other codes here ...
    return core.GenerateFile(OutFormat.EXCEL, new { FileName = "Report" });
}
这很好,但有时我想将多个Excel工作表合并到一个工作表中,如下所示:

public ActionResult GenerateExcel()
{
    Utils.XCore core = new Utils.XCore(...); // where ... are contructor params
    // ... other codes here ...
    var excel1 = core.GenerateFile(OutFormat.EXCEL, new { FileName = "rpt1" });
    var excel2 = core.GenerateFile(OutFormat.EXCEL, new { FileName = "rpt2" });
    var excel3 = core.GenerateFile(OutFormat.EXCEL, new { FileName = "rpt3" });

    var finalContent = combineFile(excel1, excel2, excel3);

    return new FileStreamResult(finalContent, "application/ms-excel")
       {
           FileDownloadName = "Report.xls"
       };
}

我现在的问题是我不知道如何从
FileStreamResult
获取内容。有什么办法吗?即使是包含网络链接的评论也非常受欢迎。谢谢

如果我正确理解了您的问题,您希望从
FileStreamResult
处理/获取内容。该类包含一个名为
FileStream
的属性,该属性是
Stream
对象。现在,可以使用以下修改的代码将流对象保存为文件:

以下是如何使用:

var excel1 = core.GenerateFile(OutFormat.EXCEL, new { FileName = "rpt1" });
string filePath = "C:\\yourFileName.xls"; // path of your newly saved file 
using (Stream reportStream = excel1.FileStream)
{
     streamToFile(reportStream, filePath);
}

在使用流保存/提示用户:@Papa我没有任何memorystream对象之前,需要重置流的seek原点。
var excel1 = core.GenerateFile(OutFormat.EXCEL, new { FileName = "rpt1" });
string filePath = "C:\\yourFileName.xls"; // path of your newly saved file 
using (Stream reportStream = excel1.FileStream)
{
     streamToFile(reportStream, filePath);
}