【问题标题】:Simple export from ASP.NET GridView into Excel VB.NET从 ASP.NET GridView 简单导出到 Excel VB.NET
【发布时间】:2014-05-29 09:45:55
【问题描述】:

我有一个需要导出到 Excel(通过按钮事件)的 GridView,我正在使用 Visual Studio 和 vb.net。

我以前从未尝试过,我有点无能为力,有没有简单的方法可以做到这一点?我认为我目前不需要任何复杂性,只需简单地导出 GridView 信息即可。

此外,我已经在 GridView 和我的数据库之间建立了连接。我尝试从其他项目添加一个有效的 Excel 导出,但我仍然错过了一些东西。

   Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered 

    End Sub

    Protected Sub exportExelBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exportExelBtn.Click
        Dim errorCheck As Integer = 0
        Dim popupscript As String = ""

        If approvalGrid.Rows.Count > 0 Then
            Try

                Response.ClearContent()
                Response.Buffer = True
                Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "TestPage.xls"))
                Response.ContentEncoding = Encoding.UTF8
                Response.ContentType = "application/ms-excel"
                ' Dim sw As New stringwriter()
                Dim tw As New IO.StringWriter()
                Dim htw As New HtmlTextWriter(tw)
                approvalGrid.RenderControl(htw)
                Response.Write(tw.ToString())
                Response.[End]()

            Catch ex As Exception
                errorCheck = 1
            End Try
        Else
            errorCheck = 1
        End If
        If errorCheck = 1 Then
            'a pop up error messege feature
            popupscript = "<script language='javascript'>" + "alert(" + Chr(34) + "There was an error in exporting to exel, please make sure there is a grid to export!" + Chr(34) + ");</script>"
        End If
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "PopUpWindow", popupscript, False)
   End Sub

问题是它在单击时创建的文件说它不是 Excel 格式,当我同意打开它时,我确实看到了我想要的 GridView 信息,但我也看到了许多以按钮日历形式出现的额外信息和我页面上的其他内容,我怎样才能防止这些其他内容的导出?

【问题讨论】:

  • 你遇到什么问题......
  • 您不能导出 GridView,您可以导出的是相同的数据(gridview 呈现)以特定格式(csv)用逗号或制表符分隔,并导出为文本和逐行,然后 Excel 会读取它并将其加载到工作表上。
  • 我会用额外的信息编辑我的帖子

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


【解决方案1】:

请试试下面的代码

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
        ' Verifies that the control is rendered 

End Sub

Protected Sub btnExport_Click(sender As Object, e As EventArgs)
    If gridview.Rows.Count > 0 Then
        Try
            gridview.Columns(0).Visible = False
            Response.ClearContent()
            Response.Buffer = True
            Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "TestPage.xls"))
            Response.ContentEncoding = Encoding.UTF8
            Response.ContentType = "application/ms-excel"
            Dim sw As New StringWriter()
            Dim htw As New HtmlTextWriter(sw)
            gridview.RenderControl(htw)
            Response.Write(sw.ToString())
            Response.[End]()

        Catch ex As Exception
        Finally
            gridview.Columns(0).Visible = True
        End Try
    End If
End Sub

【讨论】:

  • 嘿,感谢您的回复,我尝试了此代码,但它无法识别 StringWriter() 所以我使用 Dim tw As New IO.StringWriter() 现在它可以工作了,但除了 gridview 信息之外,它还需要很多其他的东西,像按钮日历文本框等等^^
  • 好的,所以在您的导出中,您将获得所有在 excel 中不需要的列。您已使这些列可见为假,并且在 catch 之后您必须添加 finally 块,您必须使列再次可见为真。
【解决方案2】:

我已经编写了上面的代码来将 gridview 导出到 excel 文件,它导出成功,但是在波斯语的 gridview 中有一些内容在导出的 excel 文件中显示为不可读。我写的代码如下:

 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If GridView1.Rows.Count > 0 Then
        Response.ClearContent()
        Response.Buffer = True
        Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "IncentiveReport.xls"))
        Response.ContentEncoding = Encoding.UTF8
        Response.ContentType = "application/ms-excel"
        Dim sw As New IO.StringWriter()
        Dim htw As New HtmlTextWriter(sw)
        GridView1.RenderControl(htw)
        Response.Write(sw.ToString())
        Response.End()
    End If
End Sub
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered 
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多