C# - Exporting OBOUT Grid to Excel in Asp .Net
OBOUT - ASP.NET Grid - Export to Excel and Word on Server-Side
In previous articles I have explained about OBOUT Grid Control - .Net, C# - Binding Obout Grid to DataTable. In this article I will show how to export Obout grid to excel file in asp .net with c#. The Grid is exported according to the settings specified using the ExportingSettings property. Find the source code below:

Default.aspx
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@
Register Assembly="obout_Grid_NET" Namespace="Obout.Grid" TagPrefix="obout" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>C# - Exporting OBOUT Grid to
Excel in Asp .Net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<obout:Grid ID="grdview_details" AllowSorting="true" PageSize="5" AllowManualPaging="true" runat="server" FolderExports="resources/exports"
OnExported="grdview_details_Exported">
</obout:Grid>
<br />
<br />
<asp:Button ID="btnExport2Excel" runat="server" Text="Export To Excel" OnClick="btnExport2Excel_Click" />
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindOboutTest();
}
}
private void BindOboutTest()
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpId",
typeof(int));
dt.Columns.Add("Name",
typeof(string));
dt.Columns.Add("Address",
typeof(string));
dt.Columns.Add("Date",
typeof(DateTime));
//
// Here we add five
DataRows.
//
dt.Rows.Add(25, "Rk",
"Gurgaon", DateTime.Now);
dt.Rows.Add(50, "Sachin",
"Noida", DateTime.Now);
dt.Rows.Add(10, "Nitin",
"Noida", DateTime.Now);
dt.Rows.Add(21, "Aditya",
"Meerut", DateTime.Now);
dt.Rows.Add(100, "Mohan",
"Banglore", DateTime.Now);
dt.Rows.Add(251, "PK",
"Gurgaon", DateTime.Now);
dt.Rows.Add(501, "Kanti",
"Noida", DateTime.Now);
dt.Rows.Add(101, "Ankit",
"Noida", DateTime.Now);
dt.Rows.Add(211, "Arun",
"Meerut", DateTime.Now);
dt.Rows.Add(101, "Tonu",
"Banglore", DateTime.Now);
grdview_details.DataSource = dt;
grdview_details.ExportingSettings.ExportTemplates = true;
grdview_details.DataBind();
}
protected void grdview_details_Exported(object sender, Obout.Grid.GridExportEventArgs e)
{
foreach (TableRow item in e.Table.Rows)
{
int
cellcount = item.Cells.Count;
for
(int i = 0; i < cellcount;
i++)
{
item.Cells[i].Style.Add("border", "thin solid
black");
}
// if not using css.
e.Table.GridLines = GridLines.None;
}
}
protected void btnExport2Excel_Click(object sender, EventArgs e)
{
try
{
string fileName =
grdview_details.ExportToExcel("OboutGrid_",
false, true, false, true);
lblMsg.Text = "The Grid has been exported
to an Excel file on the server. <br /><a
href=\"resources/exports/"
+ fileName + "\">Click here to download the file.</a>";
}
catch (Exception ex)
{
throw;
}
}
}
OBOUT Grid - Exporting to Word
Same as you can export Obout Grid to Word document using string fileName = grdview_details.ExportToWord("OboutGrid_", false, true, false, true);
Download the source code click here
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading!
C# - Exporting OBOUT Grid to Excel in Asp .Net
Reviewed by Ravi Kumar
on
11:36 AM
Rating:

Nice post Ravi. thanks for sharing
ReplyDelete