当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 在ASP.NET中将数据直接输出成Excel格式

ASP.NET
Script:WINDOWS Script 枚举运行中进程
使用Flex结合Webservice完成域名查询
VSTS Team System 总算装好了。
用于部署数据库的 数据库初始化工具 xzSQLDeploy Tools V1.0 (for SQLServer) f...
一个将阿拉伯数字转换成中文大写的最简单算法
SCRIPT:使用Windows Script 关闭和打开指定程序
Script:使用WINDOWS脚本访问WEB SERVICES
asp.net连接Access数据库
VB中IIS Application发布可能出现的问题
VB打包后的安装问题
Nhibernate的数据分页技术(续)
使用API函数复制文件,可显示进度。
VB打包技巧
VB.NET实现DirectSound9 (9) 实现示波器
VB.NET 实现DirectSound9 (10) 均衡器
[水晶报表部署系列之一]轻松搞定水晶报表9.2打包
DataGrid 中双向排序的一种办法
利用System.EventHandler来实现两个窗体间的事件调用
多线程应用程序中调用窗体的一点心得
Smart Client之旅一:用B/S方式运行Exe应用程序

在ASP.NET中将数据直接输出成Excel格式


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 144 ::
收藏到网摘: n/a

本文实现了将数据库中的数据直接输出到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