Export to word using MVC 4

In previous article I had told you how to export data into excel, in this article I will tell you how to export data into word using Entity-Framework and MVC 4. For this you need to follow some step.

1.  Open visual studio => New Project => Select ASP.Net MVC4 web Application => Name it Export_Word => Click ok

2.  Select Empty click ok

1

1

3.  Add reference of System.Data.Entity and EntityFramework to the project by right click on references in solution.

4. Add connection string and config section in web.config


<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Export-20131010113654;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Export-20131010113654.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>

5.  Add two class/model files Export_Word and StudentDetail in model. Apply following code respectively in that


using System.Data.Entity;

public class Export_Word : DbContext
{
public DbSet<StudentDetail> Studentrecord { get; set; }
}

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;

public class StudentDetail
{
[Key]
public int id { get; set; }
[Required]
public String Name { get; set; }
public String Address { get; set; }
public String Marks { get; set; }
}

6. Now build code and for adding controller right click on controller => Add => Controller.

7.  Give controller name to “studentdetail” model class “studentdetail” and data context class “Export_word” and click add. And it will create 5 .cshtml files in view.

2

2

8. Now open App_start =>RouteConfig.cs file in solution and change controller name to StudentDetail and run application.

3

3

9. After running application if it cause error then type “/StudentDetail/Create” (after http://localhost:2716) in url and add data.

4

4

10. After adding some record open studentdetail controller in solution and add code in that


public ActionResult ExportData()
{
GridView gv = new GridView();
gv.DataSource = db.Studentrecord.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Marklist.doc");
Response.ContentType = "application/vnd.ms-word ";
Response.Charset = string.Empty;

StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

return RedirectToAction("StudentDetails");
}

11. Add following code in index.cshtml


@using (Html.BeginForm("ExportData", "Studentdetail", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td></td>
<td>
<input type="submit" name="Export" id="Export" value="Export" /></td>
</tr>

</table>
}

12. Now run application and click on export button. This will export your data into work.

5

5

For download full code click here.

6 thoughts on “Export to word using MVC 4

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.