如何使用C#在Excel中为单元格设置本地化的短date格式?
使用C#和VSTO,可以使用以下代码设置Excel中单元格的types:
worksheet.Cells[i, j].NumberFormat = magicString;
其中worksheet
是Microsoft.Office.Interop.Excel.Worksheet
类的对象, i
是单元格的行号, j
是单元格的列号, magicString
是定义单元格types的一些string(注意: Excel调用格式的types,但下面我使用单词types )。
以下magicString
定义了以下Exceltypes:
-
string magicString = "";
– 定义“常规”Exceltypes; -
string magicString = "@";
– 定义“文本”Exceltypes; -
string magicString = "0%";
– 定义“百分比”Exceltypes。
当我想要设置“date”Exceltypes时,情况更加复杂。 复杂性与Excel的本地化和Windows系统的本地化有关。
所以,例如,我有一个俄语版本的Excel(尤其是所有types都使用俄语在Excel中编写) ,我的Windows系统具有以下短date格式:“dd.MM.yyyy”(可以find此设置在“控制面板”>“区域和语言”>“格式”中)。 我有一个Windows的英文版本,但是这绝对没有作用。
因此,如果我将在我的代码中使用下面的magicString
,单元格的types将被设置为短datetypes:
-
string magicString = "ДД.ММ.ГГГГ";
– 定义“date”(或更确切地说,“短date”)Exceltypes;
正如你所看到的,这里的magicString
是俄语字母(俄语 – 因为Excel是俄语)的组合和在Windows设置中设置的格式的组合。
如果,而不是这个magicString
,我使用等于“DD.MM.YYYY”(即英文字母)的magicString
,错误出现。
因此,如果我希望我的Excel加载项能够正确设置Excel的所有(英语,俄语,德语和其他所有版本)版本以及 Windows的所有本地化设置的“短date”types,能够使用一些通用的magicString
,这是独立于两个提到的因素。
作为一个选项,我可以使用下面的代码从Windows设置中读取短date格式:
string shortDatePattern = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
然后将获取的shortDatePattern
string中的字母replace为与Excel语言对应的字母。 不过,这种方式在我看来太复杂了。
我的问题是:是否有一些适用于所有Excel语言和所有Windows本地化设置的通用magicString
,比如“General”,“Text”和“Percent”等其他Exceltypes? 或者,也许有人知道其他简单的方法来达到这样一个普遍性?
你应该可以这样做:
Application xlApp = new Application(); Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet ws = wb.Worksheets[1]; var yearCode = xlApp.International[XlApplicationInternational.xlYearCode]; var monthCode = xlApp.International[XlApplicationInternational.xlMonthCode]; var dayCode = xlApp.International[XlApplicationInternational.xlDayCode]; ws.Cells[1, 1].NumberFormat = string.Format("{0}{1}.{2}{3}.{4}{5}{6}{7}", dayCode, dayCode, monthCode, monthCode, yearCode, yearCode, yearCode, yearCode);
在Application
有International
财产。 你可以使用XlApplicationInternational
枚举来查询它。 对于我来说xlYearCode
返回é
例如。 这应该是对你来说。
之后,您可以使用先前查询的格式代码来构造您的NumberFormat
。
非常感谢SzabolcsDézsi的提示。 但它只解决了我的问题的一部分。 另一部分是如何从Windows系统本地化设置中提取date格式代码? 我还没有在互联网上find答案,并提供我自己的解决scheme,结合SzabolcsDézsi的解决scheme。
首先,我们来创build下面的类:
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using Excel = Microsoft.Office.Interop.Excel; namespace MyNamespace { internal sealed class DateFormatComponentCodes { private readonly char year; private readonly char month; private readonly char day; // Constructs the object based on the system localization. public DateFormatComponentCodes() { DateTimeFormatInfo dateTimeFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat; var yearMonth = new HashSet<char>(new HashSet<char>(dateTimeFormatInfo.YearMonthPattern.ToCharArray()).Where(c => char.IsLetter(c))); var monthDay = new HashSet<char>(new HashSet<char>(dateTimeFormatInfo.MonthDayPattern.ToCharArray()).Where(c => char.IsLetter(c))); var monthHashSet = new HashSet<char>(yearMonth); monthHashSet.IntersectWith(monthDay); this.month = monthHashSet.First(); yearMonth.ExceptWith(monthHashSet); this.year = yearMonth.First(); monthDay.ExceptWith(monthHashSet); this.day = monthDay.First(); } // Constructs the object based on the Excel localization. public DateFormatComponentCodes(Excel.Application application) { this.year = application.International[Excel.XlApplicationInternational.xlYearCode].ToString()[0]; this.month = application.International[Excel.XlApplicationInternational.xlMonthCode].ToString()[0]; this.day = application.International[Excel.XlApplicationInternational.xlDayCode].ToString()[0]; } public char Year { get { return this.year; } } public char Month { get { return this.month; } } public char Day { get { return this.day; } } } }
现在让我们创build这个类的两个对象,并使用它们为Excel生成短date格式模式(以上称为“魔术string”):
private string ConstructExcelShortDatePattern() { var systemDateComponentCodes = new DateFormatComponentCodes(); var excelDateComponentCodes = new DateFormatComponentCodes(this.application); string systemShortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; string excelShortDatePattern = systemShortDatePattern.Replace(systemDateComponentCodes.Year, excelDateComponentCodes.Year).Replace(systemDateComponentCodes.Month, excelDateComponentCodes.Month).Replace(systemDateComponentCodes.Day, excelDateComponentCodes.Day); return excelShortDatePattern; }
返回的string可用于设置所有Windows本地化和所有Excel本地化的短date格式
worksheet.Cells[i, j].NumberFormat = ConstructExcelShortDatePattern();