记录不会保存到下载的CSV中

时间:2014-06-12 20:35:17

标签: c# asp.net csv csvhelper

我正在使用(呃,尝试,无论如何)CsvHelper将记录写入CSV文件,然后下载文件。现在,文件下载,但它只是在第一行的第一列写“CsvHelper.CsvWriter”。而已。我错过了什么?

启动下载的按钮处理程序:

protected void DownloadChargeDataAsCSV(object sender, EventArgs e)
{
    List<ChargeDetail> charges = ChargeDetail.GetDetailsForPatient(lblPatientNum.Text, lblPatientName.Text);

    string attachment = "attachment; filename=MyCsvLol.csv";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", attachment);
    HttpContext.Current.Response.ContentType = "text/csv";
    HttpContext.Current.Response.AddHeader("Pragma", "public");

    using (var textWriter = File.CreateText("MyCsvLol.csv"))
    using (var writer = new CsvWriter(textWriter))
    {
        foreach (var charge in charges)
        {
            writer.WriteRecord(charge);
        }

        HttpContext.Current.Response.Write(writer);
        HttpContext.Current.Response.End();
    }
}

费用明细类:

public class ChargeDetail
{
    public string PatientNumber { get; set; }
    public string PateintName { get; set; }
    public string Date { get; set; }
    public string ChargeCode { get; set; }
    public string Description { get; set; }
    public string Qty { get; set; }
    public string UnitPrice { get; set; }
    public string ExtAmt { get; set; }

    public static List<ChargeDetail> GetDetailsForPatient(string patientNumber, string patientName)
    {
        List<ChargeDetail> charges = new List<ChargeDetail>();
        DataTable thisDT = new DataTable();
        using (SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString))
        {
            string SprocName = "GetChargeDetails";
            SqlDataAdapter thisAdapter = new SqlDataAdapter(SprocName, thisConnection);
            thisAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            thisAdapter.SelectCommand.Parameters.AddWithValue("@PatientNumber", patientNumber);
            thisAdapter.Fill(thisDT);
        }

        foreach (DataRow row in thisDT.Rows)
        {
            charges.Add(new ChargeDetail(patientNumber, patientName, row));
        }

        return charges;
    }

    public ChargeDetail(string patientNumber, string patientName, DataRow row)
    {
        this.PatientNumber = patientNumber;
        this.PateintName = patientName;
        this.Date = row["SrvcDate"].ToString();
        this.ChargeCode = row["ChargeCode"].ToString();
        this.Description = row["ChargeDescription"].ToString();
        this.Qty = row["HHY_Qty"].ToString();
        this.UnitPrice = row["PatPrice"].ToString();
        this.ExtAmt = row["ExtAmt"].ToString();
    }

    public ChargeDetail()
    {
    }
}

3 个答案:

答案 0 :(得分:2)

我需要类似的东西。我使用了anwyatt发布的代码,但我偶尔会在响应中得到一个空文件或不完整的文件,除非我先刷新Stream Writer。对不起,我不允许对他的回答发表评论,但我想发布这个以防其他人有类似的问题。

protected void DownloadChargeDataAsCSV(object sender, EventArgs e)
{
    List<ChargeDetail> charges = ChargeDetail.GetDetailsForPatient(lblPatientNum.Text, lblPatientName.Text);

    string attachment = "attachment; filename=ChargeDetails.csv";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", attachment);
    HttpContext.Current.Response.ContentType = "text/csv";
    HttpContext.Current.Response.AddHeader("Pragma", "public");
    using (var memoryStream = new MemoryStream())
    using (var streamWriter = new StreamWriter(memoryStream))
    using (var streamReader = new StreamReader(memoryStream))
    using (var writer = new CsvWriter(streamWriter))
    {
        writer.WriteRecords(charges);
        streamWriter.Flush();
        memoryStream.Position = 0;
        HttpContext.Current.Response.Write(streamReader.ReadToEnd());
    }
    HttpContext.Current.Response.End();
}

答案 1 :(得分:1)

你无法阅读“作家”。保存文件后,再将其读回以发送给用户。

using (var textWriter = File.CreateText("MyCsvLol.csv"))
    using (var writer = new CsvWriter(textWriter))
    {
        foreach (var charge in charges)
        {
            writer.WriteRecord(charge);
        }       
    }

 HttpContext.Current.Response.Write(File.ReadAllText("MyCsvLol.csv"));
 HttpContext.Current.Response.End();

答案 2 :(得分:0)

这让它发挥作用。

protected void DownloadChargeDataAsCSV(object sender, EventArgs e)
{
    List<ChargeDetail> charges = ChargeDetail.GetDetailsForPatient(lblPatientNum.Text, lblPatientName.Text);

    string attachment = "attachment; filename=ChargeDetails.csv";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", attachment);
    HttpContext.Current.Response.ContentType = "text/csv";
    HttpContext.Current.Response.AddHeader("Pragma", "public");
    using (var memoryStream = new MemoryStream())
    using (var streamWriter = new StreamWriter(memoryStream))
    using (var streamReader = new StreamReader(memoryStream))
    using (var writer = new CsvWriter(streamWriter))
    {
        writer.WriteRecords(charges);
        memoryStream.Position = 0;
        HttpContext.Current.Response.Write(streamReader.ReadToEnd());
    }
    HttpContext.Current.Response.End();
}