我试图将网格视图数据传输到 excel .... 但输出是一个空白的 excel 表。如何解决这个问题?是否有任何代码可以将网格视图值传输到带有数据库的 excel 表?
protected void btnexcel_Click1(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
gvdetails.HeaderRow.Cells[0].Style.Add("background-color", "green");
gvdetails.HeaderRow.Cells[1].Style.Add("background-color", "green");
gvdetails.HeaderRow.Cells[2].Style.Add("background-color", "green");
for (int i = 0; i < gvdetails.Rows.Count;i++ )
{
GridViewRow row = gvdetails.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
}
}
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.End();
}