• ASP.NET(VB)把数据导出到EXCEL的一种方法(网上转载)


    做毕业设计时探索的这种方法还比较好用,缺点是本方法是先用DATAGRID从数据库取数据,再从页面导出到EXCEL。可以参考改进。(用Vb编写,这里的代码是脚本部分)

    Imports System
    Imports System.Data
    Imports System.Data.OleDb
    Public Class export
        Inherits System.Web.UI.Page

    #Region " Web 窗体设计器生成的代码 "

        '该调用是 Web 窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
        Protected WithEvents Button1 As System.Web.UI.WebControls.Button
        Protected WithEvents Label1 As System.Web.UI.WebControls.Label
        Protected WithEvents Label2 As System.Web.UI.WebControls.Label
        Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
        Protected WithEvents Label3 As System.Web.UI.WebControls.Label

        '注意: 以下占位符声明是 Web 窗体设计器所必需的。
        '不要删除或移动它。
        Private designerPlaceholderDeclaration As System.Object

        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
            '在此处放置初始化页的用户代码
            If Not IsPostBack Then
                OpenDataBase_And_BindToDataGrid()
            End If
        End Sub
        Sub ChangePage(ByVal sender As System.Object, ByVal e As DataGridPageChangedEventArgs)
            DataGrid1.CurrentPageIndex = e.NewPageIndex
            OpenDataBase_And_BindToDataGrid()
        End Sub
        Sub OpenDataBase_And_BindToDataGrid()
            Dim sql As String = "select ID,Rsno,Rsname,Rsmajor,Rclass,Rsgrade,Rcourse,colledge,Rcourseindex from getReportInfo "

    '定义SQL语句
            Dim Conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Inetpub\wwwroot\WebApplication1\reportsys1.mdb'")  '连接数据库
            Dim Adpt As OleDbDataAdapter
            Dim Ds As DataSet  '定义一个DataSet 
            Conn.Open()
            Adpt = New OleDbDataAdapter(sql, Conn)
            Ds = New DataSet
            Adpt.Fill(Ds, "getReportInfo")
            Dim table1 As DataTable = Ds.Tables("getReportInfo")
            DataGrid1.DataSource = table1.DefaultView
            DataGrid1.DataBind()  '和数据库绑定
            Conn.Close()
            Conn = Nothing
        End Sub
        Private Sub ExportExcelFromDataGrid(ByVal filename As String, ByVal ToExcelGrid As System.Web.UI.WebControls.DataGrid)
            Response.Clear()
            Response.Buffer = True
            Response.Charset = "utf-8"
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename))
            Response.ContentEncoding = System.Text.Encoding.Default '设置输出流为简体中文  
            Response.ContentType = "application/ms-excel"      '设置输出文件类型为excel文件。    
            EnableViewState = False   '处理过程设置为不可见,后台处理
            Dim myCItrad As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("ZH-CN", True)
            Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter(myCItrad)
            Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
            ToExcelGrid.RenderControl(oHtmlTextWriter)
            Response.Write(oStringWriter.ToString())
            Response.End()
        End Sub
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Panel1.Visible = false;           
            Dim filename As String = TextBox1.Text & ".xls"
            DataGrid1.Columns(7).Visible = True
            DataGrid1.Columns(8).Visible = True
            DataGrid1.Columns(DataGrid1.Columns.Count - 1).Visible = True
            DataGrid1.Columns(DataGrid1.Columns.Count - 2).Visible = True
            DataGrid1.AllowSorting = False
            DataGrid1.AllowPaging = True
            DataGrid1.SelectedIndex = -1
            'BindGrid()
            ExportExcelFromDataGrid(filename, DataGrid1)
        End Sub
    End Class

  • 相关阅读:
    Identity Server4学习系列三
    C# 多线程九之Timer类
    Identity Server4学习系列二之令牌(Token)的概念
    Identity Server4学习系列一
    C# 多线程八之并行Linq(ParallelEnumerable)
    使用Resourcehacker去除winrar的弹窗广告
    remote: Coding 提示: Authentication failed问题解决
    使用Python读取照片的GPS信息
    mongoose删除mongodb某个库的所有collection
    mysql数据库查询占用空间
  • 原文地址:https://www.cnblogs.com/gyxdbk/p/1398087.html
Copyright © 2020-2023  润新知