如何将数据源的选定数据导出为ex​​cel?

时间:2013-08-20 15:33:33

标签: asp.net vb.net gridview export-to-excel

我正在使用下面的代码将gridview导出到Excel工作表。问题是它只会导出页面上的记录/列(其中10个),因为我的gridview设置被设置为每页只显示10行而只显示某些列。有没有办法可以将所有记录和列从数据源导出到Excel?

 Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
    Me.EnableViewState = False
    Dim oStringWriter As New System.IO.StringWriter
    Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

    GridView1.RenderControl(oHtmlTextWriter)

    Response.Write(oStringWriter.ToString())
    Response.[End]()
End Sub

1 个答案:

答案 0 :(得分:1)

当我建立数据库级分页时,我不得不创建一个单独的“导出”页面,该页面将呈现所有数据而无需分页到单独的GridView中。然后我调用了你问题中的函数来渲染它。我建议你这样做。