真的很难找到答案,所以如果我可能忽略了重复问题,请道歉。 我有一个表,可以从数据库中提取多行。 每行都有一个唯一的 ID 和一个复选框。用户可以根据需要选中任意数量的复选框,然后单击按钮以生成包含其中每一行的CSV文件。 每个复选框将行 ID 添加到文本框中,用逗号分隔(即 146、147、148 等( - 然后我在后端将其拆分为字符串。 但是,虽然我可以得到一个 response.write 来显示记录,但我似乎无法让我的字符串在我检查的第一行的 CSV 文件栏上生成任何内容。 我本以为我的"foreach"会有所帮助,但似乎我想错了......
提供的任何建议将不胜感激!
protected void DownloadBtn_Click(object sender, EventArgs e)
{
string[] downloads = TxtDownloads.Text.Substring(1).Split(new char[] { ',' });
foreach(string downloadItem in downloads)
{
StringBuilder builder = new StringBuilder();
builder.Append("SchemeCode, SchemeCodeDescr, OwningCompany, PrimaryManagingCompany, ManagingCompany,RentAccountManagmentn");
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spR_CSVDownload", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", downloadItem);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
string entry = """ + reader["SchemeCode"].ToString() + "",";
entry += """ + reader["SchemeCodeDescr"].ToString() + "",";
entry += """ + reader["OwningCompany"].ToString() + "",";
entry += """ + reader["PrimaryManagingCompany"].ToString() + "",";
entry += """ + reader["ManagingCompany"].ToString() + "",";
entry += """ + reader["RentAccountManagment"].ToString() + "",";
builder.Append(entry);
}
}
}
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=download.csv");
Response.Write(builder.ToString());
Response.End();
}
我的建议...
using (SqlCommand cmd = new SqlCommand("spR_CSVDownload", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", downloadItem);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=download.csv");
while (reader.Read())
{
Response.Write(""" + reader["SchemeCode"].ToString() + "",");
Response.Write(""" + reader["SchemeCodeDescr"].ToString() + "",");
Response.Write(""" + reader["OwningCompany"].ToString() + "",");
Response.Write(""" + reader["PrimaryManagingCompany"].ToString() + "",");
Response.Write(""" + reader["ManagingCompany"].ToString() + "",");
Response.Write(""" + reader["RentAccountManagment"].ToString() + "",");
Response.Write(Environment.NewLine);
}
Response.End();
}
经过一番尝试,终于设法解决了这个问题。 我的原始代码有两个问题 - 首先是我在"foreach"语句之后设置了字符串生成器(需要在之前(;第二个小学生错误是用逗号而不是""结束我的"reader.read"语句(因此读者知道何时设置新行(。 下面更正的代码运行良好。
protected void DownloadBtn_Click(object sender, EventArgs e)
{
string[] downloads = TxtDownloads.Text.Substring(1).Split(new char[] { ',' });
StringBuilder builder = new StringBuilder();
builder.Append("SchemeCode, SchemeCodeDescr, OwningCompany, PrimaryManagingCompany, ManagingCompany,RentAccountManagmentn");
foreach (string downloadItem in downloads)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spR_CSVDownload", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", downloadItem);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
string entry = """ + reader["SchemeCode"].ToString() + "",";
entry += """ + reader["SchemeCodeDescr"].ToString() + "",";
entry += """ + reader["OwningCompany"].ToString() + "",";
entry += """ + reader["PrimaryManagingCompany"].ToString() + "",";
entry += """ + reader["ManagingCompany"].ToString() + "",";
entry += """ + reader["RentAccountManagment"].ToString() + ""n";
builder.Append(entry);
}
else { builder.Append("no item found for this one " + downloadItem + "n"); }
}
}
}
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "attachment;filename=download.csv");
Response.Write(builder.ToString());
Response.End();
}
}