简体   繁体   English

为什么Excel会更改我的日期显示值,如何防止它更改呢?

[英]Why is Excel changing my date display value, and how can I prevent it from doing that?

It seems that Excel is taking values I assign to cells, such as "Sep 15" and changing them to "15-Sep" 看来Excel正在采用我分配给单元格的值,例如“ Sep 15”,并将其更改为“ 15-Sep”

I have code that should assign the val in the format I want ( MMM yy ): 我有应该以我想要的格式( MMM yy )分配val的代码:

internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
    // code from http://stackoverflow.com/questions/40045761/how-can-i-convert-a-string-in-the-format-yyyymm-to-mmmyy
    DateTime intermediateDate = DateTime.ParseExact(YYYYMMVal, "yyyyMM", CultureInfo.InvariantCulture);
    return intermediateDate.ToString("MMM yy");
}

...called like so: ...这样称呼:

var monthYearCell = _xlPivotDataSheet.Cells[_lastRowAddedPivotTableData + 1, 4];
monthYearCell.Value2 = GetMMMYYFromYYYYMM(MonthYear);

...but the data is still being written/displayed as "YY-MMM", such as "15-Sep" ...但是数据仍被写入/显示为“ YY-MMM”,例如“ 15-Sep”

I debugged the value of monthYearCell after the two lines directly above execute, and find that the "Text" property of the monthYearCell object is indeed "15-Sep"; 我直接在上面两行执行后调试了monthYearCell的值,发现monthYearCell对象的“ Text”属性确实是“ 15-Sep”; it " should " be "Sep 15", as that is what is being returned from the GetMMMYYFromYYYYMM() helper method. “” 为“ 9月15日”,因为这是从GetMMMYYFromYYYYMM()帮助器方法返回的GetMMMYYFromYYYYMM()

Excel is obviously (right?) changing the values from "Sep 15" and such to "15-Sep" and such. Excel显然(对吗?)将值从“ Sep 15”等更改为“ 15-Sep”等。 I reckon Excel must be "autocorrecting" behind the scenes or something; 我认为Excel必须在幕后进行“自动更正”。 it reminds me of a movie I saw once ("Reds" maybe?) where the protagonist went semi-ballistic when his editor changed what he wrote. 这让我想起了我曾经看过的一部电影(也许是“ Reds”?),当主角改变了他的写作时,主角半弹道。 I often feel this way about Word and Excel. 我对Word和Excel经常有这种感觉。 How can I tell Excel (assuming this is the problem) to just leave it alone - "What I have written, I have written"? 我怎么能告诉Excel(假设是问题所在),别管它-“我写的东西,我写的东西”?

I did try to change the value from the raw "YYYYMM" format to what I want this way previously: 我确实尝试过将值从原始的"YYYYMM"格式更改为我以前想要的格式:

monthField.NumberFormat = "MMM yy";

...but that changed values such as "201509" and "201510" to "Sep 51" for both...?!? ...但是将两者的值都从“ 201509”和“ 201510”更改为“ 9月51日” ...?!?

如果您在值前加上'来表示Excel,则应将其视为文本,那么它将不会尝试将您的值解析为日期。

It will translate any string to dateTime when you set the Excel field with a date format. 当您将Excel字段设置为日期格式时,它将把任何字符串转换为dateTime。

use: 采用:

monthYearCell.NumberFormat = "@";

then use: 然后使用:

monthYearCell.Value = GetMMMYYFromYYYYMM(MonthYear);

I think it comes from the way Excel stores dates. 我认为它来自Excel存储日期的方式。 They are the number of days since 01-01-1900. 它们是自1900年1月1日以来的天数。 Stored dates in Excel 在Excel中存储日期

When extracting them, they are looking at your localized setting for the format. 提取它们时,他们正在查看格式的本地化设置。 Any formatting you want to keep needs to be after or while retrieving them. 您要保留的任何格式都必须在获取它们之后或同时进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM