I have successfully exported data from a gridview to Excel for a single worksheet but I want to add multiple worksheets. I have googled around and tried a few options but nothing is quite working but I am getting close. I think it is a matter of just a few lines of code. Now I have the following code:
public ActionResult Download()
{
if (Session["BP"] != null)
{
return new DownloadFileActionResult((GridView)Session["BP"], "BestPrices.xls");
}
else
{
return new JavaScriptResult();
}
}
I populate the session variable using a datable and gridview:
GridView gv = new GridView();
gv.DataSource = gridTable;
gv.DataBind();
// Session["BP"] = myGridViews;
Session["BP"] = g;
I have built an array of gridviews thinking that would help but I cannot export that now.
GridView[] g = new GridView[retailerTables.Count];
int n = 0;
foreach (string key in keys)
{
myGridViews[n] = retailerTables[key];
g[n] = new GridView();
g[n].DataSource = retailerTables[key];
g[n].DataBind();
n++;
}
My basic idea is that each worksheet in the excel file corresponds to one gridview.
I have seen various third party options but I do not know how reliable they are and if I will end up debugging interface errors. I prefer to use the core classes and options in MVC.
The application is running on a website and the user will click a link to download the file.
The retailer tables are:
Dictionary<string, DataTable> retailerTables= new Dictionary<string, DataTable>();
Hence each worksheet will have sales data for a retailer and the user can finally just click on different retailers and see the sales data.
Does anyone know how to solve the issue to exporting multiple tables to excel as I have outlined.