Click here to Skip to main content
15,885,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi members,

How do I export 2 grid views (GridView1 & GridView2) into 2 separated sheets in 1 ms-excel file? Currently, I'm able to export only 1 grid view to an excel sheet which the filename is the same as the sheet name. But I would like to 2 grid views into 2 separated sheets which I would like sheet name to be define/set by myself. thanks.

My main question is:

1.How can I insert more grid views in my code to be add into separate worksheets (e.g Gridview1 into worksheet1 and Gridview2 into worksheet2)?

2.How can I set each of the worksheet names in my codes?

3.How can I download the excel into the file path automatically by stating a file path in my code?


Currently, Im only able to export 1 gridview into an excel file with the click on a button.
C#
   public void ExportGridToExcel()
{
    Response.Clear();
    Response.Buffer = true;
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Charset = "";
    string FileName ="Export"+DateTime.Now+".xls";
    StringWriter strwritter = new StringWriter();
    HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
    GridView1.GridLines = GridLines.Both;
    GridView1.HeaderStyle.Font.Bold = true;
    GridView1.RenderControl(htmltextwrtter);
    Response.Write(strwritter.ToString());
    Response.End();
}
 
    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }
Posted
Comments
F-ES Sitecore 29-Sep-15 4:32am    
Your code isn't generating excel spread sheets, it is just writing out an html table. As worksheets are an Excel concept and you're not generating Excel documents it's not possible to dictate features specific to Excel documents. To generate sheets in a file you'll need to make an actual Excel file using a library that generates Excel documents. Look at the XML SDK, or the Excel ODBC driver might be able to create multiple worksheets too.

http://www.codeproject.com/Articles/219/Creating-Excel-Sheets-using-ODBC

And there are commercial products like Aspose etc too.
waynetan123 29-Sep-15 5:52am    
Hi thanks for the reply, due to restrictions is there other ways todo it without the need to download additional software or use other commercial products?
F-ES Sitecore 29-Sep-15 5:57am    
No. The Excel odbc driver is probably the least-obtrusive of all possible solutions, it might even already be on the machine.
Richard Deeming 29-Sep-15 9:19am    
Without commercial products is easy - there are various free and open-source libraries available:
* EPPlus[^];
* ClosedXML[^];
* The OpenXML SDK[^];

Without "additional software" is very difficult - you'd need to replicate what these libraries are doing in your own code, with no obvious benefit.
Maciej Los 29-Sep-15 13:00pm    
Sounds like an answer ;)

1 solution

The answer to all your questions you'll find on CP Knowledge Base[^]. Tons of examples is waiting for discover.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900