导出excel并将特定行格式化为文本

时间:2016-12-06 10:14:50

标签: asp.net excel vb.net

我有一个导出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数据类型,我不需要所有NUMBERText我只需要特定列为文本

示例

    Account Amount
8602241158  1000.00
8602241158  1000.00
7896476479  500.00
7896476479  500.00
0986046857  900.00

这里是来自Excel的示例数据,所有数据都是NUMBER但已被上面的代码覆盖到TEXT,然后我无法汇总Amount列,我的观点是我只需要将Account列格式化为TEXT而非Amount

0 个答案:

没有答案