从sql数据库检索数据,将其绑定到网格中并在Excel工作表中显示

问题描述:

我尝试使用以下代码从sql server中检索数据,然后将其绑定到datagrid中,最后输出显示在excel工作表中.

没有错误!但没有得到输出.帮帮我.

Am trying with the following code to retrieve data from sql server, followed by binding it in datagrid and finally output is displaying in excel sheet.

No errors! But not getting output. Help me out.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con = obj_connection_string.getSQLConnection()
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "select username,password,isactive from tbl_Login_Details "
cmd.Connection = con
da.SelectCommand = cmd
da.Fill(ds)
cmd.ExecuteNonQuery()
Grid.DataSource = ds
Grid.DataBind()
End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")

Response.Charset = ""

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Response.ContentType = "application/vnd.xls"

Dim stringWrite As New System.IO.StringWriter()

Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)

Grid.RenderControl(htmlWrite)

Response.Write(stringWrite.ToString())

Response.Close()
End Sub

尝试此按钮,点击
try this on button click
Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")

        Response.Charset = ""

        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        Response.ContentType = "application/vnd.xls"

        Dim stringWrite As New System.IO.StringWriter()

        Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
        Dim dg As System.Web.UI.WebControls.GridView = New System.Web.UI.WebControls.GridView()

        dg.DataSource = grid.DataSource
        dg.DataBind()

        dg.RenderControl(htmlWrite)

        Response.Write(stringWrite.ToString())

        Response.End()