GridView的导出到Excel Vb.net [英] Exporting Gridview to Excel Vb.net

查看:197
本文介绍了GridView的导出到Excel Vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直挣扎在这一段时间,不能似乎得到我想要的结果。我有一个页面,上面有一个gridview,它是使用母版页。出口按钮是在从母版,我需要只导出gridview的数据是实际的页面上。

So Ive been struggling on this a while and cant seem to get the results I want. I have a page with a gridview on it, which is using a master page. The export button is on the Masterpage from which I need to export ONLY the gridview data that is on the actual page.

有对gridview的一些隐藏列,这些不能被包括在导出到Excel中的数据。

There are some hidden columns on the gridview and these must not be included on the data exported to excel.

有没有人有一些code,实际上可以做到这一点,与出围绕抓在GridView任何其他格式(即在网页上)?

Does anyone have some code that can actually do this, with out grabbing any other formatting around the Gridview(i.e on the page itself) ?

我基本上使用这个网址的code:<一href=\"http://stackoverflow.com/questions/7268714/export-gridview-data-to-excel-change-the-header-name\">Export GridView控件的数据到Excel - 更改标题名称(我转换成VB.net)

I am basically using the code on this URL : Export Gridview Data to Excel - Change the header name (I converted to VB.net)

但它似乎是出口对电网的所有数据到Excel,包括隐藏的列。

but it seems to be exporting ALL data on the grid to excel, including the hidden columns.

我希望有人可以提供帮助。

I hope someone can help.

感谢。

推荐答案

我居然发现另一种方式,它不影响页面本身,而只需要在母版进行。

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 :

在母版出口Click事件:

'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

这篇关于GridView的导出到Excel Vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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