<%@ Page Language="VB" AutoEventWireup="True" Debug="true" %> <script runat="server"> dim MyConnection as OleDbConnection dim cmdSelect as OleDbCommand dim dtrReader as OleDbDataReader 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 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 DataGridPageChangedEventargs) MyDataGrid.CurrentPageIndex=e.newPageIndex BindGrid() End Sub 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> 相关文章: