尝试导出为CSV时,一切正常,但是,所有导出均缺少第一行。将同一记录导出到Excel或KML时,不会缺少此行。
试图重新编写函数并添加一行,但没有任何效果
public FileResult ExportToCSV()
{
List<Tuple<String, String>> nameValues = new
List<Tuple<string, string>>();
if (ReportSession.FilterKeys != null)
{
nameValues = (List<Tuple<String, String>>)
ReportSession.FilterKeys;
}
ReportRequestFilter dbr = (ReportRequestFilter)
ReportSession.DatabaseRequestFilter;
List<Report_Packet_History> dplist = GetAllReportData
(dbr.Group_Id, dbr.AllowChildgroups, dbr.Device_Id, dbr.user_id,
MapLimitConstants.MAX_EXPORT_BULK_ROWS, dbr.Lookback, dbr.periodEnd, 1,
dbr.ShowActiveOnly);
List<Report_Packet_History> dplistreturn = GetFilters(nameValues,
dplist);
//SetReportViewBags(dplist);
DateTime endDate = dplist.Max(dt => dt.gps_datetime);
DateTime startDate = dplist.Min(dt => dt.gps_datetime);
String[] Device = dplist.Select(dt => dt.imei).Distinct().ToArray();
String[] Group = dplist.Select(dt => dt.user_group_name).Distinct
().ToArray();
Utility.LocalisedDateTime ldt = ToolBox.ConvertToUserTimezone
(DateTime.UtcNow, Session.CurrentUserSettings.time_zone, false,
Session.CurrentUserSettings.Locale);
String fileName = "IRISReport_" + string.Format("{0}_to_{1}",
startDate.ToShortDateString(), endDate.ToShortDateString());
fileName += string.Format(Device.Count() > 1 ? "_Assets_" : "_Asset_"
+
Device[0]);
fileName += string.Format(Group.Count() > 0 ? "_Groups_" : "_Group_"
+ Group[0]);
//List<Report_Packet_History> dplistres = GetFilters(nameValues,
dplist).Take(MapLimitConstants.MAX_EXPORT_CSV_ROWS).Skip
(0).ToList<Report_Packet_History>();
//Response.Charset = "";
return GenerateCSV(dplistreturn, fileName + ".csv");
}
这是GenerateCSV方法,标头在哪里设置为false?我有第一行= true。
private FileResult GenerateCSV(List<Report_Packet_History> report, String
FileName)
{
Type rphType = typeof
(Connector.Models.Report.Report_Packet_History);
PropertyInfo[] myField =
rphType.GetProperties().ToArray();
Boolean firstRow = true;
StringBuilder csvOut = new StringBuilder();
MemoryTributary stream = new MemoryTributary();
foreach (Report_Packet_History rph in report)
{
for (int i = 0; i < myField.Length; i++)
{
if (firstRow)
{
csvOut.Append(myField[i].Name + ",");
}
else
{
if (myField[i].GetValue(rph) == null)
{
csvOut. Append("" + ",");
}
else
{
csvOut.Append(myField[i].GetValue
(rph).ToString() + ",");
}
}
}
csvOut.Append("\r\n");
byte[] byteArray = Encoding.UTF8.GetBytes
(csvOut.ToString());
csvOut.Clear();
stream.Write(byteArray, 0, byteArray.Length);
firstRow = false;
}
stream.Seek(0, SeekOrigin.Begin);
return File
(stream,System.Net.Mime.MediaTypeNames.Application.Octet,
FileName);
}
答案 0 :(得分:0)
第一行应该是“标题”行,我想您的GenerateCSV方法中的某处您已将标头设置为false。
答案 1 :(得分:0)
您将忽略日期的第一行,而不是编写标题。
使用i
开始循环时,该值为零。您的第一行标志也为true。
第一次迭代将看到第一行标志并写入标头,然后重置该标志。没关系。
但是,代码然后循环到下一个迭代,即第二行数据。
您需要先写出标题,然后开始循环遍历数据。
要全面改进,请查看File.WriteAllLines()
;)