简单的出口从ASP.NET的GridView到Excel VB.NET [英] Simple export from ASP.NET GridView into Excel VB.NET

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

问题描述

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

I have a GridView that I need to export into Excel (by button event) and I'm using Visual Studio and vb.net.

我从来没有尝试过这一点,我有点毫无章法,有一个简单的方法来做到这一点?我不认为我不需要任何复杂的时刻,在GridView信息只是一个简单的出口。

I never tried this before and I'm kinda clueless, is there a simple way to do this? I don't think I need any complications at the moment, just a simple export of the GridView information.

此外,我已经得到了GridView和我的数据库之间的连接。我试着从其他项目中增加一个工作Excel导出,但我依然怀念的东西。

Also I already got a connection between the GridView and my database. I tried adding a working Excel export from other project but I still miss something .

   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形式从我的页面和其他的东西,我怎么能prevent那些其他的东西出口?

The problem is that the file that it creates upon click says it's not Excel format and when I agree to open it I do see the GridView information as I wanted but I also see a lot of extra info in the form of buttons calanders and other stuff from my page, how can I prevent the export of those other stuff?

推荐答案

请尝试以下code

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

这篇关于简单的出口从ASP.NET的GridView到Excel VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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