I have two gridviews on my page currently. I am able to export one gridview to the excel file. But for the second gridview I want it to open in another worksheet in the same file. Can anyone just help me get started and tell me how to go about it?
Here's what my code looks like:
protected void btnExport_Click(object sender, EventArgs e)
{
GridView gridView = null;
System.IO.StringWriter stringWrite = null;
System.Web.UI.HtmlTextWriter htmlWrite = null;
try
{
gridView = new GridView();
List<string> columns = new List<string>();
columns.Add("CREATE_AR_YN");
columns.Add("GL_DEBIT_ACCT");
columns.Add("ALLOC_METHOD");
columns.Add("CUST_NAME");
columns.Add("DEFAULT_PYMT_TERMS");
columns.Add("AR_BILL_GROUP");
BoundField bf = null;
for (int i = 0; i < columns.Count; i++)
{
bf = new BoundField();
bf.DataField = columns[i];
bf.HeaderText = columns[i];
bf.HeaderStyle.BackColor = System.Drawing.Color.FromName("#81DAF5");
gridView.Columns.Add(bf);
gridView.AutoGenerateColumns = false;
}
List<FMAProfile> custList = GetSelectedCustomerProfile();
gridView.DataSource = custList;
gridView.DataBind();
Response.Clear();
Response.ClearHeaders();
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.AddHeader("content-disposition", "attachment;filename=" + "CustomerProfile" + ".xls");
Response.Charset = "";
//Response.ContentType = "application/vnd.xls";
Response.ContentType = "application/vnd.ms-excel";
//Response.Buffer = false;
stringWrite = new System.IO.StringWriter();
htmlWrite = new HtmlTextWriter(stringWrite);
gridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch (Exception ex)
{
if (ex.Message.Trim().IndexOf("Thread was being aborted") == -1)
{
Log(ex.ToString());
}
}
finally
{
if (gridView != null)
{
gridView.Dispose();
gridView = null;
}
if (stringWrite != null)
{
stringWrite.Dispose();
}
stringWrite = null;
if (htmlWrite != null)
{
htmlWrite.Dispose();
}
htmlWrite = null;
}
}