NET 2008中导出到Excel问题 [英] Export to excel problem in .net 2008

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

问题描述

我使用的是2008版.我使用以下代码将数据从gridview导出到excel,但是它不起作用.

I use 2008 version. I use following code to export data from gridview to excel, but it not working.

protected void Button1_Click(object sender, EventArgs e)
    {
  Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.Charset = "";
        // If you want the option to open the Excel file without saving than
        // comment out the line below
        // Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.xls";
        StringWriter stringWrite = new System.IO.StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView2.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();

}
public override void VerifyRenderingInServerForm(Control control)
       {


           GridView grid = control as GridView;
           if (grid != null && grid.ID == "GridView2")
               return;
           else
               base.VerifyRenderingInServerForm(control);
       }

推荐答案


嗨...

你可以试试...
私有void ExportGridView()
{
字符串附件=附件;文件名=员工details.xls";
Response.ClearContent();
Response.AddHeader("content-disposition",附件);
Response.ContentType ="application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw =新的HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Hi ...

U can try this...
private void ExportGridView()
{
string attachment = "attachment; filename=Employee details.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

private void PrepareGridViewForExport(Control gv)
    {
        LinkButton lb = new LinkButton();
        Literal l = new Literal();
        string name = String.Empty;
        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if (gv.Controls[i].GetType() == typeof(LinkButton))
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(DropDownList))
            {
                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(CheckBox))
            {
                l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }


将Gridview作为子控件添加到UpdatePanel.然后用这个

UpdatePanel.RenderControl(htmlWrite);
Add Gridview as a child control to an UpdatePanel. Then use this,

UpdatePanel.RenderControl(htmlWrite);


这篇关于NET 2008中导出到Excel问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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