问题描述
我已经验证使用 System.Text.Encoding.ASCII.GetString(ms.ToArray));
我的MemoryStream已预期的数据
I've verified using System.Text.Encoding.ASCII.GetString(ms.ToArray));
that my memorystream has the expected data.
不过使用LinqToCSV的NuGet库将不会产生我的csv文件。我没有得到任何引发的错误或异常。我刚刚得到一个空文件时提示我打开该文件。
However using the LinqToCSV nuget library will not generate my csv file. I get no errors or exceptions thrown. I just get an empty file when I'm prompted to open the file.
下面是我的动作方法
public FileStreamResult Export(){
var results = _service.GetProperties().Take(3);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.TextWriter txt = new System.IO.StreamWriter(ms);
CsvFileDescription inputFileDescription = new CsvFileDescription{
SeparatorChar =',',
FirstLineHasColumnNames = true
}
;
CsvContext csv = new CsvContext();
csv.Write(results,txt,inputFileDescription);
return File(ms , "application/x-excel");
}
我觉得很有意思,如果我改变返回类型ContentResult类型,返回方法的内容(),并将它传递 System.Text.Encoding.ASCII.GetString(ms.ToArray));
我得到一个浏览器窗口,显示我的数据。
I find it interesting, if I change the return type to contentResult, and the return method to Content() and pass it System.Text.Encoding.ASCII.GetString(ms.ToArray));
I do get a browser window showing my data.
推荐答案
请确保您重置流位置为0。另外,还要确保你刷新你的的StreamWriter
之前。
Make sure you reset stream position to 0. Also make sure you flush your StreamWriter
before that.
这篇关于CSV写入到MemoryStream的使用LinqToCSV不返回任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!