Export DataSet to excel 2007 EPPlus

本秂侑毒 提交于 2019-12-05 19:06:47

Couple things. This isnt really an epplus problem, more general web.

First, you are setting the grids DataSource to a dataSET here:

using (DataSet ds = new DataSet())
{
    ds.ReadXml(Server.MapPath("~/Customers.xml"));
    GridFornecedor.DataSource = ds;

but are later casting to a dataTABLE here:

DataTable ds = GridFornecedor.DataSource as DataTable;

when you should first cast to a dataset then get the first table of its Table collection.

But that still will not fix the problem because you have a class-level object which will not presist across postbacks. You need to use a session or viewstate variable like this:

public void BindGrid()
{
    using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath("~/Customers.xml"));
        GridFornecedor.DataSource = ds;
        GridFornecedor.DataBind();
        ViewState["GridDataSource"] = ds;
    }
}


public void btnExportClick(object sender, EventArgs e)
{
    //DataTable ds = GridFornecedor.DataSource as DataTable;
    var ds = ViewState["GridDataSource"] as DataSet;
    var dt = ds.Tables[0];
    ExportExcel(dt);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!