【发布时间】:2016-05-09 10:05:33
【问题描述】:
在我的页面中,我有两个文本框控件,其中我从日历扩展器中选择日期,在导出到 excel 按钮中我要将 Gridview 数据导出到 excel 表。 当我对 excel 表进行门控时,它会显示我从中导出 excel 表的文本框和按钮。
我已经在导出按钮中编写了导出代码。 喜欢:-
protected void Export_to_Excel_Click(object sender, EventArgs e)
{
try
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Grd_MidData.AllowPaging = false;
bindgriddata();
//Change the Header Row back to white color
Grd_MidData.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < Grd_MidData.HeaderRow.Cells.Count; i++)
{
Grd_MidData.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
Grd_MidData.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
}
catch (Exception ee)
{
}
}
当我对 excel 表进行门控时,它会显示 gridview 数据以及两个文本框和我进行过滤的按钮。
所以任何人都建议我,如何只显示 gridview 数据而不显示 Excel 工作表上的文本框和按钮。
【问题讨论】:
-
几乎所有的答案都在这里写一个(HtmlTextWriter)字符串或有互操作代码。两者都不要使用。这将导致您稍后在 DateTime 和 Decimal 格式方面出现问题。 Excel 也会发出警告,因为您生成的不是“真正的”Excel 文件,而是扩展名为 .xls 的 HTML 页面。开始使用专门的库来创建 Excel 文件,例如 EPPlus。 Example here 和 here.