gpt4 book ai didi

c# - 导出当前gridview数据

转载 作者:行者123 更新时间:2023-11-30 13:06:21 26 4
gpt4 key购买 nike

我正在使用此代码导出我的 gridview 数据。我的页面中有一个下拉菜单,当我选择下拉菜单值时,gridview 中的数据发生变化,我只需要将当前数据导出到 excel 中。

protected void ExporttoExcel(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=Report.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

我正在使用分页,当我最初导出时没有选择下拉值我需要导出所有数据,但现在只导出第一页中的数据。

谁能纠正我哪里错了?

最佳答案

我有一个自定义的方法,用于从 Datatable-Gridvieww 导出格式化样式的 excel。您可以使用它,通过将绑定(bind) Gridview 的数据表分配给表变量(分页前的整个数据表)

private void ExportToExcel(DataTable table, GridView gv)
{

Response.ClearContent();
Response.Charset = your charset//"Windows-1253";
Response.ContentEncoding = your encoding//Encoding.UTF8;
string attachment = "attachment; filename=test.xls";
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");

HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
int columnscount = table.Columns.Count;

for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("<Td style='background-color: #C0C0C0;' align=" + "center" + ">");
HttpContext.Current.Response.Write("<B>");
HttpContext.Current.Response.Write(table.Columns[j].ToString());
HttpContext.Current.Response.Write("</B>");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in table.Rows)
{
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td align=" + "center" + ">"); ;
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}

HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}

关于c# - 导出当前gridview数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23677773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com