从sql数据库检索数据,将其绑定到网格中并在Excel工作表中显示 [英] Retreive data from sql database,bind it in grid and display it in excel sheet

查看:64
本文介绍了从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()


这篇关于从sql数据库检索数据,将其绑定到网格中并在Excel工作表中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆