asp.net mvc返回文件:
public ActionResult ExportReflection(string accessToken)
{
var reflections = GetCmsReflectionList(accessToken); var sb = new StringBuilder();
sb.AppendFormat("{0,-10},{1,-10},{2,-10},{3,-10},{4,-10},{5,-100},{6,-100},{7,-50}", "创建者身份", "创建者姓名", "教练打分", "所属能力", "主题", "内容", "参与者", "创建时间");
sb.AppendLine();
foreach (var r in reflections)
{
var creatorRole = r.CreatorRole == ?"学员":"教练";
sb.AppendFormat("{0,-10},{1,-10},{2,-10},{3,-10},{4,-10},{5,-100},{6,-100},{7,-50}",
creatorRole,
r.CreatorUserName,
r.ScoreFromCoach,
r.CompetencyName,
r.Subject,
r.Content,
r.ParticipantsNameString,
r.CreatedTime);
sb.AppendLine();
}
//var encoding = Encoding.GetEncoding(936); //gb2312
var bytes = Encoding.GetEncoding().GetBytes(sb.ToString()); //return File(new MemoryStream(bytes, 0, bytes.Length), "application/csv", "reflections" + DateTime.UtcNow.ToShortDateString() + ".csv");//excel打开csv有乱码
return File(bytes, "application/csv", "reflections" + DateTime.UtcNow.ToShortDateString() + ".csv");
}