Excel单元格格式-日期通知 [英] Excel cell format - date notaton
问题描述
我有一个DataGridView,其中定义了一个日期,例如: DD / MM / YYYY
。当我使用 Microsoft.Office.Interop.Excel
功能导出日期时,Excel会自动将其识别为日期格式,但不是我想要的方式。我的解决方案:
I have a DataGridView where I define a date like: DD/MM/YYYY
. When I export my date with the use of the Microsoft.Office.Interop.Excel
functionalities, Excel automatically recognizes this as a date format, but not in the way I want it. My solution:
Microsoft.Office.Interop.Excel.Range dateColumn = mySheet.get_Range("E1").EntrireColumn;
dateColumn.NumberFormat = "DD/MM/YYYY;@";
有趣的是,这在Excel 2007 Professional中有效。当我在具有Excel 2010 basic的PC上部署该应用程序时,它不再起作用。两个Excel版本的语言设置均为英语(美国标准)。这不是基本VS的功能。专业权利吗?
The funny thing is that, this is working within Excel 2007 professional. When I deploy the application on a PC with Excel 2010 basic, it is not working anymore. The language settings of both Excel versions is English (U.S. standard). This can't be a feature of basic VS. professional right?
推荐答案
我很久以前就写了这个,我不记得它背后的所有原因,但是这里是我的用法:
I wrote this so long ago, I cannot remember all the reasoning behind it but here is what I use:
public static void ChangeDateFormat(Excel.Range cell, string format, bool useLocale)
{
if (useLocale && !format.Contains("[$"))
{
format = "[$-800]" + format;
}
SetProperty(cell, "NumberFormatLocal", new object[] { format });
}
public static void SetProperty(object target, string name, params object[] args)
{
target.GetType().InvokeMember(name,
BindingFlags.SetProperty |
BindingFlags.Public |
BindingFlags.Instance,
null, target, args, new CultureInfo(1033));
}
这篇关于Excel单元格格式-日期通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!