2

オンラインでいくつかのチュートリアルを読みましたが、何かが足りないようです。フォーマットをテキストに設定して、先頭の 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
4

2 に答える 2

5

同じ結果を達成するためのより良い方法があります。1 行追加するだけで機能します。スタイル シートを作成<TD>し、ループを使用してすべてのタグの属性を追加する代わりに、すべての TD タグにスタイルを直接適用します。

string style = @"<style> TD { mso-number-format:\@; } </style>";
于 2012-03-02T11:37:06.393 に答える
0

これをチェックしてください:GridViewをWord Excel PDFおよびCSVドキュメントにエクスポートする方法

それは正常に動作します。最善。ありがとう

于 2009-11-19T08:18:21.120 に答える