Error in exporting XLS to Webforms projects

Asked

Viewed 16 times

0

good night!

I am trying to export the dataset to error when trying to generate XLS: System.Indexoutofrangeexception: 'Unable to find table 0.'

        DataSet ds = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ds.ReadXml(Server.MapPath("~/cliente.xml"), XmlReadMode.Auto);
                gvDados.DataSource = ds;
                gvDados.DataBind();
            }
        }

        protected void btnRelatorioXLS_Click(object sender, EventArgs e)
        {
            ExportarXLS(this, ds, "Cadastro"); //Chamada do metodo para exporta Dataset para XLS
        }

        private void ExportarXLS(Page pagina, DataSet ds, String filename)
        {
            DataGrid dg =new DataGrid();
            dg.AutoGenerateColumns = true;
            dg.DataSource = ds.Tables[0]; 
            dg.DataBind();
            pagina.Response.Clear();
            pagina.Response.ContentType = "aplication/vdn.ms-excel";
            pagina.Response.AddHeader("content-disposition",
                                         "attachament;filename=" + filename + ".xlsx");
            dg.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            dg.RenderControl(hw);
            Response.Write(tw.ToString());
            pagina.Response.End();
        }

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.