Sunday, August 11, 2019

How to export gridview to excel & Word file in asp.net

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok. 
In this example, I have used one tables as below 

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish. 

Step-5: Add a Webpage and Design for Show Data in Gridview.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code 
  1. <h3>Export Gridview in Word & Excel</h3>
  2. <br />
  3. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="5" >
  4. <Columns>
  5. <asp:BoundField HeaderText="Rank" DataField="Rank" />
  6. <asp:BoundField HeaderText="River Name" DataField="RiverName" />
  7. <asp:BoundField HeaderText="Length (KM)" DataField="LengthInKM" />
  8. <asp:BoundField HeaderText="Drainage Area" DataField="DrainAgeArea" />
  9. <asp:BoundField HeaderText="Avarage Discharge" DataField="AvarageDischarge" />
  10. <asp:BoundField HeaderText="Outflow" DataField="Outflow" />
  11. </Columns>
  12. </asp:GridView>
  13. <div>
  14. <asp:Button ID="btnExportWord" runat="server" Text="Export To Word" OnClick="btnExportWord_Click" />&nbsp;
  15. <asp:Button ID="btnExportExcel" runat="server" Text="Export To Excel" OnClick="btnExportExcel_Click" />
  16. </div>

Step-6: Write following code in Page_Load event for Show data in Gridview.


  1. if (!IsPostBack)
  2. {
  3. PopulateData();
  4. }
Here is the function... 
  1. private void PopulateData()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. GridView1.DataSource = dc.Rivers.ToList();
  6. GridView1.DataBind();
  7. }
  8. }

Step-7: Write below code in a function for export data from gridview to Excel/Word File.


  1. private void ExportGrid(string fileName, string contentType)
  2. {
  3. Response.Clear();
  4. Response.Buffer = true;
  5. Response.AddHeader("content-disposition", "attachment;filename="+fileName);
  6. Response.Charset = "";
  7. Response.ContentType = contentType;
  8.  
  9. StringWriter sw = new StringWriter();
  10. HtmlTextWriter HW = new HtmlTextWriter(sw);
  11. GridView1.RenderControl(HW);
  12. Response.Output.Write(sw.ToString());
  13. Response.Flush();
  14. Response.Close();
  15. Response.End();
  16. }

Step-8: Write code in button_click event for export gridview to Word file.


  1. protected void btnExportWord_Click(object sender, EventArgs e)
  2. {
  3. // Export Gridview to Word
  4. ExportGrid("GridviewData.doc", "application/vnd.ms-word");
  5. }

Step-9: Write code in button_click event for export gridview to Excel file.


  1. protected void btnExportExcel_Click(object sender, EventArgs e)
  2. {
  3. // Export Gridview to Excel
  4. ExportGrid("GridviewData.xls", "application/vnd.ms-excel");
  5. }
And this function is also required to add. This is required to solve this problem --> Control 'MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
  1. public override void VerifyRenderingInServerForm(Control control)
  2. {
  3. // this is required for avoid error (control must be placed inside form tag)
  4. }
Step-10: Run Application

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...