c# - 导出当前gridview数据

标签 c# asp.net .net gridview export-to-excel

我正在使用此代码导出我的 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/

相关文章:

c# - 当轮廓可以撕裂时,如何检测简单形状(使用emgu cv)?

c# - 使用 asp.net 和 C# 从 javascript 调用外部服务器上的 webservice

c# - 为 Windows 编写用户模式文件系统?

c# - 如何从asp.net中的文件夹上传图像?

c# - Entity Framework 运行直接查询

c# - Azure 辅助角色的多个实例有多少个队列?

c# - 查找控件文本 (ASP.NET/C#)

asp.net - Visual Studio 2010 包括旧版本的 jquery

c# - 泛型方法返回类型作为类型参数

.net - 菜单项触发器不工作