Vb.net 导出 gridview 到 excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18922299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:09:42  来源:igfitidea点击:

Vb.net export gridview to excel

asp.netvb.netexcelgridview

提问by CYC0616

I am having trouble exporting my gridview to an excel file. Everything works except when I hit the export button, the data that is supposed to be exported to an excel file displays on the web page instead. I am suspecting that this is a problem with Response.ContentType and the file name that I specified. However, I already went through multiple documentations and examples and it doesn't seem like those two are the root problems in my case. Can Anyone help?

我无法将我的 gridview 导出到 excel 文件。一切正常,除了当我点击导出按钮时,应该导出到 excel 文件的数据显示在网页上。我怀疑这是 Response.ContentType 和我指定的文件名的问题。但是,我已经阅读了多个文档和示例,在我的案例中,这两个似乎不是根本问题。任何人都可以帮忙吗?

<asp:Button ID="btnExport" runat="server" Text="Export To MS Excel File" CssClass="btnMain" OnClick="btnExport_Click" />    




Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

        Dim filename As String = "PerformanceEval Status Report" & Date.Now.Year.ToString & "-" & _
                                    formatNumberTo2Digits(DateTime.Now.Month) & "-" & formatNumberTo2Digits(DateTime.Now.Day) & ".xls"

        With Page.Response

            .Clear()
            .Buffer = True
            .AddHeader("content_disposition", "attachment;filename" & filename)
            .Charset = ""

            '--to open the Excel file without saving then comment out the line below
            '.Cache.SetCacheability(HttpCacheability.NoCache)

            '.ContentType = "application/vnd.xls"   'data appears on web page 
            '.ContentType = "application/vnd.ms-excel"  'try to download .aspx document NOT .xml
            .ContentType = "application/ms-excel"  'data appears on web page 

        End With


        Dim strWriter As New System.IO.StringWriter
        Dim htmlWriter As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(strWriter)

        Dim gvexport As GridView = New GridView
        gvexport.AutoGenerateColumns = False
        gvexport.CssClass = "gridData"
        gvexport.GridLines = GridLines.Horizontal
        gvexport.HeaderStyle.CssClass = "gridHeader"
        gvexport.RowStyle.CssClass = "gridRow"
        gvexport.AlternatingRowStyle.CssClass = "gridRow_Alt"
        gvexport.FooterStyle.CssClass = "gridFooter"

        Dim dv As DataView = New DataView(Me.ds.Tables(0))  'default is sorted by last name
        'Dim dv As DataView = New DataView(Me.ds.Tables(0))
        dv.RowFilter = GetFilter()

        '-- add columns for report
        addGVcolumn("Employee Name", "EmployeeName", gvexport, -1, HorizontalAlign.Left)
        addGVcolumn("Employee ID", "SID", gvexport, -1, HorizontalAlign.Left)
        addGVcolumn("Email", "WorkEmail", gvexport, -1, HorizontalAlign.Left)
        addGVcolumn("StatusID", "StatusID", gvexport, 50, HorizontalAlign.Center)

        gvexport.DataSource = dv
        gvexport.DataBind()

        gvexport.RenderControl(htmlWriter)

        Response.Output.Write(strWriter.ToString())

        Response.Flush()
        Response.End()

Again, I am able to generate the content but is not able to make my data exports into excel. Thanks a lot!

同样,我能够生成内容,但无法将我的数据导出到 excel。非常感谢!

采纳答案by IvanH

You are giving the wrong header:

你给出了错误的标题:

.AddHeader "content-disposition", "attachment; filename="& filename &";"