开发者

Export Gridview to Excel without prompt window

开发者 https://www.devze.com 2023-03-18 08:57 出处:网络
I can export my gridview to excel, but I don\'t want to prompt a window to ask filename and path, How can I disable it ?

I can export my gridview to excel, but I don't want to prompt a window to ask filename and path, How can I disable it ? Moreover, How can I set the path to save the file ?

Thanks Joe

The following is my code:

Private Sub ExportGridView()

    Dim filename As String
    filename = ddlMonth.SelectedValue & "-" & ddlYear.SelectedValue

    Dim attachment As String = "attachment; filename=" & filename & ".xls"

    Response.ClearContent()
    Response.Buffer = True
    Response.Charset = "UTF-8"        
    Response.AddHeader("content-disposition", attachment)
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8")
    Me.EnableViewState = False
    Response.ContentType = "application/ms-excel"

    Dim sw As New StringWriter()
    Dim htw As New HtmlTextWriter(sw)

    'Turn off the paging for export
    Gridview1.DataSource = Session("dt")
    Gridview1.AllowPaging = False
    Gridview1.DataBind()

    GridView1.RenderControl(htw)
    Response.Write(sw.ToString())
    Response.[End]()开发者_如何学Go

    'Turn on the paging after export
    Gridview1.DataSource = Session("dt")
    Gridview1.AllowPaging = True
    Gridview1.DataBind()

End Sub


//Declaration <%= MyMethodCall() %> //And in the code behind. protected String MyMethodCall() { return "Test Value"; }

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号