我将我的文件导出为.CSV,它包含数据,但是当我打开导出的CSV文件时,它是空的,我不知道哪里出错了,我的控制器如下:
var eventResult = (from c in DB.Events
where (c.m_turbine_id == turbineid.turbineID) && (c.m_time_stamp >= frmDate && c.m_time_stamp <= toDate)
select new EventLogPartialViewModel
{
Timestamp = c.m_time_stamp,
Description = c.m_event_log_description,
WindSpeed = c.m_wind_speed,
RPM = c.m_rpm,
Power = c.m_power
}).ToList();
以下是我准备出口的部分:
var excludeTag = DataTag.NoExclusion;
byte[] csvBytes = ASCIIEncoding.ASCII.GetBytes(CSVExport.GetCsv(eventResult.ToList(), excludeTag));
return this.File(csvBytes, "text/csv",".csv");
这是我的GetBytes方法来转换文件:
public static string GetCsv<T>(List<T> csvDataObjects, DataTag excludeTag)
{
PropertyInfo[] propertyInfos = typeof(T).GetProperties();
var sb = new StringBuilder();
sb.AppendLine(GetCsvHeaderSorted(propertyInfos, excludeTag));
csvDataObjects.ForEach(d => sb.AppendLine(GetCsvDataRowSorted(d, propertyInfos, excludeTag)));
return sb.ToString();
}
最后,我有值,但导出的文件是空的!
我的eventResult是一个var,填充LINQ,因为它需要将它转换为字符串然后我可以使用ASCIIEncoding.ASCII.GetBytes转换为byte GetCsv为我做了,将我的结果转换为字符串,如图所示,最后我将csvBytes作为.csv格式返回,大小为[8986]字节,但我用excel或note打开它的导出文件是emty