我正在尝试将数据从Gridview导出到Excel,并将该Excel文件保存在服务器上的文件夹中。我已经完成了Excel生成部分。但我无法将其保存在文件夹中
请在下面找到我的代码。
代码
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "order.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gridX.AllowPaging = false;
bindX();
gridX.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < gridX.HeaderRow.Cells.Count; i++)
{
gridX.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
gridX.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
请帮我解决这个问题。
答案 0 :(得分:1)
希望这会对你有所帮助。首先填充gridview控件,然后使用RenderControl()方法将excel中的网格渲染到特定路径。
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
protected void Button1_Click(object sender, EventArgs e)
{
using (StreamWriter sw = new StreamWriter("c:\\test.xls"))
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(hw);
}
}
}