********************************************************************************
/*Add Stylesheet in your application names StyleSheet.css*/
body {}
.gridView{width:100%;background:#ffffff;border:1px solid #476db6; border-collapse:separate;}
.gridView th{border:0px;background:#a7b7fe;color:#000000;text-align:center;font-size:18px;}
.gridView td{border:0px;padding-left:10px;}
.gridView tr:hover{cursor:pointer; background:#ffff00;}
.gridView tr:last-child:hover{ background:#000;background:#a7b7fe;}
.altRow{background:#d1daff;}
.gridPager{background:#a7b7fe;color:#000000;}
.gridPager table{margin:0px auto;}
.gridPager table td{width:20px;}
.gridPager table span{cursor:pointer;font-weight:bold;color:#1a1ac9;}
.gridPager table a{cursor:pointer;text-decoration:none;color:#000000;}
.gridPager table a:hover{text-decoration:underline;}
********************************************************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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 id="Head1" runat="server">
<title>Create EXCEL Report in Asp.Net</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="butEXCEL" runat="server" Text="Generate EXCEL" onclick="butEXCEL_Click" />
</div>
<div>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false"
AllowSorting="true"
CssClass="gridView"
AlternatingRowStyle-CssClass="altRow"
PagerStyle-CssClass="gridPager">
<Columns>
<asp:BoundField DataField="Productid" HeaderText="Product Id"/>
<asp:BoundField DataField="Productname" HeaderText="Product"/>
<asp:BoundField DataField="Productdetails" HeaderText="Details"/>
<asp:BoundField DataField="Unitprice" HeaderText="Price" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
********************************************************************************
using System;
using System.IO;
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)
{
Products products = new Products();
GridView1.DataSource = products.ReadAll();
GridView1.DataBind();
}
}
protected void butEXCEL_Click(object sender, EventArgs e)
{
EXCELReport("Computers & Related Products", GridView1, "pdf1");
}
/// <summary>
/// Create Excel
/// </summary>
/// <param name="heading"></param>
/// <param name="grid"></param>
/// <param name="reportName"></param>
private void EXCELReport(String heading, GridView grid, String reportName)
{
Decimal columns = GridView1.Columns.Count;
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
table.GridLines = System.Web.UI.WebControls.GridLines.None;
#region Heading
TableRow htr = new TableRow();
htr.Cells.Add(new TableCell() { Text = heading, ColumnSpan = Convert.ToInt32(columns), HorizontalAlign = HorizontalAlign.Center });
table.Rows.Add(htr);
#endregion
#region Empty Row
table.Rows.Add(new TableRow());
#endregion
#region User,Search Information
TableRow userRow = new TableRow();
userRow.Cells.Add(new TableCell() { Text = String.Format("Customer : {0}", "Kaushik Mistry"), ColumnSpan = Convert.ToInt32(columns) });
table.Rows.Add(userRow);
TableRow SDateRow = new TableRow();
SDateRow.Cells.Add(new TableCell() { Text = String.Format("Durable goods report : {0}", "Computers & Related Products"), ColumnSpan = Convert.ToInt32(columns) });
table.Rows.Add(SDateRow);
TableRow EDateRow = new TableRow();
EDateRow.Cells.Add(new TableCell() { Text = String.Format("From Date : {0}", DateTime.Now), ColumnSpan = Convert.ToInt32(columns) });
table.Rows.Add(EDateRow);
#endregion
#region Empty Row
table.Rows.Add(new TableRow());
#endregion
//Gridlines to box the cells
table.GridLines = System.Web.UI.WebControls.GridLines.Both;
#region Header Columns
TableRow hRow = new TableRow();
for (Int32 i = 0; i <= 1; )
{
for (Int32 j = 0; j <= grid.Columns.Count - 1; j++)
{
hRow.Cells.Add(new TableCell() { Text = grid.Columns[j].ToString(), BackColor = System.Drawing.Color.LightGray, HorizontalAlign = HorizontalAlign.Center });
}
break;
}
table.Rows.Add(hRow);
#endregion
try
{
for (Int32 i = 0; i <= grid.Rows.Count - 1; i++)
{
TableRow tRow = new TableRow();
for (Int32 j = 0; j <= grid.Columns.Count - 1; j++)
{
tRow.Cells.Add(new TableCell() { Text = grid.Rows[i].Cells[j].Text });
}
table.Rows.Add(tRow);
}
}
catch (ArgumentOutOfRangeException)
{
Response.Write("Argument Out Of Range !");
}
table.RenderControl(htw);
}
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", String.Format("attachment;filename=Report-{0}.xls", reportName));
Response.Buffer = true;
Response.Write(sw.ToString());
Response.End();
}
}
}
********************************************************************************
using System;
using System.Web.Configuration;
public class Connection
{
public Connection(){}
/// <summary>
/// Get Connection string.
/// <summary>
public static String Cs
{
get
{
return WebConfigurationManager.ConnectionStrings["cs"].ConnectionString;
}
}
}
********************************************************************************
#region Namespaces
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
#endregion
/// <summary>
/// class Products
/// </summary>
public class Products
{
#region Properties
private Int32 _Productid;
/// <summary>
///Gets or Sets the Int32 value of Productid
/// </summary>
public Int32 Productid { get { return _Productid;} set { _Productid = value;} }
private String _Productname;
/// <summary>
///Gets or Sets the String value of Productname
/// </summary>
public String Productname { get { return _Productname;} set { _Productname = value;} }
private String _Productdetails;
/// <summary>
///Gets or Sets the String value of Productdetails
/// </summary>
public String Productdetails { get { return _Productdetails;} set { _Productdetails = value;} }
private Decimal _Unitprice;
/// <summary>
///Gets or Sets the Decimal value of Unitprice
/// </summary>
public Decimal Unitprice { get { return _Unitprice;} set { _Unitprice = value;} }
private Int32 _Quantity;
/// <summary>
///Gets or Sets the Int32 value of Quantity
/// </summary>
public Int32 Quantity { get { return _Quantity;} set { _Quantity = value;} }
#endregion
#region Private Meambers
private SqlCommand cmd;
private SqlConnection con;
private SqlDataReader table;
private String ConnectionString = String.Empty;
#endregion
#region Constructor
/// <summary>
///Default constructor
/// </summary>
public Products()
{
ConnectionString=Connection.Cs;
}
/// <summary>
///Parameterised constructor
/// </summary>
public Products(String connectionString)
{
ConnectionString=connectionString;
}
#endregion
#region Public Methods
/// <summary>
/// This meathod returns all records of Products table.
/// </summary>
/// <returns> All rows of Products table.</returns>
public List<Products> ReadAll()
{
using(con = new SqlConnection(ConnectionString))
{
try
{
List<Products> list = new List<Products>();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Select Top(1000)* From [dbo].[Products]",con);
using(table = cmd.ExecuteReader())
{
while (table.Read())
{
Products obj = new Products();
obj._Productid = table.GetInt32(0);
obj._Productname = table.GetString(1);
obj._Productdetails = table.GetString(2);
obj._Unitprice = table.GetDecimal(3);
obj._Quantity = table.GetInt32(4);
list.Add(obj);
}
//return (list.Count > 0) ? list : GetEmptyList(list);
return list;
}
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
#endregion
}
********************************************************************************
<add name="cs" connectionString="Data Source=Your Data Source;Initial Catalog=softwarekaffee;Persist Security Info=True;User ID=Your User Id;Password=Your Password"/>
</connectionStrings>
********************************************************************************
Go
create database SoftwareKaffee
Go
use SoftwareKaffee
Go
Create table Products
(
ProductId int identity primary key,
ProductName varchar(50),
ProductDetails nvarchar(200),
UnitPrice decimal(7,2),
Quantity int
)
Go
insert into Products values('Dell Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',55000,4)
insert into Products values( 'HCL Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',15000,30 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB', 23000 ,4 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',35000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 2GB',25000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',30000,30 )
insert into Products values( 'Dell Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',45000,10 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',55000,30 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 2GB',35000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',25000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',30000,60 )
insert into Products values( 'Dell Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',35000,30 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',55000,30 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',34000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',35000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',40000,30 )
insert into Products values( 'Dell Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',65000,30 )
insert into Products values( 'HP Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',75000,1 )
insert into Products values( 'HCL Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',45000,30 )
insert into Products values( 'Linovo Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',45000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',37000,5 )
No comments:
Post a Comment