【问题标题】:export to excel converting string to date导出到 excel 将字符串转换为日期
【发布时间】:2012-03-23 18:18:09
【问题描述】:

我正在拉两个字段并用“-”将它们放在一起。当我导出到 excel 时,它似乎认为该字符串是一个日期,并将其从 11 月 11 日转换为 11 月 11 日。我似乎无法弄清楚如何解决这个问题。

我正在将一个网格视图导出到 excel 中。此 asp.net 在 VS 2008 中使用 .net 3.5 和 vb.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()

【问题讨论】:

  • 你能把字段值用双引号括起来吗?

标签: asp.net vb.net excel


【解决方案1】:

尝试在您的内容前添加单引号 (')。

【讨论】:

  • ' 指定 Excel 单元格格式为文本。
  • 这会起作用 - 但在我看来它有点老套。大多数允许您访问 Excel 的 API 还允许您将单元格/列格式显式设置为文本/数字/日期等。
  • 除非您可以创建真实/原生 Excel 文件,否则这是正确答案。 OP 正在将 gridview 导出到 html 表,Excel 会很高兴地打开该表(从 Excel 2000 开始,如果有记忆的话)。但是,如果没有这个“hack”,您就无法“设置数据类型”。如果您想控制数据类型,而不必使用某些 3rd 方库,请查看将数据导出为 Excel XML 是否可行。当然需要注意的是,我认为这不会是“普遍的”。文本将是“最通用的”,如果是这样,撇号黑客是唯一的方法......
【解决方案2】:

这是 Excel 如何解释数据的问题。我建议您根据需要指定数据类型(通过单元格格式)。当您想以某种方式格式化日期,或者不希望从正常数据中截断前导零时,您也会遇到此问题。

虽然在文本开头使用 ' 会起作用,但它也有其他副作用。

根据您创建 excel 文件的方式,将决定您如何设置单元格格式。我在 EPPlus 上取得了很大的成功。

【讨论】:

    【解决方案3】:

    如果没有引号,您的样式将无法正常工作。

    改变

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

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

    这非常适合我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      相关资源
      最近更新 更多