如何防止 Excel 将值转换为日期格式?
How can I prevent Excel from converting values to date format?
最近(之前没有人注意到它发生过,如果它确实发生过)一些 "ID" 类型的值正在被 Excel 转换为日期。值被转换为一些逻辑,正如您在此处看到的,用户在其中添加了一列以显示基础值的真实含义,以及它们应该如何表示(原始,无转换):
因为“01”后有一个“-”Excel认为“01-”应该是一月,假设最后两个字符代表年份。
在不包含破折号的项目代码中,它们将被单独保留。如何防止 Excel 以这种方式成为 "helpful" 并将这些值转换为日期?
更新
为了回应 Scott Craner 的评论,这是我必须写出该值的代码:
using (var memberItemCodeCell = priceComplianceWorksheet.Cells[rowToPopulate, DETAIL_MEMBERITEMCODE_COL])
{
memberItemCodeCell.Style.Font.Size = DATA_FONT_SIZE;
memberItemCodeCell.Value = _memberItemCode;
memberItemCodeCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
...当我试图通过在单元格名称后键入 "for" 来弄清楚如何将值格式化为文本或常规时,我看到了这些选项:
那么我需要使用什么 - FormatedText (sic) 或 ConditionalFormatting,以及如何具体设置它们以将列格式化为 Text 或 General,如果这两者中的任何一个是首选?
我通常在准备 table 放置数据时定义数据类型,所以 Excel 不要试图找到数据的种类。
在这种情况下,我将使用文本数据类型。示例:
我想你想将数字格式更改为文本。
文本格式由 NumberFormat
属性 使用 "at" 字符指定。
memberItemCodeCell.Style.Font.Size = DATA_FONT_SIZE;
memberItemCodeCell.NumberFormat = "@";
memberItemCodeCell.Value = _memberItemCode;
memberItemCodeCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
此外,为了它的价值,请查看有关 Value
和 Value2
之间区别的文章/问题。了解差异并使用正确的差异是很好的。对于你的情况,我怀疑这是否重要。
最终对我有用的是使用这种技术将正确的数据类型分配给单元格中的值(通过调用 ConvertValueToAppropriateTypeAndAssign()),然后在事后根据需要进行格式化:
public static readonly string NUMBER_FORMAT_CURRENCY = "$#,##0.00;($#,##0.00)";
public static readonly string NUMBER_FORMAT_THOUSANDS = "#,##0";
public static readonly string PERCENTAGE_FORMAT = "0.00%;[Red]-0.00%";
public static readonly string NUMBER_FORMAT_TEXT = "@";
public static readonly string NUMBER_FORMAT_DOUBLE = "0.00";
. . .
using (var percentageCell = priceComplianceWorksheet.Cells[rowToPopulate, SUMMARY_PERCENTAGE_COL])
{
ConvertValueToAppropriateTypeAndAssign(percentageCell, totalPercentage);
percentageCell.Style.Numberformat.Format = PERCENTAGE_FORMAT;
}
. . .
// Adapted from
public static void ConvertValueToAppropriateTypeAndAssign(this ExcelRangeBase range, object value)
{
string strVal = value.ToString();
if (!String.IsNullOrEmpty(strVal))
{
decimal decVal;
double dVal;
int iVal;
if (decimal.TryParse(strVal, out decVal))
range.Value = decVal;
if (double.TryParse(strVal, out dVal))
range.Value = dVal;
else if (Int32.TryParse(strVal, out iVal))
range.Value = iVal;
else
range.Value = strVal;
}
else
range.Value = null;
}
最近(之前没有人注意到它发生过,如果它确实发生过)一些 "ID" 类型的值正在被 Excel 转换为日期。值被转换为一些逻辑,正如您在此处看到的,用户在其中添加了一列以显示基础值的真实含义,以及它们应该如何表示(原始,无转换):
因为“01”后有一个“-”Excel认为“01-”应该是一月,假设最后两个字符代表年份。
在不包含破折号的项目代码中,它们将被单独保留。如何防止 Excel 以这种方式成为 "helpful" 并将这些值转换为日期?
更新
为了回应 Scott Craner 的评论,这是我必须写出该值的代码:
using (var memberItemCodeCell = priceComplianceWorksheet.Cells[rowToPopulate, DETAIL_MEMBERITEMCODE_COL])
{
memberItemCodeCell.Style.Font.Size = DATA_FONT_SIZE;
memberItemCodeCell.Value = _memberItemCode;
memberItemCodeCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
...当我试图通过在单元格名称后键入 "for" 来弄清楚如何将值格式化为文本或常规时,我看到了这些选项:
那么我需要使用什么 - FormatedText (sic) 或 ConditionalFormatting,以及如何具体设置它们以将列格式化为 Text 或 General,如果这两者中的任何一个是首选?
我通常在准备 table 放置数据时定义数据类型,所以 Excel 不要试图找到数据的种类。
在这种情况下,我将使用文本数据类型。示例:
我想你想将数字格式更改为文本。
文本格式由 NumberFormat
属性 使用 "at" 字符指定。
memberItemCodeCell.Style.Font.Size = DATA_FONT_SIZE;
memberItemCodeCell.NumberFormat = "@";
memberItemCodeCell.Value = _memberItemCode;
memberItemCodeCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
此外,为了它的价值,请查看有关 Value
和 Value2
之间区别的文章/问题。了解差异并使用正确的差异是很好的。对于你的情况,我怀疑这是否重要。
最终对我有用的是使用这种技术将正确的数据类型分配给单元格中的值(通过调用 ConvertValueToAppropriateTypeAndAssign()),然后在事后根据需要进行格式化:
public static readonly string NUMBER_FORMAT_CURRENCY = "$#,##0.00;($#,##0.00)";
public static readonly string NUMBER_FORMAT_THOUSANDS = "#,##0";
public static readonly string PERCENTAGE_FORMAT = "0.00%;[Red]-0.00%";
public static readonly string NUMBER_FORMAT_TEXT = "@";
public static readonly string NUMBER_FORMAT_DOUBLE = "0.00";
. . .
using (var percentageCell = priceComplianceWorksheet.Cells[rowToPopulate, SUMMARY_PERCENTAGE_COL])
{
ConvertValueToAppropriateTypeAndAssign(percentageCell, totalPercentage);
percentageCell.Style.Numberformat.Format = PERCENTAGE_FORMAT;
}
. . .
// Adapted from
public static void ConvertValueToAppropriateTypeAndAssign(this ExcelRangeBase range, object value)
{
string strVal = value.ToString();
if (!String.IsNullOrEmpty(strVal))
{
decimal decVal;
double dVal;
int iVal;
if (decimal.TryParse(strVal, out decVal))
range.Value = decVal;
if (double.TryParse(strVal, out dVal))
range.Value = dVal;
else if (Int32.TryParse(strVal, out iVal))
range.Value = iVal;
else
range.Value = strVal;
}
else
range.Value = null;
}