2015-09-05 100 views
3

我注意到Range.Value在VBA和C#中爲日期值產生不同的結果。Range.Value在VBA和C中產生不同的結果#

例如,如果單元格的值爲NumberFormat d/m/yyyy,則在VBA中Range.Value將返回「5/13/1988」,而在C#中則返回「5/13/1988 12 :00:00 AM「

Range.Value2在兩種語言中都是相同的(32276)。

有誰知道爲什麼VBA和C#在這種情況下產生不一致的結果?

請注意,我知道我可以使用Range.Value2和Range.NumberFormat的組合,然後在C#中格式化該值,但我感興趣爲什麼行爲不一致。

+2

日期是天天過去31日 - 12月1899年與時代是一個小數簡單的1一天中的一部分。上午12:00:00(午夜)是.0,所以他們表達完全一樣的東西。我根本沒有發現結果不一致。 – Jeeped

+1

同意@Jeeped,格式(cdate(「5/13/1988」),「M.DD.YYYY HH:MM:SS」)將返回相同的「5.13.1988 00:00:00」結果,所以沒有不一致性 – Vasily

回答

1

簡單。在Excel Interop中(您在VSTO項目中引用)MS顯然已將Date單元格值映射到.Net DateTime數據類型。

對於他們來說最好的選擇,你不會希望像Date.Net數據類型那樣使用(記住VB經典版只有Date數據類型不是DateTime)。

如何看引擎蓋下在這裏Excel處理數據:https://stackoverflow.com/a/13983731

1

VB.Net和C#不同的方式處理它。

的Excel 2013

enter image description here

Sub Sample() 
    MsgBox Sheet1.Range("A1").Value 
End Sub 

enter image description here


VB.Net 2013

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    '~~> Opens an existing Workbook. Change path and filename as applicable 
    xlWorkBook = xlApp.Workbooks.Open("C:\Users\Siddharth\Desktop\Delete Later\Sample.xlsx") 

    '~~> Display Excel 
    xlApp.Visible = True 
    xlWorkSheet = xlWorkBook.Sheets("Sheet1") 

    MessageBox.Show(xlWorkSheet.Range("A1").Value) 

    ' 
    '~~> Rest of the code 
    ' 
End Sub 

enter image description here


Ç像你提到#2013

C#不幸的是處理它。

enter image description here

你可以做到這一點,以獲得期望的結果

private void button1_Click(object sender, EventArgs e) 
    { 
     Microsoft.Office.Interop.Excel.Application xlexcel; 
     Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
     Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 

     object misValue = System.Reflection.Missing.Value; 
     xlexcel = new Excel.Application(); 

     xlexcel.Visible=true ; 

     xlWorkBook = xlexcel.Workbooks.Open(
        "C:\\Users\\Siddharth\\Desktop\\Delete Later\\Sample.xlsx", 
        0, true, 5, "", "", true, 
        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, 
        "\t", false, false, 0, true, 1, 0); 

     // Set Sheet 1 as the sheet you want to work with 
     xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

     String cellvalue = xlWorkSheet.Cells[1, 1].Value.ToString("MM/dd/yyyy", 
          CultureInfo.InvariantCulture); 

     MessageBox.Show(cellvalue); 

     // 
     //~~> Rest of the code 
     // 
    } 

enter image description here

+0

你爲什麼說VB.NET和C#處理它的方式不同?如果正確計算文化/格式,它們都應該輸出完全相同的日期到字符串轉換。 VB。NET&C#應該總是表現相同,因此你的第一個陳述很容易讓人誤解。 – varocarbas

+1

是的,他們應該處理它一樣,但它不像我上面已經演示:)'.Value'在兩個不同的輸出... –

+0

其實我寫了我的評論的第一個版本,我刪除了(兩個顯示的消息盒子沒有顯示相同的值?)。 MessageBox.Show(xlWorkSheet.Range(「A1」).Value)(和VB.NET版本)是一種將日期類型(Excel提供的內容)轉換爲字符串的好方法,只要您沒有完全控制在發生什麼。您應該始終考慮給定的文化和/或格式(例如下面的Value.ToString(「MM/dd/yyyy」))。爲什麼你的情況有所不同?也許這兩個VB.NET/C#項目有不同的默認文化。如果你做得正確,輸出將是相同的。 – varocarbas

-1
using System.Threading; 
using System.Globalization; 
// Put the following code before InitializeComponent() 
// Sets the culture to US English 
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
// Sets the UI culture too 
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); 
相關問題