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
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
相关文章推荐
- GridView导出成Excel字符"0"丢失/数字丢失的处理方式
- GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- ASP.NET 导出正式Excel文档(可锁定首行,有效防止数字类型数据科学计数法转换)
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- Asp.net导出excel时长数字被科学计数法的解决方案。(身份证长数字作为字符处理)
- Asp.net导出excel时长数字被科学计数法的解决方案。(身份证长数字作为字符处理)
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
- java导出:防止EXCEL或WPS的将文本自动转换为日期或数字格式
- 用Excel打开csv文件时,如何处理数字内容展现会自动转换格式的问题
- mysql导出csv文件excel打开后数字用科学计数法显示且低位变0的解决方法
- excel 用宏将日期数字类型的列转换为字符文本列 c#导入excel 日期变成数字 怎么办?这是最好的解决办法
- 用Excel打开csv文件时,如何处理数字内容展现会自动转换格式的问题
- C# 导出excel文件处理科学计数法办法
- java使用poi处理excel防止数字变成科学计数法的形式
- java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的形式
- 导出excel文件 解决科学计数法问题 datagridview导出字符串变数字问题
- java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的形式