c#从gridview导出的Excel文件出现标头大小问题

时间:2018-08-27 09:53:24

标签: c# excel datagridview export

我正在将gridview数据导出到Excel文件。 excel文件只有两个列。 我已将标题颜色设置为PINK。但是,此PINK Here is my Output file颜色设置为该标头的整个行,而不是仅设置为两列。

这是我的代码...

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=User_Log.xls");
Response.Charset = "";
Response.ContentType = "application/excel ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;

myConn.Open();
SqlCommand cmd = new SqlCommand(@"select User_id , LoginDate from LoginLog where LoginDate between
                                ('" + TextBox1.Text + "') and ('" + ToDate + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;

GridView1.HeaderStyle.BackColor = Color.Red;

GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
myConn.Close();

0 个答案:

没有答案