Hi again everyone. In this tutorial, I am going to explain how to export Webgrid data into Microsoft Excel sheets. If you are new to Webgrid, please refer to the links of the posts, given below, to understand the basics of Webgrid.
If you have gone through these tutorials, you will get a basic understanding of Webgrid implementation. In this tutorial, I am going to use the code of the previous posts. I have implemented the screenshots given below of the Webgrid in the previous posts.

Exporting Webgrid to Excel in ASP.NET MVC
- Create Visual Studio Application in (2013 or 2015).
- Please refe to this tutorial Webgrid implementation in ASP.NET MVC5 Application. I am using the code in this tutorial, without creating a new Application.
- I am adding an extra code to implement exporting the data.
- Add ActionLink, given below, in an Index View, as shown below:
- <!--Create a link button to export data-->
- @Html.ActionLink("Export to Excel", "PrintExcel", "Home", new { @class="btn btn-success"})
- <br/>
- The complete code of the View is shown below:
Index.cshtml
- @model List<WebGridinMVC5.Models.UserTable>
- @{
- ViewBag.Title = "www.mitechdev.com";
- //create object for webgrid to use Webgrid helper
- var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 7);
- //pager
- grid.Pager(WebGridPagerModes.All);
- }
- <h2>Fetching and displaying data from server in WebGrid</h2><br/>
- <!--Create a link button to export data-->
- @Html.ActionLink("Export to Excel", "PrintExcel", "Home", new { @class="btn btn-success"})
- <br/>
- <!--web grid styles starts-->
- <style type="text/css">
- /*styles for altering rows*/
- .webgrid-row-style {
- padding: 3px 7px 2px;
- }
- .webgrid-alternating-row {
- background-color: #EAF2D3;
- padding: 3px 7px 2px;
- }
- </style>
- <!--web grid styles Ends-->
- <!--web grid display element-->
- <div id="webgrid">
- @grid.GetHtml(
- //styles class for table
- //here i used bootstrap table templates
- tableStyle: "table table-responsive backcolor",
- headerStyle:"wedgrid-header",
- footerStyle:"webgrid-footer",
- alternatingRowStyle:"webgrid-alternating-row",
- rowStyle:"webgrid-row-style",
- //binding table column to the grid
- columns:grid.Columns(
- grid.Column(header:"Serial No",format:@<text><div>@(item.WebGrid.Rows.IndexOf(item)+1)</div></text>),
- grid.Column(columnName:"Name",header:"Name"),
- grid.Column(header:"Email", format:@<text><a href="mailto:@item.Email">@item.Email</a></text>),
- grid.Column(columnName:"PhoneNumber",header:"Phone No"),
- grid.Column(header:"Is Active", format:@<text><input type="checkbox" checked="@item.Status" disabled="disabled" /></text>)
- )
- )
- </div>
- You can get the code, shown above, from the previous post's links also (I specified above).



Jorge GuatequePosted Mar 28, 2019, 10:59 AM
Hello from Colombia. Many thanks to you !!!
Miguel DíazPosted Aug 1, 2017, 6:53 PM
Public void PrintExcel([Bind(Include = "vpn_tb_equipo,admon_loc_tb_equipo, familia_tb_equipo,rfc8_tb_equipo, pto_patch_tb_equipo, tipo_tb_equipo, serie_sw_tb_equipo, marca_tb_equipo, inventario_tb_equipo, area_ubicacion_tb_equipo, perfil_tb_equipo, modelo_tb_equipo, serie_tb_equipo, estatus_tb_equipo, proyecto_tb_equipo, pto_switch_tb_equipo, proyecto_tb_equipo, direccion_ip_tb_equipo, direccion_mac_tb_equipo, nombre_pc_tb_equipo")] tb_equipo tb_equipo) { int cont = 0; int total = 0; string query = ""; Dictionary<string, string> param = new Dictionary<string, string>() { {"vpn_tb_equipo", tb_equipo.vpn_tb_equipo}, {"admon_loc_tb_equipo", tb_equipo.admon_loc_tb_equipo}, {"rfc8_tb_equipo",tb_equipo.rfc8_tb_equipo}, {"tipo_tb_equipo",tb_equipo.tipo_tb_equipo}, {"marca_tb_equipo",tb_equipo.marca_tb_equipo}, {"modelo_tb_equipo",tb_equipo.modelo_tb_equipo}, {"familia_tb_equipo",tb_equipo.familia_tb_equipo}, {"area_ubicacion_tb_equipo",tb_equipo.area_ubicacion_tb_equipo}, {"direccion_ip_tb_equipo",tb_equipo.direccion_ip_tb_equipo}, {"nombre_pc_tb_equipo",tb_equipo.nombre_pc_tb_equipo}, {"pto_switch_tb_equipo",tb_equipo.pto_switch_tb_equipo}, {"serie_sw_tb_equipo",tb_equipo.serie_sw_tb_equipo}, {"pto_patch_tb_equipo",tb_equipo.pto_patch_tb_equipo}, {"inventario_tb_equipo",tb_equipo.inventario_tb_equipo}, {"direccion_mac_tb_equipo",tb_equipo.direccion_mac_tb_equipo}, {"proyecto_tb_equipo",tb_equipo.proyecto_tb_equipo}, {"perfil_tb_equipo",tb_equipo.perfil_tb_equipo}, {"proyecto_dest_tb_equipo",tb_equipo.proyecto_dest_tb_equipo}, {"estatus_tb_equipo",tb_equipo.estatus_tb_equipo}, {"serie_tb_equipo",tb_equipo.serie_tb_equipo} }; query = "SELECT * from dbo.tb_equipo where"; ViewBag.Parametros = param; //total de valores recibidos no vac?os foreach (var equipo_param in param) { if (equipo_param.Value != null) if (equipo_param.Value.ToString().Trim() != "") total++; } object[] parameters = new object[total]; //llenar la consulta con los valores recibidos desde la vista Cambios foreach (var equipo_param in param) { if (equipo_param.Value != null) if (equipo_param.Value.ToString().Trim() != "") { query += " " + equipo_param.Key + " LIKE concat('%',@p" + cont + ",'%') AND"; parameters[cont] = equipo_param.Value; cont++; } } query = query.Remove(query.LastIndexOf(' ') + 1); // List<tb_equipo> equipo = db.tb_equipo.SqlQuery(query, parameters).ToList(); List<tb_equipo> equipo = null; equipo = db.tb_equipo.SqlQuery(query, parameters).ToList(); //return View(db.tb_equipo.SqlQuery(query, parameters).ToList()); //List<tb_equipo> equipo = db.tb_equipo.SqlQuery(query, parameters).ToList(); //create object to webgrid WebGrid grid = new WebGrid(source: equipo, canPage: false, canSort: false); string gridData = grid.GetHtml( columns: grid.Columns( grid.Column("vpn_tb_equipo", "vpn_tb_equipo"), grid.Column("admon_loc_tb_equipo", "admon_loc_tb_equipo"), grid.Column("rfc8_tb_equipo", "rfc8_tb_equipo"), grid.Column("tipo_tb_equipo", "tipo_tb_equipo"), grid.Column("marca_tb_equipo", "marca_tb_equipo"), grid.Column("familia_tb_equipo", "familia_tb_equipo"), grid.Column("area_ubicacion_tb_equipo", "area_ubicacion_tb_equipo"), grid.Column("direccion_ip_tb_equipo", "direccion_ip_tb_equipo"), grid.Column("nombre_pc_tb_equipo", "nombre_pc_tb_equipo"), grid.Column("pto_switch_tb_equipo", "pto_switch_tb_equipo"), grid.Column("serie_sw_tb_equipo", "serie_sw_tb_equipo"), grid.Column("pto_patch_tb_equipo", "pto_patch_tb_equipo"), grid.Column("direccion_mac_tb_equipo", "direccion_mac_tb_equipo"), grid.Column("proyecto_tb_equipo", "proyecto_tb_equipo"), grid.Column("perfil_tb_equipo", "perfil_tb_equipo"), grid.Column("proyecto_dest_tb_equipo", "proyecto_dest_tb_equipo"), grid.Column("serie_tb_equipo", "serie_tb_equipo"), grid.Column("estatus_tb_equipo", "estatus_tb_equipo") )).ToString(); Response.ClearContent(); //give name to excel sheet. Response.AddHeader("content-disposition", "attachment; filename=UserData.xls"); //specify content type Response.ContentType = "application/excel"; //write excel data using this method Response.Write(gridData); Response.End(); }
Miguel DíazPosted Aug 1, 2017, 6:52 PM
Nice, but I have a page of consult and the result is to the send to page of index and this result I want to export to Excel , but export all info, Don?t respect my query, you can help , me?, this is my code
Seshu BPosted Jun 12, 2017, 5:08 AM
Nice explanation...
Humayun Kabir MamunPosted Jul 24, 2016, 12:48 AM
Nice...
Anu VPosted Jul 23, 2016, 12:21 AM
Nice
farooq smdPosted Jul 22, 2016, 10:31 AM
Nice one
Shridhar SharmaPosted Jul 22, 2016, 1:56 AM
Nice share :)
Bhavik PatelPosted Jul 21, 2016, 9:48 PM
Nice
Nigel FernandesPosted Jul 21, 2016, 8:39 PM
Useful thanks
RakeshPosted Jul 21, 2016, 3:45 PM
Good share
kalu singh raoPosted Jul 21, 2016, 5:00 AM
Nice share