且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用VB.NET将Gridview导出到Excel

更新时间:2023-02-07 09:23:17

我实际上找到了另一种方法,它不影响页面本身,而只需要在母版页上完成即可.

I actually found another way, which does not affect the page itself, but only needs to be done on the masterpage.

不需要公共替代子VerifyRenderingInServerForm(作为控件的控件):

Public Overrides Sub VerifyRenderingInServerForm(control As Control) is not required :

'在母版页中导出点击事件:

'Export Click event in Masterpage :

Public Sub ButExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButExportExcel.Click

'*** take the paging and sorting out of the excel spreadsheet

Try
Dim sgv As GridView = CType(ContentPlaceHolder_body.FindControl("SummaryGridView"), GridView)
        If (sgv IsNot Nothing) Then
            sgv.AllowPaging = False
            sgv.AllowSorting = False
            sgv.DataBind()
        End If

        Dim sExportFileName As String = ""

        sExportFileName = Path.GetFileName(Request.PhysicalPath)
        sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"

        Export2(sExportFileName, sgv)

Catch ex As Exception

End Try

End Sub

'Export Sub
Public Sub Export2(ByVal fileName As String, ByVal gv As GridView)
    Dim sExportFileName As String = ""
    Dim sStringToReplace As String = ""

    gv.HeaderStyle.ForeColor = Drawing.Color.Black
    gv.HeaderStyle.BackColor = Drawing.Color.White
    gv.RowStyle.BackColor = Drawing.Color.White
    gv.HeaderStyle.Font.Bold = True
    gv.HeaderStyle.Font.Size = 10

    sExportFileName = Path.GetFileName(Request.PhysicalPath)
    sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"

    Dim attachment As String = "attachment; filename=" & sExportFileName
    HttpContext.Current.Response.ClearContent()
    HttpContext.Current.Response.AddHeader("content-disposition", attachment)
    HttpContext.Current.Response.ContentType = "application/ms-excel"
    Dim stw As New StringWriter()
    Dim htextw As New HtmlTextWriter(stw)

    Dim parent As Control = gv.Parent
    Dim GridIndex As Integer = 0
    If parent IsNot Nothing Then
        GridIndex = parent.Controls.IndexOf(gv)
        parent.Controls.Remove(gv)
    End If

    gv.RenderControl(htextw)

    If parent IsNot Nothing Then
        parent.Controls.AddAt(GridIndex, gv)
    End If

    'gv.RenderControl(htextw)
    HttpContext.Current.Response.Write(stw.ToString())
    Dim fi As New FileInfo(Server.MapPath("../JumpStart.css"))
    Dim sb As New System.Text.StringBuilder()
    Dim sr As StreamReader = fi.OpenText()
    'sStringToReplace = "class=""generalheader"""
    While sr.Peek() >= 0
        sb.Append(sr.ReadLine())
    End While
    sr.Close()

    Dim outputHtml = "<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>"

    Response.Write(outputHtml.ToString)

    'Response.Write("<html><head><style type='text/css'>" + sb.ToString() + "</style></head>" + stw.ToString() + "</html>")
    stw = Nothing
    htextw = Nothing
    Response.Flush()
    Response.[End]()


End Sub