将gridview导出为ex​​cel而不使用gridview格式化VB

时间:2011-09-23 11:26:42

标签: asp.net vb.net gridview

我能够将gridview导出到excel,我的问题是我无法弄清楚如何从girdview中删除格式化。这是我用来导出gridview的代码:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Response.Clear()
    Response.Charset = ""
    'Response.ContentType = "application/vnd.ms-excel"
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

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

    GridView1.GridLines = GridLines.None

    GridView1.HeaderStyle.Font.Bold = True
    GridView1.DataSourceID = SqlDataSource1.ID
    GridView1.DataBind()

    GridView1.RenderControl(htmlWrite)

    Response.Write(stringWrite.ToString)
    Response.End()

End Sub

1 个答案:

答案 0 :(得分:0)

我建议您应该迭代datasource,将每行的内容追加到StringBuilder对象中,并将该字符串写入response缓冲区。