Exports GridView and Image to Excel


MS Visual Studio 2008 Professional Edition
MS SQL Server 2005 EXPRESS

1.

1

2.

2

3.

3

4.

4

5.

5

6.

6

ออกแบบหน้าจอ

7.

 image

8.

image 

image

9.

image

image

10.

image

11.

image

12.

image

13.

image

14.

image

15.

image

16.

image

17.

image

18.

image

19.

image

20.

image

21.

image

22.

image

23.

image

24.

image

25.

image

26.

image

27.

image

28.

image

29.

image

30.

image

31.

image

<ItemTemplate>
   <asp:Image ID="Image1" runat="server" ImageUrl=’<%# Eval("PhotoPath", "{0}") %>‘ />
</ItemTemplate>

32.

image

33.

image

34.

image

35.

image 

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text=’<%# Eval("EmployeeID", "{0}") %>’></asp:Label>
&nbsp;<br />
<asp:Label ID="Label2" runat="server" Text=’<%# Eval("FirstName", "{0}") %>’></asp:Label>
&nbsp;<br />
<asp:Label ID="Label3" runat="server" Text=’<%# Eval("LastName", "{0}") %>’></asp:Label>
&nbsp;<br />
<asp:Label ID="Label4" runat="server" Text=’<%# Eval("Title", "{0}") %>’></asp:Label>
</ItemTemplate>

36.

image

37.

image

38.

image

39.

image

40.

image

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType .DataRow)

{

Image img = (Image)e.Row.Cells[0].FindControl("Image1");

System.Drawing.Bitmap b = new System.Drawing.Bitmap(Server.MapPath(img.ImageUrl));

string strUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +

HttpContext.Current.Request.ApplicationPath + "/" + img.ImageUrl;

img.ImageUrl = strUrl;

if (Convert.ToInt32(this.Session["xwid"]) < b.Width)

this.Session["xwid"] = b.Width;

e.Row.Cells[0].Width = Convert.ToInt32(this.Session["xwid"]);

e.Row.Cells[0].Height = b.Height;

}

}

//=========================================================//

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

string ImgUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +

HttpContext.Current.Request.ApplicationPath + "/Logo/logo.gif";

ImLOGO.Src = ImgUrl;

}

}

41.

image

42.

xx

 

Code markup:

//=========================================================//

<%@ 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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head runat="server">

<title>Untitled Page</title>

<style type="text/css">

.style1

{

width: 100%;

}

</style>

</head>

<body>

<form id="form1" runat="server">

<table cellpadding="2" class="style1" id="table_Data" runat ="server" width= "500">

<tr>

<td height="50">

<h2><b>Exports GridView and Image to Excel</b></h2>

</td>

</tr>

<tr>

<td height="100">

<img id = "ImLOGO" runat ="server" src="Logo/logo.gif" /></td>

</tr>

<tr>

<td width="500">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

CellPadding="4" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1"

ForeColor="#333333" GridLines="None"

onrowdatabound="GridView1_RowDataBound">

<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#E3EAEB" />

<Columns>

<asp:TemplateField HeaderText="Detail" SortExpression="Title">

<EditItemTemplate>

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>’></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Eval("EmployeeID", "{0}") %>’></asp:Label>

&nbsp;<br />

<asp:Label ID="Label2" runat="server" Text='<%# Eval("FirstName", "{0}") %>’></asp:Label>

&nbsp;<br />

<asp:Label ID="Label3" runat="server" Text='<%# Eval("LastName", "{0}") %>’></asp:Label>

&nbsp;<br />

<asp:Label ID="Label4" runat="server" Text='<%# Eval("Title", "{0}") %>’></asp:Label>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Photo" SortExpression="PhotoPath">

<EditItemTemplate>

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PhotoPath") %>’></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Image ID="Image1" runat="server"

ImageUrl='<%# Eval("PhotoPath", "{0}") %>’ />

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />

</asp:TemplateField>

</Columns>

<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#7C6F57" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

</td>

</tr>

</table>

<asp:SqlDataSource

ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [Title], [PhotoPath] FROM [Employees]">

</asp:SqlDataSource>

<asp:Button ID="btExports" runat="server" onclick="btExports_Click"

Text="Exports" />

</form>

</body>

</html>

 

Code Behind:

//=========================================================//

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.IO;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

string ImgUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +

HttpContext.Current.Request.ApplicationPath + "/Logo/logo.gif";

ImLOGO.Src = ImgUrl;

}

}

protected void btExports_Click(object sender, EventArgs e)

{

this.EnableViewState = false;

GridView1.AllowSorting = false;

GridView1.AllowPaging = false;

Response.Clear();

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

HtmlForm frm = new HtmlForm();

Response.ContentType = "application/vnd.ms-excel";

Response.AddHeader("content-disposition", "attachment;filename=GridViewImage.xls");

Response.Charset = "";

Controls.Add(frm);

frm.Controls.Add(table_Data);

frm.RenderControl(hw);

Response.Write(tw.ToString());

Response.End();

GridView1.AllowSorting = true;

GridView1.AllowPaging = true;

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType .DataRow)

{

Image img = (Image)e.Row.Cells[0].FindControl("Image1");

System.Drawing.Bitmap b = new System.Drawing.Bitmap(Server.MapPath(img.ImageUrl));

string strUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +

HttpContext.Current.Request.ApplicationPath + "/" + img.ImageUrl;

img.ImageUrl = strUrl;

if (Convert.ToInt32(this.Session["xwid"]) < b.Width)

this.Session["xwid"] = b.Width;

e.Row.Cells[0].Width = Convert.ToInt32(this.Session["xwid"]);

e.Row.Cells[0].Height = b.Height;

}

}

}

เกี่ยวกับ

Nuchit Atjanawat is a Microsoft MVP, GreatFriends Community Leader, WindowsITPro Columnist(Mr.Nano), Silverlight Business Application Instructor, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession. He has a very good skill over Silverlight, LightSwitch, C#, XAML & Blend. He shares his technical findings, tutorials in his technical blog. Follow his Blog and Facebook page to be updated on latest articles. - Technical Blog: https://janawat.wordpress.com - WindowsITPro Columnist(Mr.Nano): http://windowsitpro.net - The GreatFriends.Biz Community Discussion Board (nano): http://greatfriends.biz - witter: https://twitter.com/janawat - Facebook Page: https://www.facebook.com/nuchit - Instructor: http://bit.ly/GF250-01, http://bit.ly/GF150-02

เขียนใน Web Application
One comment on “Exports GridView and Image to Excel
  1. wichai พูดว่า:

    เยี่อมจริงๆเลย หุหุ

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

In Archive
%d bloggers like this: