asp.net mvc return file result

  asp.net mvc返回文件: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 == 0?"學員":"教練";
                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(936).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");
        }
相關文章
相關標籤/搜索