我有以下方法:
public byte[] WriteCsvWithHeaderToMemory<T>(IEnumerable<T> records) where T : class
{
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream))
using (var csvWriter = new CsvWriter(streamWriter))
{
csvWriter.WriteRecords<T>(records);
return memoryStream.ToArray();
}
}
使用对象列表调用它 - 最终来自数据库,但由于某些东西不起作用,我只是填充一个静态集合。传递的对象如下:
using CsvHelper.Configuration;
namespace Application.Models.ViewModels
{
public class Model
{
[CsvField(Name = "Field 1", Ignore = false)]
public string Field1 { get; set; }
[CsvField(Name = "Statistic 1", Ignore = false)]
public int Stat1{ get; set; }
[CsvField(Name = "Statistic 2", Ignore = false)]
public int Stat2{ get; set; }
[CsvField(Name = "Statistic 3", Ignore = false)]
public int Stat3{ get; set; }
[CsvField(Name = "Statistic 4", Ignore = false)]
public int Stat4{ get; set; }
}
}
我想要做的是将集合写入 csv 以在 MVC 应用程序中下载。不过,每次我尝试写入该方法时,MemoryStream 都会以零长度返回,并且没有任何内容传递给它。我以前用过这个,但由于某种原因它不起作用——我有点困惑。谁能指出我在这里做错了什么?
干杯