Jump to content
Sergio990

Exportar GridView [RESOLVIDO]

Recommended Posts

Sergio990

Boa tarde.

Eu tenho que criar espécies de relatórios em ASPX.NET que são gridViews com informação que tenho seleccionada.

Mas só que eu queria que desse para exportar essa gridView para um excel ou assim.

alguém me pode ajudar?

Agradecia Imenso :)

Share this post


Link to post
Share on other sites
Sergio990

Eu optei por este metodo:

        protected void  Button2_Click(object sender, EventArgs e)
{
            DataTable dtOriginal = new DataTable();
            dtOriginal = ReturnTable();  //Return Table consisting data

        //Create Tempory Table
            DataTable dtTemp = new DataTable();

        //Creating Header Row
        dtTemp.Columns.Add("<b>NomeCliente</b>");
        dtTemp.Columns.Add("<b>Morada</b>");
        dtTemp.Columns.Add("<b>nomeSite</b>");
        dtTemp.Columns.Add("<b>VlanId</b>");
        dtTemp.Columns.Add("<b>netmask</b>");
        dtTemp.Columns.Add("<b>gamaIp</b>");
        dtTemp.Columns.Add("<b>gateway</b>");
        dtTemp.Columns.Add("<b>nome</b>");
        dtTemp.Columns.Add("<b>ip</b>");
        dtTemp.Columns.Add("<b>hostname</b>");
        dtTemp.Columns.Add("<b>fqdn</b>");
        dtTemp.Columns.Add("<b>descricao</b>");

        DataRow drAddItem;
        for (int i = 0; i < dtOriginal.Rows.Count; i++)
        {
        drAddItem = dtTemp.NewRow(); 
      drAddItem[0] = dtOriginal.Rows[i][0].ToString();//NomeCliente
        drAddItem[1] = dtOriginal.Rows[i][1].ToString();//Morada
        drAddItem[2] = dtOriginal.Rows[i][2].ToString();//nomeSite
        drAddItem[3] = dtOriginal.Rows[i][3].ToString();//VlanId
        drAddItem[4] = dtOriginal.Rows[i][4].ToString();//netmask
        drAddItem[5] = dtOriginal.Rows[i][5].ToString();//gamaIp
        drAddItem[6] = dtOriginal.Rows[i][6].ToString();//gateway
        drAddItem[7] = dtOriginal.Rows[i][7].ToString();//nome
        drAddItem[8] = dtOriginal.Rows[i][8].ToString();//ip
        drAddItem[9] = dtOriginal.Rows[i][9].ToString();//hostname
        drAddItem[10] = dtOriginal.Rows[i][10].ToString();//fqdn
        drAddItem[11] = dtOriginal.Rows[i][11].ToString();//descricao

        dtTemp.Rows.Add(drAddItem);
        }

        //Temp Grid
        DataGrid dg = new DataGrid();
        dg.DataSource = dtTemp;
        dg.DataBind();
        ExportToExcel("listaIps.xls", dg);
        dg = null;
        dg.Dispose();
        }
    
    private void ExportToExcel(string strFileName, DataGrid dg)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
        Response.ContentType = "application/excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        dg.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
      }
    }

E ele ja coloca no excel o cabeçalho de cada coluna mas o conteudo nao, e da um erro onde diz Return Table ();

E nao sei porque

Share this post


Link to post
Share on other sites
Sergio990

O codigo que me deu sem problemas fui:

protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Clear();

        Response.AddHeader("content-disposition", "attachment;filename=ListaIps.xls");

        Response.Charset = "";

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        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();

    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }

Com os melhores cumprimentos,

Sérgio Rodrigues

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.