上一篇:在ASP.NET中杀死进程 >>
在ASP.NET中将数据直接输出成Excel内容
查看例子
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="宋体">
<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>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px"
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 " Web 窗体设计器生成的代码 "
@#该调用是 Web 窗体设计器所必需的。
<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: 此方法调用是 Web 窗体设计器所必需的
@#不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
@#在此处放置初始化页的用户代码
@# 定义是否是 SQL Server 数据库,这里为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 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 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
下一篇:在ASP.NET里轻松实现缩略图 >>
相关文章:
- · 如何更新父窗体
- · 如何在域控制器上安装asp.net?
- · Asp.net动态生成html页面
- · asp.net 2.0中一次性更新所有GRIDVIEW的记录
- · ASP.NET with C#使用md5,sha1加密初探
- · 创建虚拟目录的常用属性
- · asp.net控件加載
- · 在ASP.Net 中使用实现windows登陆的方法
- · 在ASP.NET中动态加载内容(用户控件和模板)
- · ASP.net组件编程中的两种事件编写方法
- · ASP.NET事件小探讨
- · asp.net连接Access数据库
- · [ASP.net(C#)]自定义数据库操作类(一)
- · asp.net 中用户和角色的验证
- · 在.NET中调用Oracle9i存储过程经验总结
- · 枚举类型的用法
- · Tag属性的妙用
- · 怎样得到文本框(TextBox)中的文本行数
- · ASP.NET IIS 注册工具 (Aspnet_regiis.exe)
- · 在Asp.net中部署和使用微软的TreeView控件
- · Word编程中,事件的作用和顺序
- · 关于大型网站的构架问题
- · ASP.NET中上传文件的方法(一)
- · 坚持学asp.net——(十二)
- · 在ASP.NET中跨页面实现多选
- · Securing an ASP.Net application...
- · Send Email and Attachments with ASP.Net...
- · asp.net中的多語言使用
- · 網頁中彈出對自定義對話框並進行傳值
- · 將 ASP .NET WebForm 的 DataGrid 中的資料 匯出至 Microsoft Excel
- · ASP.NET中树形图的实现
- · 细细品味ASP.NET
- · ASP.NET中制作图形
- · ASP.NET常用函数
- · ASP.NET开发经验(2) --- ASP.NET中的一些图形处理
- · ASP.NET域名查询系统
- · [我的ASP.net学习历程]有关于.dll文件的迷惑
- · 自己动手写屏保
