<asp:datagrid id="DataGrid1" runat="server" GridLines="Vertical" CellPadding="3" BackColor="White" BorderColor="#999999" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%" Font-Size="X-Small" Font-Names="Verdana"> <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle> <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle> <ItemStyle BorderWidth="2px" ForeColor="Black" BorderStyle="Solid" BorderColor="Black" BackColor="#EEEEEE"></ItemStyle> <HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="White" BorderStyle="Solid" BorderColor="Black" BackColor="#000084"></HeaderStyle> <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle> <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle> </asp:datagrid>
' Create a connection and open it. Dim objConn As New System.Data.SqlClient.SqlConnection("User ID=<username>;Password=<strong password>;Initial Catalog=Northwind;Data Source=SQLServer;") objConn.Open() Dim strSQL As String Dim objDataset As New DataSet() Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter() ' Get all the customers from the USA. strSQL = "Select * from customers where country='USA'" objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn) ' Fill the dataset. objAdapter.Fill(objDataset) ' Create a new view. Dim oView As New DataView(objDataset.Tables(0)) ' Set up the data grid and bind the data. DataGrid1.DataSource = oView DataGrid1.DataBind() ' Verify if the page is to be displayed in Excel. If Request.QueryString("bExcel") = "1" Then ' Set the content type to Excel. Response.ContentType = "application/vnd.ms-excel" ' Remove the charset from the Content-Type header. Response.Charset = "" ' Turn off the view state. Me.EnableViewState = False Dim tw As New System.IO.StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) ' Get the HTML for the control. DataGrid1.RenderControl(hw) ' Write the HTML back to the browser. Response.Write(tw.ToString()) ' End the response. Response.End() End If注意:請將程式碼中的 SQLServer 取代為您的 SQL Server 名稱。如果您無法存取 Northwind 範例資料庫所在的 SQL Server,請將連線字串修改為使用 Microsoft Access 2002 範例 Northwind 資料庫:
provider=microsoft.jet.oledb.4.0; data source=C:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb
如果您選取這個方法,請將 aforementioned 程式碼修改為使用 OleDbClient 命名空間 (而不使用 SqlClient 命名空間)。<html> <script language="vbscript"> Sub Button1_onclick Select Case Select1.selectedIndex Case 0 ' Use Automation. Dim sHTML sHTML = window.parent.frames("bottom").document.forms(0).children("DataGrid1").outerhtml Dim oXL, oBook Set oXL = CreateObject("Excel.Application") Set oBook = oXL.Workbooks.Add oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML oBook.HTMLProject.RefreshDocument oXL.Visible = true oXL.UserControl = true Case 1 ' Use MIME Type (In a New Window). window.open("bottom.aspx?bExcel=1") Case 2 ' Use MIME Type (In the Frame). window.parent.frames("bottom").navigate "bottom.aspx?bExcel=1" End Select End Sub </script> <body> Export to Excel Using: <SELECT id="Select1" size="1" name="Select1"> <OPTION value="0" selected>Automation</OPTION> <OPTION value="1">MIME Type (In a New Window)</OPTION> <OPTION value="2">MIME Type (In the Frame)</OPTION> </SELECT> <INPUT id="Button1" type="button" value="Go!" name="Button1"> </body> </html>
<html> <frameset rows="10%,90%"> <frame noresize="0" scrolling="no" name="top" src="top.htm"> <frame noresize="0" scrolling="yes" name="bottom" src="bottom.aspx"> </frameset> </html>
本文地址:http://com.8s8s.com/it/it43776.htm