簡體   English   中英

導出到excel將字符串轉換為日期

[英]export to excel converting string to date

我要拉兩個字段,並用“-”將它們放在一起。 當我導出到excel時,似乎認為該字符串是一個日期,並將其從11-11轉換為11月11日。 我似乎無法弄清楚如何解決此問題。

我將gridview導出到excel。 在VS 2008中使用.net 3.5和vb.net的asp.net。

    Response.Clear()

    Response.Buffer = True

    'grab filename from filename box
    'if does not exist then do default naming
    Dim filename As String
    If filenameTextBox2.Text <> "" Then
        filename = "attachment;filename=" + filenameTextBox2.Text + ".xls"
    Else
        filename = "attachment;filename=GridViewExport.xls"
    End If

    Response.AddHeader("content-disposition", filename)

    Response.Charset = ""

    Response.ContentType = "application/vnd.ms-excel"



    Dim sw As New IO.StringWriter()

    Dim hw As New HtmlTextWriter(sw)



    GridView1.AllowPaging = False

    GridView1.DataBind()


    'Change the Header Row back to white color

    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")



    'Apply style to Individual Cells

    'GridView1.HeaderRow.Cells(0).Style.Add("background-color", "green")

    'GridView1.HeaderRow.Cells(1).Style.Add("background-color", "green")

    'GridView1.HeaderRow.Cells(2).Style.Add("background-color", "green")

    'GridView1.HeaderRow.Cells(3).Style.Add("background-color", "green")



    For i As Integer = 0 To GridView1.Rows.Count - 1

        Dim row As GridViewRow = GridView1.Rows(i)



        'Change Color back to white

        row.BackColor = System.Drawing.Color.White



        'Apply text style to each Row

        row.Attributes.Add("class", "textmode")



        'Apply style to Individual Cells of Alternating Row

        If i Mod 2 <> 0 Then

            'row.Cells(0).Style.Add("background-color", "#C2D69B")

            'row.Cells(1).Style.Add("background-color", "#C2D69B")

            'row.Cells(2).Style.Add("background-color", "#C2D69B")

            'row.Cells(3).Style.Add("background-color", "#C2D69B")

        End If

    Next

    GridView1.RenderControl(hw)



    'style to format numbers to string

    Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"

    Response.Write(style)

    Response.Output.Write(sw.ToString())

    Response.Flush()

    Response.End()

嘗試在內容前添加單引號(')。

沒有報價,您的樣式無法正常運行。

更改

Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"

Dim style As String = "<style>.textmode{mso-number-format:""\@"";}</style>"

這完全適合我。

這是Excel如何解釋數據的問題。 我建議您根據需要指定數據類型(通過單元格格式)。 當您想要以某種方式設置日期格式,或者不希望從常規數據中截取前導零時,也會遇到此問題。

雖然在文字開頭使用'可以正常使用,但它還有其他副作用。

根據您創建Excel文件的方式,將決定如何設置單元格格式。 我在EPPlus上取得了很多成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM