水滴石穿

渴望成为高手--Amy.Qiu
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

控件導出EXCEL

Posted on 2007-05-07 09:11  AmyQiu  阅读(216)  评论(0编辑  收藏  举报

Private Sub ToExcel()
        
        '定義導出的EXCEL名
        Dim rnd As Random
        rnd = New Random
        Dim excelName As String
        excelName = DateTime.Now.ToString("yyyymmddhhmmss_") + rnd.Next(1000).ToString + ".xls"

        Response.Clear()
        Response.Buffer = True
        Response.Charset = "utf-8"

        Response.AppendHeader("content-disposition", "attachment;filename=" + excelName)
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8")
        Response.ContentType = "application/ms-excel"
        Me.EnableViewState = False

        Dim sw As System.IO.StringWriter
        sw = New System.IO.StringWriter

        Dim hw As System.Web.UI.HtmlTextWriter
        hw = New System.Web.UI.HtmlTextWriter(sw)

        me.RenderControl(hw)
        'me為要導出的控件名稱

        Response.Write(sw.ToString)
        Response.End()

    End Sub