This article realizes that the data in the database is output directly to the Excel file format and output in the browser. Here is an example of implementation:
View examples
<The following are the referenced contents: Excelexport.aspx
<%@ Page language= "VB" autoeventwireup= "false" codebehind= "ExcelExport.aspx.vb" inherits= "AspxWeb.mengxianhui.com.ExcelExport"%> ! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" <HTML> <HEAD> <title> Excelexport </title> <meta name= "generator" content= "Microsoft Visual Studio.NET 7.0" <meta name= "Code_language" content= "Visual Basic 7.0" <meta name= "vs_defaultClientScript" content= "JavaScript" <meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" </HEAD> <body ms_positioning= "GridLayout" <form id= "Form1" method= "POST" runat= "Server" <asp:datagrid id= "DATAGRID1" runat= "Server" cellpadding= "4" backcolor= "white" Bordercolor= "#CC9966" borderwidth= "1px" borderstyle= "None" width= "100%" height= "100%" Font-size= "9pt" font-names= "Song Body" <selecteditemstyle font-bold= "True" forecolor= "#663399" backcolor= "#FFCC66" > </SelectedItemStyle> <alternatingitemstyle backcolor= "#FFCC99" > </AlternatingItemStyle> <itemstyle borderwidth= "2px" forecolor= "#330099" borderstyle= "Solid" Bordercolor= "BLACK" backcolor= "white" > </ItemStyle> Forecolor= "#FFFFCC" borderstyle= "Solid" bordercolor= "Black" backcolor= "#990000" > </HeaderStyle> </asp:datagrid> </form> </body> </HTML>
ExcelExport.aspx.vb
Public Class Excelexport Inherits System.Web.UI.Page Protected WithEvents DataGrid1 as System.Web.UI.WebControls.DataGrid #Region the code generated by the Web Forms Designer ' This call is required by the Web Forms Designer. <system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent () End Sub
Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) _ Handles Mybase.init ' CodeGen: This method call is required by the Web Forms Designer ' Do not modify it using the Code Editor. InitializeComponent () End Sub
#End Region Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) _ Handles MyBase.Load ' The user code to place the initialization page here ' Defines whether it is a SQL Server database, False here Dim blnissqlserver as System.Boolean = False Dim strSQL as String Dim Objdataset as New DataSet () Dim objconn as Object Dim strcnn as String If Blnissqlserver Then strcnn = "User id=sa;initial catalog=northwind;data source=.\netsdk;" objconn = New System.Data.SqlClient.SqlConnection (strcnn) objConn.Open () Dim Objadapter as New System.Data.SqlClient.SqlDataAdapter () strSQL = "SELECT * FROM Customers where country= ' USA '" Objadapter.selectcommand = New System.Data.SqlClient.SqlCommand (strSQL, objconn) Objadapter.fill (Objdataset) Else strcnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath ("Test.mdb") objconn = New System.Data.OleDb.OleDbConnection (strcnn) objConn.Open () Dim Objadapter as New System.Data.OleDb.OleDbDataAdapter () strSQL = "Select top Title from Document" Objadapter.selectcommand = New System.Data.OleDb.OleDbCommand (strSQL, objconn) Objadapter.fill (Objdataset) End If Dim Oview as New DataView (objdataset.tables (0)) DataGrid1.DataSource = Oview Datagrid1.databind () Objconn.close () Objconn.dispose () objconn = Nothing If request.querystring ("bexcel") = "1" Then Response.ContentType = "Application/vnd.ms-excel" ' Remove charset settings from Content-type header Response.Charset = "" ' Close ViewState Me.enableviewstate = False Dim tw as New System.IO.StringWriter () Dim HW as New System.Web.UI.HtmlTextWriter (TW) ' Get control of HTML Datagrid1.rendercontrol (HW) ' Write HTML back to the browser Response.Write (TW. ToString ()) Response.End () End If End Sub End Class |