ITPub博客

首页 > 应用开发 > IT综合 > 在ASP.NET中将数据直接输出成Excel内容 (转)

在ASP.NET中将数据直接输出成Excel内容 (转)

原创 IT综合 作者:gugu99 时间:2007-12-05 09:53:32 0 删除 编辑
在ASP.NET中将数据直接输出成Excel内容 (转)[@more@]

在ASP.NET中将数据直接输出成excel内容

 

本文实现了将数据库中的数据直接输出到Excel文件格式并在浏览器里输出。下面就是实现的例子:

dotnetplayground.com/Exam/Data2ExcelDemo.htm" target=_target>查看例子

ExcelExport.aspx

<a href="leibie-11-1.html">web</a></a>.mengxianhui.com.ExcelExport"%> w3c</a>//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>ExcelExport</title> <meta name="GENERATOR" content="a href="tag-126-1.html""">microsoft</a> <a href="tag-39-1.html">Visual Studio</a>.NET 7.0"> <meta name="CODE_LANGUAGE" content="a href="tag-50-1.html"""><a href="leibie-7-1.html">Visual Basic</a></a> 7.0"> <meta name="vs_defaultClientScript" content="a href="tag-133-1.html"""><a href="tag-10-1.html"><a href="leibie-1-1.html">Java</a></a>script</a>"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/a href="tag-340-1.html""">intel</a>lisense/<a href="tag-393-1.html">IE5</a>"> </HEAD> <body> <fo href="tag-415-1.html">RM </a>id="Form1" method="post" runat="server"> <asp id="DataGrid1" runat="server" cellpadding="4" backcolor="White" bordercolor="#CC9966" borderwidth="1px" borderstyle="None" width="100%" height="100%" font-size="9pt" font-names="宋体"> select</a>edItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle> <AlternatingItemStyle backcolor="#FFCC99"></AlternatingItemStyle> <ItemStyle borderwidth="2px" forecolor="#330099" borderstyle="Solid" bordercolor="Black" backcolor="White"></ItemStyle> <HeaderStyle font-bold="True" horizontalalign="Center" borderwidth="2px" forecolor="#FFFFCC" borderstyle="Solid" bordercolor="Black" backcolor="#990000"></HeaderStyle> </asp> </form> </body> </HTML>

ExcelExport.aspx.vb

Public Class ExcelExport Inherits System.Web.UI.Page Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid #Region " Web 窗体设计器生成的代码 " '该<a href="tag-152-1.html">调用</a>是 Web 窗体设计器所必需的。 <System href="tag-268-1.html">bug</a>gerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.<a href="tag-252-1.html">object</a>, ByVal e As System.EventArgs) _ Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load '在此处放置初始化页的<a href="tag-271-1.html">用户</a>代码 ' 定义是否是 <a href="tag-53-1.html"><a href="tag-54-1.html">sql</a> SERVER</a> 数据库,这里为False 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 <a href="tag-162-1.html">source</a>=.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 = "Prov<a href="tag-351-1.html">ide</a>r=Microsoft.Jet.OLE<a href="tag-57-1.html">db</a>.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 10 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" ' 从Content-Type header中去除charset设置 Response.Charset = "" ' 关闭 ViewState Me.EnableViewState = False Dim tw As New System.IO.StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) ' 获取control的HTML DataGrid1.RenderControl(hw) ' 把HTML写回浏览器 Response.Write(tw.ToString()) Response.End() End If End Sub End Class

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10748419/viewspace-988555/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3121
  • 访问量
    2643835