Range.Value 在 VBA 和 C# 中产生不同的结果
Range.Value produces different results in VBA and C#
我注意到 Range.Value 在 VBA 和 C# 中对日期值产生不同的结果。
例如,如果一个单元格的值是 5/13/1988,NumberFormat d/m/yyyy,在 VBA Range.Value 中将 return "5/13/1988" 而在 C# 中 returns "5/13/1988 12:00:00 AM"
Range.Value2 在两种语言中是相同的 (32276)。
有谁知道为什么 VBA 和 C# 在这种情况下会产生不一致的结果?
请注意,我知道我可以使用 Range.Value2 和 Range.NumberFormat 的组合,然后在 C# 中格式化该值,但我对行为不一致的原因很感兴趣。
简单。在 Excel Interop(您在 VSTO 项目中引用)MS 显然已将 Date 单元格值映射到 .Net DateTime 数据类型。
他们的最佳选择,您不想像 Date .Net 数据类型那样使用(记住 VB classic 只有 Date 数据类型,没有 DateTime)。
在此处了解 excel 如何在后台处理数据:
VB.Net 和 C# 处理不同。
Excel 2013
Sub Sample()
MsgBox Sheet1.Range("A1").Value
End Sub
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
C# 2013
不幸的是,C# 会像您提到的那样处理它。
您可以这样做以获得想要的结果
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
//
}
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");
我注意到 Range.Value 在 VBA 和 C# 中对日期值产生不同的结果。
例如,如果一个单元格的值是 5/13/1988,NumberFormat d/m/yyyy,在 VBA Range.Value 中将 return "5/13/1988" 而在 C# 中 returns "5/13/1988 12:00:00 AM"
Range.Value2 在两种语言中是相同的 (32276)。
有谁知道为什么 VBA 和 C# 在这种情况下会产生不一致的结果?
请注意,我知道我可以使用 Range.Value2 和 Range.NumberFormat 的组合,然后在 C# 中格式化该值,但我对行为不一致的原因很感兴趣。
简单。在 Excel Interop(您在 VSTO 项目中引用)MS 显然已将 Date 单元格值映射到 .Net DateTime 数据类型。
他们的最佳选择,您不想像 Date .Net 数据类型那样使用(记住 VB classic 只有 Date 数据类型,没有 DateTime)。
在此处了解 excel 如何在后台处理数据:
VB.Net 和 C# 处理不同。
Excel 2013
Sub Sample()
MsgBox Sheet1.Range("A1").Value
End Sub
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
C# 2013
不幸的是,C# 会像您提到的那样处理它。
您可以这样做以获得想要的结果
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
//
}
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");