Ans:-
First create a website using your .net framework and then add a class file in this solution,automatically that class file will add in your App_code folder.Give that class file name as ExcelSheetFunctions.cs.
ExcelSheetFunctions.cs:-
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
namespace ExcelUtility
{
/// <summary>
/// Summary description for functions
/// </summary>
public class DataSetToExcel
{
public DataSetToExcel()
{
//
// TODO: Add constructor logic here
//
}
public static void Convert(DataSet ds, string filename)
{
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + ".xls\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
}
}
getExcel.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="getExcel.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Get/Download Data from DB to Xls file</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:Button ID="btnEnter" runat="server" Text="Get/Download Data from DB to Xls file"
onclick="btnEnter_Click" />
</center>
</div>
</form>
</body>
</html>
getExcel.aspx.cs:-
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using ExcelUtility;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnEnter_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=192.168.0.200;database=subsdb;user id=sa;pwd=amtpl@123");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM emp", cn);
DataSet ds = new DataSet();
ad.Fill(ds);
DataSetToExcel.Convert(ds, "Student List");
}
}
First create a website using your .net framework and then add a class file in this solution,automatically that class file will add in your App_code folder.Give that class file name as ExcelSheetFunctions.cs.
ExcelSheetFunctions.cs:-
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
namespace ExcelUtility
{
/// <summary>
/// Summary description for functions
/// </summary>
public class DataSetToExcel
{
public DataSetToExcel()
{
//
// TODO: Add constructor logic here
//
}
public static void Convert(DataSet ds, string filename)
{
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + ".xls\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
}
}
getExcel.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="getExcel.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Get/Download Data from DB to Xls file</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:Button ID="btnEnter" runat="server" Text="Get/Download Data from DB to Xls file"
onclick="btnEnter_Click" />
</center>
</div>
</form>
</body>
</html>
getExcel.aspx.cs:-
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using ExcelUtility;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnEnter_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=192.168.0.200;database=subsdb;user id=sa;pwd=amtpl@123");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM emp", cn);
DataSet ds = new DataSet();
ad.Fill(ds);
DataSetToExcel.Convert(ds, "Student List");
}
}
No comments:
Post a Comment