会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
着迷Linux
菲一打
导出DataGrid内的数据入Excel文件,并可设置其中文名
<
%@ Page Language
=
"
VB
"
AutoEventWireup
=
"
True
"
Debug
=
"
true
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Data
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Data.OleDb
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Drawing
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Drawing.Color
"
%
>
<
script runat
=
"
server
"
>
dim
MyConnection
as
OleDbConnection
dim
cmdSelect
as
OleDbCommand
dim
dtrReader
as
OleDbDataReader
Sub Page_Load()
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
MyConnection
=
New
OleDbConnection(
"
Provider=Microsoft.Jet.OLEDB.4.0; Data Source =
"
+
Server.MapPath(
"
.
"
)
+
"
/db/datagrid.mdb
"
)
If
Not
IsPostBack
Then
BindGrid()
End
If
End Sub
Sub BindGrid()
Sub
BindGrid()
Dim
MyCommand
As
OleDbDataAdapter
=
new
OleDbDataAdapter(
"
select * from [news]
"
, MyConnection)
Dim
DS
As
DataSet
=
new
DataSet()
MyCommand.Fill(DS,
"
min
"
)
MyDataGrid.DataSource
=
DS.Tables(
"
min
"
).DefaultView
MyDataGrid.DataBind()
End Sub
Sub Mydatagrid_PageIndexChanged()
Sub
Mydatagrid_PageIndexChanged(s
as
object
,e
as
DataGridPageChangedEventargs)
MyDataGrid.CurrentPageIndex
=
e.newPageIndex
BindGrid()
End Sub
Sub Button1_Click()
Sub
Button1_Click(sender
As
Object
, e
As
System.EventArgs)
'
-------------------
MyDataGrid.AllowPaging
=
"
false"
BindGrid()
'
-------------------
MyDataGrid.SelectedItemStyle.BackColor
=
Color.white
MyDataGrid.AlternatingItemStyle.BackColor
=
Color.white
MyDataGrid.ItemStyle.BackColor
=
Color.white
MyDataGrid.HeaderStyle.BackColor
=
Color.white
MyDataGrid.HeaderStyle.ForeColor
=
Color.red
'
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=WriteToExcelFilel.xls")
HttpContext.Current.Response.AddHeader(
"
Content-Disposition
"
,
"
attachment; filename=
"
&
System.Web.HttpUtility.UrlEncode(
"
导出文件
"
,System.Text.Encoding.UTF8)
&
"
.xls
"
)
'
----------
dim
resp
as
HttpResponse
=
Page.Response
'
resp.ContentEncoding = System.Text.Encoding.GetEncoding("Big5")
resp.ContentEncoding
=
System.Text.Encoding.GetEncoding(
"
GB2312
"
)
'
---------
Response.ContentType
=
"
application/vnd.ms-excel"
Response.Charset
=
"
"
Me.EnableViewState
=
False
Dim
tw
As
New
System.IO.StringWriter()
Dim
hw
As
New
System.Web.UI.HtmlTextWriter(tw)
MyDataGrid.RenderControl(hw)
Response.
Write
(tw.ToString())
Response.
End
()
'
-------------------
MyDataGrid.AllowPaging
=
"
true"
BindGrid()
End Sub
</
script
>
<
body
>
<
form runat
=
"
server
"
>
<
asp:Label id
=
"
lblFile
"
forecolor
=
"
red
"
runat
=
"
server
"
/><
BR
>
<
asp:Button id
=
"
button1
"
onclick
=
"
Button1_Click
"
text
=
"
Write To Excle File
"
runat
=
"
server
"
/>
<
BR
>
<
asp:datagrid id
=
"
MyDataGrid
"
runat
=
"
server
"
CellPadding
=
"
4
"
BackColor
=
"
White"
BorderColor
=
"
#330033
"
BorderWidth
=
"
1px
"
BorderStyle
=
"
None
"
Font
-
Size
=
"
9pt
"
AllowPaging
=
"
true
"
pageSize
=
"
2
"
OnPageIndexChanged
=
"
Mydatagrid_PageIndexChanged
"
>
<
PagerStyle Mode
=
"
NumericPages"
Font
-
Bold
=
"
true
"
BackColor
=
"
#F2F2F2"
HorizontalAlign
=
"
right
"
>
</
PagerStyle
>
<
SelectedItemStyle BackColor
=
"
#FFCC66
"
></
SelectedItemStyle
>
<
AlternatingItemStyle BackColor
=
"
#FFCC99
"
></
AlternatingItemStyle
>
<
ItemStyle BackColor
=
"
White
"
></
ItemStyle
>
<
HeaderStyle Font
-
Bold
=
"
True
"
HorizontalAlign
=
"
Center
"
ForeColor
=
"
#330033
"
BorderColor
=
"
Black
"
BackColor
=
"
#FF9900
"
></
HeaderStyle
>
</
asp:datagrid
>
</
form
>
发表于
2005-03-29 11:52
菲一打
阅读(
1063
) 评论(
2
)
收藏
举报
刷新页面
返回顶部
公告
导航
博客园
首页
新随笔
新文章
联系
订阅
管理