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

时间:2013-03-17 23:43:54

标签: c# asp.net sql sql-server

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

  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文件的不同行中。我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

您是否尝试过Response.Write(dr["data"] + "\t\n\r");