将 Gridview 导出到 Excel,并将行格式化为文本

发布于 2024-08-03 02:46:36 字数 2159 浏览 0 评论 0原文

我在网上读了一些教程,似乎遗漏了一些东西。我试图通过将格式设置为文本来让前导 0 显示在列中。

任何建议将不胜感激。

    ''' <summary>
    ''' This is required for the grid view to export properly
    ''' </summary>
    ''' <param name="control"></param>
    ''' <remarks></remarks>
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
    End Sub

    Protected Overrides Sub OnInitComplete(ByVal e As System.EventArgs)

            Dim List As System.Web.UI.WebControls.GridView = CType(Page.FindControl("List"), System.Web.UI.WebControls.GridView)
            AddHandler List.RowDataBound, AddressOf RowDataBound

            List.DataSource = myList
            List.DataBind()

            Response.Clear()
            Response.ContentType = "application/vnd.ms-excel"
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=ExportList.xls")

            Response.Write("<style> .text {mso-number-format:\@; } </style>")

            Using strwriter As New System.IO.StringWriter
                Using htmlwriter As New HtmlTextWriter(strwriter)

                    List.RenderControl(htmlwriter)

                    HttpContext.Current.Response.Write(strwriter.ToString)
                    HttpContext.Current.ApplicationInstance.CompleteRequest()
                End Using
            End Using

    End Sub

    Protected Sub RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then

            e.Row.Cells(0).Attributes.Add("class", "text")

            Dim dtview As System.Data.DataRowView
            Dim dt As DateTime
            Dim intCounter As Integer

            dtview = e.Row.DataItem

            For intCounter = 0 To dtview.Row.ItemArray.Length - 1

                If TypeOf dtview.Row.Item(intCounter) Is System.DateTime Then
                    dt = dtview.Row.Item(intCounter)
                    e.Row.Cells(intCounter).Text = dt.ToLongDateString
                End If

            Next
        End If

    End Sub

I have read several tutorials online and seem to be missing something. I am trying to have the leading 0's show up in columns by setting the format to text.

Any suggestions would be appreciated.

    ''' <summary>
    ''' This is required for the grid view to export properly
    ''' </summary>
    ''' <param name="control"></param>
    ''' <remarks></remarks>
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
    End Sub

    Protected Overrides Sub OnInitComplete(ByVal e As System.EventArgs)

            Dim List As System.Web.UI.WebControls.GridView = CType(Page.FindControl("List"), System.Web.UI.WebControls.GridView)
            AddHandler List.RowDataBound, AddressOf RowDataBound

            List.DataSource = myList
            List.DataBind()

            Response.Clear()
            Response.ContentType = "application/vnd.ms-excel"
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=ExportList.xls")

            Response.Write("<style> .text {mso-number-format:\@; } </style>")

            Using strwriter As New System.IO.StringWriter
                Using htmlwriter As New HtmlTextWriter(strwriter)

                    List.RenderControl(htmlwriter)

                    HttpContext.Current.Response.Write(strwriter.ToString)
                    HttpContext.Current.ApplicationInstance.CompleteRequest()
                End Using
            End Using

    End Sub

    Protected Sub RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then

            e.Row.Cells(0).Attributes.Add("class", "text")

            Dim dtview As System.Data.DataRowView
            Dim dt As DateTime
            Dim intCounter As Integer

            dtview = e.Row.DataItem

            For intCounter = 0 To dtview.Row.ItemArray.Length - 1

                If TypeOf dtview.Row.Item(intCounter) Is System.DateTime Then
                    dt = dtview.Row.Item(intCounter)
                    e.Row.Cells(intCounter).Text = dt.ToLongDateString
                End If

            Next
        End If

    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花ソ最美的依靠 2024-08-10 02:46:36

有一种更好的方法可以达到相同的结果,只需添加一行即可工作。
无需使用循环创建样式表并为所有 标记添加属性,而是直接在所有 TD 标记上应用样式。

string style = @"<style> TD { mso-number-format:\@; } </style>";

There is a better way to achieve the same result, just add one line and it will work.
Instead of creating style sheet and adding attributes for all <TD> tags using loop, direct apply style on the All TD Tags.

string style = @"<style> TD { mso-number-format:\@; } </style>";
淡墨 2024-08-10 02:46:36

看看这个: 如何将 GridView 导出到 Word、Excel、PDF 和 CSV 文档

它工作正常。非常好。谢谢

Check this out: how to Export GridView To Word Excel PDF and CSV documents

It works fine. Very best. Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文