2012-03-23 51 views
0

我拉兩個字段並將它們與「 - 」放在一起。當我輸出爲excel時,它似乎認爲該字符串是一個日期並將其從11-11轉換爲11-11。我似乎無法弄清楚如何解決這個問題。導出爲ex​​cel將字符串轉換爲日期

我正在將一個gridview導出到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() 
+0

你可以用雙引號包裝字段值嗎? – mgnoonan 2012-03-23 18:21:26

回答

1

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

+0

'將Excel單元格格式指定爲文本。 – 2012-03-23 18:20:54

+0

這將會起作用 - 但在我看來這有點駭人聽聞。大多數允許您訪問Excel的API也允許您將單元格/列格式顯式設置爲文本/數字/日期等。 – Origin 2012-03-23 18:48:28

+0

除非您可以創建真實/本機Excel文件,否則這是正確的答案。 OP將gridview導出到html表格,Excel將很高興地打開(因爲Excel 2000,如果內存服務的話)。 但是,如果沒有這個「黑客」,你不能「設置數據類型」。如果您想要控制數據類型,而不必使用某個第三方庫,請查看將數據導出爲Excel XML **將執行的操作。當然要注意的是,我不認爲這會是「普遍」的。 文本將是「最普遍的」,如果是這樣,撇號是唯一的方法.... – EdSF 2012-03-25 17:26:04

0

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

雖然使用'在文本的開頭將工作,它也有其他副作用。

取決於你如何創建你的Excel文件將決定你如何去設置單元格格式。我在EPPlus方面取得了很多成功。

0

如果沒有引用,您的風格無法正常工作。

變化

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

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

這prefectly爲我工作。