我有一个导出Gridview excel的方法
Public Sub Export2Excel(ByVal Response As HttpResponse, ByVal Control As GridView, ByVal Fname As String)
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=" & Fname & ".xls")
Response.Charset = "Unicode"
Response.ContentType = "application/vnd.ms-excel"
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Control.AllowPaging = False
Control.DataBind()
For i As Integer = 0 To Control.Rows.Count - 1
Dim row As GridViewRow = Control.Rows(i)
row.BackColor = System.Drawing.Color.White
row.Attributes.Add("class", "textmode")
Next
Control.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style> TD { mso-number-format:\@; } </style>"
Response.Write(style)
Response.Output.Write(st.ToString())
Response.Flush()
Response.End()
End Sub
上面的代码导出excel正确,但它覆盖了所有NUMBER
数据类型列到text
数据类型,我不需要所有NUMBER
到Text
我只需要特定列为文本
示例
Account Amount
8602241158 1000.00
8602241158 1000.00
7896476479 500.00
7896476479 500.00
0986046857 900.00
这里是来自Excel的示例数据,所有数据都是NUMBER
但已被上面的代码覆盖到TEXT
,然后我无法汇总Amount
列,我的观点是我只需要将Account
列格式化为TEXT
而非Amount
列