<%@
Page
language="c#"
Src="datagrid2.aspx.cs"
AutoEventWireup="false"
Inherits="DataGrid_import_WordExcel.WebForm1"
%>
<!DOCTYPE
HTML
PUBLIC
"-//W3C//DTD
HTML
4.0
Transitional//EN"
>
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<link
rel="stylesheet"
href="Style.css"
type="text/css">
<body>
<form
width="120%"
id="Form1"
method="post"
runat="server">
<table
width="120%"><tr><td>
<asp:datagrid
id="DataGrid1"
bgcolor="#efefef"
HeaderStyle-BackColor="#718BD6"
HeaderStyle-ForeColor="#FFFF66"
AlternatingItemStyle-BackColor="#FFFFFF"
itemstyle-backcolor="#FFFFFF"
runat="server">
<ItemStyle
HorizontalAlign="center"
Height="20"></ItemStyle>
<Columns>
</Columns>
</asp:datagrid>
</td></tr></table>
<P>
<asp:button
id="BtnImportWord"
runat="server"
Text="轉到WORD"></asp:button></P>
</form>
</body>
</HTML>
-------------------
using
System;
using
System.Collections;
using
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
using
System.Data.SqlClient
;
using
System.Text;
using
System.Configuration;
namespace
DataGrid_import_WordExcel
{
public
class
WebForm1
:
System.Web.UI.Page
{
protected
System.Web.UI.WebControls.Button
BtnImportWord;
protected
System.Web.UI.WebControls.Button
Btn_Import_Excel;
protected
System.Web.UI.WebControls.DataGrid
DataGrid1;
public
DataRow
dr;
private
DataSet
myDS
=new
DataSet();
private
void
Page_Load(object
sender,
System.EventArgs
e)
{
Data_Load();
}
#region
Web
敦极扢數?汜傖腔測鎢
override
protected
void
OnInit(EventArgs
e)
{
InitializeComponent();
base.OnInit(e);
}
private
void
InitializeComponent()
{
this.Btn_Import_Excel.Click
+=
new
System.EventHandler(this.Btn_Import_Excel_Click);
this.Load
+=
new
System.EventHandler(this.Page_Load);
}
#endregion
private
void
ExportDataGrid(string
FileType,
string
FileName)
//植DataGrid絳堤
{
Response.Charset
=
"GB2312";
Response.ContentEncoding
=
System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition",
"attachment;filename="
+HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType
=
FileType;
this.EnableViewState
=false;
StringWriter
tw
=
new
StringWriter();
HtmlTextWriter
hw
=new
HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private
void
Data_Load()
{
SqlConnection
myConnection
=
new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
SqlCommand
cmd=new
SqlCommand("page2",myConnection);
.
.
.
myConnection.Open();
DataSet
ds=new
DataSet();
SqlDataAdapter
da=new
SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
DataView
CreateDataSource()
{
string
nowDSN=ConfigurationSettings.AppSettings["data"];
SqlConnection
myConnection=new
SqlConnection(nowDSN);
SqlCommand
cmd=new
SqlCommand("page2",myConnection);
.
.
.
DataSet
ds=new
DataSet();
SqlDataAdapter
da=new
SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
return
ds.Tables["pur"].DefaultView;
myConnection.Close();
Page.DataBind();
}
void
DataBind()
{
DataView
source=CreateDataSource();
if(!IsPostBack)
{
}
DataGrid1.DataSource
=
source;
DataGrid1.DataBind();
}
private
void
BtnImportWord_Click(object
sender,
System.EventArgs
e)
{
ExportDataGrid("application/ms-word",
"Word.doc");
}
}
}
只要你不嫌长,兄弟,我给你顶了!