我正在使用(呃,尝试,无论如何)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()
{
}
}