程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 在ASP.NET中將數據直接輸出成Excel格式

在ASP.NET中將數據直接輸出成Excel格式

編輯:.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" Border 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" Border
  BorderColor="Black" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px"
  ForeColor="#FFFFCC" Border 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 
   
  
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved