从ASP.NET GridView简单导出到Excel VB.NET

时间:2014-05-29 09:45:56

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

我有一个GridView,我需要导出到Excel(通过按钮事件),我正在使用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信息,但我也看到了很多额外信息以按钮calanders的形式和我页面上的其他内容,如何防止其他东西的导出呢?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我已经编写了上面的代码以将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

https://support.google.com/mail/?p=WantAuthError