Dim strSql As String = "SELECT * FROM TableName"
        Dim filename As String = Server.MapPath("GetExcel.xls")
        Dim constring As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("DatabaseOleDBConnectionString").ToString
        Dim connect As New Data.OleDb.OleDbConnection(constring)

        Try
            connect.Open()
            Dim command As New Data.OleDb.OleDbCommand(strSql, connect)
            Dim ad As New Data.OleDb.OleDbDataAdapter(strSql, connect) '讀取資料
            Dim ds As New Data.DataSet()
            ad.Fill(ds, "aa")
            ad.Dispose()

            Dim style As String = "<style>td{mso-number-format:\@;}</script>"
            Dim sw As New System.IO.StringWriter
            Dim hw As New System.Web.UI.HtmlTextWriter(sw)

            Response.AppendHeader("Content-Disposition", "attachment; filename=GetExcel.xls")
            Response.ContentType = "application/vnd.ms-excel"
            Dim oDG As New DataGrid()
            oDG.DataSource = ds.Tables(0)
            oDG.DataBind()
            oDG.RenderControl(hw)
            Response.Write(style)
            Response.Write(sw.ToString())
            Response.End()
        Catch ex As Exception
        Finally
            connect.Close()
        End Try

goodlucky 發表在 痞客邦 PIXNET 留言(0) 人氣()