出口的GridView数据到Excel中的ASP.NET的LINQ数据绑定 [英] Export Gridview Data to Excel in ASP.NET Linq data bind

查看:99
本文介绍了出口的GridView数据到Excel中的ASP.NET的LINQ数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用怒吼code出口的GridView数据到excel,但问题是,整个页面导出到Excel。 我只希望gridview的数据不整页输出。如何才能解决这个问题呢?

I am using bellow code for export gridview data to excel but problem is that whole page export to excel. I want only gridview data not whole page export. How can solve this problem?

HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls";

Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gdvInBox.AllowPaging = false;
gdvInBox.DataBind();
form.Controls.Add(gdvInBox);
this.Controls.Add(form);
form.RenderControl(hw);

//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

在此先感谢。

推荐答案

这是pretty容易建立的Excel导出,将仅导出gridview的。这已经过测试,并且只导出您的特定网页上显示的GridView控件。

It's pretty easy to set up the excel export that will export only the gridview. This has been tested and will only export the gridview that appears on your given web page.

有关你的C#code使用以下命令:

For your C# code use the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class vxcel_export : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=file-name.xls");
        Response.ContentType = "application/vnd.xlsx";
        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();
    }    

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}

在你的aspx code使用以下命令:

In your aspx code use the following:

请确保您添加 EnableEventValidation =FALSE&LT;%@页%&GT; code。在页面的顶部。

Make sure you add EnableEventValidation="false" to the <%@Page %> code at the top of the page.

将以下code,你想要把按钮你的GridView导出到Excel:

Place the following code where you want to put the button to export your gridview to Excel:

<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Whatever you want your button to say" />

您可以修改高度和宽度要在按钮任意大小。

You can modify the Height and Width to whatever size you want in the button.

就是这样。有一点要记住的是,当你导出文件它不是一个真正的Excel文件,直到你将其保存为工作簿/ Excel文件。

That's it. One thing to keep in mind is that when you export the file it's not a true excel file until you save it as a workbook/Excel File.

这篇关于出口的GridView数据到Excel中的ASP.NET的LINQ数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆