您的位置:首页 > 其它

gridview导出excel文件且把数字类型的列转换成字符导出(防止处理成科学计数法)

2010-11-24 11:51 417 查看
Public Function OutExcel(ByVal dt As DataTable)
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Buffer = True
HttpContext.Current.Response.Charset = "UTF-8"
Dim strFileName As String = Server.UrlEncode("查询结果.xls")
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" & strFileName & ".xls")
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312") '设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/ms-excel"

Dim tw As System.IO.StringWriter = New System.IO.StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(tw)
Dim gv As New GridView
gv.DataSource = dt
AddHandler gv.RowDataBound, AddressOf gv_RowDataBound
gv.DataBind()

gv.RenderControl(hw)
HttpContext.Current.Response.Write(tw.ToString())
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()
Return True
End Function
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'防止处理成科学计数法
For i As Integer = 0 To e.Row.Cells.Count - 1
e.Row.Cells(i).Style.Add("mso-number-format", "/@")
Next
End If
End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐