2017-07-19 141 views
0

我有一個DataGridView,其中我定義了一個日期,如:DD/MM/YYYY。當我使用Microsoft.Office.Interop.Excel功能導出日期時,Excel會自動將其識別爲日期格式,但不會以我想要的方式進行識別。我的解決方案:Excel單元格格式 - 日期notaton

Microsoft.Office.Interop.Excel.Range dateColumn = mySheet.get_Range("E1").EntrireColumn; 
dateColumn.NumberFormat = "DD/MM/YYYY;@"; 

有趣的是,這是在Excel 2007專業版內工作。當我使用Excel 2010 basic在PC上部署應用程序時,它不再工作。兩種Excel版本的語言設置均爲英語(美國標準)。這不能是基本VS的功能。專業的權利?

+0

也許你應該寫'='在第一行,不'-'? – Vityata

回答

0

我不久前寫了這個,我不記得所有這背後的原因,但這裏是我使用:

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)); 
}