10

我正在尝试将代码从 ASP.net 迁移到 ASP.net 核心。

在 ASP.net 中的代码如下所示,

var progresses = db.Progresses.Where(p => p.UserId == id).Include(p => p.User.UserMetaData).Include(p => p.Quiz).Include(p => p.User.Groups).OrderByDescending(p => p.UpdatedAt).ToList();

List<ReportCSVModel> reportCSVModels = new List<ReportCSVModel>();
const string downloadName = "Reports.csv";
var csv = new CsvWriter(Response.Output);

csv.Configuration.RegisterClassMap<ReportCSVMap>();

Response.ClearContent();

Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition",
   "attachment; filename=" + downloadName);

csv.WriteHeader<ReportCSVModel>();
foreach (var progress in progresses)
{
    var reportCSVModel = new ReportCSVModel();
    reportCSVModel.Quiz = progress.Quiz.Title;
    reportCSVModel.Score = (progress.CorrectAnswersCount * progress.PointsPerQuestion).ToString();
    reportCSVModel.Status = progress.Status;
    reportCSVModel.CompletedDate = progress.UpdatedAt.ToString();
    reportCSVModel.Location = progress.User.UserMetaData != null ? progress.User.UserMetaData.Location : "";
    reportCSVModel.Group = progress.User.Groups.FirstOrDefault() != null ? progress.User.Groups.FirstOrDefault().Name : "";

    csv.WriteRecord<ReportCSVModel>(reportCSVModel);
}
Response.Flush();
Response.End();
return null;

但是在 ASP.NET 核心中使用它时,我将其转换为,

var progresses = _elearnContext.Progress.Where(p => p.UserId == id).Include(p => p.User.UserMetaData).Include(p => p.Quiz).Include(p => p.User.Groups).OrderByDescending(p => p.UpdatedAt).ToList();

// List<ReportCSVModel> reportCSVModels = new List<ReportCSVModel>();
List<ReportCSVModel> reportCSVModels = new List<ReportCSVModel>();
const string downloadName = "Reports.csv";

System.IO.TextWriter writeFile = new StreamWriter(Response.Body.ToString());
CsvWriter csv = new CsvWriter(writeFile);
csv.Configuration.RegisterClassMap<GroupReportCSVMap>();
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.Headers.Add("Content-Disposition", "attachment; filename=" + downloadName);


csv.WriteHeader<ReportCSVModel>();
foreach (var progress in progresses)
{
    var reportCSVModel = new ReportCSVModel();
    reportCSVModel.Quiz = progress.Quiz.Title;
    reportCSVModel.Score = (progress.CorrectAnswersCount * progress.PointsPerQuestion).ToString();
    reportCSVModel.Status = progress.Status;
    reportCSVModel.CompletedDate = progress.UpdatedAt.ToString();
    reportCSVModel.Location = progress.User.UserMetaData != null ? progress.User.UserMetaData.Location : "";
    reportCSVModel.Group = progress.User.Groups.FirstOrDefault() != null ? progress.User.Groups.FirstOrDefault().Name : "";

    csv.WriteRecord<ReportCSVModel>(reportCSVModel);
}

Response.Clear();
return null;

在 ASP.net 中Response.Output可用但在核心中不可用。所以我试着像这样使用它Response.Body

谁能告诉我,我哪里做错了?

4

3 回答 3

18

考虑改变方法以更符合当前建议的语法。

构造 CSV 并返回 FileResult,这样代码就不必直接操作Response对象。

[HttpGet]
public IActionResult MyExportAction() {
    var progresses = _elearnContext.Progress.Where(p => p.UserId == id)
        .Include(p => p.User.UserMetaData)
        .Include(p => p.Quiz)
        .Include(p => p.User.Groups)
        .OrderByDescending(p => p.UpdatedAt)
        .ToList()
        .Select(progress => 
            new ReportCSVModel() {
                Quiz = progress.Quiz.Title,
                Score = (progress.CorrectAnswersCount * progress.PointsPerQuestion).ToString(),
                Status = progress.Status,
                CompletedDate = progress.UpdatedAt.ToString(),
                Location = progress.User.UserMetaData != null ? progress.User.UserMetaData.Location : "",
                Group = progress.User.Groups.FirstOrDefault() != null ? progress.User.Groups.FirstOrDefault().Name : ""
            }
        );

    List<ReportCSVModel> reportCSVModels = progresses.ToList();

    var stream = new MemoryStream();
    using(var writeFile = new StreamWriter(stream, leaveOpen: true)) {
        var csv = new CsvWriter(writeFile, true);
        csv.Configuration.RegisterClassMap<GroupReportCSVMap>();            
        csv.WriteRecords(reportCSVModels);
    }
    stream.Position = 0; //reset stream
    return File(stream, "application/octet-stream", "Reports.csv");
}
于 2018-10-10T14:18:20.880 回答
6
using CsvHelper;
using CsvHelper.Configuration;

namespace projectX.Controllers {
  [Route("MyController")]
  public class MyController : Controller {
    [Route("export")]
    [HttpGet]
    public ActionResult export() {
      var cc = new CsvConfiguration(new System.Globalization.CultureInfo("en-US"));
      using (var ms = new MemoryStream()) {
        using (var sw = new StreamWriter(stream: ms, encoding: new UTF8Encoding(true))) {
          using (var cw = new CsvWriter(sw, cc)) {
            cw.WriteRecords(YourGenericList);
          }// The stream gets flushed here.
          return File(ms.ToArray(), "text/csv", $"export_{DateTime.UtcNow.Ticks}.csv");
        }
      }
    }
  }
}

感谢Exporting data to CSV from Controller

于 2020-06-01T04:58:23.777 回答
1

//安装包CsvHelper

//控制器编码

    public async Task<IActionResult> indiainfected(ExcelDownloadIndiaInfected exceldownloadindiainfected)
    { 
        var excel =await downloadReportsRepository.ExcelSheetAddIndiaInfected(exceldownloadindiainfected);
        var cc = new CsvConfiguration(new System.Globalization.CultureInfo("en-US"));
        var stream = new MemoryStream();
        using (var sw = new StreamWriter(stream: stream, encoding: newUTF8Encoding(true)))
        {
            using (var cw = new CsvWriter(sw, cc))
            {
                cw.WriteRecords(excel);
            }
            
        }
        string csvName = $"India_Infected-{DateTime.UtcNow.Ticks}.csv";
        return File(stream.ToArray(), "text/csv", csvName);
    }
于 2020-07-15T06:35:46.937 回答