C# 记录不是';t保存到下载的CSV
我正在使用(呃,无论如何尝试)CsvHelper将记录写入CSV文件,然后下载该文件。现在,该文件将下载,但它只将“CsvHelper.CsvWriter”写入第一行的第一列。就这样。我错过了什么 启动下载的按钮处理程序:C# 记录不是';t保存到下载的CSV,c#,asp.net,csv,csvhelper,C#,Asp.net,Csv,Csvhelper,我正在使用(呃,无论如何尝试)CsvHelper将记录写入CSV文件,然后下载该文件。现在,该文件将下载,但它只将“CsvHelper.CsvWriter”写入第一行的第一列。就这样。我错过了什么 启动下载的按钮处理程序: protected void DownloadChargeDataAsCSV(object sender, EventArgs e) { List<ChargeDetail> charges = ChargeDetail.GetDetailsForPati
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()
{
}
}
protectedvoid下载ChargeDataAscsv(对象发送方,事件参数e)
{
列表费用=ChargeDetail.GetDetailsForPatient(lblPatientNum.Text,lblPatientName.Text);
字符串attachment=“attachment;filename=MyCsvLol.csv”;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader(“内容处置”,附件);
HttpContext.Current.Response.ContentType=“text/csv”;
HttpContext.Current.Response.AddHeader(“Pragma”、“public”);
使用(var textWriter=File.CreateText(“MyCsvLol.csv”))
使用(var writer=new CsvWriter(textWriter))
{
foreach(费用中的var费用)
{
编剧、编剧记录(费用);
}
HttpContext.Current.Response.Write(writer);
HttpContext.Current.Response.End();
}
}
费用明细类别:
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()
{
}
}
公共类计费详细信息
{
公共字符串PatientNumber{get;set;}
公共字符串PateintName{get;set;}
公共字符串日期{get;set;}
公共字符串ChargeCode{get;set;}
公共字符串说明{get;set;}
公共字符串数量{get;set;}
公共字符串单价{get;set;}
公共字符串ExtAmt{get;set;}
公共静态列表GetDetailsForPatient(字符串patientNumber,字符串patientName)
{
列表费用=新列表();
DataTable thisDT=新DataTable();
正在使用(SqlConnection thisConnection=newsqlconnection(ConfigurationManager.ConnectionStrings[“DBConnectionString”].ConnectionString))
{
字符串SprocName=“GetChargeDetails”;
SqlDataAdapter thisAdapter=新的SqlDataAdapter(存储过程名称,thisConnection);
thisAdapter.SelectCommand.CommandType=CommandType.StoredProcess;
thisAdapter.SelectCommand.Parameters.AddWithValue(“@PatientNumber”,PatientNumber);
thisAdapter.Fill(thisDT);
}
foreach(此dt.行中的数据行)
{
费用。添加(新的费用明细(患者编号、患者姓名、行));
}
退货费用;
}
公用计费详细信息(字符串patientNumber、字符串patientName、数据行)
{
this.PatientNumber=PatientNumber;
this.PateintName=patientName;
this.Date=row[“SrvcDate”].ToString();
this.ChargeCode=行[“ChargeCode”].ToString();
this.Description=行[“ChargeDescription”].ToString();
此.Qty=行[“HHY_Qty”].ToString();
this.UnitPrice=row[“PatPrice”].ToString();
this.ExtAmt=行[“ExtAmt”].ToString();
}
公共收费详情()
{
}
}
您无法阅读“编写器”。保存文件后,将其读回以发送回用户
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();
你不能读“作家”。保存文件后,将其读回以发送回用户
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();
你不能读“作家”。保存文件后,将其读回以发送回用户
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();
你不能读“作家”。保存文件后,将其读回以发送回用户
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();
这让它起作用了
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();
}
protectedvoid下载ChargeDataAscsv(对象发送方,事件参数e)
{
列表费用=ChargeDetail.GetDetailsForPatient(lblPatientNum.Text,lblPatientName.Text);
string attachment=“附件;文件名=ChargeDetails.csv”;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader(“内容处置”,附件);
HttpContext.Current.Response.ContentType=“text/csv”;
HttpContext.Current.Response.AddHeader(“Pragma”、“public”);
使用(var memoryStream=new memoryStream())
使用(var streamWriter=新streamWriter(memoryStream))
使用(var streamReader=新streamReader(memoryStream))
使用(var writer=new CsvWriter(streamWriter))
{
书面记录(费用);
memoryStream.Position=0;
HttpContext.Current.Response.Write(streamReader.ReadToEnd());
}
HttpContext.Current.Response.End();
}
这让它工作起来了
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();
}
protectedvoid下载ChargeDataAscsv(对象发送方,事件参数e)
{
列表费用=ChargeDetail.GetDetailsForPatient(lblPatientNum.Text,lblPatientName.Text);
string attachment=“附件;文件名=ChargeDetails.csv”;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader(“内容处置”,附件);
HttpContext.Current.Response.ContentType=“text/csv”;
HttpContext.Current.Response.AddHeader(“Pragma”、“public”);
使用(var memoryStream=new memoryStream())
使用(var streamWriter=新streamWriter(memoryStream))
使用(var streamReader=新streamReader(memoryStream))
使用(var writer=new CsvWriter(streamWriter))
{
书面记录(费用);
memoryStream.Position=0;
HttpContext.Current.Response.Write(streamReader.ReadToEnd());
}
HttpContext.Current.Response.End();
}
这让它工作起来了
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();
}
protectedvoid下载ChargeDataAscsv(对象发送方,事件参数e)
{
列表费用=ChargeDetail.GetDetailsForPatient(lblPatientNum.Text,lblPatientName.Text);
string attachment=“附件;文件名=ChargeDetails.csv”;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current