本文介绍了出口的GridView到Excel与行设置为文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网上看了几教程和似乎是失去了一些东西。我想在列设置为文本格式的前导0的出现。
任何建议将是AP preciated。
'''<总结>
'''这是必需的网格视图正常出口
'''< /总结>
'''< PARAM NAME =控制>< /参数>
'''<&言论GT;< /言论>
公共覆盖子VerifyRenderingInServerForm(BYVAL控制作为System.Web.UI.Control)
结束小组 受保护的覆盖子OnInitComplete(BYVAL E上System.EventArgs) 昏暗的名单System.Web.UI.WebControls.GridView = CTYPE(Page.FindControl(目录),System.Web.UI.WebControls.GridView)
AddHandler的List.RowDataBound,AddressOf的RowDataBound List.DataSource = myList中
List.DataBind() Response.Clear()
Response.ContentType =应用程序/ vnd.ms-EXCEL
HttpContext.Current.Response.AddHeader(内容处置,附件;文件名= ExportList.xls) 的Response.Write(<风格>的.text {MSO的数字格式:\\ @;}< /风格与GT;) 使用strwriter作为新System.IO.StringWriter
使用的HTMLWriter作为新的HtmlTextWriter(strwriter) List.RenderControl(的HTMLWriter) HttpContext.Current.Response.Write(strwriter.ToString)
HttpContext.Current.ApplicationInstance.CompleteRequest()
使用完
使用完 结束小组 保护小组的RowDataBound(BYVAL发件人为对象,BYVAL E上System.Web.UI.WebControls.GridViewRowEventArgs) 如果e.Row.RowType = DataControlRowType.DataRow然后 e.Row.Cells(0).Attributes.Add(类,文本) 昏暗dtview作为System.Data.DataRowView
昏暗DT为DATETIME
昏暗intCounter作为整数 dtview = e.Row.DataItem 对于intCounter = 0〜dtview.Row.ItemArray.Length - 1 如果TypeOf运算dtview.Row.Item(intCounter)是的System.DateTime然后
DT = dtview.Row.Item(intCounter)
e.Row.Cells(intCounter)。文本= dt.ToLongDateString
万一 下一个
万一 结束小组
解决方案
有一个更好的方法来达到同样的效果,只需添加一条线,它会工作。
而不是创建样式表并为所有添加属性的< TD方式>
使用循环标签,直接对所有TD标签应用样式
字符串风格= @<风格> TD {MSO的数字格式:\\ @;}< /风格与GT;
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
解决方案
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>";
这篇关于出口的GridView到Excel与行设置为文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!