将数据库中具有相同id的多行错误下载到xls文件中

本文关键字:错误 下载 xls 文件 id 数据库 | 更新日期: 2023-09-27 18:04:17

我正在使用这段代码,目前下载存储在列'data'和'data2'驻留在sql数据库中的fildemo表中的错误。

  protected void helloGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            String connectionString = DAO.GetConnectionString();
            String sqlQuery = String.Empty;

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();
                sqlQuery = "select data,data2 from filedemo where id=@id";
                SqlCommand cmd = new SqlCommand(sqlQuery, con);
                cmd.Parameters.AddWithValue("id", surgicalGridView.SelectedRow.Cells[1].Text);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    HttpContext.Current.Response.ClearContent();
                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + dr["name"].ToString() + ".xls");
                    HttpContext.Current.Response.ContentType = "application/excel";
                    Response.Charset = "";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Write(dr["data"] + "'t");
                    Response.Write(dr["data2"] + "'t");
                    Response.End();
                }
            }
        }

我想从具有相同Id的不同行中取出错误,并将它们存储在excel文件中的单独行中。我该怎么做呢?

将数据库中具有相同id的多行错误下载到xls文件中

您试过Response.Write(dr["data"] + "'t'n'r");吗?