gpt4 book ai didi

asp.net - 将 Gridview 导出到 Excel,并将行格式化为文本

转载 作者:行者123 更新时间:2023-12-03 07:49:46 26 4
gpt4 key购买 nike

我在网上阅读了一些教程,但似乎遗漏了一些东西。我试图通过将格式设置为文本来让前导 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

最佳答案

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

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

关于asp.net - 将 Gridview 导出到 Excel,并将行格式化为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1363169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com